MSAdapter
简体中文 | English
MindSpore适配PyTorch接口工具。
简介
MSAdapter可以使PyTorch的模型在昇腾(Ascend)上运行。
- 模型支持: MSAdapter的目的是支持PyTorch语法的原生态表达,用户只需要将PyTorch源代码中
import torch
替换为import ms_adapter.pytorch
即可实现模型能支持昇腾上训练。
模型中所使用的APIs支持状态可以从这里找到 Supported List.
- MSAdapter目前主要适配PyTorch的数据处理和模型结构部分代码,目前完全支持MindSpore的PYNATIVE模式下训练,部分网络结构支持GRAPH模式训练。
训练过程部分代码需要用户自定义编写。
安装
安装MindSpore
请根据MindSpore官网安装指南,安装2.0.0及以上版本的MindSpore。
安装MSAdapter
通过pip安装 (待版本发布后)
pip install ms_adapter
通过源码安装
git clone https://git.openi.org.cn/OpenI/MSAdapter.git
cd MSAdapter
python setup.py install
如果出现权限不足的提示,请按照如下方式安装:
python setup.py install --user || exit 1
使用
在构建模型上,MSAdapter可以和PyTorch一样使用,示例如下:
from ms_adapter.pytorch.nn import Module, Linear, Flatten
class MLP(Module):
def __init__(self):
super(MLP, self).__init__()
self.flatten = Flatten()
self.line1 = Linear(in_features=1024, out_features=64)
self.line2 = Linear(in_features=64, out_features=128, bias=False)
self.line3 = Linear(in_features=128, out_features=10)
def forward(self, inputs):
x = self.flatten(inputs)
x = self.line1(x)
x = self.line2(x)
x = self.line3(x)
return x
net = MLP()
对于网络迁移工作,从PyTorch代码迁移到MindSpore只需修改导入包。
资源
- 模型库:MSAdapter支持丰富的深度学习应用,这里给出了从PyTorch官方代码迁移到MSAdapter模型。已验证模型资源
FAQ
Q:设置context.set_context(mode=context.GRAPH_MODE)后运行出现类似问题:Tensor.add_
is an in-place operation and "x.add_()" is not encouraged to use in MindSpore static graph mode. Please use "x = x.add()" or other API instead。
A:目前在设置GRAPH模式下不支持原地操作相关的接口,需要按照提示信息进行修改。需要注意的是,即使在PYNATIVE模式下,原地操作相关接口也是不鼓励使用的,因为目前在MSAdapter不会带来内存收益,而且会给反向梯度计算带来不确定性。
Q:运行代码出现类似报错信息:AttributeError: module 'ms_adapter.pytorch' has no attribute 'xxx'。
A:首先确定'xxx'是否为torch 1.12版本支持的接口,PyTorch官网明确已废弃或者即将废弃的接口和参数,MSAdapter不会兼容支持,请使用其他同等功能的接口代替。如果是PyTorch对应版本支持,而MSAdapter中暂时没有,欢迎参与MSAdapter项目贡献你的代码,也可以通过创建任务(New issue)反馈需求。