Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enrich the python error types of paddle & polish format #28124

Merged

Conversation

chenwhql
Copy link
Contributor

@chenwhql chenwhql commented Oct 20, 2020

PR types

New features

PR changes

Others

Describe

Paddle框架中C++端5000余条不合规报错的逐条完善工作已经基本收尾,框架的报错质量相比一年前已经有了显著的提升,一年前,框架中仍有数千条过短的、表意不明的、甚至没有提示的报错,所以基于我们当时的状况,是不具备隐藏C++栈的条件的,一旦隐藏,很多原本就没有报错提示的出错基本就无法调试了,尽管那时竞品默认都是没有C++报错栈的。

但这一年经过大量同学的完善,报错整体的质量提升明显,我们是时候需要正视C++栈给大多数普通用户带来的困扰了。

依据婉婷的问卷调研和深访,C++栈大多数用户并不关心,且长期以来也一直被AIStudio的用户诟病,因此我们基本得出了:要在2.0正式版发布时,默认隐藏C++栈的结论

但考虑到研发人员和资深用户可能仍然会依赖C++栈,我们也提供了便利的重新打开C++栈的方法:

配置环境变量:export FLAGS_call_stack_level=2

现在默认FLAGS_call_stack_level=2,本PR并没有设置默认隐藏C++栈,默认隐藏的配置后续再进行


基于以上背景,现在将paddle内部定义的报错类型映射至python端原生报错类型,以较为丰富的python原生报错类型替代原先理解成本较高的自定义异常paddle.fluid.core_avx.EnforceNotMet,使paddle 2.0的报错更加规范。

主要改动包括以下几点:

  1. 移除理解成本高的paddle自定义异常paddle.fluid.core_avx.EnforceNotMet
    paddle截至目前为止,整个C++端都在使用这个不易理解的异常类型,这个类型和python的ValueErrorTypeError是同级别的,但我们的EnforceNotMet着实生僻,统一使用python原生异常是更加标准的做法

image

  1. 移除自定义分隔标志Error Message Summary
    Error Message Summary确实在让用户更明显地看到的报错信息这一点上是有效果的,但是不够标准,这只是paddle独有的一种比较low的标志,一般python用户会找关键词***Error确认报错信息的位置,额外的分隔不是刚需,从长远考虑,我们应该保持自己的标准性,而不是引入一些奇怪的标志,因此本PR改为直接和原生python栈保持一样的报错效果

image

  1. 将框架报错类型体系映射到python原生的异常体系
    不增加自定义的异常类型是目前的趋势,本PR将paddle内定义的报错类型按如下规则映射到的python原生的异常类型上,paddle内的错误类型作为子类型附在主报错类型之后,降低用户理解成本。
/* Paddle Exception mapping rules:
 *   - InvalidArgumentError -> ValueError
 *   - NotFoundError -> RuntimeError
 *   - OutOfRangeError -> IndexError
 *   - AlreadyExistsError -> RuntimeError
 *   - ResourceExhaustedError -> MemoryError
 *   - PreconditionNotMetError -> RuntimeError
 *   - PermissionDeniedError -> RuntimeError
 *   - ExecutionTimeoutError -> RuntimeError
 *   - UnimplementedError -> NotImplementedError
 *   - UnavailableError -> RuntimeError
 *   - FatalError -> SystemError
 *   - ExternalError -> OSError
 */ 

注意:本PR仅针对隐藏C++栈时的报错格式进行完善!以下示例为设置FLAGS_call_stack_level=1时的显示效果,目前FLAGS_call_stack_level默认仍为2,即默认显示C++栈,在显示C++栈的情况下,报错格式仍与之前一致,基本无变化。

Dynamic mode Example

Before this PR:

Traceback (most recent call last):
  File "base_error_case2.py", line 9, in <module>
    res = linear(data)
  File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/dygraph/layers.py", line 829, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/dygraph/nn.py", line 970, in forward
    self._use_mkldnn)
paddle.fluid.core_avx.EnforceNotMet: 
----------------------
Error Message Summary:
----------------------
InvalidArgumentError: Input X's width should be equal to the Y's height, but received X's shape: [10, 2],Y's shape: [1, 10].
  [Hint: Expected mat_dim_x.width_ == mat_dim_y.height_, but received mat_dim_x.width_:2 != mat_dim_y.height_:1.] (at /work/paddle_py2/paddle/fluid/operators/matmul_op.cc:586)
  [operator < matmul > error]

After this PR:

Traceback (most recent call last):
  File "base_error_case2.py", line 9, in <module>
    res = linear(data)
  File "/usr/local/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 829, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/paddle/fluid/dygraph/nn.py", line 970, in forward
    self._use_mkldnn)
ValueError: (InvalidArgument) Input X's width should be equal to the Y's height, but received X's shape: [10, 2],Y's shape: [1, 10].
  [Hint: Expected mat_dim_x.width_ == mat_dim_y.height_, but received mat_dim_x.width_:2 != mat_dim_y.height_:1.] (at /work/paddle/paddle/fluid/operators/matmul_op.cc:586)
  [operator < matmul > error]

Static mode Example

Before this PR:

Traceback (most recent call last):
  File "base_error_case1.py", line 29, in <module>
    loss_data, = exe.run(fluid.default_main_program(), feed={'X': x, 'Y': y}, fetch_list=[avg_loss.name])
  File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/executor.py", line 1107, in run
    six.reraise(*sys.exc_info())
  File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/executor.py", line 1105, in run
    return_merged=return_merged)
  File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/executor.py", line 1229, in _run_impl
    use_program_cache=use_program_cache)
  File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/executor.py", line 1314, in _run_program
    fetch_var_name)
paddle.fluid.core_avx.EnforceNotMet: 

  Compile Traceback (most recent call last):
    File "base_error_case1.py", line 12, in <module>
      predict = fluid.layers.fc(input=x, size=1, act=None)
    File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/layers/nn.py", line 352, in fc
      "y_num_col_dims": 1})
    File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op
      return self.main_program.current_block().append_op(*args, **kwargs)
    File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/framework.py", line 2917, in append_op
      attrs=kwargs.get("attrs", None))
    File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/framework.py", line 2014, in __init__
      for frame in traceback.extract_stack():
----------------------
Error Message Summary:
----------------------
InvalidArgumentError: After flatten the input tensor X and Y to 2-D dimensions matrix X1 and Y1, the matrix X1's width must be equal with matrix Y1's height. But received X's shape = [8, 12], X1's shape = [8, 12], X1's width = 12; Y's shape = [13, 1], Y1's shape = [13, 1], Y1's height = 13.
  [Hint: Expected x_mat_dims[1] == y_mat_dims[0], but received x_mat_dims[1]:12 != y_mat_dims[0]:13.] (at /work/paddle_py2/paddle/fluid/operators/mul_op.cc:83)
  [operator < mul > error]

After this PR:

Traceback (most recent call last):
  File "base_error_case1.py", line 29, in <module>
    loss_data, = exe.run(fluid.default_main_program(), feed={'X': x, 'Y': y}, fetch_list=[avg_loss.name])
  File "/usr/local/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1107, in run
    six.reraise(*sys.exc_info())
  File "/usr/local/lib/python3.7/site-packages/six.py", line 703, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1105, in run
    return_merged=return_merged)
  File "/usr/local/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1229, in _run_impl
    use_program_cache=use_program_cache)
  File "/usr/local/lib/python3.7/site-packages/paddle/fluid/executor.py", line 1314, in _run_program
    fetch_var_name)
ValueError: In user code:

    File "base_error_case1.py", line 12, in <module>
      predict = fluid.layers.fc(input=x, size=1, act=None)
    File "/usr/local/lib/python3.7/site-packages/paddle/fluid/layers/nn.py", line 352, in fc
      "y_num_col_dims": 1})
    File "/usr/local/lib/python3.7/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op
      return self.main_program.current_block().append_op(*args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/paddle/fluid/framework.py", line 2917, in append_op
      attrs=kwargs.get("attrs", None))
    File "/usr/local/lib/python3.7/site-packages/paddle/fluid/framework.py", line 2014, in __init__
      for frame in traceback.extract_stack():

    InvalidArgumentError: After flatten the input tensor X and Y to 2-D dimensions matrix X1 and Y1, the matrix X1's width must be equal with matrix Y1's height. But received X's shape = [8, 12], X1's shape = [8, 12], X1's width = 12; Y's shape = [13, 1], Y1's shape = [13, 1], Y1's height = 13.
      [Hint: Expected x_mat_dims[1] == y_mat_dims[0], but received x_mat_dims[1]:12 != y_mat_dims[0]:13.] (at /work/paddle/paddle/fluid/operators/mul_op.cc:83)
      [operator < mul > error]

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@chenwhql chenwhql changed the title Enrich the error types of paddle Enrich the python error types of paddle Oct 20, 2020
@chenwhql chenwhql changed the title Enrich the python error types of paddle Enrich the python error types of paddle & polish format Oct 20, 2020
@phlrain phlrain self-requested a review October 26, 2020 03:20
@zhiqiu zhiqiu self-requested a review October 26, 2020 03:38
Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@jzhang533 jzhang533 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chenwhql chenwhql merged commit 813b2ad into PaddlePaddle:develop Oct 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants