-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathDaphneOps.td
More file actions
2175 lines (1816 loc) · 92.7 KB
/
DaphneOps.td
File metadata and controls
2175 lines (1816 loc) · 92.7 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SRC_IR_DAPHNEIR_DAPHNEOPS_TD
#define SRC_IR_DAPHNEIR_DAPHNEOPS_TD
// TODO Get rid of those ScalarType params.
include "ir/daphneir/DaphneDialect.td"
include "ir/daphneir/DaphneTypes.td"
include "ir/daphneir/DaphneDistributableOpInterface.td"
include "ir/daphneir/DaphneInferFrameLabelsOpInterface.td"
include "ir/daphneir/DaphneInferSymmetricOpInterface.td"
include "ir/daphneir/DaphneInferShapeOpInterface.td"
include "ir/daphneir/DaphneInferShapeTraits.td"
include "ir/daphneir/DaphneInferSparsityOpInterface.td"
include "ir/daphneir/DaphneInferSparsityTraits.td"
include "ir/daphneir/DaphneInferTypesOpInterface.td"
include "ir/daphneir/DaphneInferTypesTraits.td"
include "ir/daphneir/DaphneVectorizableOpInterface.td"
include "ir/daphneir/DaphneTypeAdaptationTraits.td"
include "ir/daphneir/CUDASupport.td"
include "ir/daphneir/FPGAOPENCLSupport.td"
include "mlir/Dialect/LLVMIR/LLVMTypes.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/CallInterfaces.td"
include "mlir/IR/AttrTypeBase.td"
// ****************************************************************************
// Custom constraints
// ****************************************************************************
// The type of `mat` is a matrix whose value type is the type of `sca`.
class TypeIsMatrixOf<string mat, string sca> : And<[
TypeIs<mat, Matrix>.predicate,
TypesMatchWith<"", mat, sca, "$_self.dyn_cast<::mlir::daphne::MatrixType>().getElementType()">.predicate
]>;
// The types of `a` and `b` must either be the same (e.g. both are matrices of
// the same value type, or both are scalars of the same type), or the type of
// `a` must be a matrix whose value type is the type of `b`, or vice-versa.
class TypesMatchOrOneIsMatrixOfOther<string a, string b> : PredOpTrait<
a # " and " # b # " must either be of the same type, or " # a # " must be a matrix whose value type is the type of " # b # ", or vice-versa",
Or<[
TypesMatchWith<"", a, b, "$_self">.predicate,
TypeIsMatrixOf<a, b>,
TypeIsMatrixOf<b, a>
]>
>;
// ****************************************************************************
// Base Daphne operation definition
// ****************************************************************************
class Daphne_Op<string mnemonic, list<Trait> traits = []> :
Op<Daphne_Dialect, mnemonic, traits>;
// ****************************************************************************
// DAPHNE Runtime Interoperability
// ****************************************************************************
def Daphne_ConvertMemRefToDenseMatrix : Daphne_Op<"convertMemRefToDenseMatrix"> {
let summary = "Return a DenseMatrix.";
let description = [{ Constructs a DenseMatrix given a rank 2 StridedMemRefType. }];
/* let arguments = (ins AnyMemRef:$arg); */
let hasCanonicalizeMethod = 1;
let arguments = (ins Size:$base, Size:$offset, Size:$size0, Size:$size1, Size:$stride0, Size:$stride1);
let results = (outs MatrixOrU:$res);
}
def Daphne_ConvertDenseMatrixToMemRef : Daphne_Op<"convertDenseMatrixToMemRef", [Pure]> {
let summary = "Given a DenseMatrix, return a StridedMemRefType.";
let description = [{ Constructs a StridedMemRefType with rank 2 from a DenseMatrix* with already allocated memory. }];
let hasCanonicalizeMethod = 1;
let arguments = (ins MatrixOrU:$arg);
let results = (outs AnyMemRef:$output);
}
def Daphne_ConvertCSRMatrixToValuesMemRef : Daphne_Op<"convertCSRMatrixToValuesMemRef", [Pure]> {
let summary = "Given a CSRMatrix, return a StridedMemRefType.";
let description = [{ Constructs a StridedMemRefType with rank 1 containing the values of a CSRMatrix* with already allocated memory. }];
let arguments = (ins MatrixOrU:$arg);
let results = (outs AnyMemRef:$resValues);
}
def Daphne_ConvertCSRMatrixToColIdxsMemRef : Daphne_Op<"convertCSRMatrixToColIdxsMemRef", [Pure]> {
let summary = "Given a CSRMatrix, return a StridedMemRefType.";
let description = [{ Constructs a StridedMemRefType with rank 1 containing the column indices of a CSRMatrix* with already allocated memory. }];
let arguments = (ins MatrixOrU:$arg);
let results = (outs AnyMemRef:$resColIdxs);
}
def Daphne_ConvertCSRMatrixToRowOffsetsMemRef : Daphne_Op<"convertCSRMatrixToRowOffsetsMemRef", [Pure]> {
let summary = "Given a CSRMatrix, return a StridedMemRefType.";
let description = [{ Constructs a StridedMemRefType with rank 1 containing the row offsets of a CSRMatrix* with already allocated memory. }];
let arguments = (ins MatrixOrU:$arg);
let results = (outs AnyMemRef:$resRowOffsets);
}
// ****************************************************************************
// Data generation
// ****************************************************************************
def Daphne_FillOp : Daphne_Op<"fill", [
DataTypeMat, ValueTypeFromFirstArg,
NumRowsFromIthScalar<1>, NumColsFromIthScalar<2>, CUDASupport,
DeclareOpInterfaceMethods<InferSymmetricOpInterface>
]> {
let arguments = (ins AnyScalar:$arg, Size:$numRows, Size:$numCols);
let results = (outs MatrixOrU:$res);
}
def Daphne_MatrixConstantOp : Daphne_Op<"matrixConstant", [
DataTypeMat,
DeclareOpInterfaceMethods<InferShapeOpInterface>
]>{
let arguments = (ins UI64:$matrixAddr);
let results = (outs MatrixOrU:$res);
}
def Daphne_CreateFrameOp : Daphne_Op<"createFrame", [
SameVariadicOperandSize,
DeclareOpInterfaceMethods<InferFrameLabelsOpInterface>,
DeclareOpInterfaceMethods<InferTypesOpInterface>,
DeclareOpInterfaceMethods<InferShapeOpInterface>,
Pure
]> {
let arguments = (ins Variadic<MatrixOrU>:$cols, Variadic<StrScalar>:$labels);
let results = (outs FrameOrU:$res);
}
def Daphne_DiagMatrixOp : Daphne_Op<"diagMatrix", [
TypeFromFirstArg,
NumRowsFromArg, NumColsFromArgNumRows,
DeclareOpInterfaceMethods<InferSparsityOpInterface>
]> {
let arguments = (ins MatrixOrU:$arg);
let results = (outs MatrixOrU:$res);
}
def Daphne_RandMatrixOp : Daphne_Op<"randMatrix", [
TypesMatchWith<"min and max arguments must be of the same type", "min", "max", "$_self">,
NumRowsFromIthScalar<0>, NumColsFromIthScalar<1>, DeclareOpInterfaceMethods<InferTypesOpInterface>,
SparsityFromIthScalar<4>, CastArgsToResTypeRandMatrixOp
]> {
let arguments = (ins Size:$numRows, Size:$numCols, AnyScalar:$min, AnyScalar:$max, F64:$sparsity, IntScalar:$seed);
let results = (outs MatrixOrU:$res);
}
def Daphne_SampleOp : Daphne_Op<"sample", [
DataTypeMat, ValueTypeFromFirstArg,
NumRowsFromIthScalar<1>, OneCol
]> {
let arguments = (ins AnyScalar:$range, Size:$size, BoolScalar:$withReplacement, Seed:$seed);
let results = (outs MatrixOrU:$res);
}
def Daphne_SeqOp : Daphne_Op<"seq", [
DataTypeMat, ValueTypeFromArgs,
OneCol, DeclareOpInterfaceMethods<InferNumRowsOpInterface>,
CompletelyDense,
CastArgsToResType
]> {
let arguments = (ins NumScalar:$from, NumScalar:$to, NumScalar:$inc);
let results = (outs MatrixOf<[NumScalar]>);
}
// ****************************************************************************
// Matrix/frame meta data
// ****************************************************************************
def Daphne_TypeOfOp : Daphne_Op<"typeOf"> {
let arguments = (ins AnyTypeOf<[AnyScalar, MatrixOrFrame]>:$arg);
let results = (outs StrScalar:$res);
}
class Daphne_NumOp<string name, list<Trait> traits = []> : Daphne_Op<name, !listconcat(traits, [
DataTypeSca, ValueTypeSize, Pure
])> {
let arguments = (ins MatrixOrFrame:$arg);
let results = (outs Size:$res);
let hasCanonicalizeMethod = 1;
}
def Daphne_NumRowsOp : Daphne_NumOp<"numRows">;
def Daphne_NumColsOp : Daphne_NumOp<"numCols">;
def Daphne_NumCellsOp : Daphne_NumOp<"numCells">;
def Daphne_SparsityOp : Daphne_Op<"sparsity", [DataTypeSca]> {
let arguments = (ins MatrixOf<[AnyScalar]>:$arg);
let results = (outs FloatScalar:$res);
let hasCanonicalizeMethod = 1;
}
def Daphne_IsSymmetricOp : Daphne_Op<"isSymmetric"> {
let summary = "Checks if a matrix is symmetric";
let description = [{
This operation checks if the input matrix is symmetric.
}];
let arguments = (ins MatrixOrU:$arg);
let results = (outs BoolScalar:$res);
}
// ****************************************************************************
// Matrix multiplication
// ****************************************************************************
def Daphne_MatMulOp : Daphne_Op<"matMul", [
DeclareOpInterfaceMethods<VectorizableOpInterface>,
DataTypeMat, ValueTypeFromArgs,
DeclareOpInterfaceMethods<InferShapeOpInterface>,
DeclareOpInterfaceMethods<InferSparsityOpInterface>, CUDASupport, FPGAOPENCLSupport,
CastFirstTwoArgsToResType, NoMemoryEffect,
Pure
]> {
let arguments = (ins MatrixOf<[NumScalar]>:$lhs, MatrixOf<[NumScalar]>:$rhs, BoolScalar:$transa, BoolScalar:$transb);
let results = (outs MatrixOf<[NumScalar]>:$res);
let hasCanonicalizeMethod = 1;
}
// ****************************************************************************
// Elementwise unary
// ****************************************************************************
class Daphne_EwUnaryOp<string name, Type scalarType, list<Trait> traits = []> : Daphne_Op<name, !listconcat(traits, [
DataTypeFromFirstArg,
ShapeFromArg,
CastArgsToResType,
NoMemoryEffect
])> {
let arguments = (ins AnyTypeOf<[MatrixOf<[scalarType]>, scalarType, Unknown]>:$arg);
let results = (outs AnyTypeOf<[MatrixOf<[scalarType]>, scalarType, Unknown]>:$res);
}
// ----------------------------------------------------------------------------
// Arithmetic/general math
// ----------------------------------------------------------------------------
// TODO EwMinusOp: Should an unsigned integer argument yield a signed integer result?
def Daphne_EwMinusOp : Daphne_EwUnaryOp<"ewMinus", NumScalar, [ValueTypeFromFirstArg]> {
let hasFolder = 1;
let hasCanonicalizeMethod = 1;
}
def Daphne_EwAbsOp : Daphne_EwUnaryOp<"ewAbs", NumScalar, [ValueTypeFromFirstArg]>;
def Daphne_EwSignOp : Daphne_EwUnaryOp<"ewSign", NumScalar, [ValueTypeFromFirstArg]>;
def Daphne_EwExpOp : Daphne_EwUnaryOp<"ewExp", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwLnOp : Daphne_EwUnaryOp<"ewLn", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwSqrtOp : Daphne_EwUnaryOp<"ewSqrt", NumScalar, [ValueTypeFromArgsFP, DeclareOpInterfaceMethods<VectorizableOpInterface>]>;
// ----------------------------------------------------------------------------
// Logical
// ----------------------------------------------------------------------------
def Daphne_EwNegOp : Daphne_EwUnaryOp<"ewNeg", NumScalar, [ValueTypeFromFirstArg]>;
// ----------------------------------------------------------------------------
// Rounding
// ----------------------------------------------------------------------------
def Daphne_EwRoundOp : Daphne_EwUnaryOp<"ewRound", NumScalar, [ValueTypeFromFirstArg]> {
let hasCanonicalizeMethod = 1;
}
def Daphne_EwFloorOp : Daphne_EwUnaryOp<"ewFloor", NumScalar, [ValueTypeFromFirstArg]> {
let hasCanonicalizeMethod = 1;
}
def Daphne_EwCeilOp : Daphne_EwUnaryOp<"ewCeil", NumScalar, [ValueTypeFromFirstArg]> {
let hasCanonicalizeMethod = 1;
}
// ----------------------------------------------------------------------------
// Trigonometric
// ----------------------------------------------------------------------------
def Daphne_EwSinOp : Daphne_EwUnaryOp<"ewSin", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwCosOp : Daphne_EwUnaryOp<"ewCos", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwTanOp : Daphne_EwUnaryOp<"ewTan", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwSinhOp : Daphne_EwUnaryOp<"ewSinh", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwCoshOp : Daphne_EwUnaryOp<"ewCosh", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwTanhOp : Daphne_EwUnaryOp<"ewTanh", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwAsinOp : Daphne_EwUnaryOp<"ewAsin", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwAcosOp : Daphne_EwUnaryOp<"ewAcos", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_EwAtanOp : Daphne_EwUnaryOp<"ewAtan", NumScalar, [ValueTypeFromArgsFP]>;
// ----------------------------------------------------------------------------
// Comparison
// ----------------------------------------------------------------------------
def Daphne_EwIsNanOp : Daphne_EwUnaryOp<"ewIsnan", NumScalar, [ValueTypeFromFirstArg]>;
// ----------------------------------------------------------------------------
// Strings
// ----------------------------------------------------------------------------
def Daphne_EwLowerOp : Daphne_EwUnaryOp<"ewLower", StrScalar, [ValueTypeFromFirstArg]>;
def Daphne_EwUpperOp : Daphne_EwUnaryOp<"ewUpper", StrScalar, [ValueTypeFromFirstArg]>;
// ****************************************************************************
// Elementwise binary
// ****************************************************************************
class Daphne_EwBinaryOp<string name, Type scalarType, list<Trait> traits = []>
: Daphne_Op<name, !listconcat(traits, [
DataTypeFromArgs,
DeclareOpInterfaceMethods<DistributableOpInterface>,
DeclareOpInterfaceMethods<VectorizableOpInterface>,
ShapeEwBinary,
NoMemoryEffect
])> {
let arguments = (ins AnyTypeOf<[MatrixOf<[scalarType]>, scalarType, Unknown]>:$lhs, AnyTypeOf<[MatrixOf<[scalarType]>, scalarType, Unknown]>:$rhs);
let results = (outs AnyTypeOf<[MatrixOf<[scalarType]>, scalarType, Unknown]>:$res);
let builders = [
OpBuilder<(ins "Value":$lhs, "Value":$rhs), [{
// TODO This is wrong if lhs is a scalar and rhs is a matrix.
$_state.addTypes(lhs.getType());
$_state.addOperands({lhs, rhs});
}]>
];
let hasFolder = 1;
}
// ----------------------------------------------------------------------------
// Arithmetic
// ----------------------------------------------------------------------------
// TODO Make EwAddOp Commutative again (see #449).
def Daphne_EwAddOp : Daphne_EwBinaryOp<"ewAdd", AnyScalar, [ValueTypeFromArgs, CastArgsToResType/*, Commutative*/, EwSparseIfBoth, CUDASupport]> {
let hasCanonicalizeMethod = 1;
}
def Daphne_EwSubOp : Daphne_EwBinaryOp<"ewSub", NumScalar, [ValueTypeFromArgs, CastArgsToResType, EwSparseIfBoth, CUDASupport]> {
let hasCanonicalizeMethod = 1;
}
def Daphne_EwMulOp : Daphne_EwBinaryOp<"ewMul", NumScalar, [ValueTypeFromArgs, CastArgsToResType, Commutative, EwSparseIfEither, CUDASupport]> {
let hasCanonicalizeMethod = 1;
}
def Daphne_EwDivOp : Daphne_EwBinaryOp<"ewDiv", NumScalar, [ValueTypeFromArgs, CastArgsToResType, CUDASupport]> {
let hasCanonicalizeMethod = 1;
}
def Daphne_EwPowOp : Daphne_EwBinaryOp<"ewPow", NumScalar, [ValueTypeFromArgs, CastArgsToResType, CUDASupport]>;
def Daphne_EwModOp : Daphne_EwBinaryOp<"ewMod", NumScalar, [ValueTypeFromArgs, CastArgsToResType]>;
def Daphne_EwLogOp : Daphne_EwBinaryOp<"ewLog", NumScalar, [ValueTypeFromArgsFP, CastArgsToResType]>;
// ----------------------------------------------------------------------------
// Min/max
// ----------------------------------------------------------------------------
def Daphne_EwMinOp : Daphne_EwBinaryOp<"ewMin", AnyScalar, [ValueTypeFromArgs, CastArgsToResType, Commutative]>;
def Daphne_EwMaxOp : Daphne_EwBinaryOp<"ewMax", AnyScalar, [ValueTypeFromArgs, CastArgsToResType, Commutative, CUDASupport]>;
// ----------------------------------------------------------------------------
// Logical
// ----------------------------------------------------------------------------
def Daphne_EwAndOp : Daphne_EwBinaryOp<"ewAnd", NumScalar, [Commutative, ValueTypeFromArgsInt, CastArgsToResType]>;
def Daphne_EwOrOp : Daphne_EwBinaryOp<"ewOr" , NumScalar, [Commutative, ValueTypeFromArgsInt, CastArgsToResType]>;
def Daphne_EwXorOp : Daphne_EwBinaryOp<"ewXor", NumScalar, [Commutative, ValueTypeFromArgsInt, CastArgsToResType]>;
// ----------------------------------------------------------------------------
// Bitwise
// ----------------------------------------------------------------------------
def Daphne_EwBitwiseAndOp : Daphne_EwBinaryOp<"ewBitwiseAnd", NumScalar, [Commutative, ValueTypeFromArgsInt, CastArgsToResType]>;
// ----------------------------------------------------------------------------
// Strings
// ----------------------------------------------------------------------------
def Daphne_EwConcatOp : Daphne_EwBinaryOp<"ewConcat", StrScalar, [ValueTypeStr, CastArgsToResType]>;
// ----------------------------------------------------------------------------
// Comparisons
// ----------------------------------------------------------------------------
class Daphne_EwCmpOp<string name, Type inputScalarType, list<Trait> traits = []>
: Daphne_EwBinaryOp<name, inputScalarType, !listconcat(traits, [ValueTypeCmp, CastArgsToMostGeneralArgType])> {
// TODO: We do not enforce (matrix of) boolean output any more, but should
// think about that again.
//let results = (outs AnyTypeOf<[MatrixOf<[BoolScalar]>, BoolScalar, Unknown]>:$res);
}
def Daphne_EwEqOp : Daphne_EwCmpOp<"ewEq" , AnyScalar, [Commutative]>;
def Daphne_EwNeqOp : Daphne_EwCmpOp<"ewNeq", AnyScalar, [Commutative, CUDASupport]>;
def Daphne_EwLtOp : Daphne_EwCmpOp<"ewLt" , AnyScalar>;
def Daphne_EwLeOp : Daphne_EwCmpOp<"ewLe" , AnyScalar>;
def Daphne_EwGtOp : Daphne_EwCmpOp<"ewGt" , AnyScalar>;
def Daphne_EwGeOp : Daphne_EwCmpOp<"ewGe" , AnyScalar>;
// ****************************************************************************
// Outer binary (generalized outer product)
// ****************************************************************************
class Daphne_OuterBinaryOp<string name, Type scalarType, list<Trait> traits = []>
: Daphne_Op<name, !listconcat(traits, [
DataTypeMat,
NumRowsFromIthArg<0>, NumColsFromIthArg<1>,
CastArgsToResType
])> {
let arguments = (ins MatrixOf<[scalarType]>:$lhs, MatrixOf<[scalarType]>:$rhs);
let results = (outs MatrixOf<[scalarType]>:$res);
}
// ----------------------------------------------------------------------------
// Arithmetic
// ----------------------------------------------------------------------------
def Daphne_OuterAddOp : Daphne_OuterBinaryOp<"outerAdd", NumScalar, [ValueTypeFromArgs]>;
def Daphne_OuterSubOp : Daphne_OuterBinaryOp<"outerSub", NumScalar, [ValueTypeFromArgs]>;
def Daphne_OuterMulOp : Daphne_OuterBinaryOp<"outerMul", NumScalar, [ValueTypeFromArgs]>;
def Daphne_OuterDivOp : Daphne_OuterBinaryOp<"outerDiv", NumScalar, [ValueTypeFromArgs]>;
def Daphne_OuterPowOp : Daphne_OuterBinaryOp<"outerPow", NumScalar, [ValueTypeFromArgs]>;
def Daphne_OuterModOp : Daphne_OuterBinaryOp<"outerMod", NumScalar, [ValueTypeFromArgs]>;
def Daphne_OuterLogOp : Daphne_OuterBinaryOp<"outerLog", NumScalar, [ValueTypeFromArgsFP]>;
// ----------------------------------------------------------------------------
// Min/max
// ----------------------------------------------------------------------------
def Daphne_OuterMinOp : Daphne_OuterBinaryOp<"outerMin", AnyScalar, [ValueTypeFromArgs]>;
def Daphne_OuterMaxOp : Daphne_OuterBinaryOp<"outerMax", AnyScalar, [ValueTypeFromArgs]>;
// ----------------------------------------------------------------------------
// Logical
// ----------------------------------------------------------------------------
def Daphne_OuterAndOp : Daphne_OuterBinaryOp<"outerAnd", NumScalar, [ValueTypeFromArgsInt]>;
def Daphne_OuterOrOp : Daphne_OuterBinaryOp<"outerOr" , NumScalar, [ValueTypeFromArgsInt]>;
def Daphne_OuterXorOp : Daphne_OuterBinaryOp<"outerXor", NumScalar, [ValueTypeFromArgsInt]>;
// ----------------------------------------------------------------------------
// Strings
// ----------------------------------------------------------------------------
def Daphne_OuterConcatOp : Daphne_OuterBinaryOp<"outerConcat", StrScalar>;
// ----------------------------------------------------------------------------
// Comparisons
// ----------------------------------------------------------------------------
class Daphne_OuterCmpOp<string name, Type inputScalarType, list<Trait> traits = []>
: Daphne_OuterBinaryOp<name, inputScalarType, !listconcat(traits, [ValueTypeFromArgs])> {
// TODO: We do not enforce (matrix of) boolean output any more, but should
// think about that again.
//let results = (outs AnyTypeOf<[MatrixOf<[BoolScalar]>, BoolScalar, Unknown]>:$res);
}
def Daphne_OuterEqOp : Daphne_OuterCmpOp<"outerEq" , AnyScalar>;
def Daphne_OuterNeqOp : Daphne_OuterCmpOp<"outerNeq", AnyScalar>;
def Daphne_OuterLtOp : Daphne_OuterCmpOp<"outerLt" , AnyScalar>;
def Daphne_OuterLeOp : Daphne_OuterCmpOp<"outerLe" , AnyScalar>;
def Daphne_OuterGtOp : Daphne_OuterCmpOp<"outerGt" , AnyScalar>;
def Daphne_OuterGeOp : Daphne_OuterCmpOp<"outerGe" , AnyScalar>;
// ****************************************************************************
// Elementwise ternary
// ****************************************************************************
def Daphne_CondOp : Daphne_Op<"cond", [
DeclareOpInterfaceMethods<InferTypesOpInterface>,
DeclareOpInterfaceMethods<InferShapeOpInterface>
]> {
let arguments = (ins AnyTypeOf<[Matrix, AnyScalar, Unknown]>:$cond, AnyType:$thenVal, AnyType:$elseVal);
let results = (outs AnyType:$res);
let hasCanonicalizeMethod = 1;
}
// ****************************************************************************
// Aggregation and statistical
// ****************************************************************************
class Daphne_AggOp<string name, Type inScalarType, list<Trait> traits = []> : Daphne_Op<name, traits> {
let arguments = (ins MatrixOf<[inScalarType]>:$arg);
}
// ----------------------------------------------------------------------------
// Full aggregation
// ----------------------------------------------------------------------------
class Daphne_AllAggOp<string name, Type scalarType, list<Trait> traits = []>
: Daphne_AggOp<name, scalarType, !listconcat(traits, [DataTypeSca, CastArgsToResType])> {
let results = (outs scalarType:$res);
}
def Daphne_AllAggSumOp : Daphne_AllAggOp<"sumAll", NumScalar, [ValueTypeFromFirstArg]>{
let hasCanonicalizeMethod = 1;
}
def Daphne_AllAggMinOp : Daphne_AllAggOp<"minAll", NumScalar, [ValueTypeFromFirstArg]>;
def Daphne_AllAggMaxOp : Daphne_AllAggOp<"maxAll", NumScalar, [ValueTypeFromFirstArg]>;
def Daphne_AllAggMeanOp : Daphne_AllAggOp<"meanAll", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_AllAggVarOp : Daphne_AllAggOp<"varAll", NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_AllAggStddevOp : Daphne_AllAggOp<"stddevAll", NumScalar, [ValueTypeFromArgsFP]>;
// ----------------------------------------------------------------------------
// Row/column-wise aggregation
// ----------------------------------------------------------------------------
class Daphne_DimAggOp<string name, Type inScalarType, Type outScalarType = inScalarType, list<Trait> traits = []>
: Daphne_AggOp<name, inScalarType, !listconcat(traits, [DataTypeMat])> {
let results = (outs MatrixOf<[outScalarType]>:$res);
}
class Daphne_RowAggOp<string name, Type inScalarType, Type outScalarType = inScalarType, list<Trait> traits = []>
: Daphne_DimAggOp<name, inScalarType, outScalarType, !listconcat(traits, [
NumRowsFromArg, OneCol
])>;
class Daphne_ColAggOp<string name, Type inScalarType, Type outScalarType = inScalarType, list<Trait> traits = []>
: Daphne_DimAggOp<name, inScalarType, outScalarType, !listconcat(traits, [
OneRow, NumColsFromArg
])>;
def Daphne_RowAggSumOp : Daphne_RowAggOp<"sumRow" , NumScalar, NumScalar, [ValueTypeFromFirstArg, CastArgsToResType,
CUDASupport, DeclareOpInterfaceMethods<VectorizableOpInterface>]>;
def Daphne_RowAggMinOp : Daphne_RowAggOp<"minRow" , AnyScalar, AnyScalar, [ValueTypeFromFirstArg, CastArgsToResType, DeclareOpInterfaceMethods<VectorizableOpInterface>]>;
def Daphne_RowAggMaxOp : Daphne_RowAggOp<"maxRow" , AnyScalar, AnyScalar, [ValueTypeFromFirstArg, CastArgsToResType,
CUDASupport, DeclareOpInterfaceMethods<VectorizableOpInterface>, DeclareOpInterfaceMethods<DistributableOpInterface>]>;
def Daphne_RowAggIdxMinOp : Daphne_RowAggOp<"idxminRow", NumScalar, Size, [ValueTypeSize]>;
def Daphne_RowAggIdxMaxOp : Daphne_RowAggOp<"idxmaxRow", NumScalar, Size, [ValueTypeSize]>;
def Daphne_RowAggMeanOp : Daphne_RowAggOp<"meanRow" , NumScalar, NumScalar, [ValueTypeFromArgsFP, CastArgsToResType]>;
def Daphne_RowAggVarOp : Daphne_RowAggOp<"varRow" , NumScalar, NumScalar, [ValueTypeFromArgsFP, CastArgsToResType]>;
def Daphne_RowAggStddevOp : Daphne_RowAggOp<"stddevRow", NumScalar, NumScalar, [ValueTypeFromArgsFP, CastArgsToResType]>;
def Daphne_ColAggSumOp : Daphne_ColAggOp<"sumCol" , NumScalar, NumScalar, [ValueTypeFromFirstArg, CastArgsToResType,
CUDASupport, DeclareOpInterfaceMethods<VectorizableOpInterface>]>;
def Daphne_ColAggMinOp : Daphne_ColAggOp<"minCol" , AnyScalar, AnyScalar, [ValueTypeFromFirstArg, CastArgsToResType]>;
def Daphne_ColAggMaxOp : Daphne_ColAggOp<"maxCol" , AnyScalar, AnyScalar, [ValueTypeFromFirstArg, CastArgsToResType]>;
def Daphne_ColAggIdxMinOp : Daphne_ColAggOp<"idxminCol", NumScalar, Size, [ValueTypeSize]>;
def Daphne_ColAggIdxMaxOp : Daphne_ColAggOp<"idxmaxCol", NumScalar, Size, [ValueTypeSize]>;
def Daphne_ColAggMeanOp : Daphne_ColAggOp<"meanCol" , NumScalar, NumScalar, [ValueTypeFromArgsFP, CastArgsToResType]>;
def Daphne_ColAggVarOp : Daphne_ColAggOp<"varCol" , NumScalar, NumScalar, [ValueTypeFromArgsFP, CastArgsToResType]>;
def Daphne_ColAggStddevOp : Daphne_ColAggOp<"stddevCol", NumScalar, NumScalar, [ValueTypeFromArgsFP, CastArgsToResType]>;
// ----------------------------------------------------------------------------
// Cumulative aggregation
// ----------------------------------------------------------------------------
class Daphne_CumAggOp<string name, Type scalarType, list<Trait> traits = []>
: Daphne_AggOp<name, scalarType, !listconcat(traits, [
DataTypeMat, ShapeFromArg
])> {
let results = (outs MatrixOf<[scalarType]>);
}
def Daphne_CumAggSumOp : Daphne_CumAggOp<"sumCum" , NumScalar, [ValueTypeFromFirstArg]>;
def Daphne_CumAggProdOp : Daphne_CumAggOp<"prodCum", NumScalar, [ValueTypeFromFirstArg]>;
def Daphne_CumAggMinOp : Daphne_CumAggOp<"minCum" , AnyScalar, [ValueTypeFromFirstArg]>;
def Daphne_CumAggMaxOp : Daphne_CumAggOp<"maxCum" , AnyScalar, [ValueTypeFromFirstArg]>;
// ----------------------------------------------------------------------------
// Grouped aggregation
// ----------------------------------------------------------------------------
class Daphne_GrpAggOp<string name, Type scalarType, list<Trait> traits = []>
: Daphne_Op<name, !listconcat(traits, [
DataTypeMat,
NumRowsFromIthScalar<2>, NumColsFromArg
])> {
let arguments = (ins MatrixOf<[scalarType]>:$arg, MatrixOf<[Size]>:$groupIds, Size:$numGroups/*, Optional<MatrixOrU>:$weights*/);
let results = (outs MatrixOf<[scalarType]>:$groupAggs);
}
def Daphne_GrpAggCountOp : Daphne_GrpAggOp<"countGrp" , AnyScalar>;
def Daphne_GrpAggSumOp : Daphne_GrpAggOp<"sumGrp" , NumScalar, [ValueTypeFromFirstArg]>;
def Daphne_GrpAggMinOp : Daphne_GrpAggOp<"minGrp" , AnyScalar, [ValueTypeFromFirstArg]>;
def Daphne_GrpAggMaxOp : Daphne_GrpAggOp<"maxGrp" , AnyScalar, [ValueTypeFromFirstArg]>;
def Daphne_GrpAggMeanOp : Daphne_GrpAggOp<"meanGrp" , NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_GrpAggVarOp : Daphne_GrpAggOp<"varGrp" , NumScalar, [ValueTypeFromArgsFP]>;
def Daphne_GrpAggStddevOp : Daphne_GrpAggOp<"stddevGrp", NumScalar, [ValueTypeFromArgsFP]>;
// ----------------------------------------------------------------------------
// Statistical for column matrices
// ----------------------------------------------------------------------------
def Daphne_MedianOp : Daphne_Op<"median"> {
let arguments = (ins MatrixOf<[FloatScalar]>:$arg, Optional<MatrixOf<[FloatScalar]>>:$weights);
let results = (outs FloatScalar:$res);
}
def Daphne_QuantileOp : Daphne_Op<"quantile"> {
let arguments = (ins MatrixOf<[FloatScalar]>:$arg, MatrixOf<[FloatScalar]>:$ps, Optional<MatrixOf<[FloatScalar]>>:$weights);
let results = (outs MatrixOf<[FloatScalar]>:$res);
}
def Daphne_MomentOp : Daphne_Op<"moment"> {
let arguments = (ins MatrixOf<[FloatScalar]>:$arg, Size:$k, Optional<MatrixOf<[FloatScalar]>>:$weights);
let results = (outs FloatScalar:$res);
}
def Daphne_CovOp : Daphne_Op<"cov"> {
let arguments = (ins MatrixOf<[FloatScalar]>:$lhs, MatrixOf<[FloatScalar]>:$rhs, Optional<MatrixOf<[FloatScalar]>>:$weights);
let results = (outs FloatScalar:$res);
}
// ****************************************************************************
// Left and right indexing
// ****************************************************************************
def Daphne_ExtractOp : Daphne_Op<"extract", [
NumRowsFromIthArg<1>, NumColsFromIthArgNumRows<2>,
DeclareOpInterfaceMethods<InferTypesOpInterface>
]> {
let summary = "Copies the specified rows and columns from the argument to the result.";
let description = [{
This operation is effectively similar to a combination of `extractRow`
and `extractCol`. Please see the documentation of those operations.
}];
let arguments = (ins MatrixOrFrame:$source, Selection:$selectedRows, AnyTypeOf<[Selection, StrScalar, Unknown]>:$selectedCols);
let results = (outs MatrixOrFrame:$res);
}
def Daphne_ExtractRowOp : Daphne_Op<"extractRow", [
TypeFromFirstArg,
DeclareOpInterfaceMethods<InferFrameLabelsOpInterface>,
NumRowsFromIthArg<1>, NumColsFromArg
]> {
let summary = "Copies the specified rows from the argument to the result.";
let description = [{
Copies the rows of a data object `source` (matrix or frame) at the
positions specified by `selectedRows` to the result. `selectedRows`
must be either a scalar or a single-column matrix of positions (row
indexes) into `source`, i.e., the entries must be between zero
(inclusive) and the number of rows in `source` (exclusive). Beyond that,
there are no restrictions.
Note that `FilterRowOp` is similar, but works with bit vectors instead
of positions lists.
}];
let arguments = (ins MatrixOrFrame:$source, Selection:$selectedRows);
let results = (outs MatrixOrFrame:$res);
}
def Daphne_SliceRowOp : Daphne_Op<"sliceRow", [
TypeFromFirstArg,
DeclareOpInterfaceMethods<InferShapeOpInterface>,
Pure
]> {
let summary = "Copies the specified rows from the argument to the result.";
let description = [{
Copies the rows of a data object `source` (matrix or frame) at the
positions in `[lowerIncl, upperExcl)` to the result.
}];
let arguments = (ins MatrixOrFrame:$source, SI64:$lowerIncl, SI64:$upperExcl);
let results = (outs MatrixOrFrame:$res);
}
def Daphne_ExtractColOp : Daphne_Op<"extractCol", [
DeclareOpInterfaceMethods<InferFrameLabelsOpInterface>,
DeclareOpInterfaceMethods<InferTypesOpInterface>,
DeclareOpInterfaceMethods<VectorizableOpInterface>,
DeclareOpInterfaceMethods<InferShapeOpInterface>, CUDASupport, Pure
]> {
let summary = "Copies the specified columns from the argument to the result.";
let description = [{
Copies the columns of a data object `source` (matrix or frame) at the
positions specified by `selectedCols` to the result. `selectedCols`
must be either a scalar or a single-column matrix of positions (column
indexes) into `source`, i.e., the entries must be between zero
(inclusive) and the number of columns in `source` (exclusive). Beyond
that, there are no restrictions.
Alternatively, `selectedCols` can be a string, in which case the column
with that label is extracted from `source` if it is a frame.
Note that `FilterRowOp` is similar, but works with bit vectors instead
of positions lists.
}];
let arguments = (ins MatrixOrFrame:$source, AnyTypeOf<[Selection, StrScalar, Unknown]>:$selectedCols);
let results = (outs MatrixOrFrame:$res);
let hasCanonicalizeMethod = 1;
}
def Daphne_SliceColOp : Daphne_Op<"sliceCol", [
DeclareOpInterfaceMethods<InferTypesOpInterface>,
DeclareOpInterfaceMethods<InferShapeOpInterface>
]> {
let summary = "Copies the specified columns from the argument to the result.";
let description = [{
Copies the columns of a data object `source` (matrix or frame) at the
positions in `[lowerIncl, upperExcl)` to the result.
}];
let arguments = (ins MatrixOrFrame:$source, SI64:$lowerIncl, SI64:$upperExcl);
let results = (outs MatrixOrFrame:$res);
let hasCanonicalizeMethod = 1;
}
// TODO Create combined InsertOp (see #238).
def Daphne_InsertRowOp : Daphne_Op<"insertRow", [
TypeFromFirstArg, // this is debatable
ShapeFromArg
]> {
let arguments = (ins MatrixOrFrame:$arg, MatrixOrFrame:$ins, SI64:$rowLowerIncl, SI64:$rowUpperExcl);
let results = (outs MatrixOrFrame:$res);
let hasCanonicalizeMethod = 1;
}
def Daphne_InsertColOp : Daphne_Op<"insertCol", [
TypeFromFirstArg, // this is debatable
ShapeFromArg,
Pure
]> {
let arguments = (ins MatrixOrFrame:$arg, MatrixOrFrame:$ins, SI64:$colLowerIncl, SI64:$colUpperExcl);
let results = (outs MatrixOrFrame:$res);
}
// ****************************************************************************
// Reorganization
// ****************************************************************************
def Daphne_ReshapeOp : Daphne_Op<"reshape", [
TypeFromFirstArg,
NumRowsFromIthScalar<1>, NumColsFromIthScalar<2>, SparsityFromArg
]> {
let arguments = (ins MatrixOrU:$arg, Size:$numRows, Size:$numCols);
let results = (outs MatrixOrU:$res);
}
def Daphne_TransposeOp : Daphne_Op<"transpose", [
TypeFromFirstArg,
DeclareOpInterfaceMethods<VectorizableOpInterface>,
NumRowsFromArgNumCols, NumColsFromArgNumRows, SparsityFromArg, CUDASupport,
DeclareOpInterfaceMethods<InferSymmetricOpInterface>,
Pure
]> {
let arguments = (ins MatrixOrU:$arg);
let results = (outs MatrixOrU:$res);
let builders = [
OpBuilder<(ins "Value":$input), [{
auto ty = input.getType();
build($_builder, $_state, ty, input);
}]>,
];
}
class Daphne_BindOp<string name, list<Trait> traits = []> : Daphne_Op<name, !listconcat(traits, [
DataTypeFromArgs,
CastArgsToResType
])> {
let arguments = (ins MatrixOrFrame:$lhs, MatrixOrFrame:$rhs);
let results = (outs MatrixOrFrame:$res);
}
def Daphne_ColBindOp : Daphne_BindOp<"colBind", [
ValueTypesConcat,
DeclareOpInterfaceMethods<InferFrameLabelsOpInterface>,
DeclareOpInterfaceMethods<VectorizableOpInterface>,
NumRowsFromAllArgs, NumColsFromSumOfAllArgs, CUDASupport,
Pure
]>;
def Daphne_RowBindOp : Daphne_BindOp<"rowBind", [
ValueTypeFromArgs,
NumRowsFromSumOfAllArgs, NumColsFromAllArgs
]>;
def Daphne_ReverseOp : Daphne_Op<"reverse", [
TypeFromFirstArg, ShapeFromArg
]> {
let arguments = (ins MatrixOrU:$arg);
let results = (outs MatrixOrU:$res);
}
def Daphne_OrderOp : Daphne_Op<"order", [
DeclareOpInterfaceMethods<InferFrameLabelsOpInterface>,
DeclareOpInterfaceMethods<InferTypesOpInterface>, // due to possibility of returning indexes
SameVariadicOperandSize,
DeclareOpInterfaceMethods<InferShapeOpInterface>
]> {
// TODO Maybe colIdxs and ascs should be attributes.
let arguments = (ins MatrixOrFrame:$arg, Variadic<Size>:$colIdxs, Variadic<BoolScalar>:$ascs, BoolScalar:$returnIdxs);
let results = (outs MatrixOrFrame:$res);
}
// ****************************************************************************
// Matrix decompositions & co
// ****************************************************************************
def Daphne_EigenOp : Daphne_Op<"eigenCal", [TypeFromFirstArg, DeclareOpInterfaceMethods<InferTypesOpInterface>,
DeclareOpInterfaceMethods<InferShapeOpInterface>]> {
let arguments = (ins MatrixOf<[FloatScalar]>:$arg);
let results = (outs MatrixOf<[FloatScalar]>:$eigenValues, MatrixOf<[FloatScalar]>:$eigenVectors);
}
def Daphne_LuOp : Daphne_Op<"lu"> {
let arguments = (ins MatrixOf<[FloatScalar]>:$arg);
let results = (outs MatrixOf<[FloatScalar]>:$p, MatrixOf<[FloatScalar]>:$l, MatrixOf<[FloatScalar]>:$u);
}
def Daphne_QrOp : Daphne_Op<"qr"> {
let arguments = (ins MatrixOf<[FloatScalar]>:$arg);
let results = (outs MatrixOf<[FloatScalar]>:$h, MatrixOf<[FloatScalar]>:$r);
}
def Daphne_SvdOp : Daphne_Op<"svd"> {
let arguments = (ins MatrixOf<[FloatScalar]>:$arg);
let results = (outs MatrixOf<[FloatScalar]>:$u, MatrixOf<[FloatScalar]>:$s, MatrixOf<[FloatScalar]>:$v);
}
// ****************************************************************************
// Deep neural network
// ****************************************************************************
// TODO The DNN-related operations share several arguments. Is there an elegant
// way to specify the *common* arguments just once?
// ----------------------------------------------------------------------------
// Activation
// ----------------------------------------------------------------------------
class Daphne_ActivationForwardOp<string name, list<Trait> traits = []> :
Daphne_Op<name, traits> {
let arguments = (ins MatrixOf<[FloatScalar]>:$input);
let results = (outs MatrixOf<[FloatScalar]>:$data);
}
def Daphne_ReluForwardOp : Daphne_ActivationForwardOp<"NN_Relu_Forward", [ ValueTypeFromFirstArg, DataTypeFromFirstArg, CUDASupport ]>;
// ----------------------------------------------------------------------------
// Affine
// ----------------------------------------------------------------------------
def Daphne_AffineForwardOp : Daphne_Op<"NN_Affine_Forward", [ ValueTypeFromFirstArg, DataTypeFromFirstArg, CUDASupport ]> {
let arguments = (ins MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$weights,
MatrixOf<[FloatScalar]>:$bias);
let results = (outs MatrixOf<[FloatScalar]>:$data);
}
// ----------------------------------------------------------------------------
// Batch Normalization
// ----------------------------------------------------------------------------
def Daphne_BatchNorm2DInferenceForwardOp : Daphne_Op<"BatchNormInferenceForward", [ ValueTypeFromFirstArg, DataTypeFromFirstArg, CUDASupport ]> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$gamma, MatrixOf<[FloatScalar]>:$beta,
MatrixOf<[FloatScalar]>:$emaMean, MatrixOf<[FloatScalar]>:$emaVar, FloatScalar:$eps);
let results = (outs MatrixOf<[FloatScalar]>:$output);
}
def Daphne_BatchNorm2DTrainForwardOp : Daphne_Op<"BatchNorm_Train_Forward", [ ValueTypeFromFirstArg, DataTypeFromFirstArg, CUDASupport ]> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$gamma, MatrixOf<[FloatScalar]>:$beta,
MatrixOf<[FloatScalar]>:$emaMean, MatrixOf<[FloatScalar]>:$emaVar, FloatScalar:$eps, FloatScalar:$mu);
let results = (outs MatrixOf<[FloatScalar]>:$output,
MatrixOf<[FloatScalar]>:$new_emaMean,
MatrixOf<[FloatScalar]>:$new_emaVar,
MatrixOf<[FloatScalar]>:$Mean,
MatrixOf<[FloatScalar]>:$invVar);
}
def Daphne_BatchNorm2DBackwardOp : Daphne_Op<"BatchNorm_Backward", [ ValueTypeFromFirstArg, DataTypeFromFirstArg, CUDASupport ]> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$mean, MatrixOf<[FloatScalar]>:$invVar, MatrixOf<[FloatScalar]>:$input,
MatrixOf<[FloatScalar]>:$dout, MatrixOf<[FloatScalar]>:$gamma, FloatScalar:$eps);
let results = (outs MatrixOf<[FloatScalar]>:$dX,
MatrixOf<[FloatScalar]>:$dGamma,
MatrixOf<[FloatScalar]>:$dBeta);
}
// ----------------------------------------------------------------------------
// Bias Addition
// ----------------------------------------------------------------------------
def Daphne_BiasAddOp : Daphne_Op<"BiasAdd", [ ValueTypeFromFirstArg, DataTypeFromFirstArg, CUDASupport ]> {
let arguments = (ins MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$bias);
let results = (outs MatrixOf<[FloatScalar]>:$data);
}
// ----------------------------------------------------------------------------
// Convolution
// ----------------------------------------------------------------------------
def Daphne_Conv2DForwardOp : Daphne_Op<"Convolution_Forward",
[ DeclareOpInterfaceMethods<InferTypesOpInterface>, DeclareOpInterfaceMethods<InferShapeOpInterface>,
CUDASupport ]> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$filter, MatrixOf<[FloatScalar]>:$bias,
// input shape
Size:$inputBatchSize, Size:$inputNumChannels, Size:$inputHeight, Size:$inputWidth,
// filter shape (num filters & channels is encoded in dimensions of filter matrix (-> [F,C*H*W]))
Size:$filterHeight, Size:$filterWidth, Size:$strideHeight, Size:$strideWidth, Size:$padHeight, Size:$padWidth);
let results = (outs MatrixOf<[FloatScalar]>:$output, Size:$outHeight, Size:$outWidth);
}
def Daphne_Conv2DBackwardFilterOp : Daphne_Op<"conv2DBackwardFilter"> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$output,
Size:$strideHeight, Size:$strideWidth, Size:$padHeight, Size:$padWidth,
// input shape
Size:$inputBatchSize, Size:$inputNumChannels, Size:$inputHeight, Size:$inputWidth,
// filter shape
Size:$filterNumFilters, Size:$filterNumChannels, Size:$filterHeight, Size:$filterWidth
);
let results = (outs MatrixOf<[FloatScalar]>:$filter);
}
def Daphne_Conv2DBackwardDataOp : Daphne_Op<"conv2DBackwardData"> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$filter, MatrixOf<[FloatScalar]>:$output,
Size:$strideHeight, Size:$strideWidth, Size:$padHeight, Size:$padWidth,
// input shape
Size:$inputBatchSize, Size:$inputNumChannels, Size:$inputHeight, Size:$inputWidth,
// filter shape
Size:$filterNumFilters, Size:$filterNumChannels, Size:$filterHeight, Size:$filterWidth
);
let results = (outs MatrixOf<[FloatScalar]>:$data);
}
// ----------------------------------------------------------------------------
// Pooling
// ----------------------------------------------------------------------------
class Daphne_PoolForwardOp<string name, list<Trait> traits = []> : Daphne_Op<name, traits> {
let arguments = (ins MatrixOf<[FloatScalar]>:$input,
// input shape (NCHW)
Size:$inputBatchSize, Size:$inputNumChannels, Size:$inputHeight, Size:$inputWidth,
// pool shape (ToDo: optional, default filter size 2x2)
Size:$poolHeight, Size:$poolWidth,
// filter application properties (ToDo: optional, default stride=1x1, padding=0x0)
Size:$strideHeight, Size:$strideWidth, Size:$padHeight, Size:$padWidth
);
let results = (outs MatrixOf<[FloatScalar]>:$data, Size:$outHeight, Size:$outWidth);
}
def Daphne_MaxPoolForwardOp : Daphne_PoolForwardOp<"NN_Max_Forward", [ DeclareOpInterfaceMethods<InferTypesOpInterface>,
DeclareOpInterfaceMethods<InferShapeOpInterface>,
CUDASupport ]>;
def Daphne_AvgPoolForwardOp : Daphne_PoolForwardOp<"NN_Avg_Forward", [ DeclareOpInterfaceMethods<InferTypesOpInterface>,
DeclareOpInterfaceMethods<InferShapeOpInterface>,
CUDASupport ]>;
def Daphne_AvgPoolBackwardOp : Daphne_Op<"avgPoolBackward", [ CUDASupport ]> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$dOut,
Size:$batch_size, Size:$num_channels, Size:$img_h, Size:$img_w,
Size:$pool_h, Size:$pool_w,
Size:$stride_h, Size:$stride_w, Size:$pad_h, Size:$pad_w
);
let results = (outs MatrixOf<[FloatScalar]>:$res);
}
def Daphne_MaxPoolBackwardOp : Daphne_Op<"maxPoolBackward", [ CUDASupport ]> {
let arguments = (ins
MatrixOf<[FloatScalar]>:$input, MatrixOf<[FloatScalar]>:$dOut,
Size:$batch_size, Size:$num_channels, Size:$img_h, Size:$img_w,
Size:$pool_h, Size:$pool_w,
Size:$stride_h, Size:$stride_w, Size:$pad_h, Size:$pad_w
);
let results = (outs MatrixOf<[FloatScalar]>:$res);
}
// ----------------------------------------------------------------------------
// Softmax
// ----------------------------------------------------------------------------
def Daphne_SoftmaxOp : Daphne_Op<"Softmax", [ DataTypeFromFirstArg, ValueTypeFromFirstArg, CUDASupport ]> {
let arguments = (ins MatrixOf<[FloatScalar]>:$input);
let results = (outs MatrixOf<[FloatScalar]>:$data);
}
// ****************************************************************************
// Other matrix operations
// ****************************************************************************
def Daphne_DiagVectorOp : Daphne_Op<"diagVector", [
TypeFromFirstArg, NumRowsFromArg, OneCol,
Pure