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

[NewIR] Add parser to deserialize #55695

Merged
merged 21 commits into from
Sep 8, 2023
Merged

[NewIR] Add parser to deserialize #55695

merged 21 commits into from
Sep 8, 2023

Conversation

xingmingyyj
Copy link
Contributor

@xingmingyyj xingmingyyj commented Jul 25, 2023

PR types

Others

PR changes

Others

Description

该PR为Paddle增加Parser模块,该模块可以完成将文本文件分序列化为计算图的功能。Parser主要支持AttributeTypeProgram的反序列化。

设计文档

Parser设计文档

如何反序列化Program

参考代码如下:

ir::IrContext *ctx = ir::IrContext::Instance();
ctx->GetOrRegisterDialect<PaddleDialect>();

std::ifstream program_text(file_path);
std::unique_ptr<ir::Program> program = ir::Program::Parse(program_text,ctx);
std::cout << *program << std::endl;

可以参考test/cpp/ir/core/program_translator_test.ccIrParserTest,提供了例子。

如何反序列化Type/Attribute

TypeAttribute的Parse方式和Program类似,如下:

std::stringstream attr_text("(Array)[(pd.DataType)bool,(pd.DataType)float32,(pd.DataType)float64]");
auto attribute = ir::Program::Parse(ctx, attr_text);
std::cout << attribute << std::endl;

可以参考test/cpp/ir/core/ir_parser_test.cctest/cpp/ir/core/TestParserText.txt

如果新定义了一个dialect,如何为它自定义parser?

在定义dialect时,需要重载下面几个方法:

virtual Type ParseType(IrParser &parser) {  // NOLINT
  IR_THROW("dialect has no registered type parsing hook");
}

virtual Attribute ParseAttribute(IrParser &parser) {  // NOLINT
  IR_THROW("dialect has no registered attribute parsing hook");
}

virtual Operation ParseOperation(IrParser &parser) {  // NOLINT
  IR_THROW("dialect has no registered operation parsing hook");
}

可以参考test/cpp/ir/core/add_dialect_parser_test.cc

@paddle-bot paddle-bot bot added the contributor External developers label Jul 25, 2023
@kangguangli kangguangli changed the title Add parser [NewIR] Add parser to deserialize Jul 26, 2023
@kangguangli kangguangli self-assigned this Jul 26, 2023
Copy link
Contributor

@kangguangli kangguangli left a comment

Choose a reason for hiding this comment

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

关于文件结构,可以把parser相关的文件在 ir/core路径下单独建一个子文件夹如ir/core/parser处理,把token.h/lexer.h/lexer.cc/ir_parser.cc放在里面,就留ir_parser.h在外面吧
记得顺便更改下ir/core/CMakeLists

paddle/ir/core/ir_parser.h Show resolved Hide resolved
paddle/ir/core/ir_parser.h Outdated Show resolved Hide resolved
paddle/ir/core/lexer.cc Outdated Show resolved Hide resolved
paddle/ir/core/ir_parser.cc Outdated Show resolved Hide resolved
paddle/ir/core/ir_parser.h Outdated Show resolved Hide resolved
paddle/fluid/ir/dialect/pd_dialect.h Outdated Show resolved Hide resolved
@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Aug 2, 2023

Sorry to inform you that 17baf61's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

paddle/ir/core/type.h Outdated Show resolved Hide resolved
@xingmingyyj xingmingyyj reopened this Aug 24, 2023
paddle/ir/core/parser/lexer.h Outdated Show resolved Hide resolved
paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h Outdated Show resolved Hide resolved
paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h Outdated Show resolved Hide resolved
paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h Outdated Show resolved Hide resolved
paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h Outdated Show resolved Hide resolved
test/cpp/ir/core/program_translator_test.cc Outdated Show resolved Hide resolved
paddle/ir/core/parser/CMakeLists.txt Outdated Show resolved Hide resolved
paddle/ir/core/ir_parser.h Outdated Show resolved Hide resolved
paddle/ir/core/parser/ir_parser.cc Outdated Show resolved Hide resolved
paddle/ir/core/dialect.h Show resolved Hide resolved
paddle/ir/core/dialect.h Show resolved Hide resolved
paddle/ir/core/ir_parser.h Show resolved Hide resolved
paddle/ir/core/parser/CMakeLists.txt Outdated Show resolved Hide resolved
paddle/ir/core/CMakeLists.txt Show resolved Hide resolved
paddle/ir/core/parser/lexer.h Outdated Show resolved Hide resolved
Copy link
Contributor

@kangguangli kangguangli left a comment

Choose a reason for hiding this comment

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

@xingmingyyj 麻烦完善下PR描述,结合这里的单测说明下新加入的parser模块的用法,如果有的话,可以把文档里面写好的部分粘贴过来,没有的话麻烦补充下。

paddle/ir/core/parser/token.h Outdated Show resolved Hide resolved
// limitations under the License.

#include "paddle/ir/core/ir_parser.h"
#include <gtest/gtest.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#include <gtest/gtest.h>
#include <fstream>
#include <iostream>
#include <gtest/gtest.h>

这个顺序更合适些

paddle/ir/core/parser/ir_parser.cc Show resolved Hide resolved
paddle/ir/core/parser/ir_parser.cc Outdated Show resolved Hide resolved
Co-authored-by: kangguangli <kangguangli@hotmail.com>
@xingmingyyj
Copy link
Contributor Author

@xingmingyyj 麻烦完善下PR描述,结合这里的单测说明下新加入的parser模块的用法,如果有的话,可以把文档里面写好的部分粘贴过来,没有的话麻烦补充下。

已添加

kangguangli
kangguangli previously approved these changes Sep 7, 2023
Copy link
Contributor

@kangguangli kangguangli left a comment

Choose a reason for hiding this comment

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

Good job!

Copy link
Contributor

@risemeup1 risemeup1 left a comment

Choose a reason for hiding this comment

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

LGTM

gtest
pd_dialect
ir)

Copy link
Contributor

Choose a reason for hiding this comment

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

建议将cc_test_old改为cc_test,link静态库会导致可执行文件比较大,会在连接的时候占用更多的内存,可能导致内存打满,coverage编译挂掉,鉴于此PR编译出来的单测可执行文件小与500M,可提前approve,建议合入之后,改为cc_test,或者使用cc_test_old依赖libpaddle.so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的👌

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

@kangguangli kangguangli merged commit a2d6145 into PaddlePaddle:develop Sep 8, 2023
@kangguangli
Copy link
Contributor

kangguangli commented Sep 8, 2023

@xingmingyyj 恭喜PR合入了,可以先解决下上面提到的cc_test_old的问题,替换成cc_test试试,看看有没有什么问题

@xingmingyyj
Copy link
Contributor Author

@xingmingyyj 恭喜PR合入了,可以先解决下上面提到的cc_test_old的问题,替换成cc_test试试,看看有没有什么问题

好的好的👌

BeingGod pushed a commit to BeingGod/Paddle that referenced this pull request Sep 9, 2023
* add parser

* add parser

* add parser

* add parser

* add parser

* add parser

* add parser

* Update test/cpp/ir/core/ir_parser_test.cc

Co-authored-by: kangguangli <kangguangli@hotmail.com>

* add parser

* add parser

* add parser

* Update test/cpp/ir/core/program_translator_test.cc

Co-authored-by: kangguangli <kangguangli@hotmail.com>

* Update test/cpp/ir/core/program_translator_test.cc

Co-authored-by: kangguangli <kangguangli@hotmail.com>

* Update dialect.h

* add parser

* add parser

* Update CMakeLists.txt

* add parser

---------

Co-authored-by: kangguangli <kangguangli@hotmail.com>
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.

6 participants