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

[Dy2St] optimize print function convertor to display Tensor at compile time #48672

Merged
merged 13 commits into from
Dec 5, 2022

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Dec 2, 2022

PR types

Function optimization

PR changes

Others

Describe

完成动转静功能扩展和旧接口退场1中的「优化 PrintTransformer 功能优化 print 函数动转静效果」

  • 组网阶段打印 Tensor
  • 移除 PrintTransformer
  • 看看能不能支持 kwargs,应该没啥问题
  • 单测……
  • 新提一个 PR 处理 <frozen importlib._bootstrap>:283: DeprecationWarning: the load_module() method is deprecated and slated for removal in Python 3.12; use exec_module() instead,见 replace deprecated load_module with exec_module #48679

convert_print 的原始行为是组网(编译)阶段仅仅打印非 Tensor 参数,而执行阶段仅仅打印 Tensor 参数,本 PR 修改后在组网阶段也打印了 Tensor 参数

另外组网阶段原始行为是遍历各个参数分别调用 print 函数,这样不仅无法支持 sepend 等关键字参数,而且与动态图行为差异很大,每个参数都会换一下行,本 PR 直接调用一次 print 函数,以使其与动态图行为基本一致,同时也支持了 sepend 等关键字参数

由于在 Python2 print 是语句而不是函数,而在 Python3 中是函数,因此曾经利用 PrintTransformer 来分别对两种 AST node 进行了 transform,最后都转写成 _jst.Print(arg1, arg2, ...),而现在 Paddle 已经不支持 Python22,可以保证 print 一定是函数,故无需专门的 PrintTransformer 来进行处理,直接利用 CallTransformer 转写成 _jst.Call(print)(arg1, arg2, ...) 之后在组网阶段交给 convert_call 处理即可(类似 convert_enumerate 等 built-in 函数的处理),因此用于转写的 PrintTransformer 可以移除,转写后的 _jst.Print 也无需再暴露

此外对单测 test_print 做了若干修改:

  • 清理部分 fluid,使用合适的其余函数代替
  • 使用动静态图统一的 Tensor 概念代替动静态图分别专有的 VarBase、Variable 概念
  • declarative -> to_static
  • 移除 docstring 里的 transform 后的 AST 表示,因为已经不再使用 PrintTransformer 了,AST 的处理与其余函数是一致的
  • 增加关键字参数的单测用例

下面是修改前后的效果对比:

代码:

import paddle
from paddle.jit import to_static, ProgramTranslator

program_translator = ProgramTranslator()

@to_static
def dyfunc_print_variable(x):
    print("dtype:", x.dtype, "shape:", x.shape, "Tensor:", x)

program_translator.enable(True)

x = paddle.to_tensor([1, 2, 3], dtype='float32')

print(dyfunc_print_variable.code)
dyfunc_print_variable(x)

修改前:

# 转写后代码(使用 `_jst.Print`):
def dyfunc_print_variable(x):
    _jst.Print('dtype:', x.dtype, 'shape:', _jst.Shape(x), 'Tensor:', x)

# 组网阶段输出结果(与动态图不一致,每行一个参数,且缺少 Tensor):
dtype:
paddle.float32
shape:
[3]
Tensor:

# 执行阶段输出结果:
Variable: generated_tensor_0
  - lod: {}
  - place: Place(cpu)
  - shape: [3]
  - layout: NCHW
  - dtype: float
  - data: [1 2 3]

修改后:

# 转写后代码(直接复用 `_jst.Call`):
def dyfunc_print_variable(x):
    _jst.Call(print)('dtype:', x.dtype, 'shape:', _jst.Shape(x), 'Tensor:', x)

# 组网阶段输出结果(格式上与动态图行为一致,不过 Tensor 在静态图下是 Variable,所以打印效果和动态图下不一致):
dtype: paddle.float32 shape: [3] Tensor: var generated_tensor_0 : LOD_TENSOR.shape(3,).dtype(float32).stop_gradient(True)

# 执行阶段输出结果(没有区别):
Variable: generated_tensor_0
  - lod: {}
  - place: Place(gpu:0)
  - shape: [3]
  - layout: NCHW
  - dtype: float
  - data: [1 2 3]

Footnotes

  1. [Dy2St] Dygraph to Static function extension tracking issue #48334

  2. [CodeStyle] Legacy python code cleanup tracking issue #46837

@paddle-bot
Copy link

paddle-bot bot commented Dec 2, 2022

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Dec 2, 2022
@SigureMo SigureMo changed the title [Dy2St] refactor convert_print to display Tensor in compile time [WIP][Dy2St] refactor convert_print to display Tensor in compile time Dec 2, 2022
@SigureMo SigureMo changed the title [WIP][Dy2St] refactor convert_print to display Tensor in compile time [WIP][Dy2St] refactor convert_print to display Tensor at compile time Dec 2, 2022
@SigureMo SigureMo changed the title [WIP][Dy2St] refactor convert_print to display Tensor at compile time [Dy2St] refactor convert_print to display Tensor at compile time Dec 3, 2022
@SigureMo SigureMo changed the title [Dy2St] refactor convert_print to display Tensor at compile time [Dy2St] optimize print function to display Tensor at compile time Dec 3, 2022
@SigureMo SigureMo changed the title [Dy2St] optimize print function to display Tensor at compile time [Dy2St] optimize print function convertor to display Tensor at compile time Dec 4, 2022
Copy link
Contributor

@0x45f 0x45f left a comment

Choose a reason for hiding this comment

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

LGTM

@0x45f 0x45f merged commit e707ee5 into PaddlePaddle:develop Dec 5, 2022
@SigureMo SigureMo deleted the dy2st/frontend/print branch December 5, 2022 09:20
lxsbupt pushed a commit to lxsbupt/Paddle that referenced this pull request Dec 17, 2022
…ile time (PaddlePaddle#48672)

* [Dy2St] refactor convert_print to display Tensor in compile time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants