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

【静态图性能优化】图依赖信息复用 #55389

Merged
merged 13 commits into from
Jul 20, 2023

Conversation

AndSonder
Copy link
Contributor

@AndSonder AndSonder commented Jul 13, 2023

PR types

Performance optimization

PR changes

Others

Description

在Paddle中,图依赖分析(op dependency)发生在Convert过程中,其依赖分析过程在BuildOperatorDependences 构建。图依赖分析的主要作用是分析 op 之间的依赖关系,对计算图进行解析和分析,构建出每个算子的调度关系和执行顺序,以保证正确的计算顺序和结果。对于一样的子图,图依赖分析的结果是一样的,因此可以进行复用。

详细介绍见Issue:

@paddle-bot
Copy link

paddle-bot bot commented Jul 13, 2023

你的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 Jul 13, 2023
// first job to other jobs. The shared build result includes op dependency,
// event analyzer, thread scheduling and GC.
for (const auto& pair : type_to_id) {
const auto& idx = pair.second;
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
const auto& idx = pair.second;
const auto& ids = pair.second;

idx是单数,ids是复数。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -115,6 +117,10 @@ class ProgramInterpreter : public InterpreterBaseImpl {
// workqueue
std::shared_ptr<interpreter::AsyncWorkQueue> GetWorkQueue();

// op dependences
interpreter::DependencyBuilder* GetDependencyBuilder();
std::shared_ptr<std::vector<size_t>> GetDependecyCount();
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
std::shared_ptr<std::vector<size_t>> GetDependecyCount();
std::shared_ptr<std::vector<size_t>> GetDependencyCount();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -311,6 +322,17 @@ ProgramInterpreter::GetWorkQueue() {
return async_work_queue_;
}

interpreter::DependencyBuilder* ProgramInterpreter::GetDependencyBuilder() {
return &dependency_builder_;
Copy link
Contributor

Choose a reason for hiding this comment

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

需要共享的数据,请用智能指针做生命周期管理。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是 dependency_builder_ 下的成员变量进行共享,dependency_builder_ 也需要进行生命周期管理吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, 换成了返回一个const引用

Comment on lines 288 to 291
dependency_builder_.ShareDependencyFrom(
reinterpret_cast<ProgramInterpreter*>(src)->GetDependencyBuilder());
dependecy_count_ =
reinterpret_cast<ProgramInterpreter*>(src)->GetDependecyCount();
Copy link
Contributor

Choose a reason for hiding this comment

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

建议GetDependencyBuilderGetDependecyCount也通过虚函数的方式实现多态,避免使用reinterpret_cast

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

std::shared_ptr<std::vector<size_t>> ProgramInterpreter::GetDependecyCount() {
if (dependecy_count_ == nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

dependecy_count_不需要延迟创建,可以直接在构造函数中创建。这样GetDependencyBuilderGetDependencyCount都可以声明成const函数。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -123,6 +129,7 @@ class ProgramInterpreter : public InterpreterBaseImpl {

bool is_build_{false};
bool static_build_{false};
bool is_shared_{false};
Copy link
Contributor

Choose a reason for hiding this comment

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

加个注释说明具体share了哪些东西

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -283,6 +283,17 @@ void ProgramInterpreter::ShareWorkQueueFrom(InterpreterBaseImpl* src) {
<< ") to InterpreterCore(" << this << ")";
}

void ProgramInterpreter::ShareBuildResultsFrom(InterpreterBaseImpl* src) {
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
void ProgramInterpreter::ShareBuildResultsFrom(InterpreterBaseImpl* src) {
void ProgramInterpreter::ShareBuildResultsFrom(const InterpreterBaseImpl& src) {

非可选输入参数通常是值参或 const 引用, 非可选输出参数或输入/输出参数通常应该是引用 (不能为空).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

void ShareDependencyFrom(DependencyBuilder* src);
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
void ShareDependencyFrom(DependencyBuilder* src);
void ShareDependencyFrom(const DependencyBuilder& src);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

std::tuple<std::shared_ptr<std::map<size_t, std::set<size_t>>>,
std::shared_ptr<std::vector<std::vector<bool>>>>
DependencyBuilder::GetDependency() {
if (op_downstream_map_ == nullptr || op_happens_before_ == nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

没必要延迟创建

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@AndSonder AndSonder requested a review from From00 July 18, 2023 08:30
Copy link
Contributor

@From00 From00 left a comment

Choose a reason for hiding this comment

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

LGTM

@From00 From00 merged commit ee65599 into PaddlePaddle:develop Jul 20, 2023
cqulilujia pushed a commit to cqulilujia/Paddle that referenced this pull request Jul 24, 2023
* add share api for DependencyBuilder

* add judge codes for sharing build results

* add ShareBuildResultsFrom

* update ShareDependencyFrom

* fix error

* add share codes

* fix memory error

* update according review

* update notes

* fix code style

* remove const_cast

* fix code style
wz1qqx pushed a commit to wz1qqx/Paddle that referenced this pull request Jul 31, 2023
* add share api for DependencyBuilder

* add judge codes for sharing build results

* add ShareBuildResultsFrom

* update ShareDependencyFrom

* fix error

* add share codes

* fix memory error

* update according review

* update notes

* fix code style

* remove const_cast

* fix code style
jinjidejinmuyan pushed a commit to jinjidejinmuyan/Paddle that referenced this pull request Aug 30, 2023
* add share api for DependencyBuilder

* add judge codes for sharing build results

* add ShareBuildResultsFrom

* update ShareDependencyFrom

* fix error

* add share codes

* fix memory error

* update according review

* update notes

* fix code style

* remove const_cast

* fix code style
@AndSonder AndSonder deleted the share_build_results branch April 23, 2024 13:57
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.

2 participants