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

pdnode_compare (#42597) #42633

Merged
merged 1 commit into from
May 10, 2022
Merged
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
43 changes: 41 additions & 2 deletions paddle/fluid/framework/ir/graph_pattern_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct PDNode {
bool IsVar() const { return type_ == Type::kVar; }

const std::string& name() const { return name_; }
const PDPattern* pdpattern() const { return pattern_; }

PDNode& operator=(const PDNode&) = delete;
PDNode(const PDNode&) = delete;
Expand Down Expand Up @@ -277,7 +278,44 @@ class PDPattern {
*/
class GraphPatternDetector {
public:
using subgraph_t = std::map<PDNode*, Node*>;
struct NodeIdCompare {
bool operator()(Node* node1, Node* node2) const {
return node1->id() < node2->id();
}
};

struct PDNodeCompare {
bool operator()(const PDNode* node1, const PDNode* node2) const {
auto& nodes1 = node1->pdpattern()->nodes();
auto& nodes2 = node2->pdpattern()->nodes();
if (nodes1.size() != nodes2.size()) {
return nodes1.size() < nodes2.size();
} else {
std::string pdnode_hash_key1 = "";
std::string pdnode_hash_key2 = "";
for (auto& node : nodes1) {
pdnode_hash_key1 += node.get()->name();
pdnode_hash_key1 += "#";
}
pdnode_hash_key1 += node1->name();
for (auto& node : nodes2) {
pdnode_hash_key2 += node.get()->name();
pdnode_hash_key2 += "#";
}
pdnode_hash_key2 += node2->name();

auto pdnode_key1 =
std::to_string(std::hash<std::string>()(pdnode_hash_key1));
auto pdnode_key2 =
std::to_string(std::hash<std::string>()(pdnode_hash_key2));

return pdnode_key1 < pdnode_key2;
}
return false;
}
};

using subgraph_t = std::map<PDNode*, Node*, PDNodeCompare>;

// Operate on the detected pattern.
using handle_t =
Expand Down Expand Up @@ -321,7 +359,8 @@ class GraphPatternDetector {
using hit_rcd_t =
std::pair<Node* /*node in graph*/, PDNode* /*node in pattern*/>;
PDPattern pattern_;
std::map<const PDNode*, std::set<Node*>> pdnodes2nodes_;
std::map<const PDNode*, std::set<Node*, NodeIdCompare>, PDNodeCompare>
pdnodes2nodes_;
};

// some helper methods.
Expand Down