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

相关推荐
openYuanrong分布式计算引擎4 分钟前
openYuanrong 打造 Agent 时代的企业级分布式底座
人工智能·分布式·ai·serverless
云端漫步19875 分钟前
HarmonyOS NEXT AI 智能生活助手:PromptManager 设计与实现
人工智能·生活·harmonyos
我是大卫15 分钟前
从零到答辩全链路:花1小时用TRAE Work搞定100页案例报告+40页答辩PPT
人工智能·trae
极序时代GEO品牌优化23 分钟前
杭州极序时代|面向地域、设备与用户画像的动态GEO
人工智能·python·算法·极序时代geo
崔高杰24 分钟前
【自动阅读笔记】Inference-Time Scaling for Generalist Reward Modeling
人工智能·笔记·机器学习
风途科技~27 分钟前
隧道氮氧化物检测仪|实时监测 NO、NO₂浓度,助力公路隧道通风智能调控
大数据·人工智能
senmuleishi38 分钟前
持续发力嵌入式AI IDE,攻坚大科学装置电源解决方案
ide·人工智能
刘婉晴41 分钟前
【大模型安全】OWASP 大语言模型十大风险
人工智能·安全·语言模型
mlidongfeng43 分钟前
[AI][C++26] SIMD 编程模型思考
开发语言·c++·人工智能
关于不上作者榜就原神启动那件事1 小时前
从 MDC 到 Agent:我手搓的文档路由协议,在 Spring AI Alibaba 里找到了正式实现
java·人工智能·spring·ai·agent