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

相关推荐
rcc8628几秒前
Harness技术原理以及Hermes Agent的实现
人工智能
有一个好名字15 分钟前
第七篇:上下文压缩 —— Agent 永续工作的秘密
人工智能·ai agent
海兰17 分钟前
【第32篇】场景示例项目
人工智能·spring boot·状态模式·spring ai
Python私教17 分钟前
给 AI 助手装上导航仪:graphify 知识图谱实战,让 Claude Code 秒懂 400 文件项目架构
人工智能
linfengfeiye17 分钟前
AI时代的核心技能不是技术,是主动性——Notion产品负责人深度访谈
人工智能·notion
TinTin Land27 分钟前
真正的 AI 优先公司:99% 代码由 AI 编写,迭代仅需 1 天
人工智能
icestone200028 分钟前
智能客服如何按客户类型切换话术?一套支持“渠道标签 + 用户自选 + 对话推断“的分类架构设计
大数据·人工智能·ai编程
有个人神神叨叨29 分钟前
Ontology-Driven Agents(本体驱动智能体)
人工智能
John_ToDebug29 分钟前
拆解AI的“五大基础设施”:算力、网络、存储、电力、软件,谁在驱动千亿市值?
网络·人工智能