欢迎光临
我们一直在努力

【AMD ROCm 实战】云端 AI 开发系列(一):在 ModelScope 上部署 MI300X 并运行第一个 PyTorch 程序

【AMD ROCm 实战】云端 AI 开发系列(一):在 ModelScope 上部署 MI300X 并运行第一个 PyTorch 程序

摘要: 本文详细记录在魔搭创空间(ModelScope)申请 AMD Instinct MI300X 云实例、配置 ROCm 6.2 环境、安装 PyTorch for ROCm 并运行第一个 GPU 加速程序的完整流程。包含详细的截图、命令输出和性能基准测试数据。


🎯 1. 为什么选择 AMD ROCm 云端开发?

作为一名后端架构师,我在企业级 AI 部署中一直面临三大痛点:

#mermaid-svg-RRUA563aDpamN6pP{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RRUA563aDpamN6pP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RRUA563aDpamN6pP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RRUA563aDpamN6pP .error-icon{fill:#552222;}#mermaid-svg-RRUA563aDpamN6pP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RRUA563aDpamN6pP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RRUA563aDpamN6pP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RRUA563aDpamN6pP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RRUA563aDpamN6pP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RRUA563aDpamN6pP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RRUA563aDpamN6pP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RRUA563aDpamN6pP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RRUA563aDpamN6pP .marker.cross{stroke:#333333;}#mermaid-svg-RRUA563aDpamN6pP svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RRUA563aDpamN6pP p{margin:0;}#mermaid-svg-RRUA563aDpamN6pP .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-RRUA563aDpamN6pP .cluster-label text{fill:#333;}#mermaid-svg-RRUA563aDpamN6pP .cluster-label span{color:#333;}#mermaid-svg-RRUA563aDpamN6pP .cluster-label span p{background-color:transparent;}#mermaid-svg-RRUA563aDpamN6pP .label text,#mermaid-svg-RRUA563aDpamN6pP span{fill:#333;color:#333;}#mermaid-svg-RRUA563aDpamN6pP .node rect,#mermaid-svg-RRUA563aDpamN6pP .node circle,#mermaid-svg-RRUA563aDpamN6pP .node ellipse,#mermaid-svg-RRUA563aDpamN6pP .node polygon,#mermaid-svg-RRUA563aDpamN6pP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RRUA563aDpamN6pP .rough-node .label text,#mermaid-svg-RRUA563aDpamN6pP .node .label text,#mermaid-svg-RRUA563aDpamN6pP .image-shape .label,#mermaid-svg-RRUA563aDpamN6pP .icon-shape .label{text-anchor:middle;}#mermaid-svg-RRUA563aDpamN6pP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-RRUA563aDpamN6pP .rough-node .label,#mermaid-svg-RRUA563aDpamN6pP .node .label,#mermaid-svg-RRUA563aDpamN6pP .image-shape .label,#mermaid-svg-RRUA563aDpamN6pP .icon-shape .label{text-align:center;}#mermaid-svg-RRUA563aDpamN6pP .node.clickable{cursor:pointer;}#mermaid-svg-RRUA563aDpamN6pP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-RRUA563aDpamN6pP .arrowheadPath{fill:#333333;}#mermaid-svg-RRUA563aDpamN6pP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RRUA563aDpamN6pP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RRUA563aDpamN6pP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RRUA563aDpamN6pP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-RRUA563aDpamN6pP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RRUA563aDpamN6pP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-RRUA563aDpamN6pP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RRUA563aDpamN6pP .cluster text{fill:#333;}#mermaid-svg-RRUA563aDpamN6pP .cluster span{color:#333;}#mermaid-svg-RRUA563aDpamN6pP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RRUA563aDpamN6pP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-RRUA563aDpamN6pP rect.text{fill:none;stroke-width:0;}#mermaid-svg-RRUA563aDpamN6pP .icon-shape,#mermaid-svg-RRUA563aDpamN6pP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RRUA563aDpamN6pP .icon-shape p,#mermaid-svg-RRUA563aDpamN6pP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-RRUA563aDpamN6pP .icon-shape .label rect,#mermaid-svg-RRUA563aDpamN6pP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RRUA563aDpamN6pP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-RRUA563aDpamN6pP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-RRUA563aDpamN6pP :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

企业 AI 部署痛点

NVIDIA 算力成本高

显存瓶颈限制大模型

CUDA 生态锁定

AMD MI300X成本降低 50-60%

192GB HBM3 显存轻松部署 70B+ 模型

ROCm 开源生态避免厂商锁定

AMD ROCm 的核心优势:

  • 性价比: 同等算力下,云端费用比 NVIDIA A100 低 50-60%
  • 大显存: MI300X 提供 192GB HBM3,是 A100(80GB)的 2.4 倍
  • 开放性: ROCm 开源生态,避免被 CUDA 绑定
  • 但很多开发者担心:ROCm 真的成熟了吗?迁移成本高吗?

    带着这些问题,我决定通过真实的云端实测来验证。


    🛠️ 2. 环境准备:在 ModelScope 申请 AMD 实例

    2.1 注册与登录

    访问 魔搭创空间,使用阿里云账号或手机号注册。

    ModelScope 登录页面

    2.2 创建 Notebook 实例

    点击"创建实例",选择以下配置:

    配置项推荐值说明
    实例类型 AMD Instinct MI300X 192GB HBM3 显存
    镜像 ROCm 6.2 + PyTorch 2.3 官方预装镜像
    系统盘 100 GB SSD 存放模型与数据
    区域 华北-北京 低延迟访问
    时长 按需计费 测试阶段建议按小时

    在这里插入图片描述

    💡 提示: 首次用户可申请 AMD 提供的免费算力券(通常 50-100 小时),足够完成本系列所有实验。

    2.3 启动实例并连接

    点击"启动"后,等待 2-3 分钟,实例状态变为"运行中"。点击"打开 JupyterLab"进入开发环境。

    实例

    🔍 3. 验证 ROCm 环境

    3.1 检查 ROCm 版本

    打开 JupyterLab 的终端(Terminal),执行:

    # 查看 ROCm 版本信息
    rocminfo | grep "Version"

    # 预期输出:
    # Version: 6.2.0

    3.2 查看 GPU 状态

    # 显示 GPU 详细信息
    rocm-smi –showproductname
    rocm-smi –showmeminfo vram
    rocm-smi –showuse

    # 预期输出:
    # ======================== ROCm System Management Interface ========================
    # ================================= Product Info ==================================
    # GPU[0] : Card series: AMD Instinct MI300X
    # GPU[0] : Card model: 0x74a1
    # GPU[0] : Card vendor: Advanced Micro Devices, Inc. [AMD/ATI]
    #
    # ================================== Memory Info ==================================
    # GPU[0] VRAM Total Memory: 196608 MB
    # GPU[0] VRAM Used Memory: 512 MB
    #
    # =================================== Utilization =================================
    # GPU[0] GPU Use (%): 0

    关键指标解读:

    • ✅ GPU 型号: AMD Instinct MI300X
    • ✅ 总显存: 196608 MB (192 GB)
    • ✅ 已用显存: 512 MB (系统占用)
    • ✅ GPU 利用率: 0% (空闲状态)

    🚀 4. Hello World:运行第一个 PyTorch ROCm 程序

    4.1 验证 PyTorch ROCm 支持

    在 JupyterLab 中创建一个新的 Python Notebook,执行:

    import torch

    print("=" * 60)
    print("PyTorch ROCm Environment Check")
    print("=" * 60)

    # 1. 检查 CUDA (ROCm) 是否可用
    print(f"\\n✅ CUDA (ROCm) available: {torch.cuda.is_available()}")

    # 2. 查看 GPU 数量
    print(f"📊 Device count: {torch.cuda.device_count()}")

    # 3. 查看 GPU 名称
    if torch.cuda.is_available():
    print(f"🎯 Current device: {torch.cuda.get_device_name(0)}")
    print(f"💾 Total memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
    print(f"💾 Used memory: {torch.cuda.memory_allocated(0) / 1e9:.2f} GB")
    else:
    print("❌ ROCm not available! Please check your installation.")

    print("\\n" + "=" * 60)

    预期输出:

    ============================================================
    PyTorch ROCm Environment Check
    ============================================================

    ✅ CUDA (ROCm) available: True
    📊 Device count: 1
    🎯 Current device: AMD Instinct MI300X
    💾 Total memory: 196.61 GB
    💾 Used memory: 0.00 GB

    ============================================================

    💡 关键发现: PyTorch 中依然使用 torch.cuda 接口调用 AMD GPU,这是 ROCm 的兼容层设计,代码几乎无需修改!


    4.2 矩阵乘法性能测试

    接下来,我们运行一个标准的矩阵乘法 benchmark,对比 CPU 与 GPU 的性能差异:

    import torch
    import time

    print("=" * 60)
    print("Matrix Multiplication Benchmark")
    print("=" * 60)

    # 定义矩阵大小
    matrix_size = 10000
    num_iterations = 10

    # CPU 测试
    print(f"\\n🖥️ Testing on CPU ({matrix_size}x{matrix_size})…")
    cpu_a = torch.randn(matrix_size, matrix_size)
    cpu_b = torch.randn(matrix_size, matrix_size)

    start_time = time.time()
    for _ in range(num_iterations):
    cpu_c = torch.matmul(cpu_a, cpu_b)
    cpu_time = (time.time() start_time) / num_iterations * 1000

    print(f" Average time: {cpu_time:.2f} ms")

    # GPU (ROCm) 测试
    if torch.cuda.is_available():
    device = torch.device("cuda:0")
    print(f"\\n🎮 Testing on GPU ({matrix_size}x{matrix_size})…")

    gpu_a = torch.randn(matrix_size, matrix_size, device=device)
    gpu_b = torch.randn(matrix_size, matrix_size, device=device)

    # 预热
    for _ in range(5):
    torch.matmul(gpu_a, gpu_b)
    torch.cuda.synchronize()

    # 正式测试
    start_event = torch.cuda.Event(enable_timing=True)
    end_event = torch.cuda.Event(enable_timing=True)

    start_event.record()
    for _ in range(num_iterations):
    gpu_c = torch.matmul(gpu_a, gpu_b)
    end_event.record()

    torch.cuda.synchronize()
    gpu_time = start_event.elapsed_time(end_event) / num_iterations

    print(f" Average time: {gpu_time:.2f} ms")
    print(f" Speedup: {cpu_time / gpu_time:.2f}x")

    # 计算 TFLOPS
    operations = 2 * matrix_size ** 3
    tflops = (operations / gpu_time / 1e9) * 1000
    print(f" Performance: {tflops:.2f} TFLOPS")

    print("\\n" + "=" * 60)

    实测结果(MI300X):

    ============================================================
    Matrix Multiplication Benchmark
    ============================================================

    🖥️ Testing on CPU (10000×10000)…
    Average time: 8523.45 ms

    🎮 Testing on GPU (10000×10000)…
    Average time: 125.67 ms
    Speedup: 67.82x
    Performance: 15.92 TFLOPS

    ============================================================

    性能对比分析:

    平台平均耗时加速比理论峰值实测性能效率
    CPU (Intel Xeon) 8523 ms 1x
    GPU (MI300X) 125.67 ms 67.82x 362 TFLOPS 15.92 TFLOPS 4.4%

    💡 说明: 简单矩阵乘法无法充分利用 MI300X 的计算单元,实际深度学习任务中效率可达 30-50%。


    ⚠️ 5. 常见问题与解决方案

    Q1: torch.cuda.is_available() 返回 False

    可能原因:

  • PyTorch 未正确安装 ROCm 版本
  • 用户权限不足
  • 解决方案:

    # 1. 确认安装了 ROCm 版 PyTorch
    pip list | grep torch

    # 应该看到:
    # torch 2.3.0+rocm6.2

    # 2. 如果安装的是 CPU 版本,重新安装:
    pip uninstall torch torchvision torchaudio
    pip install torch torchvision torchaudio –index-url https://download.pytorch.org/whl/rocm6.2

    # 3. 检查用户组权限
    groups $USER

    # 确保包含 video 和 render 组
    sudo usermod -aG video,render $USER

    # 4. 重启 JupyterLab


    Q2: rocm-smi 命令找不到

    可能原因: ROCm 工具未加入 PATH

    解决方案:

    # 临时添加
    export PATH=/opt/rocm/bin:$PATH

    # 永久添加(写入 ~/.bashrc)
    echo 'export PATH=/opt/rocm/bin:$PATH' >> ~/.bashrc
    source ~/.bashrc


    Q3: 显存占用异常高

    可能原因: 之前的进程未释放显存

    解决方案:

    # Python 中清理显存
    import torch
    torch.cuda.empty_cache()

    # 或者重启 Jupyter Kernel


    📊 6. 阶段性总结

    通过本次环境搭建,我确认了:

    ✅ ModelScope 的 AMD 实例开箱即用,ROCm 6.2 预装完善 ✅ PyTorch 对 ROCm 的支持非常成熟,torch.cuda 接口完全兼容 ✅ MI300X 性能强劲,矩阵乘法加速比达 67x ✅ 192GB 大显存优势明显,可轻松加载 70B+ 参数大模型


    🔜 7. 下一篇预告

    在《第二部分:CUDA 到 ROCm 迁移实战》中,我将:

  • 迁移 YOLOv8 目标检测模型: 从 NVIDIA A100 到 AMD MI300X
  • 性能对标测试: FPS、延迟、显存占用全面对比
  • 算子兼容性排查: 记录 3-5 个典型报错及修复方案
  • 成本效益分析: 计算 MI300X vs A100 的性价比

  • 👍 如果本文对你有帮助,欢迎点赞、收藏、转发! 💬 如果你在 ROCm 环境搭建中遇到问题,请在评论区留言,我会逐一回复! ✍️ 行文仓促,定有不足之处,欢迎各位朋友在评论区批评指正,不胜感激!

    专栏导航:

    • 📖 上一篇: –
    • 📖 下一篇: CUDA 到 ROCm 迁移实战:YOLOv8(内容准备中)

    参考资料:

    • AMD ROCm 官方文档
    • PyTorch for ROCm
    • 魔搭创空间 AMD 实例
    赞(0)
    未经允许不得转载:171主机测评 » 【AMD ROCm 实战】云端 AI 开发系列(一):在 ModelScope 上部署 MI300X 并运行第一个 PyTorch 程序
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址