mindconverter

MindConverter.

MindConverter is a migration tool to transform the model scripts from PyTorch to Mindspore. Users can migrate their PyTorch models to Mindspore rapidly with minor changes according to the conversion report.

mindconverter.pytorch2mindspore(model, dummy_inputs, output_dir=None)[source]

Convert PyTorch model to MindSpore model.

This function is to transform instantiated PyTorch model with PyTorch pre-trained CheckPoint to MindSpore model scripts and MindSpore CheckPoint file.

Parameters
  • model (torch.nn.Module) – The instantiated PyTorch model with pre-trained checkpoint loaded.

  • dummy_inputs (tuple<torch.tensor>) – Tuple of input tensors for the PyTorch model. The number of tensors, the shape and the data type of every tensor should be consistent with that of PyTorch model.

  • output_dir (str) – The directory path for generated files and migration reports. If not set, all results will be saved in $PWD/output. Default: None.

Raises
  • BaseConverterError – Unknown error occurred during runtime, please see the detail in mindconverter.log.

  • GraphInitFailError – Error in tracing the computational graph.

  • FileSaveError – Error in saving generated results.

  • GeneratorError – Error in generating code.

  • SubGraphSearchingError – Error in finding frequent sub-graph.

Examples

>>> import torch
>>> import numpy as np
>>> from transformers import BertModel
>>> from mindconverter import pytorch2mindspore
>>> model = BertModel.from_pretrained("bert-base-uncased")
>>> model.eval()
...
>>> input_ids = np.random.uniform(0, 100, (1, 512)).astype(np.int64)
>>> attention_mask = np.zeros((1, 512)).astype(np.int64)
>>> token_type_ids = np.zeros((1, 512)).astype(np.int64)
>>> dummy_inputs = (torch.tensor(input_ids), torch.tensor(attention_mask), torch.tensor(token_type_ids))
>>> with torch.no_grad():
...     model(*dummy_inputs)
...
>>> output_dir = "./output"
>>> pytorch2mindspore(model, dummy_inputs, output_dir)