未验证 提交 340c3be4 编写于 作者: A Aurelius84 提交者: GitHub

migrate en doc from old dir (#2611)

* migrate en doc from old dir

* fix typo

* fix typo

* fix typo
上级 0cd639b8
# 调试方法
本节内容将介绍动态图转静态图(下文简称动转静)推荐的几种调试方法。
本节内容将介绍动态图转静态图(下文简称:动转静)推荐的几种调试方法。
> **注解:**
>
> 请确保转换前的动态图代码能够成功运行,建议使用[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
import paddle
import numpy as np
......@@ -96,35 +99,36 @@ func(np.ones([3, 2]))
2. 使用`set_code_level(level)`或环境变量`TRANSLATOR_CODE_LEVEL=level`
通过调用`set_code_level`或设置环境变量`TRANSLATOR_CODE_LEVEL`,可以在log中查看转换后的代码
```python
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
if x > 3:
x = x - 1
return x
通过调用`set_code_level`或设置环境变量`TRANSLATOR_CODE_LEVEL`,可以在log中查看转换后的代码:
paddle.jit.set_code_level() # 也可设置 os.environ["TRANSLATOR_CODE_LEVEL"] = '100',效果相同
func(np.ones([1]))
```
运行结果:
```python
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
if x > 3:
x = x - 1
return x
paddle.jit.set_code_level() # 也可设置 os.environ["TRANSLATOR_CODE_LEVEL"] = '100',效果相同
func(np.ones([1]))
```
运行结果:
```bash
2020-XX-XX 00:00:00,980-INFO: After the level 100 ast transformer: 'All Transformers', the transformed code:
def func(x):
x = fluid.layers.assign(x)
```bash
2020-XX-XX 00:00:00,980-INFO: After the level 100 ast transformer: 'All Transformers', the transformed code:
def func(x):
x = fluid.layers.assign(x)
def true_fn_0(x):
x = x - 1
return x
def true_fn_0(x):
x = x - 1
return x
def false_fn_0(x):
def false_fn_0(x):
return x
x = fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse(x >
3, true_fn_0, false_fn_0, (x,), (x,), (x,))
return x
x = fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse(x >
3, true_fn_0, false_fn_0, (x,), (x,), (x,))
return x
```
```
`set_code_level` 函数可以设置查看不同的AST Transformer转化后的代码,详情请见[set_code_level]()<!--TODO:补充set_code_level文档链接-->。
## 使用 `print`
......@@ -133,15 +137,19 @@ def func(x):
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
# 打印x,x是Paddle Tensor,实际运行时会运行Paddle Print(x)
print(x)
# 打印注释,非Paddle Tensor,实际运行时仍运行print
print("Here call print function.")
if len(x) > 3:
x = x - 1
else:
x = paddle.ones(shape=[1])
return x
func(np.ones([1]))
```
......@@ -165,8 +173,11 @@ ProgramTranslator在日志中记录了额外的调试信息,以帮助您了解
- 2: 包括以上信息,还包括更详细函数转化日志
- 3: 包括以上信息,以及更详细的动转静日志
> **注意:**
>
> 日志中包括了源代码等信息,请在共享日志前确保它不包含敏感信息。
可以在代码运行前调用`paddle.jit.set_verbosity()`
可以在代码运行前调用`paddle.jit.set_verbosity`控制日志详细程度
```python
paddle.jit.set_verbosity(3)
```
......
# Debugging Methods
This section will introduce several debugging methods recommended by Dynamic Graph to Static Graph (hereafter called Dynamic-to-Staic).
> **NOTE:**
>
> Please ensure that the dynamic graph code before transformation can run successfully. It is recommended to call [paddle.jit.ProgramTranslator().enable(False)](../../api/dygraph/ProgramTranslator_en.html#enable) to disable Dynamic-to-Static, and run dynamic graph code as follows:
```python
import paddle
import numpy as np
paddle.disable_static()
# Disable Dynamic-to-Static
paddle.jit.ProgramTranslator().enable(False)
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
if x > 3:
x = x - 1
return x
func(np.ones([3, 2]))
```
## Breakpoint Debugging
When using Dynamic-to-Static, you can use breakpoints to debug.
For example, call `pdb.set_trace()` in your code:
```Python
import pdb
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
pdb.set_trace()
if x > 3:
x = x - 1
return x
```
Executing the following code will land the debugger in the transformed static graph code:
```Python
func(np.ones([3, 2]))
```
```bash
> /tmp/tmpR809hf.py(6)func()
-> def true_fn_0(x):
(Pdb) n
> /tmp/tmpR809hf.py(6)func()
-> def false_fn_0(x):
...
```
Calling [`paddle.jit.ProgramTranslator().enable(False)`](../../api/dygraph/ProgramTranslator_en.html#enable) before executing the code will land the debugger in the original dynamic graph code:
```python
paddle.jit.ProgramTranslator().enable(False)
func(np.ones([3, 2]))
```
```bash
> <ipython-input-22-0bd4eab35cd5>(10)func()
-> if x > 3:
...
```
## Print Transformed Code
There are two ways to print the transformed static graph code:
1. Use the attribute `code` of the decorated function:
```Python
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
if x > 3:
x = x - 1
return x
print(func.code)
```
```bash
def func(x):
x = fluid.layers.assign(x)
def true_fn_0(x):
x = x - 1
return x
def false_fn_0(x):
return x
x = fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse(x >
3, true_fn_0, false_fn_0, (x,), (x,), (x,))
return x
```
2. Call `set_code_level(level)` or set environment variable `TRANSLATOR_CODE_LEVEL=level`
You can view the transformed code in the log by calling `set_code_level` or set environment variable `TRANSLATOR_CODE_LEVEL`.
```python
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
if x > 3:
x = x - 1
return x
paddle.jit.set_code_level() # the same effect to set os.environ["TRANSLATOR_CODE_LEVEL"] = '100'
func(np.ones([1]))
```
```bash
2020-XX-XX 00:00:00,980-INFO: After the level 100 ast transformer: 'All Transformers', the transformed code:
def func(x):
x = fluid.layers.assign(x)
def true_fn_0(x):
x = x - 1
return x
def false_fn_0(x):
return x
x = fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse(x >
3, true_fn_0, false_fn_0, (x,), (x,), (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文档链接-->。
## `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`.
```python
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
# x is a Paddle Tensor, so it will run Paddle Print(x) actually.
print(x)
# The string is not a Paddle Tensor, so it will run print as-is.
print("Here call print function.")
if len(x) > 3:
x = x - 1
else:
x = paddle.ones(shape=[1])
return x
func(np.ones([1]))
```
```bash
Variable: assign_0.tmp_0
- lod: {}
- place: CPUPlace
- shape: [1]
- layout: NCHW
- dtype: double
- data: [1]
Here call print function.
```
## Log Printing
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:
- 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
- 2: includes above and more detailed function transformation logs
- 3: includes above and extremely verbose logging
> **WARNING:**
>
> The logs includes information such as source code. Please make sure logs don't contain any sensitive information before sharing them.
You can call `paddle.jit.set_verbosity` to control the verbosity level of logs:
```python
paddle.jit.set_verbosity(3)
```
or use the environment variable `TRANSLATOR_VERBOSITY`
```python
import os
os.environ["TRANSLATOR_VERBOSITY"] = '3'
```
```bash
2020-XX-XX 00:00:00,123-Level 1: Source code:
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
if len(x) > 3:
x = x - 1
else:
x = paddle.ones(shape=[1])
return x
2020-XX-XX 00:00:00,152-Level 1: Convert callable object: convert <built-in function len>.
# 报错信息处理
本节内容将介绍使用动态图转静态图(下文简称动转静)功能发生异常时,[ProgramTranslator](./program_translator_cn.html)对报错信息做的处理,以帮助您更好地理解动转静报错信息。使用动转静功能运行动态图代码时,内部可以分为2个步骤:动态图代码转换成静态图代码,运行静态图代码。接下来将分别介绍这2个步骤中的异常报错情况。
本节内容将介绍使用动态图转静态图(下文简称动转静)功能发生异常时,[ProgramTranslator](./program_translator_cn.html)对报错信息做的处理,以帮助您更好地理解动转静报错信息。使用动转静功能运行动态图代码时,内部可以分为2个步骤:动态图代码转换成静态图代码,运行静态图代码。接下来将分别介绍这2个步骤中的异常报错情况。
## 动转静过程中的异常
在动态图代码转换成静态图代码的过程中,如果ProgramTranslator无法转换一个函数时,将会显示警告信息,并尝试直接运行该函数。
如下代码中,函数`inner_func` 在调用前被转换成静态图代码,当`x = inner_func(data)`调用该函数时,不能重复转换,会给出警告信息:
如下代码中,函数`inner_func` 在调用前被转换成静态图代码,当`x=inner_func(data)`调用该函数时,不能重复转换,会给出警告信息:
```python
import paddle
import numpy as np
......@@ -21,11 +22,15 @@ def func():
return x
func()
```
ProgramTranslator打印的警告信息如下:
```bash
WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed to static function because it has been transformed before, it will be run as-is.
```
## 运行转换后的代码报错
如果在动转静后的静态图代码中发生异常,ProgramTranslator会捕获该异常,增强异常报错信息,将静态图代码报错行映射到转换前的动态图代码,并重新抛出该异常。
重新抛出的异常具有以下特点:
......@@ -34,6 +39,7 @@ WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed
- 报错信息中包含了转换前的原始动态图代码;
例如,运行以下代码,在静态图构建时,即编译期会抛出异常:
```python
import paddle
import numpy as np
......@@ -48,6 +54,7 @@ def func(x):
func(np.ones([3, 2]))
```
运行结果:
```bash
Traceback (most recent call last):
......@@ -68,7 +75,8 @@ AssertionError: In user code:
上述报错信息可以分为3点:
1. 报错栈中,涉及代码转换过程的信息栈默认会被隐藏,不对用户展示,以避免给用户带来困扰。
1. 报错栈中,涉及代码转换过程的信息栈默认会被隐藏,不进行展示,以减少干扰信息。
2. ProgramTranslator处理后的报错信息中,会包含提示"In user code:",表示之后的报错栈中,包含动转静前的动态图代码,即用户写的代码:
```bash
AssertionError: In user code:
......@@ -81,12 +89,14 @@ AssertionError: In user code:
"be -1. But received shape[%d] is also -1." % dim_idx)
```
其中,`File "<ipython-input-13-f9c3ea702e3a>", line 7, in func` 是转换前的代码位置信息,`x = fluid.layers.reshape(x, shape=[-1, -1])` 是转换前的代码。
3. 新的异常中,包含原始报错中的的报错信息,如下:
```bash
AssertionError: Only one dimension value of 'shape' in reshape can be -1. But received shape[1] is also -1.
```
运行以下代码,在静态图运行时,即运行期会抛出异常:
```Python
@paddle.jit.to_static
def func(x):
......@@ -97,7 +107,9 @@ def func(x):
func(np.ones([3]).astype("int32"))
```
运行结果:
```bash
Traceback (most recent call last):
File "<ipython-input-57-c63d6a351262>", line 10, in <module>()
......@@ -144,4 +156,5 @@ InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'si
[Hint: Expected capacity == in_size, but received capacity:2 != in_size:3.] (at /paddle/paddle/fluid/operators/reshape_op.cc:206)
[operator < reshape2 > error] [operator < run_program > error]
```
上述异常中,除了隐藏部分报错栈、报错定位到转换前的动态图代码外,报错信息中包含了C++报错栈`C++ Traceback``Error Message Summary`,这是Paddle的C++端异常信息,经处理后在Python的异常信息中显示。
# 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.
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.
## 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.
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
import paddle
import numpy as np
paddle.disable_static()
@paddle.jit.to_static
def func():
def inner_func(x):
x_tensor = paddle.to_tensor(x)
return x_tensor
data = np.ones([3]).astype("int32")
x = inner_func(data)
return x
func()
```
The warning message is as follows:
```bash
WARNING: <function inner_func at 0x7fa9bcaacf50> doesn't have to be transformed to static function because it has been transformed before, it will be run as-is.
```
## 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.
Among the features of the re-raised exception:
- Some useless call stacks of Dynamic-to-Static are hidden;
- A prompt will be given before the code un-transformed: "In User Code:";
- 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:
```python
import paddle
import numpy as np
paddle.disable_static()
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
x = paddle.reshape(x, shape=[-1, -1])
return x
func(np.ones([3, 2]))
```
```bash
Traceback (most recent call last):
<ipython-input-13-f9c3ea702e3a> in <module>()
func(np.ones([3, 2]))
File "paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 332, in __call__
raise new_exception
AssertionError: In user code:
File "<ipython-input-13-f9c3ea702e3a>", line 7, in func
x = fluid.layers.reshape(x, shape=[-1, -1])
File "paddle/fluid/layers/nn.py", line 6193, in reshape
attrs["shape"] = get_attr_shape(shape)
File "paddle/fluid/layers/nn.py", line 6169, in get_attr_shape
"be -1. But received shape[%d] is also -1." % dim_idx)
AssertionError: Only one dimension value of 'shape' in reshape can be -1. But received shape[1] is also -1.
```
The above error information can be divided into three points:
1. In the error stack, the call stacks related to the code transformation process are hidden by default and not displayed, so as to avoid confusion.
2. In the error message processed by ProgramTranslator, a prompt "In user code:" will be included, which means that the following error stacks contains the original dynamic graph code, that is, the code written by the user:
```bash
AssertionError: In user code:
File "<ipython-input-13-f9c3ea702e3a>", line 7, in func
x = fluid.layers.reshape(x, shape=[-1, -1])
File "paddle/fluid/layers/nn.py", line 6193, in reshape
attrs["shape"] = get_attr_shape(shape)
File "paddle/fluid/layers/nn.py", line 6169, in get_attr_shape
"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.
3. The new exception contains the message that the exception originally reported, as follows:
```bash
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:
```Python
@paddle.jit.to_static
def func(x):
x = paddle.to_tensor(x)
two = paddle.fill_constant(shape=[1], value=2, dtype="int32")
x = paddle.reshape(x, shape=[1, two])
return x
func(np.ones([3]).astype("int32"))
```
```bash
Traceback (most recent call last):
File "<ipython-input-57-c63d6a351262>", line 10, in <module>()
func(np.ones([3]).astype("int32"))
File "paddle/fluid/dygraph/dygraph_to_static/program_translator.py", line 332, in __call__
raise new_exception
EnforceNotMet: In user code:
File "<ipython-input-57-c63d6a351262>", line 7, in func
x = paddle.reshape(x, shape=[1, two])
File "paddle/tensor/manipulation.py", line 1347, in reshape
return paddle.fluid.layers.reshape(x=x, shape=shape, name=name)
File "paddle/fluid/layers/nn.py", line 6209, in reshape
"XShape": x_shape})
File "paddle/fluid/layer_helper.py", line 43, in append_op
return self.main_program.current_block().append_op(*args, **kwargs)
File "paddle/fluid/framework.py", line 2880, in append_op
attrs=kwargs.get("attrs", None))
File "paddle/fluid/framework.py", line 1977, in __init__
for frame in traceback.extract_stack():
--------------------------------------
C++ Traceback (most recent call last):
--------------------------------------
0 paddle::imperative::Tracer::TraceOp(std::string const&, paddle::imperative::NameVarBaseMap const&, paddle::imperative::NameVarBaseMap const&, paddle::framework::AttributeMap, paddle::platform::Place const&, bool)
1 paddle::imperative::OpBase::Run(paddle::framework::OperatorBase const&, paddle::imperative::NameVarBaseMap const&, paddle::imperative::NameVarBaseMap const&, paddle::framework::AttributeMap const&, paddle::platform::Place const&)
2 paddle::imperative::PreparedOp::Run(paddle::imperative::NameVarBaseMap const&, paddle::imperative::NameVarBaseMap const&, paddle::framework::AttributeMap const&)
3 std::_Function_handler<void (paddle::framework::ExecutionContext const&), paddle::framework::OpKernelRegistrarFunctor<paddle::platform::CPUPlace, false, 0ul, paddle::operators::RunProgramOpKernel<paddle::platform::CPUDeviceContext, float> >::operator()(char const*, char const*, int) const::{lambda(paddle::framework::ExecutionContext const&)#1}>::_M_invoke(std::_Any_data const&, paddle::framework::ExecutionContext const&)
4 paddle::operators::RunProgramOpKernel<paddle::platform::CPUDeviceContext, float>::Compute(paddle::framework::ExecutionContext const&) const
5 paddle::framework::Executor::RunPartialPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, long, long, bool, bool, bool)
6 paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, paddle::platform::Place const&)
7 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&) const
8 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, paddle::platform::Place const&, paddle::framework::RuntimeContext*) const
9 paddle::operators::ReshapeKernel::operator()(paddle::framework::ExecutionContext const&) const
10 paddle::operators::ReshapeOp::ValidateShape(std::vector<int, std::allocator<int> >, paddle::framework::DDim const&)
11 paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const*, int)
12 paddle::platform::GetCurrentTraceBackString()
----------------------
Error Message Summary:
----------------------
InvalidArgumentError: The 'shape' in ReshapeOp is invalid. The input tensor X'size must be equal to the capacity of 'shape'. But received X's shape = [3], X's size = 3, 'shape' is [1, 2], the capacity of 'shape' is 2.
[Hint: Expected capacity == in_size, but received capacity:2 != in_size:3.] (at /paddle/paddle/fluid/operators/reshape_op.cc:206)
[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.
......@@ -15,9 +15,9 @@
.. toctree::
:hidden:
grammar_list_cn.rst
program_translator_cn.rst
grammar_list_cn.rst
input_spec_cn.rst
error_handling_cn.md
debugging_cn.md
......@@ -4,11 +4,20 @@ Dygraph to Static Graph
- `Dygraph to Static Graph <program_translator_cn.html>`_ :Introduce the basic usage for transforming dygraph code into static code and the architecture of ProgramTranslator.
- `Supported Grammars <grammar_list_en.html>`_ :Introduce the grammars supported by ProgramTranslator and list unsupport grammars.
- `Supported Grammars <grammar_list_en.html>`_ :Introduce the grammars supported by ProgramTranslator and list unsupported grammars.
- `Introduction of InputSpec <input_spec_en.html>`_ :Introduce the usage of InputSpec to specify the input signature from dygraph to static program.
- `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::
:hidden:
grammar_list_en.rst
program_translator_en.rst
grammar_list_en.rst
input_spec_en.rst
error_handling_en.md
debugging_en.md
......@@ -18,4 +18,4 @@ Let's start with studying basic concept of PaddlePaddle:
tensor_introduction_en.md
migration_en.rst
dynamic_to_static/index_en.rst
dygraph_to_static/index_en.rst
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册