动手学深度学习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版本的教程

相关推荐
goujunwe5 分钟前
GEO 的胜负藏在标签里
人工智能
Young丶25 分钟前
讲透 Claude Code 系列 (四):Claude Skills 完全指南:可复用的“专业能力包”从入门到精通
人工智能·ai·ai编程·ai coding
阿里云大数据AI技术43 分钟前
Hologres:一站式 Agentic 多模态检索分析平台
人工智能
回眸&啤酒鸭44 分钟前
【回眸】AI新鲜事——百度 AI 搜索实战应用场景与落地
人工智能·百度
jsl_jsl_jsl1 小时前
《Agent 的记忆怎么做:实时记忆、被动压缩与主动整理的三层设计》
人工智能
词却1 小时前
深度学习入门:数据预处理与自定义数据集
深度学习
林浩杨_1 小时前
SIGIR 2026|南京大学:Video-GAR:“通过生成 Query 来验证视频语义理解”的生成增强范式
论文阅读·人工智能·算法
speop1 小时前
Hello-Agents | Task00 环境配置
人工智能
7177771 小时前
基于 Gitee 的软件成分分析(SCA)工具选型与集成参考
人工智能·gitee
子非鱼eva1 小时前
Ascend950PR版本速配表
人工智能·ai