This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathexec_utils.cc
More file actions
99 lines (92 loc) · 3.27 KB
/
exec_utils.cc
File metadata and controls
99 lines (92 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file exec_utils.cc
* \brief Implementation of executor util functions.
*/
#include "exec_utils.h"
#include <unordered_set>
#include <unordered_map>
#include <string>
namespace mxnet {
namespace common {
void CopyGraph(nnvm::Graph* dst, const nnvm::Graph& src, bool copy_variables) {
using nnvm::Node;
using nnvm::NodeEntry;
using nnvm::ObjectPtr;
std::unordered_map<Node*, ObjectPtr> old_new;
// use DFSVisit to copy all the nodes
DFSVisit(src.outputs, [&old_new, copy_variables](const ObjectPtr& node) {
ObjectPtr np;
if (copy_variables || !node->is_variable()) {
np = Node::Create();
np->attrs = node->attrs;
} else {
np = node;
}
old_new[node.get()] = std::move(np);
});
// connect nodes of new graph
for (const auto& kv : old_new) {
for (const NodeEntry& e : kv.first->inputs) {
Node* ptr = e.node.get();
kv.second->inputs.emplace_back(NodeEntry{old_new[ptr], e.index, e.version});
}
for (const ObjectPtr& p : kv.first->control_deps) {
kv.second->control_deps.emplace_back(old_new[p.get()]);
}
}
// set the head
for (const NodeEntry& e : src.outputs) {
(*dst).outputs.emplace_back(NodeEntry{old_new[e.node.get()], e.index, e.version});
}
}
bool CheckForInputNameDuplicates(const nnvm::IndexedGraph& idx) {
std::unordered_set<std::string> names;
for (const auto& nid : idx.input_nodes()) {
const std::string& name = idx[nid].source->attrs.name;
if (names.count(name)) {
LOG(WARNING) << "Variable name " << name << " is used more than once!";
return false;
}
names.insert(name);
}
return true;
}
void PrintGraph(const nnvm::IndexedGraph& idx, std::ostream& os) {
auto node_str = [&idx](uint32_t nid) {
return std::to_string(nid) + " " + idx[nid].source->attrs.name;
};
for (size_t i = 0; i < idx.num_nodes(); ++i) {
const auto& attrs = idx[i].source->attrs;
os << "node " << node_str(i) << " " << (attrs.op ? attrs.op->name : "(var)") << "\n";
for (auto [k, v] : attrs.dict)
os << "attr " << k << " " << v << "\n";
for (const auto& inp : idx[i].inputs)
os << "inp " << node_str(inp.node_id) << " " << inp.index << " " << inp.version << "\n";
for (auto dep : idx[i].control_deps)
os << "dep " << node_str(dep) << "\n";
for (const auto& sub : attrs.subgraphs) {
std::string name;
os << "sub " << (sub->GetAttr("name", &name) ? name : "(noname)") << "\n";
}
}
}
} // namespace common
} // namespace mxnet