> ## Documentation Index
> Fetch the complete documentation index at: https://docs.autodl.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 摩尔线程GPU

### 使用PyTorch

首先建议选择我们已经做好的基础镜像中PyTorch框架的镜像创建实例，然后对于基于NVIDIA的PyTorch代码，需要进行简单几步改造即可在摩尔线程GPU上运行，请看下方示例：

```python theme={null}
# NVIDIA PyTorch 代码
import torch

x = torch.randn(10000, 10000).cuda()
y = torch.randn(10000, 10000).cuda()
for _ in range(1000):
    z = x.mm(y)
print(z)


# 改造为摩尔线程GPU上可运行的代码
# 第一步：import torch后，立即import torch_musa（musa为摩尔的'cuda'）
# 第二步：cuda()函数换为musa()
import torch
import torch_musa

x = torch.randn(10000, 10000).musa()
y = torch.randn(10000, 10000).musa()
for _ in range(1000):
    z = x.mm(y)
print(z)
```

**更加复杂的例子**

以下是**NVIDIA** PyTorch ResNet网络使用MNIST数据集训练的代码

```bash theme={null}
# 下载代码
wget https://autodl-public.ks3-cn-beijing.ksyuncs.com/debug/dp_res18.py

# 执行代码训练，自动会下载MNIST数据集
python dp_res18.py
```

以下是基于上面代码改造后的可使用**摩尔线程GPU**的代码

```bash theme={null}
# 下载代码
wget https://autodl-public.ks3-cn-beijing.ksyuncs.com/npu_chips/moore_threads/dp_res18_mt.py

# 执行代码训练，自动会下载MNIST数据集
python dp_res18_mt.py
```

两者代码的差异：

```bash theme={null}
# diff dp_res18.py dp_res18_mt.py
8a9
> import torch_musa
29c30,31
< device = 'cuda' if torch.cuda.is_available() else 'cpu'
---
> device = 'musa' if torch.musa.is_available() else 'cpu'
> print("Detect device:", device)


# 从diff两个文件的结果可以看出，除多了import torch_musa和替换了cuda→musa，其他均相同
```

### 摩尔线程GPU使用率监控

**查看GPU使用率、显存使用量等**

使用`mthreads-gmi`命令

```bash theme={null}
# mthreads-gmi
Fri Apr 18 15:41:55 2025
---------------------------------------------------------------
    mthreads-gmi:1.14.0          Driver Version:2.7.0
---------------------------------------------------------------
ID   Name           |PCIe                |%GPU  Mem
     Device Type    |Pcie Lane Width     |Temp  MPC Capable
                                         |      ECC Mode
+-------------------------------------------------------------+
0    MTT S4000      |00000000:12:00.0    |0%    4MiB(49152MiB)
     Physical       |16x(16x)            |41C   YES
                                         |      N/A
---------------------------------------------------------------

---------------------------------------------------------------
Processes:
ID   PID       Process name                         GPU Memory
                                                         Usage
+-------------------------------------------------------------+
   No running processes found
---------------------------------------------------------------
```

更多算法和模型即将上线[AutoDL.art社区](https://www.autodl.art/image?page=1\&chip_corp_id=74)，您也可以访问摩尔线程官方[GitHub模型仓库](https://github.com/MooreThreads/tutorial_on_musa)获取更多模型！


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.