forked from onnx/onnx-tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OnnxAttrs.cpp
236 lines (214 loc) · 7.5 KB
/
OnnxAttrs.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
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "OnnxAttrs.hpp"
#include "ShapedWeights.hpp"
#include "onnx2trt_utils.hpp"
template<> float OnnxAttrs::get<float>(const std::string& key) const {
return this->at(key)->f();
}
template<> int OnnxAttrs::get<int>(const std::string& key) const {
return this->at(key)->i();
}
template<> bool OnnxAttrs::get<bool>(const std::string& key) const {
int value = this->at(key)->i();
assert(value == bool(value));
return bool(value);
}
template<> std::string OnnxAttrs::get<std::string>(const std::string& key) const {
return this->at(key)->s();
}
template<> std::vector<int> OnnxAttrs::get<std::vector<int>>(const std::string& key) const {
auto attr = this->at(key)->ints();
return std::vector<int>(attr.begin(), attr.end());
}
template<> std::vector<int64_t> OnnxAttrs::get<std::vector<int64_t>>(const std::string& key) const {
auto attr = this->at(key)->ints();
return std::vector<int64_t>(attr.begin(), attr.end());
}
template<> std::vector<float> OnnxAttrs::get<std::vector<float>>(const std::string& key) const {
auto attr = this->at(key)->floats();
return std::vector<float>(attr.begin(), attr.end());
}
template<> nvinfer1::Dims OnnxAttrs::get<nvinfer1::Dims>(const std::string& key) const {
auto values = this->get<std::vector<int>>(key);
nvinfer1::Dims dims;
dims.nbDims = values.size();
std::copy(values.begin(), values.end(), dims.d);
// Note: No dimension type information is included
return dims;
}
template<> nvinfer1::DimsHW OnnxAttrs::get<nvinfer1::DimsHW>(const std::string& key) const {
nvinfer1::Dims dims = this->get<nvinfer1::Dims>(key);
assert(dims.nbDims == 2);
return nvinfer1::DimsHW(dims.d[0], dims.d[1]);
}
template<> nvinfer1::Permutation OnnxAttrs::get<nvinfer1::Permutation>(const std::string& key) const {
auto values = this->get<std::vector<int>>(key);
nvinfer1::Permutation perm;
std::copy(values.begin(), values.end(), perm.order);
// Fill unused values with identity permutation
for( int i=values.size(); i<nvinfer1::Dims::MAX_DIMS; ++i ) {
perm.order[i] = i;
}
return perm;
}
template<> onnx2trt::ShapedWeights OnnxAttrs::get<onnx2trt::ShapedWeights>(const std::string& key) const {
::ONNX_NAMESPACE::TensorProto const& onnx_weights_tensor = this->at(key)->t();
onnx2trt::ShapedWeights weights;
convert_onnx_weights(onnx_weights_tensor, &weights);
return weights;
}
template<> nvinfer1::DataType OnnxAttrs::get<nvinfer1::DataType>(const std::string& key) const {
::ONNX_NAMESPACE::TensorProto::DataType onnx_dtype = static_cast<::ONNX_NAMESPACE::TensorProto::DataType>(this->at(key)->i());
nvinfer1::DataType dtype{};
if (!onnx2trt::convert_dtype(onnx_dtype, &dtype)) {
dtype = static_cast<nvinfer1::DataType>(-1);
}
return dtype;
}
template <>
std::vector<nvinfer1::DataType> OnnxAttrs::get<std::vector<nvinfer1::DataType>>(const std::string& key) const {
auto attr = this->at(key)->ints();
auto onnx_dtypes = std::vector<int64_t>(attr.begin(), attr.end());
std::vector<nvinfer1::DataType> dtypes{};
for (auto onnx_dtype : onnx_dtypes)
{
nvinfer1::DataType dtype{};
if (!onnx2trt::convert_dtype(static_cast<int32_t>(onnx_dtype), &dtype))
{
dtype = static_cast<nvinfer1::DataType>(-1);
}
dtypes.push_back(dtype);
}
return dtypes;
}
template <>
::ONNX_NAMESPACE::GraphProto OnnxAttrs::get<::ONNX_NAMESPACE::GraphProto>(const std::string& key) const
{
return this->at(key)->g();
}
template <>
nvinfer1::RNNOperation OnnxAttrs::get<nvinfer1::RNNOperation>(const std::string& key) const
{
std::string op = this->get<std::string>(key);
if (op == std::string("relu"))
{
return nvinfer1::RNNOperation::kRELU;
}
if (op == std::string("tanh"))
{
return nvinfer1::RNNOperation::kTANH;
}
if (op == std::string("lstm"))
{
return nvinfer1::RNNOperation::kLSTM;
}
if (op == std::string("gru"))
{
return nvinfer1::RNNOperation::kGRU;
}
throw std::runtime_error("Unknown RNNOperation: " + op);
}
template <>
nvinfer1::RNNInputMode OnnxAttrs::get<nvinfer1::RNNInputMode>(const std::string& key) const
{
std::string mode = this->get<std::string>(key);
if (mode == std::string("skip"))
{
return nvinfer1::RNNInputMode::kSKIP;
}
if (mode == std::string("linear"))
{
return nvinfer1::RNNInputMode::kLINEAR;
}
throw std::runtime_error("Unknown RNNInputMode: " + mode);
}
template <>
nvinfer1::RNNDirection OnnxAttrs::get<nvinfer1::RNNDirection>(const std::string& key) const
{
std::string direction = this->get<std::string>(key);
if (direction == std::string("unidirection"))
{
return nvinfer1::RNNDirection::kUNIDIRECTION;
}
if (direction == std::string("bidirection"))
{
return nvinfer1::RNNDirection::kBIDIRECTION;
}
throw std::runtime_error("Unknown RNNDirection: " + direction);
}
template <>
std::vector<std::string> OnnxAttrs::get<std::vector<std::string>>(const std::string& key) const
{
auto attr = this->at(key)->strings();
return std::vector<std::string>(attr.begin(), attr.end());
}
template <>
nvinfer1::ScaleMode OnnxAttrs::get<nvinfer1::ScaleMode>(const std::string& key) const
{
std::string s = this->get<std::string>(key);
if (s == "uniform")
{
return nvinfer1::ScaleMode::kUNIFORM;
}
if (s == "channel")
{
return nvinfer1::ScaleMode::kCHANNEL;
}
if (s == "elementwise")
{
return nvinfer1::ScaleMode::kELEMENTWISE;
}
throw std::runtime_error("Unknown ScaleMode: " + s);
}
template <>
nvinfer1::MatrixOperation OnnxAttrs::get<nvinfer1::MatrixOperation>(const std::string& key) const
{
std::string s = this->get<std::string>(key);
if (s == "none")
{
return nvinfer1::MatrixOperation::kNONE;
}
if (s == "transpose")
{
return nvinfer1::MatrixOperation::kTRANSPOSE;
}
if (s == "vector")
{
return nvinfer1::MatrixOperation::kVECTOR;
}
throw std::runtime_error("Unknown MatrixOperation: " + s);
}
template <>
nvinfer1::ResizeMode OnnxAttrs::get<nvinfer1::ResizeMode>(const std::string& key) const
{
std::string mode = this->get<std::string>(key);
if (mode == std::string("nearest"))
{
return nvinfer1::ResizeMode::kNEAREST;
}
if (mode == std::string("linear"))
{
return nvinfer1::ResizeMode::kLINEAR;
}
throw std::runtime_error("Unknown ResizeMode: " + mode);
}