-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[Paddle Inference] add constant folding pass #45494
Merged
cyj1986
merged 53 commits into
PaddlePaddle:develop
from
zhoutianzi666:constant_folding
Aug 30, 2022
Merged
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
d43724e
first commit
zhoutianzi666 be08d0f
commit
zhoutianzi666 f20477b
commit
zhoutianzi666 a6e7297
commit
zhoutianzi666 ba3b70b
commit
zhoutianzi666 695c947
commit
zhoutianzi666 cd594be
commit
zhoutianzi666 4b5c900
commit
zhoutianzi666 ab0b526
commit
zhoutianzi666 11481bf
commit
zhoutianzi666 f897c8e
commit
zhoutianzi666 473d2ac
commit
zhoutianzi666 6b8dc57
support fill_constant
zhoutianzi666 c2b1dd2
support fill_constant
zhoutianzi666 36ab05a
support fill_constant
zhoutianzi666 9c01f94
support fill_constant
zhoutianzi666 53e9a87
add variable in node.h
zhoutianzi666 dbb6595
more common folding
zhoutianzi666 041321e
remove debug code
zhoutianzi666 ed8d4a9
Merge branch 'develop' into constant_folding
zhoutianzi666 5b63ea4
remove debug code
zhoutianzi666 6819c24
only unsqueezze2 folding
zhoutianzi666 e7b9859
Merge branch 'develop' into constant_folding
zhoutianzi666 b423ef7
merge develop
zhoutianzi666 858f0d3
merge develop
zhoutianzi666 3f52b8c
merge develop
zhoutianzi666 9a8c34a
Merge branch 'develop' into constant_folding
zhoutianzi666 ad8a90d
clean code
zhoutianzi666 5b07d1d
Merge branch 'constant_folding' of https://github.com/zhoutianzi666/P…
zhoutianzi666 26398a4
clean code
zhoutianzi666 a7d8ffe
clean code
zhoutianzi666 b9c3935
clean code
zhoutianzi666 5272e3f
clean code
zhoutianzi666 abe7009
clean code
zhoutianzi666 9baaf5c
clean code
zhoutianzi666 42a5485
clean code
zhoutianzi666 adcd21b
clean code
zhoutianzi666 7540b9d
clean code
zhoutianzi666 8efa96e
clean code
zhoutianzi666 a3dc3d1
clean code
zhoutianzi666 fc03d5c
clean code
zhoutianzi666 3383a75
clean code
zhoutianzi666 e2585a5
clean code
zhoutianzi666 72cbfc7
clean code
zhoutianzi666 7204b4c
clean code
zhoutianzi666 2da81dc
clean code
zhoutianzi666 d73c0cd
clean code
zhoutianzi666 317f31b
clean code
zhoutianzi666 9f6b47b
clean code
zhoutianzi666 43de7e8
clean code
zhoutianzi666 1758ed7
clean code
zhoutianzi666 20b3695
clean code
zhoutianzi666 8228590
clean code
zhoutianzi666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include "paddle/fluid/framework/ir/constant_folding_pass.h" | ||
#include <string> | ||
#include <vector> | ||
#include "glog/logging.h" | ||
#include "paddle/fluid/framework/ir/graph_helper.h" | ||
#include "paddle/fluid/framework/ir/graph_pattern_detector.h" | ||
#include "paddle/fluid/framework/ir/pass.h" | ||
#include "paddle/fluid/framework/op_registry.h" | ||
#include "paddle/fluid/framework/op_version_registry.h" | ||
#include "paddle/fluid/platform/enforce.h" | ||
|
||
#include "paddle/fluid/framework/convert_utils.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
namespace ir { | ||
class Node; | ||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle | ||
|
||
/* | ||
* When a op's inputs and outputs is determined before feeding data to the | ||
* model, we can remove this op from the model. This ConstantFolding pass can | ||
* remove all these like ops. | ||
* | ||
*/ | ||
|
||
namespace paddle { | ||
namespace framework { | ||
namespace ir { | ||
namespace patterns { | ||
|
||
struct ConstantFolding : public PatternBase { | ||
ConstantFolding(PDPattern *pattern, const std::string &name_scope) | ||
: PatternBase(pattern, name_scope, "constant_folding_pass") {} | ||
}; | ||
} // namespace patterns | ||
|
||
ConstantFoldingPass::ConstantFoldingPass() {} | ||
|
||
void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { | ||
PADDLE_ENFORCE_NOT_NULL( | ||
graph, platform::errors::PreconditionNotMet("graph should not be null.")); | ||
FusePassBase::Init("constant_folding", graph); | ||
auto *scope = param_scope(); | ||
|
||
PADDLE_ENFORCE_NOT_NULL( | ||
scope, | ||
platform::errors::Fatal( | ||
"scope must not be null when applying constant floding.")); | ||
|
||
// Now, I don't want to fold fill_constant op in Paddle-TRT | ||
std::vector<std::string> blacklist{"fill_constant", "feed"}; | ||
|
||
auto op_node_sorted = framework::ir::TopologyVarientSort( | ||
*graph, static_cast<framework::ir::SortKind>(0)); | ||
for (auto *op_node : op_node_sorted) { | ||
if (!op_node->IsOp()) continue; | ||
if (std::find(blacklist.begin(), blacklist.end(), op_node->Name()) != | ||
blacklist.end()) | ||
continue; | ||
|
||
bool input_persis = true; | ||
// map is used to record how many time a name string occures in the whole | ||
// graph's nodes | ||
std::map<std::string, int> map; | ||
for (auto in_node : op_node->inputs) { | ||
map[in_node->Name()] = 0; | ||
if (!in_node->Var()->Persistable()) { | ||
input_persis = false; | ||
} | ||
} | ||
for (auto out_node : op_node->outputs) { | ||
map[out_node->Name()] = 0; | ||
} | ||
// Forbid other node in graph having the same name with nodes in map | ||
for (auto iter : map) { | ||
for (auto node : graph->Nodes()) { | ||
if (node->IsVar() && node->Name() == iter.first) { | ||
map[node->Name()]++; | ||
if (map[node->Name()] > 1) { | ||
input_persis = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
framework::Scope *local_scope = new framework::Scope(); | ||
std::unordered_set<const paddle::framework::ir::Node *> remove_nodes; | ||
std::unique_ptr<OperatorBase> op; | ||
|
||
if (input_persis) { | ||
for (auto in_node : op_node->inputs) { | ||
local_scope->Var(in_node->Var()->Name()); | ||
local_scope->FindVar(in_node->Var()->Name())->GetMutable<LoDTensor>(); | ||
// This persistable input node is exclusive, and can be removed | ||
if (in_node->outputs.size() == 1L) remove_nodes.emplace(in_node); | ||
|
||
auto in_shape = in_node->Var()->GetShape(); | ||
auto *global_persis_x_tensor = | ||
scope->FindVar(in_node->Name())->GetMutable<LoDTensor>(); | ||
auto *local_x_tensor = | ||
local_scope->FindVar(in_node->Name())->GetMutable<LoDTensor>(); | ||
local_x_tensor->Resize(global_persis_x_tensor->dims()); | ||
*local_x_tensor = *global_persis_x_tensor; | ||
} | ||
|
||
op = paddle::framework::OpRegistry::CreateOp(*op_node->Op()); | ||
remove_nodes.emplace(op_node); | ||
for (auto out_node : op_node->outputs) { | ||
local_scope->Var(out_node->Var()->Name()); | ||
local_scope->FindVar(out_node->Var()->Name())->GetMutable<LoDTensor>(); | ||
// useless out_node can be removed, not need set it persistable ! | ||
if (out_node->outputs.size() == 0L) remove_nodes.emplace(out_node); | ||
} | ||
op->Run(*local_scope, platform::CPUPlace()); | ||
for (auto out_node : op_node->outputs) { | ||
// this out_node is useless, do not set it persistable | ||
if (out_node->outputs.size() == 0L) continue; | ||
auto out_desc = out_node->Var(); | ||
auto out_name = out_desc->Name(); | ||
auto *local_out_tensor = | ||
local_scope->FindVar(out_name)->GetMutable<LoDTensor>(); | ||
std::vector<int64_t> out_shape; | ||
for (int64_t i = 0; i < local_out_tensor->dims().size(); i++) { | ||
out_shape.push_back(local_out_tensor->dims()[i]); | ||
} | ||
out_desc->SetShape(out_shape); | ||
out_desc->SetPersistable(true); | ||
auto *global_out_tensor = scope->Var(out_name)->GetMutable<LoDTensor>(); | ||
*global_out_tensor = *local_out_tensor; | ||
} | ||
GraphSafeRemoveNodes(graph, remove_nodes); | ||
} | ||
delete local_scope; | ||
} | ||
} | ||
|
||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle | ||
|
||
REGISTER_PASS(constant_folding_pass, | ||
paddle::framework::ir::ConstantFoldingPass); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#pragma once | ||
|
||
#include "paddle/fluid/framework/ir/fuse_pass_base.h" | ||
|
||
namespace paddle { | ||
|
||
namespace framework { | ||
namespace ir { | ||
|
||
class Graph; | ||
|
||
class ConstantFoldingPass : public FusePassBase { | ||
public: | ||
ConstantFoldingPass(); | ||
virtual ~ConstantFoldingPass() {} | ||
|
||
protected: | ||
void ApplyImpl(ir::Graph* graph) const override; | ||
}; | ||
|
||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
增加nulltpr判断