-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
matmul.cc
280 lines (240 loc) · 9.09 KB
/
matmul.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "core/providers/cpu/math/matmul.h"
#include "core/providers/cpu/math/gemm_matmul_common.h"
#include "core/providers/cpu/math/matmul_helper.h"
#include "core/util/math.h"
#include "core/util/math_cpuonly.h"
namespace onnxruntime {
ONNX_CPU_OPERATOR_VERSIONED_TYPED_KERNEL(
MatMul,
1, 8,
float,
KernelDefBuilder().TypeConstraint("T", DataTypeImpl::GetTensorType<float>()),
MatMul<float>);
ONNX_CPU_OPERATOR_VERSIONED_TYPED_KERNEL(
MatMul,
1, 8,
double,
KernelDefBuilder().TypeConstraint("T", DataTypeImpl::GetTensorType<double>()),
MatMul<double>);
// opset 9 supports more types
ONNX_CPU_OPERATOR_VERSIONED_TYPED_KERNEL(
MatMul,
9,
12,
float,
KernelDefBuilder().TypeConstraint("T", DataTypeImpl::GetTensorType<float>()),
MatMul<float>);
ONNX_CPU_OPERATOR_VERSIONED_TYPED_KERNEL(
MatMul,
9,
12,
double,
KernelDefBuilder().TypeConstraint("T", DataTypeImpl::GetTensorType<double>()),
MatMul<double>);
ONNX_CPU_OPERATOR_VERSIONED_TYPED_KERNEL(
MatMul,
9,
12,
int32_t,
KernelDefBuilder()
.TypeConstraint("T", BuildKernelDefConstraints<int32_t, uint32_t>()),
MatMul<int32_t>);
ONNX_CPU_OPERATOR_VERSIONED_TYPED_KERNEL(
MatMul,
9,
12,
int64_t,
KernelDefBuilder()
.TypeConstraint("T", BuildKernelDefConstraints<int64_t, uint64_t>()),
MatMul<int64_t>);
ONNX_CPU_OPERATOR_TYPED_KERNEL(
MatMul,
13,
float,
KernelDefBuilder().TypeConstraint("T", DataTypeImpl::GetTensorType<float>()),
MatMul<float>);
ONNX_CPU_OPERATOR_TYPED_KERNEL(
MatMul,
13,
double,
KernelDefBuilder().TypeConstraint("T", DataTypeImpl::GetTensorType<double>()),
MatMul<double>);
ONNX_CPU_OPERATOR_TYPED_KERNEL(
MatMul,
13,
int32_t,
KernelDefBuilder()
.TypeConstraint("T", BuildKernelDefConstraints<int32_t, uint32_t>()),
MatMul<int32_t>);
ONNX_CPU_OPERATOR_TYPED_KERNEL(
MatMul,
13,
int64_t,
KernelDefBuilder()
.TypeConstraint("T", BuildKernelDefConstraints<int64_t, uint64_t>()),
MatMul<int64_t>);
template <typename T>
Status MatMul<T>::Compute(OpKernelContext* ctx) const {
concurrency::ThreadPool* thread_pool = ctx->GetOperatorThreadPool();
const auto* a = ctx->Input<Tensor>(0);
const auto* b = ctx->Input<Tensor>(1);
MatMulComputeHelper helper;
ORT_RETURN_IF_ERROR(helper.Compute(a->Shape(), b->Shape()));
Tensor* y = ctx->Output(0, helper.OutputShape());
// Bail out early if the output is going to be empty
if (y->Shape().Size() == 0)
return Status::OK();
// Using DataRaw as int32_t/uint32_t and int64_t/uint64_t share a common
// operator body.
const auto* a_data = reinterpret_cast<const T*>(a->DataRaw());
const auto* b_data = reinterpret_cast<const T*>(b->DataRaw());
auto* y_data = reinterpret_cast<T*>(y->MutableDataRaw());
// TODO: replace it with GemmBatch for performance, it's OK for now as GemmBatch unrolls as well
size_t max_len = helper.OutputOffsets().size();
for (size_t i = 0; i < max_len; i++) {
math::MatMul<T>(
static_cast<int>(helper.M()),
static_cast<int>(helper.N()),
static_cast<int>(helper.K()),
a_data + helper.LeftOffsets()[i],
b_data + helper.RightOffsets()[i],
y_data + helper.OutputOffsets()[i],
thread_pool);
}
return Status::OK();
}
#if defined(__aarch64__) && defined(__linux__)
bool GemmPackBBfloat16(AllocatorPtr& alloc,
const Tensor& tensor_b,
bool trans_b,
IAllocatorUniquePtr<void>& packed_b,
size_t& packed_b_size,
TensorShape& b_shape) {
// Only handle the common case of a 2D weight matrix. Additional matrices
// could be handled by stacking the packed buffers.
if (tensor_b.Shape().NumDimensions() != 2) {
return false;
}
b_shape = tensor_b.Shape();
const size_t K = trans_b ? static_cast<size_t>(b_shape[1]) : static_cast<size_t>(b_shape[0]);
const size_t N = trans_b ? static_cast<size_t>(b_shape[0]) : static_cast<size_t>(b_shape[1]);
packed_b_size = MlasSBGemmPackBSize(N, K);
if (packed_b_size == 0) {
return false;
}
packed_b = IAllocator::MakeUniquePtr<void>(alloc, packed_b_size, true);
auto* packed_b_data = packed_b.get();
// Initialize memory to 0 as there could be some padding associated with pre-packed
// buffer memory and we don not want it uninitialized and generate different hashes
// if and when we try to cache this pre-packed buffer for sharing between sessions.
memset(packed_b_data, 0, packed_b_size);
MlasSBGemmConvertPackB(N,
K,
tensor_b.Data<float>(),
trans_b ? K : N,
packed_b_data);
return true;
}
#endif
Status MatMul<float>::PrePack(const Tensor& tensor, int input_idx, /*out*/ AllocatorPtr alloc,
/*out*/ bool& is_packed,
/*out*/ PrePackedWeights* prepacked_weights) {
is_packed = false;
// only pack Matrix B
if (input_idx == 1) {
size_t packed_b_size;
#if defined(__aarch64__) && defined(__linux__)
size_t dim1 = 0;
size_t dim2 = 0;
TensorShape b_shape = tensor.Shape();
if (b_shape.NumDimensions() == 2) {
dim1 = static_cast<size_t>(b_shape[0]);
dim2 = static_cast<size_t>(b_shape[1]);
}
if (use_fastmath_mode_ && (trans_b_attr_ == 0) && ((dim1 * dim2) >= kFastMathModeKernelsizeThreshold)) {
is_packed = GemmPackBBfloat16(alloc, tensor, trans_b_attr_ != 0, packed_b_, packed_b_size, b_shape_);
} else
#endif
{
is_packed = GemmPackBFp32(alloc, tensor, trans_b_attr_ != 0, packed_b_, packed_b_size, b_shape_);
}
bool share_prepacked_weights = (prepacked_weights != nullptr);
if (is_packed && share_prepacked_weights) {
prepacked_weights->buffers_.push_back(std::move(packed_b_));
prepacked_weights->buffer_sizes_.push_back(packed_b_size);
}
}
return Status::OK();
}
Status MatMul<float>::UseSharedPrePackedBuffers(std::vector<BufferUniquePtr>& prepacked_buffers,
int input_idx,
/*out*/ bool& used_shared_buffers) {
used_shared_buffers = false;
if (input_idx == 1) {
used_shared_buffers = true;
packed_b_ = std::move(prepacked_buffers[0]);
}
return Status::OK();
}
Status MatMul<float>::Compute(OpKernelContext* ctx) const {
concurrency::ThreadPool* thread_pool = ctx->GetOperatorThreadPool();
const Tensor* a = ctx->Input<Tensor>(0);
const Tensor* b = packed_b_ ? nullptr : ctx->Input<Tensor>(1);
const auto& b_shape = b ? b->Shape() : b_shape_;
// match CUDA kernel implementation, ignore transpose for vectors
const bool trans_a = trans_a_attr_ && a->Shape().NumDimensions() != 1;
const bool trans_b = trans_b_attr_ && b_shape.NumDimensions() != 1;
MatMulComputeHelper helper;
ORT_RETURN_IF_ERROR(helper.Compute(a->Shape(), b_shape, trans_a, trans_b, trans_batch_a_, trans_batch_b_));
Tensor* y = ctx->Output(0, helper.OutputShape());
// Bail out early if the output is going to be empty
if (y->Shape().Size() == 0)
return Status::OK();
const auto* a_data = a->Data<float>();
const auto* b_data = b ? b->Data<float>() : nullptr;
auto* y_data = y->MutableData<float>();
const size_t max_len = helper.OutputOffsets().size();
const size_t M = static_cast<size_t>(helper.M());
const size_t N = static_cast<size_t>(helper.N());
const size_t K = static_cast<size_t>(helper.K());
const size_t lda = helper.Lda(trans_a);
const size_t ldb = helper.Ldb(trans_b);
#if defined(__aarch64__) && defined(__linux__)
if (use_fastmath_mode_ && !trans_b && ((N * K) >= kFastMathModeKernelsizeThreshold)) {
std::vector<MLAS_SBGEMM_DATA_PARAMS> data(max_len);
for (size_t i = 0; i < max_len; i++) {
data[i].BIsfp32 = !(bool(packed_b_));
data[i].AIsfp32 = true;
data[i].A = a_data + helper.LeftOffsets()[i];
data[i].lda = lda;
data[i].B = data[i].BIsfp32 ? b_data + helper.RightOffsets()[i] : (float*)packed_b_.get();
data[i].ldb = ldb;
data[i].C = y_data + helper.OutputOffsets()[i];
data[i].ldc = N;
data[i].Bias = nullptr;
data[i].OutputProcessor = nullptr;
}
MlasSBGemmBatch(M, N, K, max_len, data.data(), thread_pool);
} else
#endif
{
std::vector<MLAS_SGEMM_DATA_PARAMS> data(max_len);
for (size_t i = 0; i < max_len; i++) {
data[i].BIsPacked = bool(packed_b_);
data[i].A = a_data + helper.LeftOffsets()[i];
data[i].lda = lda;
data[i].B = data[i].BIsPacked ? (float*)packed_b_.get() : b_data + helper.RightOffsets()[i];
data[i].ldb = ldb;
data[i].C = y_data + helper.OutputOffsets()[i];
data[i].ldc = N;
data[i].alpha = alpha_attr_;
data[i].beta = 0.0f;
}
MlasGemmBatch(trans_a ? CblasTrans : CblasNoTrans, trans_b ? CblasTrans : CblasNoTrans,
M, N, K, data.data(), max_len, thread_pool);
}
return Status::OK();
}
} // namespace onnxruntime