-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathFrontendDialectHelper.cpp
215 lines (190 loc) · 7.28 KB
/
FrontendDialectHelper.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
/*
* SPDX-License-Identifier: Apache-2.0
*/
//===--------------------- FrontendDialectHelper.cpp ----------------------===//
//
// Copyright 2019 The IBM Research Authors.
//
// =============================================================================
//
// Helper methods for handling input ONNX models.
//
//===----------------------------------------------------------------------===//
#include "src/Builder/FrontendDialectHelper.hpp"
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/SwapByteOrder.h"
#include "src/Dialect/ONNX/ElementsAttr/Arrays.hpp"
#include "src/Dialect/ONNX/ElementsAttr/BType.hpp"
#include "src/Dialect/ONNX/ONNXOps/OpHelper.hpp"
#include "src/Dialect/ONNX/OnnxElementsAttrBuilder.hpp"
#include "src/Support/SmallFP.hpp"
using namespace mlir;
namespace onnx_mlir {
namespace {
template <typename T>
struct TransformValueToONNXData {
static const google::protobuf::RepeatedField<int32_t> &data(
const onnx::TensorProto &tp) {
// int32_data is used for:
// int32, uint8, int8, uint16, int16, bool, float_16, bfloat_16,
// float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz
return tp.int32_data();
}
};
template <>
struct TransformValueToONNXData<double> {
static const google::protobuf::RepeatedField<double> &data(
const onnx::TensorProto &tp) {
return tp.double_data();
}
};
template <>
struct TransformValueToONNXData<float> {
static const google::protobuf::RepeatedField<float> &data(
const onnx::TensorProto &tp) {
return tp.float_data();
}
};
template <>
struct TransformValueToONNXData<int64_t> {
static const google::protobuf::RepeatedField<int64_t> &data(
const onnx::TensorProto &tp) {
return tp.int64_data();
}
};
template <>
struct TransformValueToONNXData<uint32_t> {
static const google::protobuf::RepeatedField<uint64_t> &data(
const onnx::TensorProto &tp) {
return tp.uint64_data();
}
};
template <>
struct TransformValueToONNXData<uint64_t> {
static const google::protobuf::RepeatedField<uint64_t> &data(
const onnx::TensorProto &tp) {
return tp.uint64_data();
}
};
template <typename T, typename Range, typename Transformation>
ElementsAttr createElmAttrFromArray(RankedTensorType tensorType,
const Range &array, const Transformation &transformation) {
MLIRContext *ctx = tensorType.getContext();
assert(tensorType.getElementType() == toMlirType<T>(ctx));
return OnnxElementsAttrBuilder(ctx).fromArray<T>(
tensorType, [array, &transformation](MutableArrayRef<T> copy) {
std::transform(array.begin(), array.end(), copy.data(), transformation);
});
}
// Perform byte swap if system endianness is BE.
// ONNX tensor content raw data is always in LE.
// Don't byte swap single byte types, because that's unnecessary
// and llvm::sys::getSwappedBytes(bool) also happens to be broken.
template <typename T>
constexpr bool shouldSwapLEBytes =
sizeof(T) > 1 && llvm::support::endian::system_endianness() !=
llvm::support::endianness::little;
// Extension of llvm::sys::getSwappedBytes to also handle float_16, bfloat_16.
template <typename T>
T swappedBytes(T x) {
if constexpr (isSmallFPType<T>)
return T::bitcastFromUInt(llvm::sys::getSwappedBytes(x.bitcastToUInt()));
else
return llvm::sys::getSwappedBytes(x);
}
template <typename T>
ElementsAttr createElementsAttrFromMemoryBuffer_LE(
RankedTensorType tensorType, const ExternalDataFileSlice &fileSlice) {
MLIRContext *ctx = tensorType.getContext();
assert(tensorType.getElementType() == toMlirType<T>(ctx));
if constexpr (shouldSwapLEBytes<T>) {
ArrayRef<T> array = asArrayRef<T>(fileSlice.getBufferSlice());
return createElmAttrFromArray<T>(tensorType, array, swappedBytes<T>);
} else {
return OnnxElementsAttrBuilder(ctx).fromMemoryBuffer(
tensorType, fileSlice.file, fileSlice.offset, fileSlice.length);
}
}
template <typename T>
ElementsAttr createElmAttrFromRawBytes_LE(
RankedTensorType tensorType, ArrayRef<char> bytes) {
ArrayRef<T> array = castArrayRef<T>(bytes);
return createElmAttrFromArray<T>(tensorType, array, [](T x) {
if constexpr (shouldSwapLEBytes<T>)
return swappedBytes<T>(x);
else
return x;
});
}
// Converts to the cpp type 'To' that correspond's to the tensor element type
// (bool, int8, float_16, uint32, etc) from the the proto data field type
// which may be a wider type (int32, uint64). In most cases the conversion is
// just standard C implicit conversion. The exception is float_16 and bfloat_16
// which must be bit-wise converted from uint16_t.
template <typename To, typename From>
To deserializeDatum(const From &from) {
if constexpr (isSmallFPType<To>)
return To::bitcastFromUInt(from);
else
return from;
}
template <typename T, typename U>
ElementsAttr createElmAttrFromProtoData(RankedTensorType tensorType,
const google::protobuf::RepeatedField<U> &data) {
// "Deserialize" the data to the correct bitwidth.
return createElmAttrFromArray<T>(tensorType, data, deserializeDatum<T, U>);
}
// Returns ElementsAttr with tp's data.
template <typename T>
ElementsAttr createElmAttr(RankedTensorType tensorType,
const onnx::TensorProto &tp,
const ExternalDataFileSlice *externalDataFileSlice) {
if (tp.has_data_location() &&
tp.data_location() == onnx::TensorProto::EXTERNAL) {
return createElementsAttrFromMemoryBuffer_LE<T>(
tensorType, *externalDataFileSlice);
}
if (tp.has_raw_data()) {
return createElmAttrFromRawBytes_LE<T>(
tensorType, asArrayRef(tp.raw_data()));
}
// Not raw, no need to take care of endianness.
const auto &data = TransformValueToONNXData<T>::data(tp);
return createElmAttrFromProtoData<T>(tensorType, data);
}
ElementsAttr createStringElmAttr(
RankedTensorType tensorType, const onnx::TensorProto &tp) {
// The string type is different from other data types in that it cannot be
// raw or external data, it cannot be represented as a DisposableElementsAttr,
// and it needs to be converted to StringRef (or StringAttr) to construct a
// DenseElementsAttr.
assert(!(tp.has_data_location() &&
tp.data_location() == onnx::TensorProto::EXTERNAL) &&
"string TensorProto cannot be external data");
assert(!tp.has_raw_data() && "string TensorProto cannot be raw data");
auto data = tp.string_data();
SmallVector<StringRef> copy(data.begin(), data.end());
return DenseElementsAttr::get(tensorType, ArrayRef(copy));
}
} // namespace
ElementsAttr onnxTensorProtoToElmAttr(MLIRContext *ctx,
const onnx::TensorProto &tp,
const ExternalDataFileSlice *externalDataFileSlice) {
// Tensor dimensions.
ArrayRef<int64_t> tensorDims(tp.dims().data(), tp.dims().size());
if (tp.data_type() == onnx::TensorProto::STRING) {
Type elmType = ONNXStringType::get(ctx);
auto tensorType = RankedTensorType::get(tensorDims, elmType);
return createStringElmAttr(tensorType, tp);
}
BType btype = btypeOfOnnxDataType(tp.data_type());
Type elmType = mlirTypeOfBType(btype, ctx);
auto tensorType = RankedTensorType::get(tensorDims, elmType);
return dispatchByBType(btype, [&](auto btype) {
using cpptype = CppType<btype>;
return createElmAttr<cpptype>(tensorType, tp, externalDataFileSlice);
});
}
} // namespace onnx_mlir