forked from ROCm/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_matmul_op.cc
277 lines (253 loc) · 8.54 KB
/
batch_matmul_op.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
#include "caffe2/operators/batch_matmul_op.h"
#include "caffe2/core/operator_schema.h"
#include "caffe2/core/types.h"
namespace caffe2 {
REGISTER_CPU_OPERATOR(BatchMatMul, BatchMatMulOp<CPUContext>);
vector<TensorShape> TensorInferenceForBatchMatMul(
const OperatorDef& def,
const vector<TensorShape>& in) {
ArgumentHelper helper(def);
bool broadcast = helper.GetSingleArgument<int>("broadcast", 0);
if (!broadcast) {
const auto ndim = in[0].dims_size();
CAFFE_ENFORCE_GE(ndim, 2);
CAFFE_ENFORCE_GE(in[1].dims_size(), 2);
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int a_dim0;
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int b_dim1;
if (helper.GetSingleArgument<int>("trans_a", 0)) {
a_dim0 = in[0].dims(ndim - 1);
} else {
a_dim0 = in[0].dims(ndim - 2);
}
if (helper.GetSingleArgument<int>("trans_b", 0)) {
b_dim1 = in[1].dims(ndim - 2);
} else {
b_dim1 = in[1].dims(ndim - 1);
}
auto output_dims =
vector<int64_t>{in[0].dims().begin(), in[0].dims().end()};
output_dims[ndim - 2] = a_dim0;
output_dims[ndim - 1] = b_dim1;
return vector<TensorShape>{
CreateTensorShape(vector<int64_t>{output_dims}, in[0].data_type())};
} else {
auto ndims_A = in[0].dims_size();
auto ndims_B = in[1].dims_size();
std::vector<int64_t> dims_A(ndims_A), dims_B(ndims_B);
for (int i = 0; i < ndims_A; ++i) {
dims_A[i] = in[0].dims(i);
}
for (int i = 0; i < ndims_B; ++i) {
dims_B[i] = in[1].dims(i);
}
bool A_broadcasted = false, B_broadcasted = false;
if (ndims_A == 1) {
dims_A.insert(dims_A.begin(), 1);
ndims_A = 2;
A_broadcasted = true;
}
if (ndims_B == 1) {
dims_B.push_back(1);
ndims_B = 2;
B_broadcasted = true;
}
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
size_t M, N;
if (helper.GetSingleArgument<int>("trans_a", 0)) {
M = dims_A[ndims_A - 1];
} else {
M = dims_A[ndims_A - 2];
}
if (helper.GetSingleArgument<int>("trans_b", 0)) {
N = dims_B[ndims_B - 2];
} else {
N = dims_B[ndims_B - 1];
}
const int ndims = std::max(ndims_A, ndims_B);
std::vector<int64_t> new_dims(ndims - 2);
std::vector<int64_t> dims_A_broadcast(ndims - 2, 1);
std::vector<int64_t> dims_B_broadcast(ndims - 2, 1);
std::copy_n(dims_A.begin(), ndims_A - 2, dims_A_broadcast.begin() + ndims - ndims_A);
std::copy_n(dims_B.begin(), ndims_B - 2, dims_B_broadcast.begin() + ndims - ndims_B);
for (int i = 0; i < ndims - 2; ++i) {
if (!dims_A_broadcast[i] || !dims_B_broadcast[i]) {
new_dims[i] = 0;
} else {
new_dims[i] = std::max(dims_A_broadcast[i], dims_B_broadcast[i]);
}
}
if (!A_broadcasted) {
new_dims.push_back(M);
}
if (!B_broadcasted) {
new_dims.push_back(N);
}
if (A_broadcasted && B_broadcasted) {
new_dims.push_back(1);
}
return vector<TensorShape>{
CreateTensorShape(vector<int64_t>{new_dims}, in[0].data_type())};
}
}
OpSchema::Cost CostInferenceForBatchMatMul(
const OperatorDef& def,
const vector<TensorShape>& in) {
CAFFE_ENFORCE_EQ(in.size(), 2U, "BatchMatMul requires two inputs");
ArgumentHelper helper(def);
struct OpSchema::Cost c;
const auto& A = in[0];
const auto& B = in[1];
const TensorShape Y = TensorInferenceForBatchMatMul(def, in)[0];
uint64_t nElemA = nElemFromDim(A);
uint64_t nElemB = nElemFromDim(B);
uint64_t nElemY = nElemFromDim(Y);
auto ndims_A = A.dims_size();
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
size_t K;
if (helper.GetSingleArgument<int>("trans_a", 0)) {
K = in[0].dims(ndims_A - 2);
} else {
K = in[0].dims(ndims_A - 1);
}
auto const& A_element_size_byte =
DataTypeToTypeMeta(A.data_type()).itemsize();
auto const& Y_element_size_byte =
DataTypeToTypeMeta(Y.data_type()).itemsize();
c.flops = 2 * nElemY * K;
c.bytes_read = (nElemA + nElemB) * A_element_size_byte;
c.bytes_written = nElemY * Y_element_size_byte;
c.params_bytes = 0;
return c;
}
OPERATOR_SCHEMA(BatchMatMul)
.NumInputs(2)
.NumOutputs(1)
.SetDoc(R"DOC(
Batch Matrix multiplication Yi = Ai * Bi, where A has shape (dim0, dim1, ... M, K),
B has shape (dim0, dim1, ... K, N), Y has shape (dim0, dim1, ... M, N) and i ranges
from 0 to (dim0 * dim1 ...) - 1. rank(A) == rank(B) >= 2. In case of A and B being
two dimensional, it behaves like normal matrix multiplication.
)DOC")
.Input(0, "A", "tensor of shape (dim0, dim1 ... M, K)")
.Input(1, "B", "tensor of shape (dim0, dim1 ... K, N)")
.Output(0, "Y", "tensor of shape (dim0, dim1 ... M, N)")
.Arg(
"trans_a",
"Pass 1 to transpose the last two dimensions of A before "
"doing multiplication")
.Arg(
"trans_b",
"Pass 1 to transpose the last two dimensions of B before "
"doing multiplication")
.Arg(
"broadcast",
"Pass 1 to allow broadcasting of dimensions. Behavior is the same as numpy.matmul. Gradient is currently not supported when running in broadcast mode.")
.TensorInferenceFunction(TensorInferenceForBatchMatMul)
.CostInferenceFunction(
OpSchema::CostInferenceFunctionType(CostInferenceForBatchMatMul))
.InheritOnnxSchema();
class GetBatchMatMulGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
vector<OperatorDef> GetGradientDefs() override {
CAFFE_ENFORCE_EQ(def_.input_size(), 2);
bool broadcast = false;
if (ArgumentHelper::HasArgument(Def(), "broadcast")) {
broadcast = GetArgument(Def(), "broadcast").i();
}
CAFFE_ENFORCE(
!broadcast,
"Gradient is currently not supported with "
"broadcast=1 for BatchMatMul.");
// NOLINTNEXTLINE(modernize-use-bool-literals)
bool trans_a = 0;
// NOLINTNEXTLINE(modernize-use-bool-literals)
bool trans_b = 0;
if (ArgumentHelper::HasArgument(Def(), "trans_a")) {
trans_a = GetArgument(Def(), "trans_a").i();
}
if (ArgumentHelper::HasArgument(Def(), "trans_b")) {
trans_b = GetArgument(Def(), "trans_b").i();
}
auto no_trans_arg = vector<Argument>();
auto trans_a_arg = vector<Argument>{MakeArgument<int>("trans_a", 1)};
auto trans_b_arg = vector<Argument>{MakeArgument<int>("trans_b", 1)};
auto trans_both_arg = vector<Argument>{
MakeArgument<int>("trans_a", 1), MakeArgument<int>("trans_b", 1)};
if (trans_a) {
if (trans_b) {
// A'B':
// dA = B'G', dB = G'A'
return vector<OperatorDef>{
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{I(1), GO(0)},
vector<string>{GI(0)},
trans_both_arg),
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{GO(0), I(0)},
vector<string>{GI(1)},
trans_both_arg)};
} else {
// A'B:
// dA = BG', dB = AG
return vector<OperatorDef>{
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{I(1), GO(0)},
vector<string>{GI(0)},
trans_b_arg),
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{I(0), GO(0)},
vector<string>{GI(1)},
no_trans_arg)};
}
} else {
if (trans_b) {
// AB':
// dA = GB, dB = G'A
return vector<OperatorDef>{
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{GO(0), I(1)},
vector<string>{GI(0)},
no_trans_arg),
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{GO(0), I(0)},
vector<string>{GI(1)},
trans_a_arg)};
} else {
// AB:
// dA = GB', dB = A'G
return vector<OperatorDef>{
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{GO(0), I(1)},
vector<string>{GI(0)},
trans_b_arg),
CreateOperatorDef(
"BatchMatMul",
"",
vector<string>{I(0), GO(0)},
vector<string>{GI(1)},
trans_a_arg)};
}
}
}
bool CopyArguments() const override {
return false;
}
};
REGISTER_GRADIENT(BatchMatMul, GetBatchMatMulGradient);
} // namespace caffe2