conda创建环境
CONDA_SUBDIR=osx-arm64 conda create -n ml python=3.9 -c conda-forge
conda env config vars set CONDA_SUBDIR=osx-arm64
conda activate ml
pip安装包
pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
pip install transformers datasets
pip install matplotlib
下载del源文件
del链接
放到本地项目内
修改del下的torch.py的两个函数内容
# 修改try gpu函数
def try_gpu(i=0):"""Return gpu(i) if exists, otherwise return cpu().Defined in :numref:`sec_use_gpu`"""if torch.cuda.device_count() >= i + 1:return torch.device(f'cuda:{i}')try:return torch.device('mps')except:return torch.device('cpu')# 修改try gpu函数
def try_all_gpus():"""Return all available GPUs, or [cpu(),] if no GPU exists.Defined in :numref:`sec_use_gpu`"""devices = [torch.device(f'cuda:{i}')for i in range(torch.cuda.device_count())]try:device_macos = torch.device('mps')except:device_macos = torch.device('cpu')return devices if devices else [device_macos]
测试
运行lenet.ipynb测试效果
速度还可以。
还不懂的可以看M1版本的教程