From cf7c063c3f7f58f3934e8939718625f665c8ac6f Mon Sep 17 00:00:00 2001 From: huan <3174348550@qq.com> Date: Wed, 20 Aug 2025 15:26:37 +0800 Subject: [PATCH] modify code inconsistency --- .../docs/source_en/infer/quick_start_c.md | 2 +- tutorials/source_en/compile/statements.md | 17 ++++++++++++-- tutorials/source_en/compile/static_graph.md | 23 ++++++++++++++++++- .../source_zh_cn/compile/static_graph.md | 11 +++------ .../debug/error_analysis/mindir.md | 2 +- .../debug/error_analysis/mindrt_debug.md | 6 ++--- 6 files changed, 45 insertions(+), 16 deletions(-) diff --git a/docs/lite/docs/source_en/infer/quick_start_c.md b/docs/lite/docs/source_en/infer/quick_start_c.md index df3ed319d2..9877c016c3 100644 --- a/docs/lite/docs/source_en/infer/quick_start_c.md +++ b/docs/lite/docs/source_en/infer/quick_start_c.md @@ -92,7 +92,7 @@ Performing inference with MindSpore Lite consists of the following main steps: When the execution is completed, the following results will be obtained. Print the name, the size and the number of the output Tensor and the first 50 data: ```text - Tensor name: Softmax-65, tensor size is 4004 ,elements num: 1001. + Tensor name: Softmax-65, tensor size is 4004, elements num: 1001. output data is: 0.000011 0.000015 0.000015 0.000079 0.000070 0.000702 0.000120 0.000590 0.000009 0.000004 0.000004 0.000002 0.000002 0.000002 0.000010 0.000055 0.000006 0.000010 0.000003 0.000010 0.000002 0.000005 0.000001 0.000002 0.000004 0.000006 0.000008 0.000003 0.000015 0.000005 0.000011 0.000020 0.000006 0.000002 0.000011 0.000170 0.000005 0.000009 0.000006 0.000002 0.000003 0.000009 0.000005 0.000006 0.000003 0.000011 0.000005 0.000027 0.000003 0.000050 0.000016 ``` diff --git a/tutorials/source_en/compile/statements.md b/tutorials/source_en/compile/statements.md index 84b3f508e7..8a2a00ae23 100644 --- a/tutorials/source_en/compile/statements.md +++ b/tutorials/source_en/compile/statements.md @@ -137,6 +137,11 @@ res = foo() assert res is None ``` +```text +x: +3 +``` + ### break Statements The `break` statement is used to terminate a loop statement, i.e., it stops execution of the loop statement even if the loop condition does not have a `False` condition or if the sequence is not fully recursive, usually used in `while` and `for` loops. In nested loops, the `break` statement stops execution of the innermost loop. @@ -660,8 +665,16 @@ The use of multi-layer nested iterators is not supported in graph mode. The example usage of the restriction is as follows (two levels of iterators are used): ```python -x = ({'a': 1, 'b': 2}, {'d': 1, 'e': 2}, {'g': 1, 'h': 2}) -res = {k: v for y in x for (k, v) in y.items()} +import mindspore + +@mindspore.jit +def test(): + x = ({'a': 1, 'b': 2}, {'d': 1, 'e': 2}, {'g': 1, 'h': 2}) + res = {k: v for y in x for (k, v) in y.items()} + return res + +ret = test() +print('ret:{}'.format(ret)) ``` An error will be prompted: diff --git a/tutorials/source_en/compile/static_graph.md b/tutorials/source_en/compile/static_graph.md index a6a0738034..0e415bb74e 100644 --- a/tutorials/source_en/compile/static_graph.md +++ b/tutorials/source_en/compile/static_graph.md @@ -1090,7 +1090,7 @@ ret:[[3. 3. 3. 3.]] Currently supported Python statements include raise statement, assert statement, pass statement, return statement, break statement, continue statement, if statement, for statement, while statement, with statement, list comprehension, generator expression and function definition statement. For more details, please refer to -[Statements](https://www.mindspore.cn/tutorials/en/master/compile/statements.html) +[Statements](https://www.mindspore.cn/tutorials/en/master/compile/statements.html). ### Python Built-in Functions @@ -2019,6 +2019,13 @@ res = net() assert res is None ``` +The results are as follows: + +``` text +x: +3 +``` + As in the example below, \'None\' is used as the default input parameter for the top graph. @@ -2042,6 +2049,14 @@ res = net(x) assert res is None ``` +The results are as follows: + +``` text +y is None +x: +[1, 2] +``` + ### Built-in Functions Support More Data Types Extend the support for built-in functions. Python built-in functions @@ -2205,6 +2220,12 @@ shape is (2, 2) net() ``` + The results are as follows: + + ``` text + net.m is 3 + ``` + - Set and modify Cell objects and jit_class objects in the static graph Supporting property modification of objects jit_class graph mode, diff --git a/tutorials/source_zh_cn/compile/static_graph.md b/tutorials/source_zh_cn/compile/static_graph.md index f2200e4aba..fced9b6089 100644 --- a/tutorials/source_zh_cn/compile/static_graph.md +++ b/tutorials/source_zh_cn/compile/static_graph.md @@ -7,21 +7,16 @@ 在即时编译(Just-In-Time Compilation,JIT)模式下,Python代码并不是由Python解释器直接执行,而是先将代码编译成静态计算图,再执行该静态计算图。 -在静态图模式下,MindSpore通过源码转换的方式,将Python的源码转换成中间表达IR(Intermediate -Representation),并在此基础上对IR图进行优化,最终在硬件设备上执行优化后的图。MindSpore使用基于图表示的函数式IR,称为MindIR。 +在静态图模式下,MindSpore通过源码转换的方式,将Python的源码转换成中间表达IR(Intermediate Representation),并在此基础上对IR图进行优化,最终在硬件设备上执行优化后的图。MindSpore使用基于图表示的函数式IR,称为MindIR。 目前,将Python源码转换为中间表示(IR)的方法主要有三种:基于抽象语法树(Abstract -Syntax Tree, -AST)的解析、基于字节码(ByteCode)的解析,以及基于算子调用追踪(Trace)的方法 -。这三种模式在语法支持程度上存在一定差异。本文档将首先详细阐述基于抽象语法树(AST)场景下的语法支持情况,随后分别介绍基于字节码(ByteCode)和基于算子追踪(Trace)方式构建计算图时,语法支持的差异。 +Syntax Tree, AST)的解析、基于字节码(ByteCode)的解析,以及基于算子调用追踪(Trace)的方法。这三种模式在语法支持程度上存在一定差异。本文档将首先详细阐述基于抽象语法树(AST)场景下的语法支持情况,随后分别介绍基于字节码(ByteCode)和基于算子追踪(Trace)方式构建计算图时,语法支持的差异。 MindSpore的静态图执行过程实际包含两步,对应静态图的Define和Run阶段,但在实际使用中,在实例化的Cell对象被调用时用户并不会分别感知到这两阶段,MindSpore已将这两阶段均封装在Cell的`__call__`方法中,因此实际调用过程为: `model(inputs) = model.compile(inputs) + model.construct(inputs)`,其中`model`为实例化的Cell对象。 -即时编译可以使用 [JIT接口](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/mindspore.jit.html) -,或者通过设置`ms.set_context(mode=ms.GRAPH_MODE)`进入Graph模式,并在`Cell`类的`construct`函数中编写执行代码,此时`construct`函数的代码将会被编译成静态计算图。`Cell`定义详见[Cell -API文档](https://www.mindspore.cn/docs/zh-CN/master/api_python/nn/mindspore.nn.Cell.html)。 +即时编译可以使用 [JIT接口](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/mindspore.jit.html),或者通过设置`ms.set_context(mode=ms.GRAPH_MODE)`进入Graph模式,并在`Cell`类的`construct`函数中编写执行代码,此时`construct`函数的代码将会被编译成静态计算图。`Cell`定义详见[Cell API文档](https://www.mindspore.cn/docs/zh-CN/master/api_python/nn/mindspore.nn.Cell.html)。 由于语法解析的限制,当前在编译构图时,支持的数据类型、语法以及相关操作并没有完全与Python语法保持一致,部分使用受限。借鉴传统JIT编译的思路,从图模式的角度考虑动静图的统一,扩展图模式的语法能力,使得静态图提供接近动态图的语法使用体验,从而实现动静统一。为了便于用户选择是否扩展静态图语法,提供了JIT语法支持级别选项`jit_syntax_level`,其值必须在\[STRICT,LAX\]范围内,选择`STRICT`则认为使用基础语法,不扩展静态图语法。默认值为`LAX`。全部级别都支持所有后端。 diff --git a/tutorials/source_zh_cn/debug/error_analysis/mindir.md b/tutorials/source_zh_cn/debug/error_analysis/mindir.md index 85c6b8593c..95c2a37083 100644 --- a/tutorials/source_zh_cn/debug/error_analysis/mindir.md +++ b/tutorials/source_zh_cn/debug/error_analysis/mindir.md @@ -237,7 +237,7 @@ print(out) 对于具体的图来说(此处以图`@19_1___main___Net_construct_304`为例): -- 第23-81行展示了图结构的信息,图中含有若干个节点,即`CNode`。该图包含`Sub`、`Add`、`Mul`这些在网路所调用的接口中所用到的算子。 +- 第23-81行展示了图结构的信息,图中含有若干个节点,即`CNode`。该图包含`Sub`、`Add`、`Mul`这些在网络所调用的接口中所用到的算子。 `CNode`的信息遵循如下格式,从左到右分别为序号、节点名称-debug_name、算子名称-op_name、输入节点-arg、节点的属性-primitive_attrs、输入和输出的规格、源码解析调用栈等信息。 由于ANF图为单向无环图,所以此处仅根据输入关系来体现节点与节点的连接关系。关联代码行则体现了`CNode`与脚本源码之间的关系,例如第75行表明该节点是由脚本中`if b`这一行解析而来。 diff --git a/tutorials/source_zh_cn/debug/error_analysis/mindrt_debug.md b/tutorials/source_zh_cn/debug/error_analysis/mindrt_debug.md index 495c4766bc..0abe5685c9 100644 --- a/tutorials/source_zh_cn/debug/error_analysis/mindrt_debug.md +++ b/tutorials/source_zh_cn/debug/error_analysis/mindrt_debug.md @@ -2,11 +2,11 @@ [![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/tutorials/source_zh_cn/debug/error_analysis/mindrt_debug.md)   -静态图模式下,网络构建与训练过程的常见的报错类型如下所示: +静态图模式下,网络构建与训练过程的常见报错类型如下所示: ## context配置问题 -执行网络训练时,需要指定后端设备,使用方式是:`set_context(device_target=device)`。MindSpore支持CPU,GPU和昇腾后端Ascend。如果在GPU设备上,错误指定后端设备为Ascend,即`set_context(device_target="Ascend")`,会得到如下报错信息: +执行网络训练时,需要指定后端设备,使用方式是:`set_context(device_target=device)`。MindSpore支持CPU、GPU和昇腾后端Ascend。如果在GPU设备上错误指定后端设备为Ascend,即`set_context(device_target="Ascend")`,会得到如下报错信息: ```python ValueError: For 'set_context', package type mindspore-gpu support 'device_target' type gpu or cpu, but got Ascend. @@ -24,7 +24,7 @@ ValueError: For 'set_context', package type mindspore-gpu support 'device_target ### construct函数参数错误 -MindSpore中神经网络的基本构成单元为`nn.Cell`。模型或神经网络层应当继承该基类。基类的成员函数`construct`是定义要执行的计算逻辑,所有继承类都必须重写此方法。`construct`函数的定义原型为: +MindSpore中神经网络的基本构成单元为`nn.Cell`。模型或神经网络层应当继承该基类。基类的成员函数`construct`定义要执行的计算逻辑,所有继承类都必须重写此方法。`construct`函数的定义原型为: ```python def construct(self, *inputs, **kwargs): -- Gitee