-
Notifications
You must be signed in to change notification settings - Fork 546
/
TensorOrWeights.hpp
171 lines (161 loc) · 4.5 KB
/
TensorOrWeights.hpp
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
/*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "ShapedWeights.hpp"
#include <NvInfer.h>
#include <cassert>
#include <stdexcept>
namespace onnx2trt
{
//! Abstract representation of a tensor, which might be a nvinfer1::ITensor or ShapedWeights.
class TensorOrWeights
{
union
{
nvinfer1::ITensor* _tensor;
ShapedWeights _weights;
};
enum
{
NODE_TENSOR,
NODE_WEIGHTS
} _variant;
public:
//! Represents "null tensor", which is used to denote "missing tensor".
TensorOrWeights()
: _tensor(nullptr)
, _variant(NODE_TENSOR)
{
}
TensorOrWeights(nvinfer1::ITensor* tensor)
: _tensor(tensor)
, _variant(NODE_TENSOR)
{
}
TensorOrWeights(ShapedWeights const& weights)
: _weights(weights)
, _variant(NODE_WEIGHTS)
{
}
bool is_tensor() const
{
return _variant == NODE_TENSOR;
}
bool is_weights() const
{
return _variant == NODE_WEIGHTS;
}
bool isNullTensor() const
{
return is_tensor() && _tensor == nullptr;
}
nvinfer1::ITensor& tensor()
{
if (is_weights() || isNullTensor())
{
throw std::runtime_error("Trying to access weights or a null tensor!");
}
return *_tensor;
}
nvinfer1::ITensor const& tensor() const
{
if (is_weights() || isNullTensor())
{
throw std::runtime_error("Trying to access weights or a null tensor!");
}
return *_tensor;
}
ShapedWeights& weights()
{
if (is_tensor())
{
throw std::runtime_error("Trying to access a null weights!");
}
return _weights;
}
ShapedWeights const& weights() const
{
if (is_tensor())
{
throw std::runtime_error("Trying to access a null weights!");
}
return _weights;
}
nvinfer1::Dims shape() const
{
return is_tensor() ? tensor().getDimensions() : weights().shape;
}
explicit operator bool() const
{
return is_tensor() ? _tensor != nullptr : static_cast<bool>(_weights);
}
bool isFp32() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kFLOAT
: weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
}
bool isFp16() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kHALF
: weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
}
bool isBFp16() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kBF16
: weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16;
}
bool isInt32() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kINT32
: weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_INT32;
}
bool isInt64() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kINT64
: weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_INT64;
}
bool isInt8() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kINT8
: weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_INT8;
}
bool isBool() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kBOOL : weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_BOOL;
}
bool isFp8() const
{
return is_tensor() ? tensor().getType() == nvinfer1::DataType::kFP8 : weights().type == ::ONNX_NAMESPACE::TensorProto_DataType_FLOAT8E4M3FN;
}
std::string getName() const
{
return is_tensor() ? tensor().getName() : weights().getName();
}
std::string getType() const;
nvinfer1::DataType convertONNXDataType(ShapedWeights::DataType datatype) const;
ShapedWeights::DataType convertTRTDataType(nvinfer1::DataType datatype) const;
nvinfer1::DataType getDataType() const
{
if (is_tensor())
{
return tensor().getType();
}
else
{
return convertONNXDataType(weights().type);
}
}
ShapedWeights::DataType getONNXDataType() const
{
if (is_tensor())
{
return convertTRTDataType(tensor().getType());
}
else
{
return weights().type;
}
}
};
} // namespace onnx2trt