MSAdapter is a toolkit for support the PyTorch model running on Ascend.
Project description
Introduction
MSAdapter is MindSpore tool for adapting the PyTorch interface, which is designed to make PyTorch code perform efficiently on Ascend without changing the habits of the original PyTorch users.
Install
MSAdapter has some prerequisites that need to be installed first, including MindSpore, PIL, NumPy.
# for last stable version
pip install msadapter
# for latest release candidate
pip install --upgrade --pre msadapter
Alternatively, you can install the latest or development version by directly pulling from OpenI:
pip3 install git+https://openi.pcl.ac.cn/OpenI/MSAdapter.git
User guide
For data processing and model building, MSAdapter can be used in the same way as PyTorch, while the model training part of the code needs to be customized, as shown in the following example.
Data processing (only modify the import package)
from msadapter.pytorch.utils.data import DataLoader
from msadapter.torchvision import datasets, transforms
transform = transforms.Compose([transforms.Resize((224, 224), interpolation=InterpolationMode.BICUBIC),
transforms.ToTensor(),
transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.247, 0.2435, 0.2616])
])
train_images = datasets.CIFAR10('./', train=True, download=True, transform=transform)
train_data = DataLoader(train_images, batch_size=128, shuffle=True, num_workers=2, drop_last=True)
Model construction (modify import package only)
from msadapter.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
3.Model training (custom training)
import msadapter.pytorch as torch
import msadapter.pytorch.nn as nn
import mindspore as ms
net = MLP()
net.train()
epochs = 500
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9, weight_decay=0.0005)
# Define the training process
loss_net = ms.nn.WithLossCell(net, criterion)
train_net = ms.nn.TrainOneStepCell(loss_net, optimizer)
for i in range(epochs):
for X, y in train_data:
res = train_net(X, y)
print("epoch:{}, loss:{:.6f}".format(i, res.asnumpy()))
# Save model
ms.save_checkpoint(net, "save_path.ckpt")
License
MSAdapter is released under the Apache 2.0 license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file msadapter-0.1.0b0.tar.gz
.
File metadata
- Download URL: msadapter-0.1.0b0.tar.gz
- Upload date:
- Size: 423.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecabb65d1348202ba84e237c0c3096c6d86af118ac0d61182ada719ca5f3b63c |
|
MD5 | 3d02b5acafbc29a6eb3fc82182a8c410 |
|
BLAKE2b-256 | 18259c218265fbfbdc777ced07f7bf2c01ff05a2ad9b6b42be82e879a8415090 |
File details
Details for the file msadapter-0.1.0b0-py3-none-any.whl
.
File metadata
- Download URL: msadapter-0.1.0b0-py3-none-any.whl
- Upload date:
- Size: 573.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 922bafca55c5097a1068b16fe3542fd10d0ca2e1902989b2c3b54fb250e9a2d2 |
|
MD5 | f8bce87c91e72e1cacf2ad20519fc7d8 |
|
BLAKE2b-256 | 61d10a046bfd33093831998b0d42c506a081dc56377f8fa0d0555893d580ef46 |