forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netdef_converter.cpp
244 lines (227 loc) · 6.69 KB
/
netdef_converter.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <torch/csrc/jit/netdef_converter.h>
namespace torch {
namespace jit {
static AttributeKind getArgKind(const caffe2::Argument& arg) {
if (arg.has_i()) {
return AttributeKind::i;
} else if (arg.has_f()) {
return AttributeKind::f;
} else if (arg.has_s()) {
return AttributeKind::s;
} else if (arg.has_t()) {
return AttributeKind::t;
} else if (arg.has_n()) {
return AttributeKind::g;
} else if (arg.ints().size()) {
return AttributeKind::is;
} else if (arg.floats().size()) {
return AttributeKind::fs;
} else if (arg.strings().size()) {
return AttributeKind::ss;
} else if (arg.tensors().size()) {
return AttributeKind::ts;
} else if (arg.nets().size()) {
return AttributeKind::gs;
}
// Unknown type.
abort();
}
static void convertArg(const caffe2::Argument& arg, Node* node) {
std::string attrName = "attr::" + arg.name();
auto attrSymbol = Symbol::fromQualString(attrName);
AttributeKind kind = getArgKind(arg);
switch (kind) {
case AttributeKind::i: {
node->i_(attrSymbol, (int64_t)arg.i());
break;
}
case AttributeKind::f: {
node->f_(attrSymbol, arg.f());
break;
}
case AttributeKind::s: {
node->s_(attrSymbol, arg.s());
break;
}
case AttributeKind::is: {
std::vector<int64_t> is(arg.ints().begin(), arg.ints().end());
node->is_(attrSymbol, is);
break;
}
case AttributeKind::fs: {
std::vector<double> fs(arg.floats().begin(), arg.floats().end());
node->fs_(attrSymbol, fs);
break;
}
case AttributeKind::ss: {
std::vector<std::string> ss(arg.strings().begin(), arg.strings().end());
node->ss_(attrSymbol, ss);
break;
}
default: {
std::cout << "Unsupported type '" << toString(kind) << "' of attribute '"
<< attrName << "'"
<< " in node:" << std::endl;
node->dump();
abort();
}
}
}
void convertNetDefToIR(
const caffe2::NetDef& net,
Graph* g,
std::unordered_map<std::string, Value*>* valueMapPtr,
const std::string& prefix) {
if (!valueMapPtr) {
std::unordered_map<std::string, Value*> localValueMap;
// If valueMapPtr is null, we just use a local map since we don't need
// to return the valueMap to the caller.
return convertNetDefToIR(net, g, &localValueMap, prefix);
}
std::unordered_map<std::string, Value*>& valueMap = *valueMapPtr;
std::unordered_map<Value*, std::string> namesMap;
valueMap.clear();
for (const auto& inputName : net.external_input()) {
AT_ASSERT(!valueMap.count(inputName));
valueMap[inputName] = g->addInput();
namesMap[valueMap.at(inputName)] = inputName;
}
for (const auto& op : net.op()) {
std::string name = prefix + op.type();
Node* node =
g->create(Symbol::fromQualString(name), {}, op.output().size());
g->insertNode(node);
for (const auto& input : op.input()) {
AT_ASSERT(valueMap.count(input));
node->addInput(valueMap[input]);
}
int idx = 0;
for (const auto& output : op.output()) {
// If output already exists in valueMap, overwrite it. This way we will
// have the last definition of a value named 'output' in valueMap.
Value* v = node->outputs()[idx++];
valueMap[output] = v;
namesMap[v] = output;
}
for (const auto& arg : op.arg()) {
convertArg(arg, node);
}
}
for (const auto& outputName : net.external_output()) {
AT_ASSERT(valueMap.count(outputName));
g->registerOutput(valueMap.at(outputName));
namesMap[valueMap.at(outputName)] = outputName;
}
// Set proper unique names for all values.
// We will set the names for external inputs and outputs last, so that if the
// names are reused, then intermediate values will be renamed and the external
// values will keep the original names.
for (Node* n : g->nodes()) {
for (Value* v : n->outputs()) {
AT_ASSERT(namesMap.count(v));
const std::string& name = namesMap.at(v);
if (Value::isValidName(name)) {
v->setDebugName(name);
}
}
}
for (Value* v : g->inputs()) {
AT_ASSERT(namesMap.count(v));
const std::string& name = namesMap.at(v);
if (Value::isValidName(name)) {
v->setDebugName(name);
}
}
for (Value* v : g->outputs()) {
AT_ASSERT(namesMap.count(v));
const std::string& name = namesMap.at(v);
if (Value::isValidName(name)) {
v->setDebugName(name);
}
}
}
static void convertAttrToCaffe2Arg(
const Node* node,
const Symbol& name,
caffe2::Argument* arg) {
arg->set_name(name.toUnqualString());
switch (node->kindOf(name)) {
case AttributeKind::i: {
arg->set_i(node->i(name));
break;
}
case AttributeKind::f: {
arg->set_f(node->f(name));
break;
}
case AttributeKind::s: {
arg->set_s(node->s(name));
break;
}
case AttributeKind::is: {
for (int64_t i : node->is(name)) {
arg->add_ints(i);
}
break;
}
case AttributeKind::fs: {
for (double f : node->fs(name)) {
arg->add_floats(f);
}
break;
}
case AttributeKind::ss: {
for (const std::string& s : node->ss(name)) {
arg->add_strings(s);
}
break;
}
default: {
std::cout << "Unsupported type '" << toString(node->kindOf(name))
<< "' of attribute '" << name.toUnqualString() << "'"
<< " in node:" << std::endl;
node->dump();
abort();
}
}
}
static std::string removePrefixIfNeeded(const std::string& name,
const std::string& prefix) {
if (!name.compare(0, prefix.size(), prefix)) {
return name.substr(prefix.size());
} else {
return name;
}
}
static void convertNodeToCaffe2Op(const Node* node, caffe2::NetDef* net,
const std::string& prefix = "") {
caffe2::OperatorDef op;
op.set_type(removePrefixIfNeeded(node->kind().toQualString(), prefix));
for (const Value* input : node->inputs()) {
op.add_input(input->debugName());
}
for (const Value* output : node->outputs()) {
op.add_output(output->debugName());
}
std::vector<Symbol> names = node->attributeNames();
for (const Symbol& name : names) {
caffe2::Argument* arg = op.add_arg();
convertAttrToCaffe2Arg(node, name, arg);
}
*net->add_op() = op;
}
void convertIRToNetDef(caffe2::NetDef* net, const Graph& g,
const std::string& prefix) {
net->mutable_op()->Clear();
for (const Value* value : g.inputs()) {
net->add_external_input(value->debugName());
}
for (const Node* node : g.nodes()) {
convertNodeToCaffe2Op(node, net, prefix);
}
for (const Value* value : g.outputs()) {
net->add_external_output(value->debugName());
}
}
} // namespace jit
} // namespace torch