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

相关推荐
网易云信5 分钟前
网易智企亮相 2026 世界人工智能大会:一站式企业 AI 应用覆盖三大企业现场
人工智能·aigc·线下活动
中微极客6 分钟前
GPT-4o图像生成:从艺术创作到API工程实践
人工智能
土星云SaturnCloud10 分钟前
MP_SENet轻量语音降噪模型在土星云边缘设备的部署实战
服务器·人工智能·ai·边缘计算·语音识别
Lifangyun_WD12 分钟前
RTX 5090跑Stable Diffusion XL:生图速度、显存占用与商业应用边界
人工智能·stable diffusion·gpu算力·rtx 5090·gpu容器·gpu租赁
hey you~14 分钟前
2026出海语音机器人全栈选型:从ASR引擎评测到GDPR合规落地
人工智能·机器人·语音识别
人工干智能14 分钟前
神经网络:业务分层 vs 网络分层
网络·人工智能·神经网络
GC_ESD24 分钟前
AI芯片时代,ESD静电保护缘何成为设计刚需
人工智能·集成电路·芯片·半导体·esd设计
mftang27 分钟前
TensorFlow Lite Micro:面向TinyML系统的嵌入式机器学习推理框架
人工智能·机器学习·tensorflow
kp0000033 分钟前
如何平衡模型输出的“有用性”和“安全性
人工智能·安全·网络安全·信息安全·ai安全
长风23033 分钟前
Day 18:自动备份和恢复自定义UI —— 构建Dashboard自动恢复脚本
人工智能·安全