动手学深度学习d2l包M4芯片 gpu加速

conda创建环境

bash 复制代码
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安装包

bash 复制代码
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的两个函数内容

python 复制代码
# 修改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版本的教程

相关推荐
数数科技的数据干货35 分钟前
从爆款到厂牌:解读游戏工业化的业务持续增长道路
运维·数据库·人工智能
amhjdx4 小时前
星巽短剧以科技赋能影视创新,构建全球短剧新生态!
人工智能·科技
听风南巷4 小时前
机器人全身控制WBC理论及零空间原理解析(数学原理解析版)
人工智能·数学建模·机器人
美林数据Tempodata5 小时前
“双新”指引,AI驱动:工业数智应用生产性实践创新
大数据·人工智能·物联网·实践中心建设·金基地建设
电科_银尘5 小时前
【大语言模型】-- 私有化部署
人工智能·语言模型·自然语言处理
翔云 OCR API6 小时前
人工智能驱动下的OCR API技术演进与实践应用
人工智能·ocr
南方者7 小时前
重磅升级!文心 ERNIE-5.0 新一代原生全模态大模型,这你都不认可它吗?!
人工智能·aigc
庄周迷蝴蝶7 小时前
旋转位置编码(Rotary Position Embedding,RoPE)
人工智能·机器学习
xier_ran7 小时前
深度学习:RMSprop 优化算法详解
人工智能·深度学习·算法
大千AI助手7 小时前
Prefix-Tuning:大语言模型的高效微调新范式
人工智能·神经网络·自然语言处理·llm·prefix-tuning·大千ai助手·前缀微调