Skip to content

Commit cfa6bbb

Browse files
authored
move nodeid from graph to node (#13065)
1 parent f88a8ba commit cfa6bbb

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

paddle/fluid/framework/ir/graph.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ bool IsDistTrainOp(ir::Node *node, const std::vector<std::string> &send_vars,
8787
}
8888

8989
Graph::Graph(const ProgramDesc &program) : program_(program) {
90+
// Make the nodes id start from 0.
91+
Node::ResetId();
92+
9093
VLOG(3) << "block in program:" << program_.Size();
9194
std::unordered_map<std::string, VarDesc *> all_vars;
9295
for (auto *var : program.Block(0).AllVars()) {

paddle/fluid/framework/ir/graph.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ class Graph {
9999
// Create a normal variable with non-null VarDesc.
100100
ir::Node *CreateVarNode(VarDesc *var_desc) {
101101
PADDLE_ENFORCE(var_desc);
102-
return AddNode(new ir::Node(var_desc, node_count_++));
102+
return AddNode(new ir::Node(var_desc));
103103
}
104104

105105
// Create a normal runnable operator with OpDesc.
106106
ir::Node *CreateOpNode(OpDesc *op_desc) {
107107
PADDLE_ENFORCE(op_desc);
108-
return AddNode(new ir::Node(op_desc, node_count_++));
108+
return AddNode(new ir::Node(op_desc));
109109
}
110110

111111
// Create a control dependency var that connects 2 operations. The
@@ -115,14 +115,13 @@ class Graph {
115115
// TODO(panyx0718): control var name should be really unique.
116116
const std::string name = string::Sprintf(
117117
"%s@%llu", ir::Node::kControlDepVarName, node_set_.size());
118-
return AddNode(
119-
new ir::Node(name, ir::Node::Type::kVariable, node_count_++));
118+
return AddNode(new ir::Node(name, ir::Node::Type::kVariable));
120119
}
121120

122121
// A more free style way of creating a graph node. Mostly use for test
123122
// or "copy" from another node. Avoid using it if possible.
124123
ir::Node *CreateEmptyNode(const std::string &name, ir::Node::Type type) {
125-
return AddNode(new ir::Node(name, type, node_count_++));
124+
return AddNode(new ir::Node(name, type));
126125
}
127126

128127
// Clear all node information of the graph and return the ownership of the
@@ -143,9 +142,13 @@ class Graph {
143142
nodes_.erase(node);
144143
}
145144

145+
// NOTE low performance, but simple and secure.
146146
Node *RetriveNode(int id) {
147-
auto it = id2node_.find(id);
148-
if (it != id2node_.end()) return it->second;
147+
for (auto &node : nodes_) {
148+
if (node.second->id() == id) {
149+
return node.second.get();
150+
}
151+
}
149152
return nullptr;
150153
}
151154

@@ -155,8 +158,6 @@ class Graph {
155158
PADDLE_ENFORCE(node_set_.find(node) == node_set_.end());
156159
nodes_[node].reset(node);
157160
node_set_.insert(node);
158-
PADDLE_ENFORCE(!id2node_.count(node->id()), "duplicate id %d", node->id());
159-
id2node_[node->id()] = node;
160161
return node;
161162
}
162163

@@ -166,7 +167,6 @@ class Graph {
166167
std::map<std::string, std::function<void(void)>> attr_dels_;
167168
std::map<ir::Node *, std::unique_ptr<ir::Node>> nodes_;
168169
std::unordered_set<ir::Node *> node_set_;
169-
std::map<int, Node *> id2node_;
170170
int node_count_{0};
171171
};
172172

paddle/fluid/framework/ir/node.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace paddle {
1818
namespace framework {
1919
namespace ir {
2020
constexpr char Node::kControlDepVarName[];
21+
int Node::count_ = 0;
2122
} // namespace ir
2223
} // namespace framework
2324
} // namespace paddle

paddle/fluid/framework/ir/node.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ class Node {
2929
enum class Type { kOperation, kVariable };
3030
static constexpr char kControlDepVarName[] = "__control_var";
3131

32-
explicit Node(const std::string& name, Type type, int id = -1)
32+
explicit Node(const std::string& name, Type type)
3333
: name_(name),
3434
var_desc_(nullptr),
3535
op_desc_(nullptr),
3636
type_(type),
37-
id_(id) {}
37+
id_(count_++) {}
3838

39-
explicit Node(VarDesc* var_desc, int id = -1)
39+
explicit Node(VarDesc* var_desc)
4040
: name_(var_desc->Name()),
4141
var_desc_(new VarDesc(*var_desc)),
4242
op_desc_(nullptr),
4343
type_(Type::kVariable),
44-
id_(id) {}
44+
id_(count_++) {}
4545

46-
explicit Node(OpDesc* op_desc, int id = -1)
46+
explicit Node(OpDesc* op_desc)
4747
: name_(op_desc->Type()),
4848
var_desc_(nullptr),
4949
op_desc_(new OpDesc(*op_desc, op_desc->Block())),
5050
type_(Type::kOperation),
51-
id_(id) {}
51+
id_(count_++) {}
5252

5353
Type NodeType() const { return type_; }
5454

@@ -80,6 +80,9 @@ class Node {
8080
int id_;
8181

8282
private:
83+
friend class Graph;
84+
static int count_;
85+
static void ResetId() { count_ = 0; }
8386
DISABLE_COPY_AND_ASSIGN(Node);
8487
};
8588

paddle/fluid/inference/analysis/analyzer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class DfgPassManagerImpl final : public DfgPassManager {
102102
Analyzer::Analyzer() { Register("manager1", new DfgPassManagerImpl); }
103103

104104
void Analyzer::Run(Argument* argument) {
105-
// Ungly support fluid-to-ir-pass
105+
// Ugly support fluid-to-ir-pass
106106
argument->Set(kFluidToIrPassesAttr,
107107
new std::vector<std::string>({
108108
// Manual update the passes here.

0 commit comments

Comments
 (0)