diff --git a/tutorials/source_en/compile/statements.md b/tutorials/source_en/compile/statements.md index 9b3582d442484b375a53750ddfa0f86fdf96fa42..84b3f508e7fe3e218417f67e8228a05c2f6c0396 100644 --- a/tutorials/source_en/compile/statements.md +++ b/tutorials/source_en/compile/statements.md @@ -212,6 +212,8 @@ Restrictions: - If `cond` is not a constant, the variable or constant assigned to a same sign in different branches should have same data type. If the data type of assigned variables or constants is `Tensor`, the variables and constants should have same shape and element type. +- In the graph mode, variables must be defined before use. Defining them inside control flow and using them externally will result in an error, as shown in Example 4. + Example 1: ```python @@ -300,6 +302,24 @@ The result is as follows: ret:1 ``` +Example 4: If variable z is used outside the control flow, it must be defined externally. Otherwise, an error will be raised: UnboundLocalError: The local variable 'z' is not defined in false branch, but defined in true branch. + +```python +import mindspore + +x = mindspore.tensor([1, 4], mindspore.int32) +y = mindspore.tensor([0, 3], mindspore.int32) + +@mindspore.jit +def test_if_cond(x, y): + if (x > y).any(): + z = x + 1 + return z + +ret = test_if_cond(x, y) +print('ret:{}'.format(ret)) +``` + ### Loop Statements #### for Statements @@ -320,7 +340,9 @@ Restrictions: - The `for...else...` statement is not supported. -Example: +- In graph mode, variables must be defined before use. If a variable is defined inside a control flow (e.g., a for loop) but used outside, an error will occur. See Example 2 for illustration. + +Example1: ```python import numpy as np @@ -346,6 +368,22 @@ ret:[[7. 7. 7.] [7. 7. 7.]] ``` +Example 2: This will raise an error: NameError: The name 'z' is not defined, or not supported in graph mode. + +```python +import mindspore + +@mindspore.jit +def test_cond(): + x = (1, 2, 3) + for i in x: + z += i + return z + +ret = test_cond() +print('ret:{}'.format(ret)) +``` + #### while Statements Usage: @@ -364,6 +402,8 @@ Restrictions: - The `while...else...` statement is not supported. +- In graph mode, variables must be defined before use. If a variable is defined inside a control flow (e.g., a while loop) but used outside, an error will occur. See Example 3 for illustration. + Example 1: ```python @@ -422,6 +462,22 @@ The result is as follows: ret:15 ``` +Example 3: UnboundLocalError: The local variable 'z' defined in the 'while' loop body cannot be used outside of the loop body. Please define variable 'z' before 'while'. + +```python +import mindspore as ms + +@ms.jit() +def test_cond(x, y): + while x < y: + x += 1 + z = x + y + return z + +ret = test_cond(1, 5) +print('ret:{}'.format(ret)) +``` + ### Function Definition Statements #### def Keyword diff --git a/tutorials/source_zh_cn/compile/statements.ipynb b/tutorials/source_zh_cn/compile/statements.ipynb index ee1f0b6a210a8e4816f2bcc166e72b1808a35316..5c479a551074c206c30211fdcb27633d77542445 100644 --- a/tutorials/source_zh_cn/compile/statements.ipynb +++ b/tutorials/source_zh_cn/compile/statements.ipynb @@ -329,6 +329,8 @@ "\n", "- 如果`cond`不为常量,在不同分支中同一符号被赋予的变量或者常量的数据类型应一致。如果是被赋予变量或者常量数据类型是`Tensor`,则要求`Tensor`的type和shape也应一致。\n", "\n", + "- 图模式中,要求变量必须在使用前定义。在控制流内部定义,外部使用将会报错。例如示例4。\n", + "\n", "示例1:" ] }, @@ -421,8 +423,7 @@ "id": "d2148097", "metadata": {}, "source": [ - "`if`分支中`out`被赋值的变量或者常量`m`与`else`分支中`out`被赋值的变量或者常量`n`的数据类型必须一致。\n", - "\n" + "`if`分支中`out`被赋值的变量或者常量`m`与`else`分支中`out`被赋值的变量或者常量`n`的数据类型必须一致。" ] }, { @@ -471,7 +472,30 @@ "metadata": {}, "source": [ "`if`分支中`out`被赋值的变量或者常量`m`与`out`初始赋值的数据类型必须一致。\n", - "\n" + "\n", + "示例4:在控制流外部使用变量z,需要在外部定义。否则将报错:UnboundLocalError: The local variable 'z' is not defined in false branch, but defined in true branch." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3c2df5b", + "metadata": {}, + "outputs": [], + "source": [ + "import mindspore\n", + "\n", + "x = mindspore.tensor([1, 4], mindspore.int32)\n", + "y = mindspore.tensor([0, 3], mindspore.int32)\n", + "\n", + "@mindspore.jit\n", + "def test_if_cond(x, y):\n", + " if (x > y).any():\n", + " z = x + 1\n", + " return z\n", + "\n", + "ret = test_if_cond(x, y)\n", + "print('ret:{}'.format(ret))" ] }, { @@ -499,7 +523,9 @@ "\n", "- 不支持`for...else...`语句。\n", "\n", - "示例:" + "- 图模式中,要求变量必须在使用前定义。for循环后要使用的变量,在控制流内部定义,外部使用将会报错。例如示例2。\n", + "\n", + "示例1:" ] }, { @@ -534,6 +560,34 @@ "print('ret:{}'.format(ret))" ] }, + { + "cell_type": "markdown", + "id": "5fe31ded", + "metadata": {}, + "source": [ + "示例2:将报错:NameError: The name 'z' is not defined, or not supported in graph mode." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6f0dc01c", + "metadata": {}, + "outputs": [], + "source": [ + "import mindspore\n", + "\n", + "@mindspore.jit\n", + "def test_cond():\n", + " x = (1, 2, 3)\n", + " for i in x:\n", + " z += i\n", + " return z\n", + "\n", + "ret = test_cond()\n", + "print('ret:{}'.format(ret))" + ] + }, { "cell_type": "markdown", "id": "0b7a6630", @@ -557,6 +611,8 @@ "\n", "- 不支持`while...else...`语句。\n", "\n", + "- 图模式中,要求变量必须在使用前定义。while循环后要使用的变量,在控制流内部定义,外部使用将会报错。例如示例3。\n", + "\n", "示例1:" ] }, @@ -652,6 +708,32 @@ "\n" ] }, + { + "cell_type": "markdown", + "id": "b94b195a", + "metadata": {}, + "source": [ + "示例3:将报错:UnboundLocalError: The local variable 'z' defined in the 'while' loop body cannot be used outside of the loop body. Please define variable 'z' before 'while'." + ] + }, + { + "cell_type": "markdown", + "id": "999e0b2f", + "metadata": {}, + "source": [ + "import mindspore as ms\n", + "\n", + "@ms.jit()\n", + "def test_cond(x, y):\n", + " while x < y:\n", + " x += 1\n", + " z = x + y\n", + " return z\n", + "\n", + "ret = test_cond(1, 5)\n", + "print('ret:{}'.format(ret))" + ] + }, { "cell_type": "markdown", "id": "277d411d",