/
Matmul.cs
270 lines (268 loc) · 13.6 KB
/
Matmul.cs
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
using Tensornet.Common;
using Tensornet.Native;
using Tensornet.Exceptions;
namespace Tensornet{
public static class MatmulExtension{
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<T> Matmul<T>(this Tensor<T> lhs, Tensor<T> rhs) where T : struct, IEquatable<T>, IConvertible{
return MatmulInternal<T, T, T>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<double> lhs, Tensor<int> rhs){
return MatmulInternal<double, int, double>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<double> lhs, Tensor<float> rhs){
return MatmulInternal<double, float, double>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<double> lhs, Tensor<long> rhs){
return MatmulInternal<double, long, double>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<double> lhs, Tensor<bool> rhs){
return MatmulInternal<double, bool, double>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<int> lhs, Tensor<double> rhs){
return MatmulInternal<int, double, double>(lhs, rhs);
}
public static Tensor<float> Matmul(this Tensor<int> lhs, Tensor<float> rhs){
return MatmulInternal<int, float, float>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<long> Matmul(this Tensor<int> lhs, Tensor<long> rhs){
return MatmulInternal<int, long, long>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<int> Matmul(this Tensor<int> lhs, Tensor<bool> rhs){
return MatmulInternal<int, bool, int>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<long> lhs, Tensor<double> rhs){
return MatmulInternal<long, double, double>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<float> Matmul(this Tensor<long> lhs, Tensor<float> rhs){
return MatmulInternal<long, float, float>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<long> Matmul(this Tensor<long> lhs, Tensor<int> rhs){
return MatmulInternal<long, int, long>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<long> Matmul(this Tensor<long> lhs, Tensor<bool> rhs){
return MatmulInternal<long, bool, long>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<float> Matmul(this Tensor<float> lhs, Tensor<long> rhs){
return MatmulInternal<float, long, float>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<float> Matmul(this Tensor<float> lhs, Tensor<int> rhs){
return MatmulInternal<float, int, float>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<float> Matmul(this Tensor<float> lhs, Tensor<bool> rhs){
return MatmulInternal<float, bool, float>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<float> lhs, Tensor<double> rhs){
return MatmulInternal<float, double, double>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<double> Matmul(this Tensor<bool> lhs, Tensor<double> rhs){
return MatmulInternal<bool, double, double>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<float> Matmul(this Tensor<bool> lhs, Tensor<float> rhs){
return MatmulInternal<bool, float, float>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<long> Matmul(this Tensor<bool> lhs, Tensor<long> rhs){
return MatmulInternal<bool, long, long>(lhs, rhs);
}
/// <summary>
/// Get the matrix mutiply result of the two tensors. For the details, please refer to https://numpy.org/doc/stable/reference/generated/numpy.matmul.html
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <returns></returns>
public static Tensor<int> Matmul(this Tensor<bool> lhs, Tensor<int> rhs){
return MatmulInternal<bool, int, int>(lhs, rhs);
}
private static Tensor<TC> MatmulInternal<TA, TB, TC>(Tensor<TA> lhs, Tensor<TB> rhs)
where TA : struct, IEquatable<TA>, IConvertible
where TB : struct, IEquatable<TB>, IConvertible
where TC : struct, IEquatable<TC>, IConvertible
{
TensorLayout leftLayout = new TensorLayout(lhs.TLayout);
TensorLayout rightLayout = new TensorLayout(rhs.TLayout);
Tensor<TC> res = new Tensor<TC>(DeduceLayout(leftLayout, rightLayout));
res.TLayout.InitContiguousLayout();
IntPtr status = NativeExecutor.Execute(NativeApi.Matmul, lhs.TMemory, rhs.TMemory, res.TMemory, leftLayout, rightLayout, res.TLayout, IntPtr.Zero, Tensor<TC>.Provider);
NativeStatus.AssertOK(status);
return res;
}
private static TensorLayout DeduceLayout(TensorLayout lhs, TensorLayout rhs){
TensorLayout res = new TensorLayout();
res.DType = DefaultTypeDeduce.Deduce(lhs.DType, rhs.DType);
if (lhs.NDim <= 0 || rhs.NDim <= 0)
throw new MismatchedShapeException(lhs, rhs, "Matmul");
if (lhs.IsScalar() && rhs.IsScalar()) {
res.Shape[0] = 1;
res.NDim = 1;
return res;
}
if (lhs.NDim == 1 && rhs.NDim == 1) {
res.Shape[0] = lhs.Shape[0];
res.Shape[1] = rhs.Shape[0];
res.NDim = 2;
lhs.BroadcastInplace(new TensorShape(lhs.Shape[0], 1));
rhs.BroadcastInplace(new TensorShape(1, rhs.Shape[0]));
return res;
}
int dim = lhs.NDim > rhs.NDim ? lhs.NDim : rhs.NDim;
int min_dim = lhs.NDim < rhs.NDim ? lhs.NDim : rhs.NDim;
res.NDim = dim;
TensorShape aDstShape = new TensorShape(lhs);
TensorShape bDstShape = new TensorShape(rhs);
for (int i = 0; i < dim - 2; i++) {
int aIdx = lhs.NDim - i - 3;
int bIdx = rhs.NDim - i - 3;
int targetIdx = dim - i - 3;
if (aIdx >= 0 &&
(bIdx < 0 || lhs.Shape[aIdx] == 1 || rhs.Shape[bIdx] == 1 ||
lhs.Shape[aIdx] == rhs.Shape[bIdx])) {
if (bIdx < 0)
res.Shape[targetIdx] = lhs.Shape[aIdx];
else
res.Shape[targetIdx] =
lhs.Shape[aIdx] == 1 ? System.Math.Max(rhs.Shape[bIdx], lhs.Shape[aIdx]) : lhs.Shape[aIdx];
} else if (aIdx < 0) {
res.Shape[targetIdx] = rhs.Shape[bIdx];
} else {
throw new MismatchedShapeException(lhs, rhs, "Matmul");
}
aDstShape.Shape[targetIdx] = res.Shape[targetIdx];
bDstShape.Shape[targetIdx] = res.Shape[targetIdx];
}
if (lhs.NDim == 1 && rhs.Shape[rhs.NDim - 2] != lhs.Shape[0] ||
rhs.NDim == 1 && rhs.Shape[0] != lhs.Shape[lhs.NDim - 1] ||
lhs.NDim != 1 && rhs.NDim != 1 &&
lhs.Shape[lhs.NDim - 1] != rhs.Shape[rhs.NDim - 2]) {
throw new MismatchedShapeException(lhs, rhs, "Matmul");
}
res.Shape[dim - 2] = aDstShape.Shape[dim - 2] =
lhs.NDim == 1 ? 1 : lhs.Shape[lhs.NDim - 2];
res.Shape[dim - 1] = bDstShape.Shape[dim - 1] =
rhs.NDim == 1 ? 1 : rhs.Shape[rhs.NDim - 1];
aDstShape.Shape[dim - 1] = bDstShape.Shape[dim - 2] = lhs.Shape[lhs.NDim - 1];
if (rhs.NDim == 1) {
rhs.Shape[1] = 1;
rhs.NDim = 2;
rhs.Stride[0] = 1;
rhs.Stride[1] = 0;
}
aDstShape.NDim = bDstShape.NDim = res.NDim;
lhs.BroadcastInplace(aDstShape);
rhs.BroadcastInplace(bDstShape);
return res;
}
}
}