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

modify graph_pattern to thread_local #43945

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 103 additions & 43 deletions paddle/fluid/framework/ir/graph_pattern_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ using string::Style;

size_t PDPattern::id_ = 0UL;

#ifdef PADDLE_WITH_TENSORRT
namespace patterns {
thread_local std::unordered_map<std::string, size_t> KeyCounter::dic_;
}
#endif

PDNode *PDPattern::NewNode(const std::string &name) {
if (!name.empty()) {
PADDLE_ENFORCE_EQ(
node_map_.count(name), 0UL,
node_map_.count(name),
0UL,
platform::errors::PreconditionNotMet(
"PDNode's name should be unique, get duplicate [%s]", name));
}
Expand All @@ -45,7 +52,8 @@ PDNode *PDPattern::NewNode(const std::string &name) {
PDNode *PDPattern::NewNode(PDNode::teller_t &&teller, const std::string &name) {
if (!name.empty()) {
PADDLE_ENFORCE_EQ(
node_map_.count(name), 0UL,
node_map_.count(name),
0UL,
platform::errors::PreconditionNotMet(
"PDNode's name should be unique, get duplicate [%s]", name));
}
Expand All @@ -70,8 +78,10 @@ void PDPattern::AddEdge(PDNode *a, PDNode *b) {
a, platform::errors::NotFound("PDNode %s is not found.", a->name()));
PADDLE_ENFORCE_NOT_NULL(
b, platform::errors::NotFound("PDNode %s is not found.", b->name()));
PADDLE_ENFORCE_NE(a, b, platform::errors::PermissionDenied(
"Cannot connect the same node in the graph."));
PADDLE_ENFORCE_NE(a,
b,
platform::errors::PermissionDenied(
"Cannot connect the same node in the graph."));
edges_.emplace_back(a, b);
}

Expand Down Expand Up @@ -128,7 +138,8 @@ void GraphPatternDetector::ValidateByNodeRole(

subgraphs->erase(
std::remove_if(
subgraphs->begin(), subgraphs->end(),
subgraphs->begin(),
subgraphs->end(),
[](const GraphPatternDetector::subgraph_t &subgraph) -> bool {
// Collect the inputs and outputs.
std::set<Node *> ios;
Expand Down Expand Up @@ -310,7 +321,8 @@ void GraphPatternDetector::SortSubgraphs(
}

std::sort(
subgraphs->begin(), subgraphs->end(),
subgraphs->begin(),
subgraphs->end(),
[](const GraphPatternDetector::subgraph_t &a,
const GraphPatternDetector::subgraph_t &b) {
for (auto &item : a) {
Expand Down Expand Up @@ -438,7 +450,8 @@ PDNode *PDNode::assert_is_persistable_var() {
}

PDNode *PDNode::assert_is_op_nth_input(const std::string &op_type,
const std::string &argument, int nth) {
const std::string &argument,
int nth) {
assert_is_var();
assert_is_op_input(op_type);
asserts_.emplace_back([=](Node *x) {
Expand All @@ -453,7 +466,8 @@ PDNode *PDNode::assert_is_op_nth_input(const std::string &op_type,
}

PDNode *PDNode::assert_is_op_nth_output(const std::string &op_type,
const std::string &argument, int nth) {
const std::string &argument,
int nth) {
assert_is_var();
asserts_.emplace_back([=](Node *x) {
for (auto *op : x->inputs) {
Expand Down Expand Up @@ -580,7 +594,8 @@ PDNode *PDNode::assert_is_ops(const std::unordered_set<std::string> &op_types) {

PDNode *PDNode::assert_is_ops_nth_input(
const std::unordered_set<std::string> &op_types,
const std::string &argument, int nth) {
const std::string &argument,
int nth) {
assert_is_var();
assert_is_ops_input(op_types);
asserts_.emplace_back([=](Node *x) {
Expand All @@ -596,7 +611,8 @@ PDNode *PDNode::assert_is_ops_nth_input(

PDNode *PDNode::assert_is_ops_nth_output(
const std::unordered_set<std::string> &op_types,
const std::string &argument, int nth) {
const std::string &argument,
int nth) {
assert_is_var();
asserts_.emplace_back([=](Node *x) {
for (auto *op : x->inputs) {
Expand Down Expand Up @@ -693,11 +709,13 @@ bool VarLinksToOp(Node *node, const std::string &op_type) {

bool IsNthInput(Node *var, Node *op, const std::string &argument, size_t nth) {
PADDLE_ENFORCE_EQ(
var->IsVar(), true,
var->IsVar(),
true,
platform::errors::InvalidArgument(
"First parameter of function IsNthInput must be Node::Var"));
PADDLE_ENFORCE_EQ(
op->IsOp(), true,
op->IsOp(),
true,
platform::errors::InvalidArgument(
"Second parameter of function IsNthInput must be Node::Op"));
if (!HasInput(op, argument) || op->Op()->Input(argument).size() <= nth)
Expand All @@ -707,7 +725,8 @@ bool IsNthInput(Node *var, Node *op, const std::string &argument, size_t nth) {

bool HasInput(Node *op, const std::string &argument) {
PADDLE_ENFORCE_EQ(
op->IsOp(), true,
op->IsOp(),
true,
platform::errors::InvalidArgument(
"First parameter of function HasInput must be Node::Op"));
auto const &names = op->Op()->InputNames();
Expand All @@ -718,7 +737,8 @@ bool HasInput(Node *op, const std::string &argument) {

bool HasOutput(Node *op, const std::string &argument) {
PADDLE_ENFORCE_EQ(
op->IsOp(), true,
op->IsOp(),
true,
platform::errors::InvalidArgument(
"First parameter of function HasOuput must be Node::Op"));
auto const &names = op->Op()->OutputNames();
Expand All @@ -729,11 +749,13 @@ bool HasOutput(Node *op, const std::string &argument) {

bool IsNthOutput(Node *var, Node *op, const std::string &argument, size_t nth) {
PADDLE_ENFORCE_EQ(
var->IsVar(), true,
var->IsVar(),
true,
platform::errors::InvalidArgument(
"First parameter of function IsNthOutput must be Node::Var"));
PADDLE_ENFORCE_EQ(
op->IsOp(), true,
op->IsOp(),
true,
platform::errors::InvalidArgument(
"Second parameter of function IsNthOutput must be Node::Op"));
if (!HasOutput(op, argument) || op->Op()->Output(argument).size() <= nth)
Expand Down Expand Up @@ -875,22 +897,35 @@ PDNode *patterns::ConvBN::operator()(paddle::framework::ir::PDNode *conv_input,
eltwise_op->LinksFrom({conv_out_var, eltwise_y_in_var})
.LinksTo({eltwise_out_var});
batch_norm_op
->LinksFrom({eltwise_out_var, bn_scale_var, bn_bias_var, bn_mean_var,
->LinksFrom({eltwise_out_var,
bn_scale_var,
bn_bias_var,
bn_mean_var,
bn_variance_var})
.LinksTo({bn_out_var, bn_mean_out_var, bn_variance_out_var,
bn_saved_mean_var, bn_saved_variance_var});
.LinksTo({bn_out_var,
bn_mean_out_var,
bn_variance_out_var,
bn_saved_mean_var,
bn_saved_variance_var});
} else {
batch_norm_op
->LinksFrom({conv_out_var, bn_scale_var, bn_bias_var, bn_mean_var,
->LinksFrom({conv_out_var,
bn_scale_var,
bn_bias_var,
bn_mean_var,
bn_variance_var})
.LinksTo({bn_out_var, bn_mean_out_var, bn_variance_out_var,
bn_saved_mean_var, bn_saved_variance_var});
.LinksTo({bn_out_var,
bn_mean_out_var,
bn_variance_out_var,
bn_saved_mean_var,
bn_saved_variance_var});
}
return bn_out_var;
}

PDNode *patterns::ConvActivation::operator()(
paddle::framework::ir::PDNode *conv_input, std::string conv_type,
paddle::framework::ir::PDNode *conv_input,
std::string conv_type,
std::string activation_type) {
// Create Operators
conv_input->assert_is_op_input(conv_type, "Input");
Expand Down Expand Up @@ -920,7 +955,8 @@ PDNode *patterns::ConvActivation::operator()(

PDNode *patterns::ElementwiseActivation::operator()(
paddle::framework::ir::PDNode *elementwise_a,
const std::string &elementwise_type, const std::string &activation_type) {
const std::string &elementwise_type,
const std::string &activation_type) {
// Create Operators
elementwise_a->assert_is_op_input(elementwise_type, "X");
auto *elementwise_op =
Expand Down Expand Up @@ -995,7 +1031,8 @@ PDNode *patterns::SeqConvEltAddRelu::operator()(
}

PDNode *patterns::FC::operator()(paddle::framework::ir::PDNode *x,
bool with_bias, bool with_relu) {
bool with_bias,
bool with_relu) {
// Create shared nodes.
x->assert_is_op_input("mul", "X");
auto *mul = pattern->NewNode(mul_repr())->assert_is_op("mul");
Expand Down Expand Up @@ -1261,8 +1298,12 @@ PDNode *patterns::BatchNormAct::operator()(

bn->LinksFrom(
{bn_x_var, bn_scale_var, bn_bias_var, bn_variance_var, bn_mean_var})
.LinksTo({bn_mean_out_var, bn_variance_out_var, bn_saved_variance_var,
bn_saved_mean_var, bn_reserve_space, bn_out_var});
.LinksTo({bn_mean_out_var,
bn_variance_out_var,
bn_saved_variance_var,
bn_saved_mean_var,
bn_reserve_space,
bn_out_var});
act->LinksFrom({bn_out_var}).LinksTo({act_out_var});

return act_out_var;
Expand Down Expand Up @@ -1319,8 +1360,13 @@ PDNode *patterns::BatchNormActGrad::operator()(
.LinksTo({d_intermediate_var});

bn_grad
->LinksFrom({bn_x_var, d_intermediate_var, bn_scale_var, bn_bias_var,
bn_saved_mean_var, bn_saved_variance_var, bn_reserve_space})
->LinksFrom({bn_x_var,
d_intermediate_var,
bn_scale_var,
bn_bias_var,
bn_saved_mean_var,
bn_saved_variance_var,
bn_reserve_space})
.LinksTo({d_bn_x_var, d_bn_scale_var, d_bn_bias_var});

return bn_grad;
Expand Down Expand Up @@ -1404,8 +1450,12 @@ PDNode *patterns::BatchNormAddAct::operator()(
pattern->NewNode(act_out_repr())->assert_is_ops_output(act_types, "Out");

bn->LinksFrom({bn_x_var, bn_scale_var, bn_bias_var})
.LinksTo({bn_mean_out_var, bn_variance_out_var, bn_saved_variance_var,
bn_saved_mean_var, bn_reserve_space, bn_out_var});
.LinksTo({bn_mean_out_var,
bn_variance_out_var,
bn_saved_variance_var,
bn_saved_mean_var,
bn_reserve_space,
bn_out_var});
elewise_add->LinksFrom({elewise_add_in_var, bn_out_var})
.LinksTo({elewise_add_out_var});
act->LinksFrom({elewise_add_out_var}).LinksTo({act_out_var});
Expand Down Expand Up @@ -1484,8 +1534,13 @@ PDNode *patterns::BatchNormAddActGrad::operator()(
.LinksTo({d_elewise_add_in_var, d_bn_out_var});

bn_grad
->LinksFrom({bn_x_var, d_bn_out_var, bn_scale_var, bn_bias_var,
bn_saved_mean_var, bn_saved_variance_var, bn_reserve_space})
->LinksFrom({bn_x_var,
d_bn_out_var,
bn_scale_var,
bn_bias_var,
bn_saved_mean_var,
bn_saved_variance_var,
bn_reserve_space})
.LinksTo({d_bn_x_var, d_bn_scale_var, d_bn_bias_var});

return bn_grad;
Expand Down Expand Up @@ -1558,7 +1613,8 @@ PDNode *patterns::ElewiseAddAct::operator()(

PDNode *patterns::LinearAct::operator()(
paddle::framework::ir::PDNode *linear_x_var,
const std::unordered_set<std::string> &act_types, bool with_grad_link,
const std::unordered_set<std::string> &act_types,
bool with_grad_link,
bool is_act_grad_x_from_act) {
auto *matmul_w_var =
pattern->NewNode(matmul_w_repr())->assert_is_op_input("matmul_v2", "Y");
Expand Down Expand Up @@ -1621,7 +1677,8 @@ PDNode *patterns::LinearAct::operator()(
PDNode *patterns::ElewiseAddMatmulAct::operator()(
paddle::framework::ir::PDNode *dout_var,
const std::unordered_set<std::string> &act_grad_types,
bool without_x_gradient, bool is_act_grad_x_from_act) {
bool without_x_gradient,
bool is_act_grad_x_from_act) {
auto *ele_grad_bias_var =
pattern->NewNode(ele_grad_bias_repr())
->assert_is_op_input("elementwise_add_grad", "Y");
Expand Down Expand Up @@ -2052,7 +2109,8 @@ PDNode *patterns::Pool::operator()() {
return output_var;
}

PDNode *patterns::Elementwise::operator()(PDNode *x_var, PDNode *y_var,
PDNode *patterns::Elementwise::operator()(PDNode *x_var,
PDNode *y_var,
const std::string elementwise_type) {
auto elementwise_op =
pattern->NewNode(elementwise_op_repr())->assert_is_op(elementwise_type);
Expand Down Expand Up @@ -2084,7 +2142,9 @@ PDNode *patterns::ElementwiseOp::operator()(
}

PDNode *patterns::ResidualElementwise::operator()(
PDNode *op_var, PDNode *residual_var, const std::string elementwise_type,
PDNode *op_var,
PDNode *residual_var,
const std::string elementwise_type,
bool as_x) {
auto elementwise_op =
pattern->NewNode(elementwise_op_repr())->assert_is_op(elementwise_type);
Expand Down Expand Up @@ -3065,7 +3125,8 @@ void patterns::DeleteQuantDequantLinearOpPattern::operator()() {
}

PDNode *patterns::ReshapeTransposeMatmulPattern::operator()(
const std::string &op_name, bool with_reshape_xshape,
const std::string &op_name,
bool with_reshape_xshape,
bool with_transpose_xshape) {
auto reshape_op =
pattern->NewNode(reshape_op_repr())->assert_is_op("reshape2");
Expand Down Expand Up @@ -3098,11 +3159,10 @@ PDNode *patterns::ReshapeTransposeMatmulPattern::operator()(
transpose_out->assert_is_only_output_of_op("transpose2");

auto transpose_xshape =
with_transpose_xshape
? pattern->NewNode(transpose_xshape_repr())
->AsIntermediate()
->assert_is_op_output("transpose2", "XShape")
: nullptr;
with_transpose_xshape ? pattern->NewNode(transpose_xshape_repr())
->AsIntermediate()
->assert_is_op_output("transpose2", "XShape")
: nullptr;

auto matmul_out = pattern->NewNode(matmul_out_repr())
->AsOutput()
Expand Down
Loading