-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy pathShapeHelper.hpp
947 lines (841 loc) · 46.6 KB
/
ShapeHelper.hpp
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
/*
* SPDX-License-Identifier: Apache-2.0
*/
//===---------------- ONNXShapeHelper.hpp - help for shapes ---------------===//
//
// Copyright 2020-2024 The IBM Research Authors.
//
// =============================================================================
//
// This file has the computations to compute the shapes using the new index expr
// approach.
//
//===----------------------------------------------------------------------===//
#pragma once
#include <utility>
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/ValueRange.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "src/Dialect/Mlir/IndexExpr.hpp"
#include "src/Dialect/Mlir/IndexExprBuilder.hpp"
#include "src/Dialect/ONNX/ONNXDimAnalysis.hpp"
#define GET_OP_FWD_DEFINES 1
#include "src/Dialect/ONNX/ONNXOps.hpp.inc"
// ONNXOpShapeHelper is defined in the interface file below.
#include "src/Interface/ShapeHelperOpInterface.hpp"
namespace onnx_mlir {
//===----------------------------------------------------------------------===//
// Support functions.
//===----------------------------------------------------------------------===//
// Check if axis is in [-rank, rank), or [-rank, rank] when includeRank is
// true. Assert when not in range. Return positive axis.
int64_t getAxisInRange(int64_t axis, int64_t rank, bool includeRank = false);
int64_t getAxisInRange(int64_t axis, mlir::Value val, bool includeRank = false);
// Check if axis is in [-rank, rank), or [-rank, rank] when includeRank is true.
// Return false when not in range; set axis to positive value when in range.
bool isAxisInRange(int64_t &axis, int64_t rank, bool includeRank = false);
bool isAxisInRange(int64_t &axis, mlir::Value val, bool includeRank = false);
//===----------------------------------------------------------------------===//
// Support functions.
//===----------------------------------------------------------------------===//
// Update a tensor type by using the given shape, elementType and encoding.
// TODO: when all ops are migrated to the new scheme, make this function private
// to ONNXOpShapeHelper.
// Parameters:
// Val: this function will update val's type.
// shape: shape of the ranked tensor type of val.
// elementType: When nullptr, pick the elementary type from val.
// encoding: When nullptr, pick the encoding from val if defined.
void updateType(mlir::Operation *op, mlir::Value val,
llvm::ArrayRef<int64_t> shape, mlir::Type elementType = nullptr,
mlir::Attribute encoding = nullptr, bool refineShape = true);
// When we perform shape inference, we always assume that the type's shape in
// onnx is correct. There are rare instance where we transform an existing op
// (see RNN's handling of layout in RNNOpRewriteLayoutPattern) and then seek to
// perform shape inference on it. As the operation has changed, then we must
// first "erase" its constant shape's for the output type as they are not
// correct anymore. It might be wiser to not reuse an existing op, but since we
// currently have this pattern, this function must be called prior to infer
// shapes of existing but modified operations.
void resetTypesShapeToQuestionmarks(mlir::Operation *op);
//===----------------------------------------------------------------------===//
// Unimplemented Ops (to be used sparingly, currently for Loop and Scan).
// Other uses should be converted to shape inferences.
//===----------------------------------------------------------------------===//
struct ONNXUnimplementedOpShapeHelper : public ONNXOpShapeHelper {
ONNXUnimplementedOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXUnimplementedOpShapeHelper() {}
bool isImplemented() override { return false; }
mlir::LogicalResult computeShape() final { return mlir::success(); }
};
// Classes for unsupported ops, including shape inference and shape helpers.
#define UNSUPPORTED_OPS(OP_TYPE) \
using OP_TYPE##ShapeHelper = ONNXUnimplementedOpShapeHelper;
#include "src/Dialect/ONNX/ONNXUnsupportedOps.hpp"
#undef UNSUPPORTED_OPS
// Classes with implemented shape inference but not shape helper.
// clang-format off
using ONNXCallOpShapeHelper = ONNXUnimplementedOpShapeHelper;
using ONNXIfOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: recursive, Opt, Seq
using ONNXLoopOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: recursive, Opt, Seq
using ONNXOptionalGetElementOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Opt, Seq
using ONNXOptionalHasElementOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Opt, Seq
using ONNXOptionalOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Opt, Seq
using ONNXScanOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: recursive
using ONNXSequenceAtOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Seq
using ONNXSequenceConstructOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Seq
using ONNXSequenceEmptyOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Seq
using ONNXSequenceEraseOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Seq
using ONNXSequenceInsertOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Seq
using ONNXSequenceLengthOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Seq
using ONNXSplitToSequenceOpShapeHelper = ONNXUnimplementedOpShapeHelper; // Reason: Seq
// clang-format on
//===----------------------------------------------------------------------===//
// Broadcast Ops
//===----------------------------------------------------------------------===//
// Compute a broadcasted shape from the shapes of given operands. Operands must
// be ranked in advance.
struct ONNXBroadcastOpShapeHelper : public ONNXOpShapeHelper {
ONNXBroadcastOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr,
bool hasUniBroadcasting = false)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), inputsDims(),
outputRank(0), hasUniBroadcasting(hasUniBroadcasting) {}
virtual ~ONNXBroadcastOpShapeHelper() {}
// Custom shape compute which takes additional parameters.
mlir::LogicalResult customComputeShape(
mlir::ValueRange initialOperands, DimsExpr *additionalOperand);
// Default shape compute (every operands of the operation and no additional
// parameters).
mlir::LogicalResult computeShape() override {
return customComputeShape(operands, nullptr);
}
// Compute access indices to load/store value from/to a given 'operand'.
// Used in a loop to access the operand.
// Parameters:
// - operand: operand to access.
// - i: index of the operand in Index Expr Dims 'this->inputsDims'.
// - loopAccessExprs: IndexExprs for the loop's IVs.
// - operandAccessExprs: access indices to access the operand.
// This is the output of this function. Use it in subsequent load/stores.
// - flattenedInnerDims: whether the innermost dimension corresponds to a
// collapsed/flattened loop index or not.
// - ruledOutBroadcast: determined using shape analysis that there is no
// broadcasting here.
virtual mlir::LogicalResult getAccessExprs(mlir::Value operand, int64_t i,
const llvm::SmallVectorImpl<IndexExpr> &loopAccessExprs,
llvm::SmallVectorImpl<IndexExpr> &operandAccessExprs,
bool flattenedInnerDims = false, bool ruledOutBroadcast = false);
// Determine if broadcast can be ruled out at compile time. Use DimAnalysis
// when available. Broadcasting is defined if one value of one input is used
// two or more times with a value of another input (when only looking at the
// tensors, not the actual algorithms).
//
// Examples with broadcasts:
// * 2x5xf32 and 1x5xf32 has broadcast as the second input's value are used
// for each of the two instances of 5xf32 in the first input.
// * Same holds for 2x5xf32 and 5xf32 as shorter ranked
// inputs are extended by prepending 1x.
//
// Example without broadcast:
// * 3x5xf32 and 3x5xf32 have no broadcast.
// * 1x5xf32 and 5xf32 have also no broadcast as prepending 1x results as
// comparing 1x5xf32 with 1x5xf32.
virtual bool hasNoBroadcast(DimAnalysis *dimAnalysis = nullptr);
// Determine if the broadcast operation has operands with different ranks.
// Use DimAnalysis when available. The broadcasting will then add dimensions
// with size 1.
//
// Examples with rank broadcast:
// * 2x5xf32 and 5xf32 has rank broadcast for the second type. It will be
// interpreted as 1x5xf32
//
// Examples without rank broadcast:
// * 2x5xf32 and 1x5xf32 does not have rank broadcasting because the ranks are
// already equal
virtual bool hasRankBroadcast();
// Determine of the broadcast operation has manageable broadcast (MB), and if
// so, at which level/rank. We first attempt to see if the innermost dimension
// has MB, and if it does, we then attempt to test at the next innermost
// level... until we fail or we run out of dimensions.
//
// Manageable broadcast (MB) is either that:
// 1) we have no broadcast up to that level, or
// 2) we have scalars up to that level being broadcasted.
//
// The function return true if there is some MB, and then
// * collapsedInnermostLoops: indicates how many inner loops are involved in
// the MB. They are named "collapsed" as in the SIMD code execution, we may
// collapse these dimensions in a single long iteration. For example,
// `0x?x4x5` and `0x?x4x5` have a collapsedInnermostLoops==2 (if the two `?`
// cannot be shown as equals). This means that we may implement operations
// on these inputs as `?x20` and `?x20` respectively.
// * collapsedLiteralSize: cumulative static size of the collapsed inner
// loops.
// * collapsedDynamicSize: cumulative dynamic size of the collapsed inner
// loops.
//
// Below are examples of Manageable Broadcast (MB) at a given
// collapsedInnermostLoops (CIL) level.
//
// What is a scalar: `4x2x1x1 is scalar at CIL==1 and 2, but not 3 and 4.
//
// - (?1, 1, 4, 1) and (10, 1, 4, 1) have MB at CIL 1, 2, 3, not 4,
// unless dynamic analysis can show ?1 to be equal to 10 (unlikely).
// - (?1, 1, ?2, 1) and (10, 1, ?3, 1) have MB at CIL 1 and at 2
// if dynamic analysis can show ?2 and ?3 to be the same.
// - (1, 4, 1) and (2, 4, 1) have MB at CIL 1 and 2, but not 3 as
// there is broadcasting (1 vs 2) at CIL 3... but the first
// operand is not a scalar at CIL 3.
// - (1, 1, 1) and (2, 4, 1) have MB at CIL 1, 2, and 3 as there is
// broadcast at inner dim 2 and 3, and the first operand is a scalar at
// CIL 1, 2, and 3.
// - (1,3) and (1, 1) have MB at CIL 1; technically, CIL 2 is also a MB but
// there is nothing to be gained by collapsing dimensions where all
// inputs have dimensions of 1. We thus do not include them in our CILs.
virtual bool hasManageableBroadcastForInnerDims(
int64_t &collapsedInnermostLoops, int64_t &collapsedLiteralSize,
IndexExpr &collapsedDynamicSize, DimAnalysis *dimAnalysis);
// A vector of input shapes where dimensions are padded with 1 if necessary,
// so that all inputs have the same rank. Instantiated during ComputeShape.
llvm::SmallVector<DimsExpr, 4> inputsDims;
// Rank of the output shape.
uint64_t outputRank;
protected:
// When unidirectional broadcasting is true, the other operands are always
// unidirectional broadcastable to the first operand.
bool hasUniBroadcasting;
};
// clang-format off
using ONNXAddOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXAndOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXBitwiseAndOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXBitwiseOrOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXBitwiseXorOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXBitShiftOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXDivOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXEqualOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXGreaterOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXGreaterOrEqualOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXLessOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXLessOrEqualOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXMaxOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXMeanOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXMinOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXModOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXMulOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXOrOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXPowOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXSubOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXSumOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXWhereOpShapeHelper = ONNXBroadcastOpShapeHelper;
using ONNXXorOpShapeHelper = ONNXBroadcastOpShapeHelper;
// clang-format on
// Helper for ExpandOp
struct ONNXExpandOpShapeHelper : public ONNXBroadcastOpShapeHelper {
ONNXExpandOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXBroadcastOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXExpandOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
};
// Helper for ONNXPReluOp
struct ONNXPReluOpShapeHelper : public ONNXBroadcastOpShapeHelper {
ONNXPReluOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXBroadcastOpShapeHelper(op, operands, ieBuilder, scope,
/*hasUniBroadcasting*/ true) {}
virtual ~ONNXPReluOpShapeHelper() {}
mlir::LogicalResult computeShape() final {
return ONNXBroadcastOpShapeHelper::computeShape();
}
};
// Template for Layer Normalization (LN) ops
template <typename OP_TYPE>
struct ONNXLNOpShapeHelper : public ONNXBroadcastOpShapeHelper {
ONNXLNOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXBroadcastOpShapeHelper(op, operands, ieBuilder, scope,
/*hasUniBroadcasting*/ true) {}
virtual ~ONNXLNOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
};
// clang-format off
using ONNXLayerNormalizationOpShapeHelper = ONNXLNOpShapeHelper<mlir::ONNXLayerNormalizationOp>;
using ONNXRMSLayerNormalizationOpShapeHelper = ONNXLNOpShapeHelper<mlir::ONNXRMSLayerNormalizationOp>;
// clang-format on
//===----------------------------------------------------------------------===//
// Unary Ops
//===----------------------------------------------------------------------===//
/// Compute an output shape for a unary element-wise operation. The output and
/// input of an unary element-wise operation have the same shape.
struct ONNXUnaryOpShapeHelper : public ONNXBroadcastOpShapeHelper {
ONNXUnaryOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXBroadcastOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXUnaryOpShapeHelper() {}
mlir::LogicalResult computeShape() override;
// Inherited methods that return trivial results
mlir::LogicalResult getAccessExprs(mlir::Value operand, int64_t i,
const llvm::SmallVectorImpl<IndexExpr> &loopAccessExprs,
llvm::SmallVectorImpl<IndexExpr> &operandAccessExprs,
bool flattenedInnerDims = false, bool ruledOutBroadcast = false) override;
bool hasNoBroadcast(DimAnalysis *dimAnalysis = nullptr) override;
bool hasManageableBroadcastForInnerDims(int64_t &collapsedInnermostLoops,
int64_t &collapsedLiteralSize, IndexExpr &collapsedDynamicSize,
DimAnalysis *dimAnalysis) override;
};
// Handle shape inference for unary element-wise operators. Perform the entire
// operation: (1) create the shape helper, compute shape, apply shape to the
// output types.
mlir::LogicalResult inferShapeForUnaryOps(mlir::Operation *op);
// Same as above, but allow a type change.
mlir::LogicalResult inferShapeForUnaryOps(
mlir::Operation *op, mlir::Type elementType);
// Same as above, but allow a type and encoding change.
mlir::LogicalResult inferShapeForUnaryOps(
mlir::Operation *op, mlir::Type elementType, mlir::Attribute encoding);
// clang-format off
using ONNXAbsOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXAcosOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXAcoshOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXAsinOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXAsinhOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXAtanOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXAtanhOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXBernoulliOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXBitwiseNotOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXCastOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXCeilOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXCeluOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXClipOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXCosOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXCoshOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXCumSumOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXEluOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXErfOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXExpOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXFloorOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXGeluOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXHardSigmoidOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXHardSwishOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXHardmaxOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXInstanceNormalizationOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXIsInfOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXIsNaNOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXLayoutTransformOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXLeakyReluOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXLogOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXLogSoftmaxOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXLpNormalizationOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXMeanVarianceNormalizationOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXNegOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXNotOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXRandomNormalLikeOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXReciprocalOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXReluOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXRoundOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXScalerOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXScatterElementsOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXScatterNDOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXScatterOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSeluOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXShrinkOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSigmoidOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSignOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSinOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSinhOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSoftmaxOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSoftmaxV11OpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSoftplusOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSoftsignOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXSqrtOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXTanOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXTanhOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXThresholdedReluOpShapeHelper = ONNXUnaryOpShapeHelper;
using ONNXTriluOpShapeHelper = ONNXUnaryOpShapeHelper;
// clang-format on
//===----------------------------------------------------------------------===//
// Shape op
//===----------------------------------------------------------------------===//
struct ONNXShapeOpShapeHelper : public ONNXOpShapeHelper {
ONNXShapeOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), start(-1), end(-1) {}
virtual ~ONNXShapeOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Compute the shape values of input data for dimensions between start and
// end.
void computeSelectedDataShape(DimsExpr &selectedDataShape);
// Compute start & end value without calls to computeShape.
static void getStartEndValues(
mlir::ONNXShapeOp shapeOp, int64_t &startVal, int64_t &endVal);
// Additional data for ShapeOp.
int64_t start, end; // Start and end properly normalized (-1 is undef).
};
//===----------------------------------------------------------------------===//
// Pooling Ops (ONNXMaxPoolSingleOutOp, ONNXAveragePoolOp, ONNXConvOp)
//===----------------------------------------------------------------------===//
// Generic pool shape helper.
template <typename OP_TYPE>
struct ONNXGenericPoolOpShapeHelper : public ONNXOpShapeHelper {
ONNXGenericPoolOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXGenericPoolOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Actual computation of the pool shape and parameters using every different
// switches that differs between pooling and conv ops.
mlir::LogicalResult customComputeShape(mlir::Value X /* image */,
mlir::Value W /* filter */, std::optional<mlir::ArrayAttr> kernelShapeOpt,
llvm::StringRef autoPad, std::optional<mlir::ArrayAttr> padOpt,
std::optional<mlir::ArrayAttr> strideOpt,
std::optional<mlir::ArrayAttr> dilationOpt,
bool hasFilter, // If has filter, it also has CO and optional kernel.
bool ceilMode); // Use ceil or floor for auto_pad=NOTSET policy.
// Values set by customComputeShape.
llvm::SmallVector<IndexExpr, 2> kernelShape;
llvm::SmallVector<IndexExpr, 4> pads;
llvm::SmallVector<int64_t, 2> strides;
llvm::SmallVector<int64_t, 2> dilations;
};
// clang-format off
using ONNXAveragePoolOpShapeHelper = ONNXGenericPoolOpShapeHelper<mlir::ONNXAveragePoolOp>;
using ONNXConvOpShapeHelper = ONNXGenericPoolOpShapeHelper<mlir::ONNXConvOp>;
using ONNXConvIntegerOpShapeHelper = ONNXGenericPoolOpShapeHelper<mlir::ONNXConvIntegerOp>;
using ONNXQLinearConvOpShapeHelper = ONNXGenericPoolOpShapeHelper<mlir::ONNXQLinearConvOp>;
using ONNXMaxPoolSingleOutOpShapeHelper = ONNXGenericPoolOpShapeHelper<mlir::ONNXMaxPoolSingleOutOp>;
// clang-format on
//===----------------------------------------------------------------------===//
// ConvTranspose Op
//===----------------------------------------------------------------------===//
struct ONNXConvTransposeOpShapeHelper : public ONNXOpShapeHelper {
ONNXConvTransposeOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), kernelShape(),
pads(), strides(), dilations(), outputPadding() {}
virtual ~ONNXConvTransposeOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Values set by computeShape.
llvm::SmallVector<IndexExpr, 2> kernelShape;
llvm::SmallVector<IndexExpr, 4> pads;
llvm::SmallVector<int64_t, 2> strides;
llvm::SmallVector<int64_t, 2> dilations;
llvm::SmallVector<int64_t, 2> outputPadding;
};
//===----------------------------------------------------------------------===//
// Global pooling ops
//===----------------------------------------------------------------------===//
template <typename OP_TYPE>
struct ONNXGenericGlobalPoolOpShapeHelper : public ONNXOpShapeHelper {
ONNXGenericGlobalPoolOpShapeHelper(mlir::Operation *op,
mlir::ValueRange operands, IndexExprBuilder *ieBuilder = nullptr,
IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXGenericGlobalPoolOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
};
// clang-format off
using ONNXGlobalAveragePoolOpShapeHelper = ONNXGenericGlobalPoolOpShapeHelper<mlir::ONNXGlobalAveragePoolOp>;
using ONNXGlobalLpPoolOpShapeHelper = ONNXGenericGlobalPoolOpShapeHelper<mlir::ONNXGlobalLpPoolOp>;
using ONNXGlobalMaxPoolOpShapeHelper = ONNXGenericGlobalPoolOpShapeHelper<mlir::ONNXGlobalMaxPoolOp>;
// clang-format on
//===----------------------------------------------------------------------===//
// Slice Op
//===----------------------------------------------------------------------===//
struct ONNXSliceOpShapeHelper : public ONNXOpShapeHelper {
ONNXSliceOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope){};
virtual ~ONNXSliceOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Additional data for SliceOp.
llvm::SmallVector<IndexExpr, 4> starts, ends, steps;
};
//===----------------------------------------------------------------------===//
// Gemm Op
//===----------------------------------------------------------------------===//
struct ONNXGemmOpShapeHelper : public ONNXOpShapeHelper {
ONNXGemmOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), aDims(), bDims(),
cDims(), hasBias(/*dummy value*/ false), cRank(-1) {}
virtual ~ONNXGemmOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Additional data for GemmOp: output = a * b.
llvm::SmallVector<IndexExpr, 4> aDims; // Dim after applying transpose.
llvm::SmallVector<IndexExpr, 4> bDims; // Dim after applying transpose.
llvm::SmallVector<IndexExpr, 4> cDims; // Dim after padding 1 when broadcast.
bool hasBias; // Whether there is a bias (aka C exists).
int cRank; // Dim of the original C (not padding dims by 1).
};
//===----------------------------------------------------------------------===//
// Matmul Ops (ONNXMatMulOp, ONNXMatMulIntegerOp, ONNXQLinearMatMulOp)
//===----------------------------------------------------------------------===//
template <typename OP_TYPE>
struct ONNXGenericMatMulOpShapeHelper : public ONNXOpShapeHelper {
ONNXGenericMatMulOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), aDims(), bDims(),
aPadDims(), bPadDims() {}
virtual ~ONNXGenericMatMulOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Additional data for MatMulOp: output = a * b.
llvm::SmallVector<IndexExpr, 4> aDims; // Dim after applying padding.
llvm::SmallVector<IndexExpr, 4> bDims; // Dim after applying padding.
llvm::BitVector aPadDims; // When true, that dim was padded.
llvm::BitVector bPadDims; // When true, that dim was padded.
};
// clang-format off
using ONNXMatMulOpShapeHelper = ONNXGenericMatMulOpShapeHelper<mlir::ONNXMatMulOp>;
using ONNXMatMulIntegerOpShapeHelper = ONNXGenericMatMulOpShapeHelper<mlir::ONNXMatMulIntegerOp>;
using ONNXQLinearMatMulOpShapeHelper = ONNXGenericMatMulOpShapeHelper<mlir::ONNXQLinearMatMulOp>;
// clang-format on
//===----------------------------------------------------------------------===//
// Pad Op
//===----------------------------------------------------------------------===//
struct ONNXPadOpShapeHelper : public ONNXOpShapeHelper {
ONNXPadOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), pads() {}
virtual ~ONNXPadOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Additional data for PadOp.
llvm::SmallVector<IndexExpr, 4> pads;
};
//===----------------------------------------------------------------------===//
// OneHot Op
//===----------------------------------------------------------------------===//
struct ONNXOneHotOpShapeHelper : public ONNXOpShapeHelper {
ONNXOneHotOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), axis(-1), depth() {}
virtual ~ONNXOneHotOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Additional data for OneHotOp.
int64_t axis; // Default value.
IndexExpr depth; // Depth which may/may not be known at compile time.
};
//===----------------------------------------------------------------------===//
// RoiAlign Op
//===----------------------------------------------------------------------===//
struct ONNXRoiAlignOpShapeHelper : public ONNXOpShapeHelper {
ONNXRoiAlignOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope), xDims(),
batchIndicesDims() {}
virtual ~ONNXRoiAlignOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Additional data for RoiAlignOp.
llvm::SmallVector<IndexExpr, 4> xDims; // Dim of X.
llvm::SmallVector<IndexExpr, 1> batchIndicesDims; // Dim of batch_indices.
};
//===----------------------------------------------------------------------===//
// Arg Min/Max Op
//===----------------------------------------------------------------------===//
// Arg Min and Max operations use the same code, we just have to use their
// respective identical operand adaptor, so specialize with templated code.
template <typename OP_TYPE>
struct ONNXArgMinMaxOpShapeHelper : public ONNXOpShapeHelper {
ONNXArgMinMaxOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXArgMinMaxOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
};
// clang-format off
using ONNXArgMaxOpShapeHelper = ONNXArgMinMaxOpShapeHelper<mlir::ONNXArgMaxOp>;
using ONNXArgMinOpShapeHelper = ONNXArgMinMaxOpShapeHelper<mlir::ONNXArgMinOp>;
// clang-format on
//===----------------------------------------------------------------------===//
// Split ops
//===----------------------------------------------------------------------===//
// Different versions of split op use common code, so specialize with
// templated code.
template <typename OP_TYPE>
struct ONNXCommonSplitOpShapeHelper : public ONNXOpShapeHelper {
ONNXCommonSplitOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXCommonSplitOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Common code for compute shape.
mlir::LogicalResult customComputeShape(
mlir::ArrayRef<IndexExpr> indexExprArray);
};
// clang-format off
using ONNXSplitOpShapeHelper = ONNXCommonSplitOpShapeHelper<mlir::ONNXSplitOp>;
using ONNXSplitV11OpShapeHelper = ONNXCommonSplitOpShapeHelper<mlir::ONNXSplitV11Op>;
using ONNXSplitV13OpShapeHelper = ONNXCommonSplitOpShapeHelper<mlir::ONNXSplitV13Op>;
// clang-format on
//===----------------------------------------------------------------------===//
// Squeeze ops
//===----------------------------------------------------------------------===//
// Different versions of split op use common code, so specialize with
// templated code.
template <typename OP_TYPE>
struct ONNXCommonSqueezeOpShapeHelper : public ONNXOpShapeHelper {
ONNXCommonSqueezeOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXCommonSqueezeOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Custom method to save axes in the op/graph.
void saveAxes();
// Common code for compute shape.
mlir::LogicalResult customComputeShape(
DimsExpr &squeezedDims, bool squeezeFromShape);
// Data: squeezedAxes contains all of the axles to squeeze, normalized (i.e.
// between 0 and dataRank).
llvm::SmallVector<int64_t, 4> squeezedAxes;
};
// clang-format off
using ONNXSqueezeOpShapeHelper = ONNXCommonSqueezeOpShapeHelper<mlir::ONNXSqueezeOp>;
using ONNXSqueezeV11OpShapeHelper = ONNXCommonSqueezeOpShapeHelper<mlir::ONNXSqueezeV11Op>;
// clang-format on
//===----------------------------------------------------------------------===//
// Unique ops
//===----------------------------------------------------------------------===//
// Different versions of split op use common code, so specialize with
// templated code.
struct ONNXUniqueOpShapeHelper : public ONNXOpShapeHelper {
ONNXUniqueOpShapeHelper(mlir::Operation *op,
mlir::ArrayRef<mlir::Value> operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXUniqueOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Additional data for UniqueOp:
};
//===----------------------------------------------------------------------===//
// Unsqueeze ops
//===----------------------------------------------------------------------===//
// Different versions of split op use common code, so specialize with
// templated code.
template <typename OP_TYPE>
struct ONNXCommonUnsqueezeOpShapeHelper : public ONNXOpShapeHelper {
ONNXCommonUnsqueezeOpShapeHelper(mlir::Operation *op,
mlir::ValueRange operands, IndexExprBuilder *ieBuilder = nullptr,
IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXCommonUnsqueezeOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Custom method to save axes in the op/graph.
void saveAxes();
// Common code for compute shape.
mlir::LogicalResult customComputeShape(DimsExpr &unsqueezedDims);
// Data: unsqueezedAxes contains all of the axles to unsqueeze, normalized
// (i.e. between 0 and dataRank).
llvm::SmallVector<int64_t, 4> unsqueezedAxes;
};
// clang-format off
using ONNXUnsqueezeOpShapeHelper = ONNXCommonUnsqueezeOpShapeHelper<mlir::ONNXUnsqueezeOp>;
using ONNXUnsqueezeV11OpShapeHelper = ONNXCommonUnsqueezeOpShapeHelper<mlir::ONNXUnsqueezeV11Op>;
// clang-format on
//===----------------------------------------------------------------------===//
// DFT Ops
//===----------------------------------------------------------------------===//
// Generic DFT shape helper.
template <typename OP_TYPE>
struct ONNXGenericDFTOpShapeHelper : public ONNXOpShapeHelper {
ONNXGenericDFTOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXGenericDFTOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
mlir::LogicalResult customComputeShape(IndexExpr &axis);
};
// clang-format off
using ONNXDFTOpShapeHelper = ONNXGenericDFTOpShapeHelper<mlir::ONNXDFTOp>;
//===----------------------------------------------------------------------===//
// Reduction Ops
//===----------------------------------------------------------------------===//
// Generic Reduction shape helper.
template <typename OP_TYPE>
struct ONNXGenericReductionOpShapeHelper : public ONNXOpShapeHelper {
ONNXGenericReductionOpShapeHelper(mlir::Operation *op,
mlir::ValueRange operands, IndexExprBuilder *ieBuilder = nullptr,
IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXGenericReductionOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Actual computation of the pool shape and parameters using every different
// switches that differs between pooling and conv ops.
mlir::LogicalResult customComputeShape(DimsExpr &axes, int noopWithEmptyAxes);
// Values set by customComputeShape.
llvm::SmallVector<bool, 4> isReductionAxis;
};
// clang-format off
using ONNXReduceL1OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceL1Op>;
using ONNXReduceL1V13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceL1V13Op>;
using ONNXReduceL2OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceL2Op>;
using ONNXReduceL2V13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceL2V13Op>;
using ONNXReduceLogSumOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceLogSumOp>;
using ONNXReduceLogSumV13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceLogSumV13Op>;
using ONNXReduceLogSumExpOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceLogSumExpOp>;
using ONNXReduceLogSumExpV13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceLogSumExpV13Op>;
using ONNXReduceMaxOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMaxOp>;
using ONNXReduceMaxV13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMaxV13Op>;
using ONNXReduceMaxV18OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMaxV18Op>;
using ONNXReduceMeanOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMeanOp>;
using ONNXReduceMeanV13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMeanV13Op>;
using ONNXReduceMinOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMinOp>;
using ONNXReduceMinV13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMinV13Op>;
using ONNXReduceMinV18OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceMinV18Op>;
using ONNXReduceProdOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceProdOp>;
using ONNXReduceProdV13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceProdV13Op>;
using ONNXReduceSumOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceSumOp>;
using ONNXReduceSumV11OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceSumV11Op>;
using ONNXReduceSumSquareOpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceSumSquareOp>;
using ONNXReduceSumSquareV13OpShapeHelper = ONNXGenericReductionOpShapeHelper<mlir::ONNXReduceSumSquareV13Op>;
// clang-format on
//===----------------------------------------------------------------------===//
// RNN Ops (ONNXRNNOp, ONNXLSTMOp, ONNXRNNOp)
//===----------------------------------------------------------------------===//
// Generic Reduction shape helper.
template <typename OP_TYPE>
struct ONNXGenericRNNShapeHelper : public ONNXOpShapeHelper {
ONNXGenericRNNShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXGenericRNNShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Actual computation of the RNN shape and parameters using every different
// switches that differs between pooling and conv ops.
mlir::LogicalResult customComputeShape(int gates);
// Values set by customComputeShape.
llvm::SmallVector<bool, 4> isReductionAxis;
};
// clang-format off
using ONNXGRUOpShapeHelper = ONNXGenericRNNShapeHelper<mlir::ONNXGRUOp>;
using ONNXLSTMOpShapeHelper = ONNXGenericRNNShapeHelper<mlir::ONNXLSTMOp>;
using ONNXRNNOpShapeHelper = ONNXGenericRNNShapeHelper<mlir::ONNXRNNOp>;
// clang-format on
//===----------------------------------------------------------------------===//
// Resize Op
//===----------------------------------------------------------------------===//
struct ONNXResizeOpShapeHelper : public ONNXOpShapeHelper {
ONNXResizeOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXResizeOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
// Values set by computeShape: scales is a float index expression. It is
// directly the `scale` argument when scale is provided by the op. When
// `size` is provided, then scale is float(`size`)/float(dim).
llvm::SmallVector<IndexExpr, 4> scales;
};
//===----------------------------------------------------------------------===//
// Non specific Ops, namely ops that
// * need customization only for themselves (no sharing of code)
// * have no specific parameters
//===----------------------------------------------------------------------===//
/*
These ops require a template instantiation where computeShape is defined.
For example like this for ONNXCategoryMapperOp:
namespace onnx_mlir {
template struct ONNXNonSpecificOpShapeHelper<ONNXCategoryMapperOp>;
} // namespace onnx_mlir
*/
template <typename OP_TYPE>
struct ONNXNonSpecificOpShapeHelper : public ONNXOpShapeHelper {
ONNXNonSpecificOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr)
: ONNXOpShapeHelper(op, operands, ieBuilder, scope) {}
virtual ~ONNXNonSpecificOpShapeHelper() {}
mlir::LogicalResult computeShape() final;
};
// Ops listed in alphabetical order. Disable formatting for easier sorting.
// clang-format off
using ONNXBatchNormalizationInferenceModeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXBatchNormalizationInferenceModeOp>;
using ONNXCategoryMapperOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXCategoryMapperOp>;
using ONNXCompressOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXCompressOp>;
using ONNXConcatOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXConcatOp>;
using ONNXConcatShapeTransposeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXConcatShapeTransposeOp>;
using ONNXConstantOfShapeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXConstantOfShapeOp>;
using ONNXConstantOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXConstantOp>;
using ONNXDepthToSpaceOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXDepthToSpaceOp>;
using ONNXDequantizeLinearOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXDequantizeLinearOp>;
using ONNXDimOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXDimOp>;
using ONNXDropoutOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXDropoutOp>;
using ONNXDynamicQuantizeLinearOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXDynamicQuantizeLinearOp>;
using ONNXEinsumOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXEinsumOp>;
using ONNXEyeLikeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXEyeLikeOp>;
using ONNXFlattenOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXFlattenOp>;
using ONNXGatherElementsOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXGatherElementsOp>;
using ONNXGatherNDOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXGatherNDOp>;
using ONNXGatherOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXGatherOp>;
using ONNXIdentityOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXIdentityOp>;
using ONNXLRNOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXLRNOp>;
using ONNXMaxRoiPoolOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXMaxRoiPoolOp>;
using ONNXNonMaxSuppressionOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXNonMaxSuppressionOp>;
using ONNXNonZeroOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXNonZeroOp>;
using ONNXOneHotEncoderOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXOneHotEncoderOp>;
using ONNXQuantizeLinearOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXQuantizeLinearOp>;
using ONNXRandomNormalOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXRandomNormalOp>;
using ONNXRangeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXRangeOp>;
using ONNXReshapeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXReshapeOp>;
using ONNXReverseSequenceOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXReverseSequenceOp>;
using ONNXShapeTransformOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXShapeTransformOp>;
using ONNXSizeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXSizeOp>;
using ONNXSpaceToDepthOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXSpaceToDepthOp>;
using ONNXTileOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXTileOp>;
using ONNXTopKOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXTopKOp>;
using ONNXTransposeOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXTransposeOp>;
using ONNXUpsampleOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXUpsampleOp>;
using ONNXForkOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXForkOp>;
using ONNXParallelOpShapeHelper = ONNXNonSpecificOpShapeHelper<mlir::ONNXParallelOp>;
// clang-format on
//===----------------------------------------------------------------------===//
// CustomOp Shape Helper.
//===----------------------------------------------------------------------===//
struct ONNXCustomOpShapeHelper : public ONNXUnaryOpShapeHelper {
ONNXCustomOpShapeHelper(mlir::Operation *op, mlir::ValueRange operands,
IndexExprBuilder *ieBuilder = nullptr, IndexExprScope *scope = nullptr,
bool hasUniBroadcasting = false);
// Default shape compute (every operands of the operation and no additional
// parameters).
mlir::LogicalResult computeShape() override;
protected:
// Shape inference pattern
int pattern;
};
//===----------------------------------------------------------------------===//
// Setting a new constant or attribute value.
//===----------------------------------------------------------------------===//
/*
Save into op's mutable operand a newly formed ONNX Constant that holds the
integer values provided in "vals".
Example:
ONNXUnsqueezeOp unsqueezeOp = llvm::cast<ONNXUnsqueezeOp>(op);
SaveOnnxConstInOp(op, unsqueezeOp.axesMutable(), unsqueezedAxes);
*/
void SaveOnnxConstInOp(mlir::Operation *op, mlir::MutableOperandRange operand,
const llvm::SmallVectorImpl<int64_t> &vals);
/*
Save into op's attributes a newly formed attributes that holds the integer
values provided in "vals". The actual setting is performed by a lambda
function with parameter OP_TYPE op and new Attribute. The user can then set
that value in the proper attribute.
Example:
SaveOnnxAttrInOp<ONNXUnsqueezeV11Op>(op, unsqueezedAxes,
[](ONNXUnsqueezeV11Op op, ArrayAttr attr) { op.setAxesAttr(attr); });
*/
template <typename OP_TYPE>
void SaveOnnxAttrInOp(mlir::Operation *op,
const llvm::SmallVectorImpl<int64_t> &vals,
mlir::function_ref<void(OP_TYPE op, mlir::ArrayAttr &attr)> setAttr) {
// Inlined so that we don't need template instantiation.
mlir::OpBuilder builder(op->getContext());
mlir::ArrayAttr newAttr = builder.getI64ArrayAttr(vals);
OP_TYPE specificOp = llvm::cast<OP_TYPE>(op);
setAttr(specificOp, newAttr);
}
} // namespace onnx_mlir