未验证 提交 2196220f 编写于 作者: L liym27 提交者: GitHub

[cherry-pick 2.0-beta][Dy2Stat] Fix doc of error_handling and debugging...

[cherry-pick 2.0-beta][Dy2Stat] Fix doc of error_handling and debugging according to reviews (#2620) (#2638)
上级 91556483
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
> **注解:** > **注解:**
> >
> 请确保转换前的动态图代码能够成功运行,建议使用[paddle.jit.ProgramTranslator().enable(False)](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable)关闭动转静功能,直接运行动态图,如下: > 请确保转换前的动态图代码能够成功运行,建议使用 [paddle.jit.ProgramTranslator().enable(False)](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable)关闭动转静功能,直接运行动态图,如下:
```python ```python
import paddle import paddle
...@@ -25,7 +25,7 @@ func(np.ones([3, 2])) ...@@ -25,7 +25,7 @@ func(np.ones([3, 2]))
## 断点调试 ## 断点调试
使用动转静功能时,您可以使用断点调试代码。 使用动转静功能时,您可以使用断点调试代码。
例如,在代码中,调用`pdb.set_trace()` 例如,在代码中,调用 `pdb.set_trace()`
```Python ```Python
import pdb import pdb
...@@ -52,7 +52,7 @@ func(np.ones([3, 2])) ...@@ -52,7 +52,7 @@ func(np.ones([3, 2]))
... ...
``` ```
如果您想在原始的动态图代码中使用调试器,请先调用[`paddle.jit.ProgramTranslator().enable(False)`](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable),如下: 如果您想在原始的动态图代码中使用调试器,请先调用 [`paddle.jit.ProgramTranslator().enable(False)`](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable),如下:
```python ```python
paddle.jit.ProgramTranslator().enable(False) paddle.jit.ProgramTranslator().enable(False)
func(np.ones([3, 2])) func(np.ones([3, 2]))
...@@ -68,7 +68,8 @@ func(np.ones([3, 2])) ...@@ -68,7 +68,8 @@ func(np.ones([3, 2]))
## 打印转换后的代码 ## 打印转换后的代码
您可以打印转换后的静态图代码,有2种方法: 您可以打印转换后的静态图代码,有2种方法:
1. 使用被装饰函数的`code` 属性 1. 使用被装饰后的函数的 `code` 属性
如下代码中,装饰器 `paddle.jit.to_static` 会将函数 `func` 转化为一个类对象 `StaticLayer`,可以使用 StaticLayer 的 `code` 属性来获得转化后的代码。
```Python ```Python
@paddle.jit.to_static @paddle.jit.to_static
def func(x): def func(x):
...@@ -97,9 +98,9 @@ func(np.ones([3, 2])) ...@@ -97,9 +98,9 @@ func(np.ones([3, 2]))
return x return x
``` ```
2. 使用`set_code_level(level)`或环境变量`TRANSLATOR_CODE_LEVEL=level` 2. 使用 `set_code_level(level)` 或环境变量 `TRANSLATOR_CODE_LEVEL=level`
通过调用`set_code_level`或设置环境变量`TRANSLATOR_CODE_LEVEL`,可以在log中查看转换后的代码: 通过调用 `set_code_level` 或设置环境变量 `TRANSLATOR_CODE_LEVEL`,可以在日志中查看转换后的代码:
```python ```python
@paddle.jit.to_static @paddle.jit.to_static
...@@ -129,10 +130,10 @@ func(np.ones([3, 2])) ...@@ -129,10 +130,10 @@ func(np.ones([3, 2]))
3, true_fn_0, false_fn_0, (x,), (x,), (x,)) 3, true_fn_0, false_fn_0, (x,), (x,), (x,))
return x return x
``` ```
`set_code_level` 函数可以设置查看不同的AST Transformer转化后的代码,详情请见[set_code_level]()<!--TODO:补充set_code_level文档链接--> `set_code_level` 函数可以设置查看不同的AST Transformer转化后的代码,详情请见 [set_code_level](../../../paddle/api/paddle/fluid/dygraph/jit/set_code_level_cn.html)
## 使用 `print` ## 使用 `print`
`print` 函数可以用来查看变量,该函数在动转静中会被转化。当仅打印Paddle Tensor时,实际运行时会被转换为Paddle算子[Print](../../api_cn/layers_cn/Print_cn.html),否则仍然运行`print` `print` 函数可以用来查看变量,该函数在动转静中会被转化。当仅打印 Paddle Tensor 时,实际运行时会被转换为 Paddle 算子 [Print](../../api_cn/layers_cn/Print_cn.html),否则仍然运行 `print`
```python ```python
@paddle.jit.to_static @paddle.jit.to_static
def func(x): def func(x):
...@@ -167,7 +168,7 @@ Here call print function. ...@@ -167,7 +168,7 @@ Here call print function.
## 日志打印 ## 日志打印
ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解动转静过程中函数是否被成功转换。 ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解动转静过程中函数是否被成功转换。
您可以调用`paddle.jit.set_verbosity(level)` 或设置环境变量`TRANSLATOR_VERBOSITY=level`来设置日志详细等级,并查看不同等级的日志信息。目前,`level`可以取值0-3: 您可以调用 [`paddle.jit.set_verbosity(level)`]((../../../paddle/api/paddle/fluid/dygraph/jit/set_verbosity_cn.html)) 或设置环境变量 `TRANSLATOR_VERBOSITY=level` 来设置日志详细等级,并查看不同等级的日志信息。目前,`level` 可以取值0-3:
- 0: 无日志 - 0: 无日志
- 1: 包括了动转静转化流程的信息,如转换前的源码、转换的可调用对象 - 1: 包括了动转静转化流程的信息,如转换前的源码、转换的可调用对象
- 2: 包括以上信息,还包括更详细函数转化日志 - 2: 包括以上信息,还包括更详细函数转化日志
...@@ -177,11 +178,11 @@ ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解 ...@@ -177,11 +178,11 @@ ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解
> >
> 日志中包括了源代码等信息,请在共享日志前确保它不包含敏感信息。 > 日志中包括了源代码等信息,请在共享日志前确保它不包含敏感信息。
可以在代码运行前调用`paddle.jit.set_verbosity`控制日志详细程度: 可以在代码运行前调用 `paddle.jit.set_verbosity` 控制日志详细程度:
```python ```python
paddle.jit.set_verbosity(3) paddle.jit.set_verbosity(3)
``` ```
或者设置环境变量`TRANSLATOR_VERBOSITY` 或者设置环境变量 `TRANSLATOR_VERBOSITY`
```python ```python
import os import os
os.environ["TRANSLATOR_VERBOSITY"] = '3' os.environ["TRANSLATOR_VERBOSITY"] = '3'
......
...@@ -71,7 +71,9 @@ func(np.ones([3, 2])) ...@@ -71,7 +71,9 @@ func(np.ones([3, 2]))
There are two ways to print the transformed static graph code: There are two ways to print the transformed static graph code:
1. Use the attribute `code` of the decorated function: 1. Use the attribute `code` of the decorated function
In the following code, the decorator `paddle.jit.to_static` transforms `func` into a class object `StaticLayer`. You can use the `code` attribute of `StaticLayer` to get the transformed code.
```Python ```Python
@paddle.jit.to_static @paddle.jit.to_static
def func(x): def func(x):
...@@ -128,7 +130,7 @@ There are two ways to print the transformed static graph code: ...@@ -128,7 +130,7 @@ There are two ways to print the transformed static graph code:
3, true_fn_0, false_fn_0, (x,), (x,), (x,)) 3, true_fn_0, false_fn_0, (x,), (x,), (x,))
return x return x
``` ```
`set_code_level` can set different levels to view the code transformed by different ast transformers. For details, please refer to [set_code_level]()<!--TODO:补充set_code_level文档链接--> `set_code_level` can set different levels to view the code transformed by different ast transformers. For details, please refer to [set_code_level](../../../paddle/api/paddle/fluid/dygraph/jit/set_code_level_en.html)
## `print` ## `print`
You can call `print` to view variables. `print` will be transformed when using Dynamic-to-Static. When only Paddle Tensor is printed, `print` will be transformed and call Paddle operator [Print](../../api/layers/Print.html) in runtime. Otherwise, call python `print`. You can call `print` to view variables. `print` will be transformed when using Dynamic-to-Static. When only Paddle Tensor is printed, `print` will be transformed and call Paddle operator [Print](../../api/layers/Print.html) in runtime. Otherwise, call python `print`.
...@@ -166,7 +168,7 @@ Here call print function. ...@@ -166,7 +168,7 @@ Here call print function.
## Log Printing ## Log Printing
ProgramTranslator can log additional debugging information to help you know whether the function was successfully transformed or not. ProgramTranslator can log additional debugging information to help you know whether the function was successfully transformed or not.
You can call `paddle.jit.set_verbosity(level)` or set environment variable `TRANSLATOR_VERBOSITY=level` to enable logging and view logs of different levels. The argument `level` varies from 0 to 3: You can call [`paddle.jit.set_verbosity(level)`](../../../paddle/api/paddle/fluid/dygraph/jit/set_verbosity_en.html) or set environment variable `TRANSLATOR_VERBOSITY=level` to enable logging and view logs of different levels. The argument `level` varies from 0 to 3:
- 0: no logging - 0: no logging
- 1: includes the information in Dynamic-to-Static tranformation process, such as the source code not transformed, the callable object to transform and so on - 1: includes the information in Dynamic-to-Static tranformation process, such as the source code not transformed, the callable object to transform and so on
- 2: includes above and more detailed function transformation logs - 2: includes above and more detailed function transformation logs
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
## 动转静过程中的异常 ## 动转静过程中的异常
在动态图代码转换成静态图代码的过程中,如果ProgramTranslator无法转换一个函数时,将会显示警告信息,并尝试直接运行该函数。 在动态图代码转换成静态图代码的过程中,如果ProgramTranslator无法转换一个函数时,将会显示警告信息,并尝试直接运行该函数。
如下代码中,函数`inner_func` 在调用前被转换成静态图代码,当`x=inner_func(data)`调用该函数时,不能重复转换,会给出警告信息: 如下代码中,函数 `inner_func` 在调用前被转换成静态图代码,当 `x = inner_func(data)` 调用该函数时,不能重复转换,会给出警告信息:
```python ```python
import paddle import paddle
...@@ -31,7 +31,7 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed ...@@ -31,7 +31,7 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed
## 运行转换后的代码报错 ## 运行转换后的代码报错
如果在动转静后的静态图代码中发生异常,ProgramTranslator会捕获该异常,增强异常报错信息,将静态图代码报错行映射到转换前的动态图代码,并重新抛出该异常。 如果在动转静后的静态图代码中发生异常,ProgramTranslator 会捕获该异常,增强异常报错信息,将静态图代码报错行映射到转换前的动态图代码,并重新抛出该异常。
重新抛出的异常具有以下特点: 重新抛出的异常具有以下特点:
- 隐藏了部分对用户无用的动转静过程调用栈; - 隐藏了部分对用户无用的动转静过程调用栈;
...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si ...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si
[operator < reshape2 > error] [operator < run_program > error] [operator < reshape2 > error] [operator < run_program > error]
``` ```
上述异常中,除了隐藏部分报错栈、报错定位到转换前的动态图代码外,报错信息中包含了C++报错栈`C++ Traceback``Error Message Summary`,这是Paddle的C++端异常信息,经处理后在Python的异常信息中显示。 上述异常中,除了隐藏部分报错栈、报错定位到转换前的动态图代码外,报错信息中包含了C++报错栈 `C++ Traceback``Error Message Summary`,这是 Paddle 的 C++ 端异常信息,经处理后在 Python 的异常信息中显示。
# Error Handling # Error Handling
This section will introduce the error information when an exception occurs, so as to help you better understand the Dynamic-to-Static error information. This section will introduce the error information when an exception occurs, so as to help you better understand the Dynamic-to-Static error information.
When running the transformed static graph code, the internal can be divided into two steps: the dynamic graph code is transformed into the static graph code, and the static graph code is run. We will introduce the error reporting in these two steps. When running the transformed static graph code, the internal procedure can be divided into two steps: the dynamic graph code is transformed into the static graph code, and the static graph code is run. We will introduce the error reporting in these two steps.
## Exceptions in Dynamic-to-Static Transformation ## Exceptions in Dynamic-to-Static Transformation
If ProgramTranslator cannot transform a function, it will display a warning message and try to run the function as-is. If ProgramTranslator cannot transform a function, it will display a warning message and try to run the function as-is.
In the following code, the function `inner_func` is transformed before calling. When calling `inner_func` in `x=inner_func(data)`, it is not allowed to transform repeatedly, and a warning message will be given: In the following code, the function `inner_func` is transformed before calling. When calling `inner_func` in `x = inner_func(data)`, it is not allowed to transform repeatedly, and a warning message will be given:
```python ```python
import paddle import paddle
...@@ -32,12 +32,12 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed ...@@ -32,12 +32,12 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed
``` ```
## Exceptions in Running Transformed Code ## Exceptions in Running Transformed Code
When an exception occurs in the transformed code by ProgramTranslator, the exception is be catched and the error message is augmented. It maps the error line of the static graph code to the dynamic graph code un-transformed, and then re-raise the exception. When an exception occurs in the transformed code by ProgramTranslator, the exception is caught and the error message is augmented. It maps the error line of the static graph code to the un-transformed dynamic graph code, and then re-raises the exception.
Among the features of the re-raised exception: Among the features of the re-raised exception:
- Some useless call stacks of Dynamic-to-Static are hidden; - Some useless call stacks of Dynamic-to-Static are hidden;
- A prompt will be given before the code un-transformed: "In User Code:"; - A prompt will be given before the un-transformed code: "In User Code:";
- The error message includes references to the original dynamic graph code before transformation; - The error message includes references to the original dynamic graph code before transformation;
For example, if executing the following code, an exception is raised when the static graph is built, that is, at compile time: For example, if executing the following code, an exception is raised when the static graph is built, that is, at compile time:
...@@ -90,14 +90,14 @@ The above error information can be divided into three points: ...@@ -90,14 +90,14 @@ The above error information can be divided into three points:
File "paddle/fluid/layers/nn.py", line 6169, in get_attr_shape File "paddle/fluid/layers/nn.py", line 6169, in get_attr_shape
"be -1. But received shape[%d] is also -1." % dim_idx) "be -1. But received shape[%d] is also -1." % dim_idx)
``` ```
`File "<ipython-input-13-f9c3ea702e3a>", line 7, in func` is the location information of un-transformed code, `x = fluid.layers.reshape(x, shape=[-1, -1])` is the code un-transformed. `File "<ipython-input-13-f9c3ea702e3a>", line 7, in func` is the location information of un-transformed code, `x = fluid.layers.reshape(x, shape=[-1, -1])` is the un-transformed code.
3. The new exception contains the message that the exception originally reported, as follows: 3. The new exception contains the message that the exception originally reported, as follows:
```bash ```bash
AssertionError: Only one dimension value of 'shape' in reshape can be -1. But received shape[1] is also -1. AssertionError: Only one dimension value of 'shape' in reshape can be -1. But received shape[1] is also -1.
``` ```
If executing the following code, an exception is raised when the static graph is executed, that is, at runtime: If execute the following code, an exception is raised when the static graph is executed at runtime:
```Python ```Python
@paddle.jit.to_static @paddle.jit.to_static
...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si ...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si
[operator < reshape2 > error] [operator < run_program > error] [operator < reshape2 > error] [operator < run_program > error]
``` ```
In the above exception, in addition to hiding part of the error stack and locating the error to the dynamic graph code un-transformed, the error information includes the c++ error stack `C++ Traceback` and `Error Message Summary`, which are the exception from C++ and are displayed in Python exception after processing. In the above exception, in addition to hiding part of the error stack and locating the error to the un-transformed dynamic graph code, the error information includes the c++ error stack `C++ Traceback` and `Error Message Summary`, which are the exception from C++ and are displayed in Python exception after processing.
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
> **注解:** > **注解:**
> >
> 请确保转换前的动态图代码能够成功运行,建议使用[paddle.jit.ProgramTranslator().enable(False)](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable)关闭动转静功能,直接运行动态图,如下: > 请确保转换前的动态图代码能够成功运行,建议使用 [paddle.jit.ProgramTranslator().enable(False)](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable)关闭动转静功能,直接运行动态图,如下:
```python ```python
import paddle import paddle
...@@ -25,7 +25,7 @@ func(np.ones([3, 2])) ...@@ -25,7 +25,7 @@ func(np.ones([3, 2]))
## 断点调试 ## 断点调试
使用动转静功能时,您可以使用断点调试代码。 使用动转静功能时,您可以使用断点调试代码。
例如,在代码中,调用`pdb.set_trace()` 例如,在代码中,调用 `pdb.set_trace()`
```Python ```Python
import pdb import pdb
...@@ -52,7 +52,7 @@ func(np.ones([3, 2])) ...@@ -52,7 +52,7 @@ func(np.ones([3, 2]))
... ...
``` ```
如果您想在原始的动态图代码中使用调试器,请先调用[`paddle.jit.ProgramTranslator().enable(False)`](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable),如下: 如果您想在原始的动态图代码中使用调试器,请先调用 [`paddle.jit.ProgramTranslator().enable(False)`](../../api_cn/dygraph_cn/ProgramTranslator_cn.html#enable),如下:
```python ```python
paddle.jit.ProgramTranslator().enable(False) paddle.jit.ProgramTranslator().enable(False)
func(np.ones([3, 2])) func(np.ones([3, 2]))
...@@ -68,7 +68,8 @@ func(np.ones([3, 2])) ...@@ -68,7 +68,8 @@ func(np.ones([3, 2]))
## 打印转换后的代码 ## 打印转换后的代码
您可以打印转换后的静态图代码,有2种方法: 您可以打印转换后的静态图代码,有2种方法:
1. 使用被装饰函数的`code` 属性 1. 使用被装饰后的函数的 `code` 属性
如下代码中,装饰器 `paddle.jit.to_static` 会将函数 `func` 转化为一个类对象 `StaticLayer`,可以使用 StaticLayer 的 `code` 属性来获得转化后的代码。
```Python ```Python
@paddle.jit.to_static @paddle.jit.to_static
def func(x): def func(x):
...@@ -97,9 +98,9 @@ func(np.ones([3, 2])) ...@@ -97,9 +98,9 @@ func(np.ones([3, 2]))
return x return x
``` ```
2. 使用`set_code_level(level)`或环境变量`TRANSLATOR_CODE_LEVEL=level` 2. 使用 `set_code_level(level)` 或环境变量 `TRANSLATOR_CODE_LEVEL=level`
通过调用`set_code_level`或设置环境变量`TRANSLATOR_CODE_LEVEL`,可以在log中查看转换后的代码: 通过调用 `set_code_level` 或设置环境变量 `TRANSLATOR_CODE_LEVEL`,可以在日志中查看转换后的代码:
```python ```python
@paddle.jit.to_static @paddle.jit.to_static
...@@ -129,10 +130,10 @@ func(np.ones([3, 2])) ...@@ -129,10 +130,10 @@ func(np.ones([3, 2]))
3, true_fn_0, false_fn_0, (x,), (x,), (x,)) 3, true_fn_0, false_fn_0, (x,), (x,), (x,))
return x return x
``` ```
`set_code_level` 函数可以设置查看不同的AST Transformer转化后的代码,详情请见[set_code_level]()<!--TODO:补充set_code_level文档链接--> `set_code_level` 函数可以设置查看不同的AST Transformer转化后的代码,详情请见 [set_code_level](../../../paddle/api/paddle/fluid/dygraph/jit/set_code_level_cn.html)
## 使用 `print` ## 使用 `print`
`print` 函数可以用来查看变量,该函数在动转静中会被转化。当仅打印Paddle Tensor时,实际运行时会被转换为Paddle算子[Print](../../api_cn/layers_cn/Print_cn.html),否则仍然运行`print` `print` 函数可以用来查看变量,该函数在动转静中会被转化。当仅打印 Paddle Tensor 时,实际运行时会被转换为 Paddle 算子 [Print](../../api_cn/layers_cn/Print_cn.html),否则仍然运行 `print`
```python ```python
@paddle.jit.to_static @paddle.jit.to_static
def func(x): def func(x):
...@@ -167,7 +168,7 @@ Here call print function. ...@@ -167,7 +168,7 @@ Here call print function.
## 日志打印 ## 日志打印
ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解动转静过程中函数是否被成功转换。 ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解动转静过程中函数是否被成功转换。
您可以调用`paddle.jit.set_verbosity(level)` 或设置环境变量`TRANSLATOR_VERBOSITY=level`来设置日志详细等级,并查看不同等级的日志信息。目前,`level`可以取值0-3: 您可以调用 [`paddle.jit.set_verbosity(level)`]((../../../paddle/api/paddle/fluid/dygraph/jit/set_verbosity_cn.html)) 或设置环境变量 `TRANSLATOR_VERBOSITY=level` 来设置日志详细等级,并查看不同等级的日志信息。目前,`level` 可以取值0-3:
- 0: 无日志 - 0: 无日志
- 1: 包括了动转静转化流程的信息,如转换前的源码、转换的可调用对象 - 1: 包括了动转静转化流程的信息,如转换前的源码、转换的可调用对象
- 2: 包括以上信息,还包括更详细函数转化日志 - 2: 包括以上信息,还包括更详细函数转化日志
...@@ -177,11 +178,11 @@ ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解 ...@@ -177,11 +178,11 @@ ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解
> >
> 日志中包括了源代码等信息,请在共享日志前确保它不包含敏感信息。 > 日志中包括了源代码等信息,请在共享日志前确保它不包含敏感信息。
可以在代码运行前调用`paddle.jit.set_verbosity`控制日志详细程度: 可以在代码运行前调用 `paddle.jit.set_verbosity` 控制日志详细程度:
```python ```python
paddle.jit.set_verbosity(3) paddle.jit.set_verbosity(3)
``` ```
或者设置环境变量`TRANSLATOR_VERBOSITY` 或者设置环境变量 `TRANSLATOR_VERBOSITY`
```python ```python
import os import os
os.environ["TRANSLATOR_VERBOSITY"] = '3' os.environ["TRANSLATOR_VERBOSITY"] = '3'
......
...@@ -71,7 +71,9 @@ func(np.ones([3, 2])) ...@@ -71,7 +71,9 @@ func(np.ones([3, 2]))
There are two ways to print the transformed static graph code: There are two ways to print the transformed static graph code:
1. Use the attribute `code` of the decorated function: 1. Use the attribute `code` of the decorated function
In the following code, the decorator `paddle.jit.to_static` transforms `func` into a class object `StaticLayer`. You can use the `code` attribute of `StaticLayer` to get the transformed code.
```Python ```Python
@paddle.jit.to_static @paddle.jit.to_static
def func(x): def func(x):
...@@ -128,7 +130,7 @@ There are two ways to print the transformed static graph code: ...@@ -128,7 +130,7 @@ There are two ways to print the transformed static graph code:
3, true_fn_0, false_fn_0, (x,), (x,), (x,)) 3, true_fn_0, false_fn_0, (x,), (x,), (x,))
return x return x
``` ```
`set_code_level` can set different levels to view the code transformed by different ast transformers. For details, please refer to [set_code_level]()<!--TODO:补充set_code_level文档链接--> `set_code_level` can set different levels to view the code transformed by different ast transformers. For details, please refer to [set_code_level](../../../paddle/api/paddle/fluid/dygraph/jit/set_code_level_en.html)
## `print` ## `print`
You can call `print` to view variables. `print` will be transformed when using Dynamic-to-Static. When only Paddle Tensor is printed, `print` will be transformed and call Paddle operator [Print](../../api/layers/Print.html) in runtime. Otherwise, call python `print`. You can call `print` to view variables. `print` will be transformed when using Dynamic-to-Static. When only Paddle Tensor is printed, `print` will be transformed and call Paddle operator [Print](../../api/layers/Print.html) in runtime. Otherwise, call python `print`.
...@@ -166,7 +168,7 @@ Here call print function. ...@@ -166,7 +168,7 @@ Here call print function.
## Log Printing ## Log Printing
ProgramTranslator can log additional debugging information to help you know whether the function was successfully transformed or not. ProgramTranslator can log additional debugging information to help you know whether the function was successfully transformed or not.
You can call `paddle.jit.set_verbosity(level)` or set environment variable `TRANSLATOR_VERBOSITY=level` to enable logging and view logs of different levels. The argument `level` varies from 0 to 3: You can call [`paddle.jit.set_verbosity(level)`](../../../paddle/api/paddle/fluid/dygraph/jit/set_verbosity_en.html) or set environment variable `TRANSLATOR_VERBOSITY=level` to enable logging and view logs of different levels. The argument `level` varies from 0 to 3:
- 0: no logging - 0: no logging
- 1: includes the information in Dynamic-to-Static tranformation process, such as the source code not transformed, the callable object to transform and so on - 1: includes the information in Dynamic-to-Static tranformation process, such as the source code not transformed, the callable object to transform and so on
- 2: includes above and more detailed function transformation logs - 2: includes above and more detailed function transformation logs
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
## 动转静过程中的异常 ## 动转静过程中的异常
在动态图代码转换成静态图代码的过程中,如果ProgramTranslator无法转换一个函数时,将会显示警告信息,并尝试直接运行该函数。 在动态图代码转换成静态图代码的过程中,如果ProgramTranslator无法转换一个函数时,将会显示警告信息,并尝试直接运行该函数。
如下代码中,函数`inner_func` 在调用前被转换成静态图代码,当`x=inner_func(data)`调用该函数时,不能重复转换,会给出警告信息: 如下代码中,函数 `inner_func` 在调用前被转换成静态图代码,当 `x = inner_func(data)` 调用该函数时,不能重复转换,会给出警告信息:
```python ```python
import paddle import paddle
...@@ -31,7 +31,7 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed ...@@ -31,7 +31,7 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed
## 运行转换后的代码报错 ## 运行转换后的代码报错
如果在动转静后的静态图代码中发生异常,ProgramTranslator会捕获该异常,增强异常报错信息,将静态图代码报错行映射到转换前的动态图代码,并重新抛出该异常。 如果在动转静后的静态图代码中发生异常,ProgramTranslator 会捕获该异常,增强异常报错信息,将静态图代码报错行映射到转换前的动态图代码,并重新抛出该异常。
重新抛出的异常具有以下特点: 重新抛出的异常具有以下特点:
- 隐藏了部分对用户无用的动转静过程调用栈; - 隐藏了部分对用户无用的动转静过程调用栈;
...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si ...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si
[operator < reshape2 > error] [operator < run_program > error] [operator < reshape2 > error] [operator < run_program > error]
``` ```
上述异常中,除了隐藏部分报错栈、报错定位到转换前的动态图代码外,报错信息中包含了C++报错栈`C++ Traceback``Error Message Summary`,这是Paddle的C++端异常信息,经处理后在Python的异常信息中显示。 上述异常中,除了隐藏部分报错栈、报错定位到转换前的动态图代码外,报错信息中包含了C++报错栈 `C++ Traceback``Error Message Summary`,这是 Paddle 的 C++ 端异常信息,经处理后在 Python 的异常信息中显示。
# Error Handling # Error Handling
This section will introduce the error information when an exception occurs, so as to help you better understand the Dynamic-to-Static error information. This section will introduce the error information when an exception occurs, so as to help you better understand the Dynamic-to-Static error information.
When running the transformed static graph code, the internal can be divided into two steps: the dynamic graph code is transformed into the static graph code, and the static graph code is run. We will introduce the error reporting in these two steps. When running the transformed static graph code, the internal procedure can be divided into two steps: the dynamic graph code is transformed into the static graph code, and the static graph code is run. We will introduce the error reporting in these two steps.
## Exceptions in Dynamic-to-Static Transformation ## Exceptions in Dynamic-to-Static Transformation
If ProgramTranslator cannot transform a function, it will display a warning message and try to run the function as-is. If ProgramTranslator cannot transform a function, it will display a warning message and try to run the function as-is.
In the following code, the function `inner_func` is transformed before calling. When calling `inner_func` in `x=inner_func(data)`, it is not allowed to transform repeatedly, and a warning message will be given: In the following code, the function `inner_func` is transformed before calling. When calling `inner_func` in `x = inner_func(data)`, it is not allowed to transform repeatedly, and a warning message will be given:
```python ```python
import paddle import paddle
...@@ -32,12 +32,12 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed ...@@ -32,12 +32,12 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed
``` ```
## Exceptions in Running Transformed Code ## Exceptions in Running Transformed Code
When an exception occurs in the transformed code by ProgramTranslator, the exception is be catched and the error message is augmented. It maps the error line of the static graph code to the dynamic graph code un-transformed, and then re-raise the exception. When an exception occurs in the transformed code by ProgramTranslator, the exception is caught and the error message is augmented. It maps the error line of the static graph code to the un-transformed dynamic graph code, and then re-raises the exception.
Among the features of the re-raised exception: Among the features of the re-raised exception:
- Some useless call stacks of Dynamic-to-Static are hidden; - Some useless call stacks of Dynamic-to-Static are hidden;
- A prompt will be given before the code un-transformed: "In User Code:"; - A prompt will be given before the un-transformed code: "In User Code:";
- The error message includes references to the original dynamic graph code before transformation; - The error message includes references to the original dynamic graph code before transformation;
For example, if executing the following code, an exception is raised when the static graph is built, that is, at compile time: For example, if executing the following code, an exception is raised when the static graph is built, that is, at compile time:
...@@ -90,14 +90,14 @@ The above error information can be divided into three points: ...@@ -90,14 +90,14 @@ The above error information can be divided into three points:
File "paddle/fluid/layers/nn.py", line 6169, in get_attr_shape File "paddle/fluid/layers/nn.py", line 6169, in get_attr_shape
"be -1. But received shape[%d] is also -1." % dim_idx) "be -1. But received shape[%d] is also -1." % dim_idx)
``` ```
`File "<ipython-input-13-f9c3ea702e3a>", line 7, in func` is the location information of un-transformed code, `x = fluid.layers.reshape(x, shape=[-1, -1])` is the code un-transformed. `File "<ipython-input-13-f9c3ea702e3a>", line 7, in func` is the location information of un-transformed code, `x = fluid.layers.reshape(x, shape=[-1, -1])` is the un-transformed code.
3. The new exception contains the message that the exception originally reported, as follows: 3. The new exception contains the message that the exception originally reported, as follows:
```bash ```bash
AssertionError: Only one dimension value of 'shape' in reshape can be -1. But received shape[1] is also -1. AssertionError: Only one dimension value of 'shape' in reshape can be -1. But received shape[1] is also -1.
``` ```
If executing the following code, an exception is raised when the static graph is executed, that is, at runtime: If execute the following code, an exception is raised when the static graph is executed at runtime:
```Python ```Python
@paddle.jit.to_static @paddle.jit.to_static
...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si ...@@ -157,4 +157,4 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si
[operator < reshape2 > error] [operator < run_program > error] [operator < reshape2 > error] [operator < run_program > error]
``` ```
In the above exception, in addition to hiding part of the error stack and locating the error to the dynamic graph code un-transformed, the error information includes the c++ error stack `C++ Traceback` and `Error Message Summary`, which are the exception from C++ and are displayed in Python exception after processing. In the above exception, in addition to hiding part of the error stack and locating the error to the un-transformed dynamic graph code, the error information includes the c++ error stack `C++ Traceback` and `Error Message Summary`, which are the exception from C++ and are displayed in Python exception after processing.
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
- `InputSpec功能介绍 <input_spec_cn.html>`_ :介绍了动态图转静态图指定输入InputSpec的功能和用法 - `InputSpec功能介绍 <input_spec_cn.html>`_ :介绍了动态图转静态图指定输入InputSpec的功能和用法
- `报错信息处理 <error_handling_cn.html>`_ :介绍了动态图转静态图支持的报错信息处理方法 - `报错信息处理 <error_handling_cn.html>`_ :介绍了动态图转静态图的报错信息处理方法
- `调试方法 <debugging_cn.html>`_ :介绍了动态图转静态图支持的调试方法 - `调试方法 <debugging_cn.html>`_ :介绍了动态图转静态图支持的调试方法
......
...@@ -12,6 +12,10 @@ Dygraph to Static Graph ...@@ -12,6 +12,10 @@ Dygraph to Static Graph
- `Debugging Methods <debugging_en.html>`_ :Introduce the debugging methods when using ProgramTranslator. - `Debugging Methods <debugging_en.html>`_ :Introduce the debugging methods when using ProgramTranslator.
- `Error Handling <error_handling_en.html>`_ :Introduce the error handling by ProgramTranslator.
- `Debugging Methods <debugging_en.html>`_ :Introduce the debugging methods when using ProgramTranslator.
.. toctree:: .. toctree::
:hidden: :hidden:
...@@ -20,4 +24,3 @@ Dygraph to Static Graph ...@@ -20,4 +24,3 @@ Dygraph to Static Graph
input_spec_en.rst input_spec_en.rst
error_handling_en.md error_handling_en.md
debugging_en.md debugging_en.md
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册