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

【PIR】Dygraph_to_static 下需要修复 PIR 的用例。 #56607

Closed
yangguohao opened this issue Aug 24, 2023 · 3 comments
Closed

【PIR】Dygraph_to_static 下需要修复 PIR 的用例。 #56607

yangguohao opened this issue Aug 24, 2023 · 3 comments
Assignees
Labels
PFCC Paddle Framework Contributor Club,https://github.com/PaddlePaddle/community/tree/master/pfcc status/close 已关闭

Comments

@yangguohao
Copy link
Contributor

yangguohao commented Aug 24, 2023

问题描述 Please describe your issue

子图验证&核心机制完善

本项目是新IR推全测试,兼容现有动转静执行逻辑的重要环节。目前我们通过动态图组网 -> AST翻译 ->
静态图表示 -> ProgramTranslator -> 新IR下的静态图表示 -> NewIRInterpreter 这样的路径支持了在新IR下飞桨的动转静功能。

目前从旧静态图到新静态图下存在某些问题,需要经过充分的测试。
我们希望通过复用现有的子图级别的测试(即test/dygraph_to_static目录下的测试)验证新IR的功能完备性和正确性。

目前动转静测试中存在问题的内容有:

  1. 在 PIR 下缺少对应的算子
  2. ProgramTranslator 转译过程中部分 Op 在新旧 IR 下定义不同导致需要特殊处理。

修复

为新 IR 添加算子

对于缺少对应的算子,报错信息类似于 Op xxx should have corresponding OpInfo pd_op.xxx

以下几个步骤对应来解决

1. 配置 ops.yaml 和 backward.yaml

  • 首先根据该 Op 在旧 IR 下的定义,来补充该 Op 在新 ir 下的定义。
    旧 IR 的 Op 定义文件名称一般都为 xxx_op.cc,我们可以直接看到 xxxOpMaker 下的 make 来查看 input、output等信息。
    如果该 Op 有反向过程,就需要对前反向同时进行补充。
  • 新 ir 下的 Op 可以通过 yaml 定义自动生成,文件在 paddle/fluid/pir/dialect/operator/ir/ops.yamlpaddle/fluid/pir/dialect/operator/ir/backward.yaml
    yaml 文件的前反向规则可以参考 C++算子开发文档的3.1 算子 Yaml 文件配置

2. 配置 op_compat.yaml

  • 转译过程中新旧 IR 的 Op 参数名称不同,其映射定义在 paddle/phi/api/yaml/op_compat.yaml 中。
  • 一般地我们只需要将旧 IR 下对应驼峰命名转为新 IR 下的下划线命名即可。

3.配置 infermeta 信息

  • 根据旧 IR 下的 Op 文件中的 InferShape 来进行具体的配置。
  • 一般情况下我们只需要将 InferShape 内的函数直接迁移到 InferMeta 下,修改一下函数的名称,删除 InferShapeContext 的相关逻辑即可。
  • 详细的信息可以查看C++算子开发文档的3.2 实现 InferMeta 函数

一般情况下根据上面几步骤就能解决问题,有较为特殊的算子需要具体分析。

转译过程中的特殊处理

转译的过程一般通过 ProgramTranslator 来执行,在文件 paddle/fluid/ir_adaptor/translator/program_translator.cc

TranslateBlock 负责 BlockDesc 的转译,而 BlockDesc 内包含的 OpDesc 则是由 OpTranslator 负责,在文件 paddle/fluid/ir_adaptor/translator/op_translator.cc 下

通用的翻译规则一般是以下几步

  1. LoopkUpOpInfo 获得 yaml 里定义的 Op 的 input、output、attr 等等信息。
  2. GenerateOperationInput、GenerateOperationOutput 生成新 ir 下的输入输出。
  3. TranslateOpAttribute 转译 attr。
  4. 生成新 op,放入新 IR 中。
  5. RecordOpResultMapping 记录该 Op 新旧 ir 的 result 的映射关系。

在分析了特殊的情况后,可以根据定位的报错信息来具体改写相对应的函数。

验收标准

  1. 打开 FLAGS_enable_pir_in_executor 的情况下单测成功运行
  2. 为单测内的 test 函数添加装饰器 test_legacy_and_pir 并且成功运行。

Q&A

待修复的测试内容

经过测试,以下的案例在 new IR 下未通过测试。

序号 任务 失败原因 PR 开发者
1 test_build_strategy 已部分修复,仍存在问题:反向中没有 Intermediate_out_grad #57827 @yangguohao
2 test_seq2seq 部分解决,现在还存在 Lod_array_length,tensor_array 等问题
3 test_lstm CUDNN value error
6 test_len/test_slice/test_list 部分修复,单测内都存在 lod_array_length 的问题
7 test_error pd_op.reshape raises an EnforceNotMet exception @chen2016013
8 test_program_translator IfOp 翻译过程出现问题
9 test_lac UserWarning: full_graph=False don't support input_spec arguments. It will not produce any effect.You can set full_graph=True, then you can assign input spec.
10 test_pylayer Op pylayer should have corresponding OpInfo pd_op.pylayer
12 test_jit_setitem While_instruction 的执行过程存在问题

以下的测试用例不会运行到转译和 PIR 执行器中,或是无需进行测试,在此记录。

序号 任务 失败原因 PR 开发者
32 test_eval_frame
35 test_op_attr
36 test_origin_info
37 test_params_no_grad
38 test_local_cast
39 test_tensor_hook 无需修复
@yangguohao
Copy link
Contributor Author

yangguohao commented Aug 24, 2023

之前的表格有错误,重新整理了一下。

@paddle-bot paddle-bot bot added the PFCC Paddle Framework Contributor Club,https://github.com/PaddlePaddle/community/tree/master/pfcc label Aug 24, 2023
@yangguohao
Copy link
Contributor Author

test_cinn_prim 在我加上了 test_and_compare_with_new_ir 后测试都可以通过,具体可见该 PR

但是在直接运行 FLAGS_enable_new_ir_in_executor=1 python test_cinn_prim.py 时,却报了以下错误

Error occured at: /home/aistudio/Paddle/paddle/fluid/ir_adaptor/translator/op_translator.cc:498 :
Op reshape2 arg XShape should be optional if it can be empty
terminate called after throwing an instance of 'ir::IrNotMetException'
  what():  Error occured at: /home/aistudio/Paddle/paddle/fluid/ir_adaptor/translator/op_translator.cc:498 :
Op reshape2 arg XShape should be optional if it can be empty

不知道是我加上装饰器的方式错误,还是开启 FLAGS 运行的方式错误了

@kangguangli
Copy link
Contributor

prim/组合算子相关的测试可以不用处理,这个是已知问题,reshape这个算子在新IR和组合算子下的签名不一致,目前不支持。 @yangguohao

@Ligoml Ligoml removed status/new-issue 新建 type/others 其他问题 labels Aug 29, 2023
@Aurelius84 Aurelius84 changed the title 【NEWIR】Dygraph_to_static 下需要修复 new IR 的用例。 【PIR】Dygraph_to_static 下需要修复 PIR 的用例。 Sep 22, 2023
@paddle-bot paddle-bot bot added the status/close 已关闭 label Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PFCC Paddle Framework Contributor Club,https://github.com/PaddlePaddle/community/tree/master/pfcc status/close 已关闭
Projects
None yet
Development

No branches or pull requests

3 participants