forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_transformer_base.cc
179 lines (165 loc) · 5.27 KB
/
backend_transformer_base.cc
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
#include "caffe2/opt/backend_transformer_base.h"
#include "caffe2/onnx/onnx_exporter.h"
#include "caffe2/utils/proto_utils.h"
namespace caffe2 {
// Populate 'net_pos' argument for any ops that don't already have it. 'net_pos'
// we populate here starts after the max 'net_pos' value we encountered.
void BackendTransformerBase::annotateOpIndex(NetDef* net) {
// find the max net_pos that we have so far.
int i = -1;
for (const auto& op : net->op()) {
ArgumentHelper helper(op);
int old_index = helper.GetSingleArgument(op, kNetPos, -1);
i = std::max(i, old_index);
}
// populate net_pos for any op that doesn't already have it.
for (auto& op : *(net->mutable_op())) {
if (!ArgumentHelper::HasArgument(op, kNetPos)) {
AddArgument(kNetPos, ++i, &op);
}
}
}
std::string BackendTransformerBase::getModelId(const NetDef& net) {
static std::atomic<size_t> seq_id{0};
std::string model_id;
for (const auto& arg : net.arg()) {
if (arg.name() == kModelId) {
if (arg.has_s()) {
model_id = arg.s();
} else if (arg.has_i()) {
model_id = c10::to_string(arg.i());
}
break;
}
}
if (model_id.empty()) {
model_id = "unnamed_" + c10::to_string(seq_id++);
}
return model_id;
}
TensorProto wrapShapeInfoIntoTensorProto(
const std::string& name,
const ShapeInfo& shape_info) {
TensorProto t;
t.set_name(name);
t.set_data_type(shape_info.shape.data_type());
for (const auto i : shape_info.shape.dims()) {
t.add_dims(i);
}
for (const auto& dimType : shape_info.getDimType()) {
t.add_int32_data(static_cast<int32_t>(dimType));
}
return t;
}
QTensorProto wrapShapeInfoIntoQTensorProto(
const std::string& name,
const ShapeInfo& shape_info) {
QTensorProto t;
CAFFE_ENFORCE(
shape_info.is_quantized == true,
"Only quantized shapeinfo can be extracted into QTensor!");
t.set_name(name);
t.set_data_type(shape_info.shape.data_type());
t.set_axis(shape_info.q_info.axis);
t.set_is_multiparam(true);
for (const auto i : shape_info.q_info.scale) {
t.add_scales(i);
}
t.set_scale(1.0);
for (const auto i : shape_info.q_info.offset) {
t.add_biases(i);
}
t.set_bias(0.0);
// precision and is_signed is not used in onnxifi workflow, but it is required
// field
t.set_precision(0);
t.set_is_signed(0);
for (const auto i : shape_info.shape.dims()) {
t.add_dims(i);
}
for (const auto& dimType : shape_info.getDimType()) {
t.add_data(static_cast<int32_t>(dimType));
}
return t;
}
ShapeInfoMap BackendTransformerBase::ssaRewriteAndMapNames(
Workspace* ws,
NetDef* pred_net,
const ShapeInfoMap& input_shape_hints) {
input_mapping_ = onnx::SsaRewrite(nullptr, pred_net);
// Annote the ops with net position
annotateOpIndex(pred_net);
// Since we are going to create a mapped workspace, we need to make sure that
// the parent workspace has the mapped blob names. If the blobs don't exist
// (usually such blobs are input tensor names), we exclude them from mapping.
std::vector<std::string> exclude_mapping;
for (const auto kv : input_mapping_) {
if (!ws->HasBlob(kv.second)) {
exclude_mapping.emplace_back(kv.first);
}
}
for (const auto& i : exclude_mapping) {
input_mapping_.erase(i);
}
ShapeInfoMap shape_hints_mapped;
for (const auto& kv : input_shape_hints) {
shape_hints_mapped.emplace(kv.first, kv.second);
}
return shape_hints_mapped;
}
ShapeInfoMap BackendTransformerBase::inferShapes(
Workspace* ws,
NetDef* pred_net,
const ShapeInfoMap& shape_hints_mapped,
const BoundShapeSpec& spec) {
ShapeInfoMap shape_map = shape_hints_mapped;
// Populate shapes from workplace
const std::vector<std::string> ws_blobs = ws->Blobs();
for (const auto& s : ws_blobs) {
auto shape_info = getShapeInfoFromBlob(ws->GetBlob(s));
if (shape_info.dimTypeIsSet()) {
shape_map.emplace(s, shape_info);
}
}
auto eng = BoundShapeInferencerRegistry()->Create("C10", spec);
eng->InferBoundShapeAndType(*pred_net, shape_map, ws);
const auto& out_map = eng->shape_info();
shape_map.clear();
for (const auto& kv : out_map) {
shape_map.emplace(
std::piecewise_construct,
std::forward_as_tuple(kv.first),
std::forward_as_tuple(
kv.second.getDimType(),
kv.second.shape,
kv.second.is_quantized,
kv.second.q_info));
}
return shape_map;
}
void BackendTransformerBase::addShapeToNet(
NetDef& shape_net,
const ShapeInfoMap& shape_hints) const {
auto* shape_arg = shape_net.add_arg();
auto* qshape_arg = shape_net.add_arg();
shape_arg->set_name("shape_info");
qshape_arg->set_name("qshape_info");
for (const auto& kv : shape_hints) {
if (!kv.second.is_quantized) {
auto t = wrapShapeInfoIntoTensorProto(kv.first, kv.second);
shape_arg->mutable_tensors()->Add()->CopyFrom(t);
} else {
auto t = wrapShapeInfoIntoQTensorProto(kv.first, kv.second);
qshape_arg->mutable_qtensors()->Add()->CopyFrom(t);
}
}
}
void BackendTransformerBase::dumpNet(
const NetDef& pred_net,
const ShapeInfoMap& shape_hints,
const std::string& fname) const {
NetDef shape_net(pred_net);
addShapeToNet(shape_net, shape_hints);
WriteProtoToTextFile(shape_net, fname);
}
} // namespace caffe2