diff --git a/tutorials/source_en/model_infer/ms_infer/model_export.md b/tutorials/source_en/model_infer/ms_infer/model_export.md index d7cc5ee196440c15374598ceb9fe6e0369a132f3..ef787b2a71f64d34d5f0dda9770e3f7fd4631934 100644 --- a/tutorials/source_en/model_infer/ms_infer/model_export.md +++ b/tutorials/source_en/model_infer/ms_infer/model_export.md @@ -20,3 +20,50 @@ ms.export(net, input_tensor, file_name='lenet', file_format='MINDIR') For details about the API, see [mindspore.export](https://www.mindspore.cn/docs/en/master/api_python/mindspore/mindspore.export.html?highlight=export#mindspore.export). The model export result is provided for MindSpore Lite. For details about how to use the result, see [Lite Inference Overview](../lite_infer/overview.md). + +## Syntax Support Scope + +Not all Python syntax and data types are supported for MindIR export. MindIR export has a specific support scope, and if the syntax falls outside this scope, an error will be reported during the export process. + +First, MindIR export only supports **strict-level graph mode**. For detailed support scope, please refer to the [Static Graph Syntax Support Documentation](https://www.mindspore.cn/tutorials/en/master/compile/static_graph.html). + +Second, in addition to the syntax restrictions of strict-level graph mode, MindIR has additional constraints on the types of return values. For example, returning `mindspore.dtype` is not supported. The following program will raise an error during MindIR export. + +```python +import mindspore as ms +from mindspore import nn, ops, Tensor + +class Model(nn.Cell): + def __init__(self): + super().__init__() + self.dtype = ops.DType() + + def construct(self, x: Tensor) -> ms.dtype: + return self.dtype(x) +``` + +Furthermore, if a `Parameter` object is created outside of `nn.Cell`, MindIR does not support exporting that Parameter. This typically occurs in the following scenarios: + +- A `Parameter` is created directly in the global scope of the script. +- A `Parameter` is created in a non `nn.Cell` class. +- Random number generation api from the [mindspore.mint](https://www.mindspore.cn/docs/en/master/api_python/mindspore.mint.html) package are used, such as `mint.randn`, `mint.randperm`, etc., because these apis create `Parameter` objects in the global scope. + +For example, the following two programs will raise errors during the export process. + +```python +from mindspore import Tensor, Parameter, nn + +param = Parameter(Tensor([1, 2, 3, 4])) # Created outside nn.Cell + +class Model(nn.Cell): + def construct(self, x: Tensor) -> Tensor: + return x + param +``` + +```python +from mindspore import Tensor, nn, mint + +class Model(nn.Cell): + def construct(self, n: int) -> Tensor: + return mint.randn(n) +``` diff --git a/tutorials/source_zh_cn/model_infer/ms_infer/model_export.md b/tutorials/source_zh_cn/model_infer/ms_infer/model_export.md index b85d8551982ab9a365b3bb7589a09e7317c3a95f..4d20206bea59915e934db5b8071e3b7da4d1f033 100644 --- a/tutorials/source_zh_cn/model_infer/ms_infer/model_export.md +++ b/tutorials/source_zh_cn/model_infer/ms_infer/model_export.md @@ -20,3 +20,50 @@ ms.export(net, input_tensor, file_name='lenet', file_format='MINDIR') 详细接口参考链接:[mindspore.export](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/mindspore.export.html?highlight=export#mindspore.export)。 模型导出结果提供给MindSpore Lite使用,使用方式可参考[MindSpore Lite推理](../lite_infer/overview.md)。 + +## 语法支持范围 + +并不是所有的 Python 语法和数据类型都支持 MindIR 导出,MindIR 导出有特定的支持范围,若不在支持范围内,导出时会报错。 + +首先,MindIR 导出仅支持**严格图模式**,详细的支持范围,可参考[静态图语法支持](https://www.mindspore.cn/tutorials/zh-CN/master/compile/static_graph.html)。 + +其次,除了严格图模式的语法限制,MindIR 对返回值的类型有额外约束,比如不支持返回`mindspore.dtype`。例如下面的程序,MindIR 导出的时候就会报错。 + +```python +import mindspore as ms +from mindspore import nn, ops, Tensor + +class Model(nn.Cell): + def __init__(self): + super().__init__() + self.dtype = ops.DType() + + def construct(self, x: Tensor) -> ms.dtype: + return self.dtype(x) +``` + +另外,如果在`nn.Cell`外创建了`Parameter`对象,MindIR 不支持导出该`Parameter`。这种情况通常发生在: + +- 直接在脚本的全局作用域中创建了`Parameter`。 +- 在非`nn.Cell`类中创建了`Parameter`。 +- 使用了 [mindspore.mint](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore.mint.html) 包下的随机数生成接口,如`mint.randn`、`mint.randperm`等等,因为这些随机数接口会在全局作用域中创建`Parameter`。 + +例如下面的两个程序,在导出过程中,就会报错。 + +```python +from mindspore import Tensor, Parameter, nn + +param = Parameter(Tensor([1, 2, 3, 4])) # 在nn.Cell外创建 + +class Model(nn.Cell): + def construct(self, x: Tensor) -> Tensor: + return x + param +``` + +```python +from mindspore import Tensor, nn, mint + +class Model(nn.Cell): + def construct(self, n: int) -> Tensor: + return mint.randn(n) +```