-
Notifications
You must be signed in to change notification settings - Fork 40
/
ConvToGemm.cpp
1447 lines (1269 loc) · 58.1 KB
/
ConvToGemm.cpp
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
//===- ConvToGemm.cpp - MLIR Rock ops lowering passes ------------===//
//
// Copyright 2020 The MLIR Authors.
//
// 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.
// ============================================================
//
// This pass converts rock.conv into rock.transform and
// rock.gemm.
//
//===-----------------------------------------------------===//
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/Rock/IR/GemmSize.h"
#include "mlir/Dialect/Rock/IR/Rock.h"
#include "mlir/Dialect/Rock/IR/RockConvInterface.h"
#include "mlir/Dialect/Rock/IR/TransformMapBuilder.h"
#include "mlir/Dialect/Rock/Passes.h"
#include "mlir/Dialect/Rock/Tuning/ConvContext.h"
#include "mlir/Dialect/Rock/Tuning/GridwiseGemmParams.h"
#include "mlir/Dialect/Rock/Tuning/UtilityParams.h"
#include "mlir/Dialect/Rock/utility/builderUtils.h"
#include "mlir/Dialect/Rock/utility/loweringUtils.h"
#include "mlir/Dialect/Rock/utility/math.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include <iterator>
namespace mlir {
namespace rock {
#define GEN_PASS_DEF_ROCKCONVTOGEMMPASS
#include "mlir/Dialect/Rock/Passes.h.inc"
} // namespace rock
} // namespace mlir
#define DEBUG_TYPE "rock-conv-to-gemm"
using namespace mlir;
using namespace mlir::arith;
using namespace mlir::rock;
//===----------------------------------------------------------------------===//
// Conv (forward, backward) lowering.
//===----------------------------------------------------------------------===//
// High level convolution operation always have
// [filter, input, output]
// as the convolution argument. The only difference between different
// height level convolution operations is the argument sequence. For
// simplicity, we always arrange the first two arguments to be input
// and the last argument to be output
namespace {
// The ArgumentFields keep track of differences between conv operations
struct ArgumentFields {
int gridwiseGemmArgumentPosition[3];
StringRef gemmTargetCharName[3];
};
struct RockConvToGemmPass
: public rock::impl::RockConvToGemmPassBase<RockConvToGemmPass> {
void runOnOperation() override;
};
template <typename T>
LogicalResult checkNames(ArrayRef<StringRef> actual,
ArrayRef<StringRef> expected, StringRef argName,
T op) {
if (actual.size() != expected.size()) {
return op.emitOpError("Layout mismatch in ")
<< argName << " tensor: Expected " << expected.size()
<< " dimensions but have " << actual.size();
}
for (StringRef name : expected) {
if (llvm::find(actual, name) == actual.end()) {
return op.emitOpError("Layout mismatch in ")
<< argName << " tensor: Expected it to have a `" << name
<< "` dimension";
}
}
return success();
}
// Sort the dimensions in `names` so that they are in the order they appear in
// within `transform`. This allows Merge{} operations to not preform
// transposes that are not needed.
void matchUnderlyingOrder(SmallVectorImpl<StringRef> &names,
BottomUpTMBuilder &transform) {
std::sort(names.begin(), names.end(),
[&transform](const StringRef &v1, const StringRef &v2) -> bool {
return transform.startIndex(v1) < transform.startIndex(v2);
});
}
/// Get the dimension names for the given `op` into `filterNames`, `inputNames`
/// and `outputNames`, returning failure if `op`'s layout doesn't contain all of
/// the expected dimension names.
template <typename T>
LogicalResult getConvDimNames(T op, SmallVectorImpl<StringRef> &filterNames,
SmallVectorImpl<StringRef> &inputNames,
SmallVectorImpl<StringRef> &outputNames) {
auto filterLayoutAttr =
op->template getAttrOfType<ArrayAttr>("filter_layout");
auto inputLayoutAttr = op->template getAttrOfType<ArrayAttr>("input_layout");
auto outputLayoutAttr =
op->template getAttrOfType<ArrayAttr>("output_layout");
unsigned size = filterLayoutAttr.size();
if (size != inputLayoutAttr.size() || size != outputLayoutAttr.size())
return op.emitOpError("All convolution layouts must have the same length");
filterNames.reserve(size);
inputNames.reserve(size);
outputNames.reserve(size);
auto update_old_name = [](StringAttr name) {
auto ctx = name.getContext();
if (name == "y")
return StringAttr::get(ctx, "0");
if (name == "x")
return StringAttr::get(ctx, "1");
if (name.strref().starts_with_insensitive("h")) {
auto namestr = name.str();
return StringAttr::get(ctx, std::string("0") +
namestr.substr(1, namestr.length() - 1));
}
if (name.strref().starts_with_insensitive("w")) {
auto namestr = name.str();
return StringAttr::get(ctx, std::string("1") +
namestr.substr(1, namestr.length() - 1));
}
return name;
};
for (unsigned i = 0; i < size; ++i) {
auto filterAttr = update_old_name(
filterLayoutAttr.getValue()[i].template cast<StringAttr>());
auto inputAttr = update_old_name(
inputLayoutAttr.getValue()[i].template cast<StringAttr>());
auto outputAttr = update_old_name(
outputLayoutAttr.getValue()[i].template cast<StringAttr>());
filterNames.push_back(filterAttr.getValue());
inputNames.push_back(inputAttr.getValue());
outputNames.push_back(outputAttr.getValue());
}
SmallVector<StringRef> filterCheck{"k", "g", "c"};
SmallVector<StringRef> inputCheck{"ni", "gi", "ci"};
SmallVector<StringRef> outputCheck{"no", "go", "ko"};
auto ctx = op.getContext();
for (size_t i = 0; i < filterNames.size() - 3; i++) {
filterCheck.push_back(StringAttr::get(ctx, std::to_string(i)));
inputCheck.push_back(StringAttr::get(ctx, std::to_string(i) + "i"));
outputCheck.push_back(StringAttr::get(ctx, std::to_string(i) + "o"));
}
if (failed(checkNames(filterNames, filterCheck, "filter", op)) ||
failed(checkNames(inputNames, inputCheck, "input", op)) ||
failed(checkNames(outputNames, outputCheck, "output", op))) {
return failure();
}
return success();
}
/// Return the type of v if the underlying convolution has a result, otherwise
/// return null, allowing the lowering here to be, in principle, generic over
/// tensors and memrefs.
Type getResultType(Operation *convOp, Value outArg) {
if (convOp->getNumResults() == 1)
return outArg.getType();
return nullptr;
}
static int64_t getUtilityVectorizationLen(ShapedType shape,
int64_t elemsPerThread) {
int64_t numElems = shape.getNumElements();
constexpr int64_t kMaxVectorOpLen = 4; // words
int64_t elemsPerWord = (32 / shape.getElementTypeBitWidth());
return math_util::gcd(math_util::gcd(numElems, elemsPerThread),
kMaxVectorOpLen * elemsPerWord);
}
/// Create an elementwise utility kernel.
/// The callback has type (builder, location, collapsedBuffers, coordinate).
/// Note: you are expected to handle out of bounds, such as by using
/// rock.buffer_store
template <typename OpType>
LogicalResult createElementwiseLoop(
OpBuilder &b, Location loc, OpType kernelOp, ValueRange memrefs,
int64_t vectorLen,
function_ref<void(OpBuilder &, Location, ValueRange, Value)> emitBodyFunc) {
if (!kernelOp.getBlockSize().has_value())
return kernelOp.emitOpError("block size not defined for utility kernel");
if (!kernelOp.getGridSize().has_value())
return kernelOp.emitOpError("grid size not defined for utility kernel");
if (!kernelOp.getElemsPerThread().has_value())
return kernelOp.emitOpError(
"elemsPerThread not defined fer utility kernel");
uint32_t blockSize = *kernelOp.getBlockSize();
int64_t elemsPerThread = kernelOp.getElemsPerThread()->getSExtValue();
if (elemsPerThread % vectorLen != 0)
return kernelOp.emitOpError("unevenly vectorized elementwise kernel");
Value workgroupId = b.create<WorkgroupIdOp>(loc, b.getIndexType());
Value workgroupDim = b.create<ConstantIndexOp>(loc, blockSize);
Value elemsPerThreadOp = b.create<ConstantIndexOp>(loc, elemsPerThread);
Value workitemId = b.create<WorkitemIdOp>(loc, b.getIndexType());
SmallVector<Value, 2> collapsedBufs;
for (Value memref : memrefs) {
if (!memref.getType().isa<MemRefType>()) {
// TODO: determine if we can relax this if we push bufferization down
return kernelOp.emitOpError(
"arguments to utility kernels must be memrefs");
}
if (auto transform =
dyn_cast_or_null<TransformOp>(memref.getDefiningOp())) {
return kernelOp.emitOpError(
"arguments to utility kernels must be pure memrefs");
}
Value collapsed = createCollapseShapeOp(b, loc, memref);
collapsedBufs.push_back(collapsed);
}
int64_t collapsedLen =
collapsedBufs[0].getType().cast<MemRefType>().getShape()[0];
for (Value c : collapsedBufs)
if (c.getType().cast<MemRefType>().getNumElements() != collapsedLen)
return kernelOp.emitOpError(
"utility kernel arguments have different lengths");
Value offset = b.create<MulIOp>(
loc,
b.create<AddIOp>(loc, b.create<MulIOp>(loc, workgroupId, workgroupDim),
workitemId),
elemsPerThreadOp);
Value zero = b.create<arith::ConstantIndexOp>(loc, 0);
Value vectorLenOp = b.create<arith::ConstantIndexOp>(loc, vectorLen);
auto loop = b.create<scf::ForOp>(loc, zero, elemsPerThreadOp, vectorLenOp);
{
OpBuilder::InsertionGuard guard(b);
b.setInsertionPointToStart(loop.getBody());
Value index = b.create<arith::AddIOp>(loc, offset, loop.getInductionVar());
emitBodyFunc(b, loc, collapsedBufs, index);
}
return success();
}
/// Create a private buffer that can hold type `type`.
static Value makePrivateGpuAlloc(OpBuilder &b, Location loc, Type type) {
Type elemTy = type;
int64_t numElems = 1;
if (auto vecTy = dyn_cast<VectorType>(type)) {
elemTy = vecTy.getElementType();
numElems = vecTy.getNumElements();
}
auto memrefTy =
MemRefType::get(numElems, elemTy, nullptr,
gpu::AddressSpaceAttr::get(type.getContext(),
gpu::AddressSpace::Private));
Value memref = b.create<rock::GpuAllocOp>(loc, memrefTy);
return memref;
}
/// Store `value` into a private memref buffer to make it an acceptable argument
/// to memref.store. Returns the allocated buffer.
static Value makeGpuAllocContaining(OpBuilder &b, Value v) {
Location loc = v.getLoc();
Type type = v.getType();
Value memref = makePrivateGpuAlloc(b, loc, type);
b.create<rock::InBoundsStoreOp>(
loc, v, memref, b.createOrFold<arith::ConstantIndexOp>(loc, 0));
return memref;
}
/// 0-initialize a given buffer.
struct ZeroInitKernelRewritePattern final
: public OpConversionPattern<InitKernelOp> {
using OpConversionPattern<InitKernelOp>::OpConversionPattern;
LogicalResult matchAndRewrite(InitKernelOp op, InitKernelOpAdaptor adaptor,
ConversionPatternRewriter &b) const override {
Location loc = op.getLoc();
TypedValue<ShapedType> buffer = op.getBuffer();
Type bufferType = buffer.getType().getElementType();
if (!op.getElemsPerThread().has_value())
return op->emitOpError("elems per thread not set");
int64_t initVectorLen = getUtilityVectorizationLen(
buffer.getType(), op.getElemsPerThread()->getZExtValue());
Type storeType = vectorTypeOrSelf(bufferType, initVectorLen);
bool needs64BitIdx = is4GBMemoryType(buffer.getType());
Type elementType = mlir::getElementTypeOrSelf(storeType);
Value initOp;
auto initValueAttr = op.getInitValueAttr();
if (initValueAttr) {
if (auto floatInitValueAttr = initValueAttr.value().cast<FloatAttr>()) {
auto initValue = floatInitValueAttr.getValue().convertToFloat();
initOp =
createConstantFloatOp(b, loc, storeType, elementType, initValue);
} else if (auto intInitValueAttr =
initValueAttr.value().cast<IntegerAttr>()) {
auto initValue = intInitValueAttr.getValue().getSExtValue();
initOp = createConstantIntOp(b, loc, storeType, elementType, initValue);
} else {
return failure();
}
} else {
initOp = createZeroConstantOp(b, loc, storeType);
}
Value zeroIndex = b.createOrFold<arith::ConstantIndexOp>(loc, 0);
Value memref = makeGpuAllocContaining(b, initOp);
Value trueOp =
b.createOrFold<arith::ConstantIntOp>(loc, true, b.getI1Type());
GemmFeatures features = op.getFeatures();
auto loopBody = [&memref, &initVectorLen, &trueOp, &features, &zeroIndex,
&needs64BitIdx](OpBuilder &b, Location loc,
ValueRange collapsed, Value index) {
b.create<GlobalStoreOp>(loc, memref, collapsed[0],
APInt(64, initVectorLen), features,
StoreMethod::Set, /*sourceCoord=*/zeroIndex,
/*valid=*/trueOp, index, needs64BitIdx,
/*canStoreOffEnd=*/true);
};
LogicalResult res =
createElementwiseLoop(b, loc, op, buffer, initVectorLen, loopBody);
if (failed(res))
return failure();
b.eraseOp(op);
return success();
}
};
/// Element-wise conversion from the workspace to the output (filter tensor)
/// for a backward weight convolution which uses atomic adds.
struct ConvertingCopyKernelRewritePattern final
: public OpConversionPattern<ConvertingCopyKernelOp> {
using OpConversionPattern<ConvertingCopyKernelOp>::OpConversionPattern;
LogicalResult matchAndRewrite(ConvertingCopyKernelOp op,
ConvertingCopyKernelOpAdaptor adaptor,
ConversionPatternRewriter &b) const override {
Location loc = op.getLoc();
auto input = cast<TypedValue<ShapedType>>(adaptor.getInput());
auto output = cast<TypedValue<ShapedType>>(adaptor.getOutput());
Type inputDataType = input.getType().getElementType();
Type outputDataType = output.getType().getElementType();
if (!op.getElemsPerThread().has_value())
return op->emitOpError("elems per thread not set");
int64_t conversionVectorLen = getUtilityVectorizationLen(
input.getType(), op.getElemsPerThread()->getZExtValue());
Type loadType = vectorTypeOrSelf(inputDataType, conversionVectorLen);
Type storeType = vectorTypeOrSelf(outputDataType, conversionVectorLen);
Value trueOp = b.create<arith::ConstantIntOp>(loc, true, b.getI1Type());
GemmFeatures features = op.getFeatures();
bool needs64BitIdx =
is4GBMemoryType(input.getType()) || is4GBMemoryType(output.getType());
Value storeMemref = makePrivateGpuAlloc(b, loc, storeType);
Value zeroIndex = b.createOrFold<arith::ConstantIndexOp>(loc, 0);
auto loopBody = [&loadType, &storeType, &conversionVectorLen, &trueOp,
&storeMemref, &zeroIndex, &features,
&needs64BitIdx](OpBuilder &b, Location loc,
ValueRange collapsed, Value index) {
Value loaded = b.create<GlobalLoadOp>(
loc, loadType, collapsed[0], /*valid=*/trueOp, index, needs64BitIdx,
/*canReadOffEnd=*/true);
Value converted = createTypeConversionOp(b, loc, loaded, storeType);
b.create<InBoundsStoreOp>(loc, converted, storeMemref, zeroIndex);
b.create<GlobalStoreOp>(
loc, storeMemref, collapsed[1], APInt(64, conversionVectorLen),
features, StoreMethod::Set, /*sourceCoord=*/zeroIndex,
/*valid=*/trueOp, index, needs64BitIdx, /*canWriteOffEnd=*/true);
};
LogicalResult res = createElementwiseLoop(b, loc, op, {input, output},
conversionVectorLen, loopBody);
if (failed(res))
return failure();
b.eraseOp(op);
return success();
}
};
/// Layout normalization.
/// Make the dimensions that are the values in `mapping` and exist within
/// `toLayout` be in the same relative order as the dimensions that the keys of
/// `mapping` have within `fromLayout`, where both layout are given by the
/// names of the attributes containing them.
///
/// To enable usage in rewrite patterns, returns failure() when no change is
/// made.
LogicalResult makeToLayoutLikeFromLayoutAlong(
PatternRewriter &b, RockConvInterface op, StringRef fromLayoutAttrName,
TypedValue<ShapedType> toArg, StringRef toLayoutAttrName,
const llvm::StringMap<StringAttr> &mapping) {
llvm::SmallVector<StringAttr> expectedOrder;
auto fromLayout = op->getAttrOfType<ArrayAttr>(fromLayoutAttrName);
auto toLayout = op->getAttrOfType<ArrayAttr>(toLayoutAttrName);
for (StringRef fromName : fromLayout.getAsValueRange<StringAttr>()) {
auto maybeCorresponding = mapping.find(fromName);
if (maybeCorresponding != mapping.end())
expectedOrder.push_back(maybeCorresponding->getValue());
}
llvm::SmallDenseMap<StringAttr, size_t> toLayoutIdxs;
for (auto pair : llvm::enumerate(toLayout.getAsRange<StringAttr>()))
toLayoutIdxs.insert({pair.value(), pair.index()});
bool inOrder = true;
size_t prevIndex = 0;
for (StringAttr expected : expectedOrder) {
size_t thisIndex = toLayoutIdxs.find(expected)->getSecond();
if (thisIndex <
prevIndex) { // the values are not in the relative expected order
inOrder = false;
break;
}
prevIndex = thisIndex;
}
if (inOrder)
return failure();
/// And now we have to actually do the thing
// Is just an attribute to allow array builder
SmallVector<Attribute> newToLayout;
llvm::SmallDenseSet<StringAttr> permutedDimsSet;
for (StringAttr toPermute : expectedOrder)
permutedDimsSet.insert(toPermute);
SmallVector<StringAttr>::const_iterator expectedOrderIter =
expectedOrder.begin();
for (StringAttr dim : toLayout.getAsRange<StringAttr>()) {
if (permutedDimsSet.contains(dim)) {
newToLayout.push_back(*expectedOrderIter);
++expectedOrderIter;
} else {
newToLayout.push_back(dim);
}
}
SmallVector<StringRef> oldToLayoutRefs;
llvm::copy(toLayout.getAsValueRange<StringAttr>(),
std::back_inserter(oldToLayoutRefs));
ArrayRef<int64_t> toShape = toArg.getType().getShape();
BottomUpTMBuilder relayout(b, oldToLayoutRefs, toShape, op.getLoc());
llvm::StringMap<uint32_t> newToLayoutIdxs;
for (auto pair : llvm::enumerate(newToLayout)) {
StringRef value = pair.value().cast<StringAttr>().getValue();
newToLayoutIdxs.insert({value, pair.index()});
}
BottomUpTMTopDimsWrapper relayoutWrapped(relayout,
std::move(newToLayoutIdxs));
relayoutWrapped.passThrough(oldToLayoutRefs);
TransformMapAttr relayoutAttr = relayout.get();
Value transformed = b.create<TransformOp>(op.getLoc(), toArg, relayoutAttr);
for (OpOperand &operand : op->getOpOperands())
if (operand.get() == toArg)
operand.set(transformed);
op->setAttr(toLayoutAttrName, b.getArrayAttr(newToLayout));
return success();
}
struct MatchLayoutsToInput final
: public OpInterfaceRewritePattern<RockConvInterface> {
using OpInterfaceRewritePattern<RockConvInterface>::OpInterfaceRewritePattern;
LogicalResult matchAndRewrite(RockConvInterface op,
PatternRewriter &b) const override {
TypedValue<ShapedType> filter = op.getFilter(), output = op.getOutput();
llvm::StringMap<StringAttr> inputToFilter = {{"ci", b.getStringAttr("c")},
{"hi", b.getStringAttr("y")},
{"wi", b.getStringAttr("x")}};
llvm::StringMap<StringAttr> inputToOutput = {{"ni", b.getStringAttr("no")},
{"hi", b.getStringAttr("ho")},
{"wi", b.getStringAttr("wo")}};
for (auto i = 0; i < filter.getType().getRank() - 3; i++) {
auto key = b.getStringAttr(Twine(i) + Twine("i"));
inputToFilter.insert_or_assign(key, b.getStringAttr(Twine(i)));
inputToOutput.insert_or_assign(key,
b.getStringAttr(Twine(i) + Twine("o")));
}
LogicalResult didReLayoutFilter = makeToLayoutLikeFromLayoutAlong(
b, op, "input_layout", filter, "filter_layout", inputToFilter);
LogicalResult didReLayoutOutput = makeToLayoutLikeFromLayoutAlong(
b, op, "input_layout", output, "output_layout", inputToOutput);
return success(didReLayoutFilter.succeeded() ||
didReLayoutOutput.succeeded());
}
};
/// Lowerings for particular convolution algorithms (TODO, new file?)
LogicalResult backwardWeightAtomicAdd(ConvBwdWeightOp op, PatternRewriter &b) {
Location loc = op.getLoc();
Attribute tuningParams = op.getParamsAttr();
if (!tuningParams) {
return op.emitOpError("can't lower without tuning parameters\n");
}
if (!op.getKBlocks().has_value())
return op.emitOpError("must have kBlocks set at lowering");
int64_t gemmKBlocks = op.getKBlocks()->getZExtValue();
ConvolutionContext ctx = populateConvContext(op);
// Get shape of filter tensor.
ShapedType filterType = op.getFilter().getType();
auto filterShape = filterType.getShape();
GemmFeatures features = op.getFeatures();
bool isAccel = rock::isAccel(features);
// Determine whether to use workspace.
bool hasWorkspace =
(filterType.getElementType() == b.getF16Type() && isAccel);
if (hasWorkspace && !op.getWorkspace()) {
return op.emitOpError(
"workspace needed for f16 atomic add but none provided");
}
// The 1st kernel will conduct the actual backward weight convolution using
// atomic adds.
if (!isAccel)
return op->emitOpError("atomic add kernel requires gemm acceleration");
// Get shape of input tensor.
ShapedType inputType = op.getInput().getType();
ArrayRef<int64_t> inputShape = inputType.getShape();
// Get shape of output tensor.
ShapedType outputType = op.getOutput().getType();
ArrayRef<int64_t> outputShape = outputType.getShape();
// Obtain convolution parameters: padding / dilation / stride.
auto pads = ctx.getPaddingVal();
auto dilations = ctx.getDilationVal();
auto strides = ctx.getStrideVal();
ConvolutionDims convDims = ctx.getConvDims();
llvm::SmallVector<StringRef, 5> filterNames, inputNames, outputNames;
if (failed(getConvDimNames(op, filterNames, inputNames, outputNames))) {
return failure();
}
Value gemmFilter, gemmInput, gemmOutput;
// Transform filter tensor.
{
SmallVector<StringRef, 5> nonKDims;
for (StringRef name : filterNames)
if (name != "g" && name != "k")
nonKDims.push_back(name);
// Add a dimension, that'll be ignored when writing the output, for KBlock
// The existence of this dimension makes the mapping between the C matrix
// and the filter tensor uninvertable, hence the need for atomic add
llvm::StringMap<uint32_t> kBlockDims =
expandNamesInPlace(filterNames, {{{"k", {"kBlock", "k"}}}});
BottomUpTMBuilder addKBlockTransform(b, filterNames, filterShape, loc);
BottomUpTMTopDimsWrapper addKBlockWrap(addKBlockTransform,
std::move(kBlockDims));
addKBlockWrap.passThrough("g");
addKBlockWrap.addDim("kBlock", gemmKBlocks);
SmallVector<StringRef, 5> throughDims{"k", "c"};
for (size_t i = 0; i < convDims.fil.size(); i++)
throughDims.push_back(b.getStringAttr(Twine(i)));
addKBlockWrap.passThrough(throughDims);
TransformMapAttr addKBlockTransformAttr = addKBlockTransform.get();
Value filterTensorInUse =
(hasWorkspace) ? op.getWorkspace() : op.getFilter();
Value withKBlock = b.create<rock::TransformOp>(loc, filterTensorInUse,
addKBlockTransformAttr);
// Create GEMM filter tensor
// Here, we merge the KBlock dimension into the G dimension
// keeping the kBlock dimension as the minor index
// and send K to the M dimension and CYX to the N dimension as usual
auto gemmTransform =
BottomUpTMBuilder::above(addKBlockTransform, addKBlockTransformAttr);
gemmTransform.merge("gemmG", 0, {"g", "kBlock"});
gemmTransform.passThrough({"gemmM"}, {1}, {"k"});
gemmTransform.merge("gemmN", 2, nonKDims);
TransformMapAttr gemmTransformAttr = gemmTransform.get();
gemmFilter = b.create<TransformOp>(loc, withKBlock, gemmTransformAttr);
// This kernel is only invoked when there's no need for gemm padding
}
// Transform input tensor
{
// Pad H and W and split N into n0 and n1 where n0 has size kBlocks and n1
// is what's left
llvm::StringMap<SmallVector<StringRef, 2>> expansions;
expansions.insert({"ni", {"n0", "n1"}});
for (int i = 0; i < 2; i++) {
StringAttr key = b.getStringAttr(Twine(i) + "i");
StringAttr val = b.getStringAttr(Twine(i) + "ipad");
expansions.insert({key, {val}});
}
llvm::StringMap<uint32_t> firstTransformOutDims =
expandNamesInPlace(inputNames, expansions);
BottomUpTMBuilder firstTransform(b, inputNames, inputShape, loc);
BottomUpTMTopDimsWrapper firstWrap(firstTransform,
std::move(firstTransformOutDims));
firstWrap.passThrough("gi");
firstWrap.unmerge({"n0", "n1"}, "ni",
{gemmKBlocks, convDims.n / gemmKBlocks});
firstWrap.passThrough("ci");
SmallVector<StringRef, 3> outs;
SmallVector<StringRef, 3> ins;
for (size_t i = 0; i < convDims.in.size(); i++) {
outs.push_back(b.getStringAttr(Twine(i) + "ipad"));
ins.push_back(b.getStringAttr(Twine(i) + "i"));
}
firstWrap.pad(outs, ins, pads);
TransformMapAttr firstTransformAttr = firstTransform.get();
Value firstTransformed =
b.create<TransformOp>(loc, op.getInput(), firstTransformAttr);
// The usual mapping of input space to dimensions such that filter elements
// get multiplied by the right thing
expansions.clear();
for (size_t i = 0; i < convDims.out.size(); i++) {
StringAttr key = b.getStringAttr(Twine(i) + "ipad");
StringAttr val1 = b.getStringAttr(Twine(i));
StringAttr val2 = b.getStringAttr(Twine(i) + "o");
expansions.insert({key, {val1, val2}});
}
llvm::StringMap<uint32_t> embedOutDims =
expandNamesInPlace(firstTransform, expansions);
auto embedTransform =
BottomUpTMBuilder::above(firstTransform, firstTransformAttr);
BottomUpTMTopDimsWrapper embedWrap(embedTransform, std::move(embedOutDims));
embedWrap.passThrough({"gi", "n0", "n1", "ci"});
assert(convDims.fil.size() == convDims.out.size());
for (size_t i = 0; i < convDims.fil.size(); i++) {
StringAttr val1 = b.getStringAttr(Twine(i));
StringAttr val2 = b.getStringAttr(Twine(i) + "o");
StringAttr val3 = b.getStringAttr(Twine(i) + "ipad");
embedWrap.embed({val1, val2}, {convDims.fil[i], convDims.out[i]}, val3,
{dilations[i], strides[i]});
}
TransformMapAttr embedTransformAttr = embedTransform.get();
Value embedded =
b.create<TransformOp>(loc, firstTransformed, embedTransformAttr);
// Merge N1HoWO to gemmK and CYX to gemmN
auto gemmInputTransform =
BottomUpTMBuilder::above(embedTransform, embedTransformAttr);
llvm::SmallVector<StringRef, 3> nonNHWDims = {"ci"};
for (size_t i = 0; i < convDims.in.size(); i++)
nonNHWDims.push_back(b.getStringAttr(Twine(i)));
matchUnderlyingOrder(nonNHWDims, gemmInputTransform);
llvm::SmallVector<StringRef, 3> nhwDims = {"n1"};
for (size_t i = 0; i < convDims.out.size(); i++)
nhwDims.push_back(b.getStringAttr(Twine(i) + "o"));
matchUnderlyingOrder(nhwDims, gemmInputTransform);
// In the gemmG dimension, unlike with gemmN, we don't have the same
// traversal order concerns - a step in the G dimension always first visits
// kBlock/N0 and then moves on to the next G
gemmInputTransform.merge("gemmG", 0, {"gi", "n0"});
gemmInputTransform.merge("gemmK", 1, nhwDims);
gemmInputTransform.merge("gemmN", 2, nonNHWDims);
TransformMapAttr gemmInputTransformAttr = gemmInputTransform.get();
gemmInput = b.create<TransformOp>(loc, embedded, gemmInputTransformAttr);
}
// Transform output tensor
{
// First, split the N dimension as in the input
llvm::StringMap<uint32_t> outDims =
expandNamesInPlace(outputNames, {{"no", {"n0", "n1"}}});
BottomUpTMBuilder firstTransform(b, outputNames, outputShape, loc);
BottomUpTMTopDimsWrapper firstWrap(firstTransform, std::move(outDims));
firstWrap.passThrough("go");
firstWrap.unmerge({"n0", "n1"}, "no",
{gemmKBlocks, convDims.n / gemmKBlocks});
SmallVector<StringRef, 3> names{"ko"};
for (size_t i = 0; i < convDims.out.size(); i++)
names.push_back(b.getStringAttr(Twine(i) + "o"));
firstWrap.passThrough(names);
TransformMapAttr firstTransformAttr = firstTransform.get();
Value transformed =
b.create<TransformOp>(loc, op.getOutput(), firstTransformAttr);
// Map G and N0 to gemmG, N1HW to gemmK and K to gemmM
auto gemmOutputTransform =
BottomUpTMBuilder::above(firstTransform, firstTransformAttr);
llvm::SmallVector<StringRef, 3> nhwDims = {"n1"};
for (size_t i = 0; i < convDims.out.size(); i++)
nhwDims.push_back(b.getStringAttr(Twine(i) + "o"));
matchUnderlyingOrder(nhwDims, gemmOutputTransform);
gemmOutputTransform.merge("gemmG", 0, {"go", "n0"});
gemmOutputTransform.merge("gemmK", 1, nhwDims);
gemmOutputTransform.passThrough({"gemmM"}, {2}, {"ko"});
TransformMapAttr gemmOutputTransformAttr = gemmOutputTransform.get();
gemmOutput =
b.create<TransformOp>(loc, transformed, gemmOutputTransformAttr);
}
// This kernel is not run when there is padding on the GEMM
auto storeMethod = b.getAttr<StoreMethodAttr>(StoreMethod::AtomicAdd);
b.create<GemmOp>(
loc, getResultType(op, gemmFilter), gemmOutput, gemmInput, gemmFilter,
/*aTransposed=*/b.getUnitAttr(), /*bTransposed=*/nullptr,
/*cTransposed=*/nullptr, op.getArchAttr(), op.getNumCUAttr(),
op.getFeaturesAttr(), storeMethod, op.getDerivedBlockSizeAttr(),
op.getGridSizeAttr(), op.getParamsAttr());
// Finally, erase the original Conv op.
b.eraseOp(op);
return success();
}
LogicalResult backwardData(ConvBwdDataOp op, PatternRewriter &b) {
Location loc = op.getLoc();
IntegerAttr kernelIdAttr = op.getKernelIdAttr();
ConvolutionContext ctx = populateConvContext(op);
// Get shape of filter tensor.
ShapedType filterType = op.getFilter().getType();
ArrayRef<int64_t> filterShape = filterType.getShape();
// Get shape of input tensor.
ShapedType inputType = op.getInput().getType();
ArrayRef<int64_t> inputShape = inputType.getShape();
// Get shape of output tensor.
ShapedType outputType = op.getOutput().getType();
ArrayRef<int64_t> outputShape = outputType.getShape();
// Obtain convolution parameters: padding / dilation / stride.
auto pads = ctx.getPaddingVal();
auto dilations = ctx.getDilationVal();
auto strides = ctx.getStrideVal();
ConvolutionDims convDims = ctx.getConvDims();
SmallVector<StringRef, 5> filterNames, inputNames, outputNames;
if (failed(getConvDimNames(op, filterNames, inputNames, outputNames))) {
return failure();
}
SmallVector<int64_t, 5> gcdStrideDilations;
assert(strides.size() == dilations.size());
for (const auto &[stride, dilation] : zip(strides, dilations)) {
gcdStrideDilations.push_back(math_util::gcd(stride, dilation));
}
SmallVector<int64_t, 5> filTilda;
for (const auto &[stride, gcdSD] : zip(strides, gcdStrideDilations)) {
filTilda.push_back(stride / gcdSD);
}
SmallVector<int64_t, 5> filDots;
for (const auto &[fil, tilda] : zip(convDims.fil, filTilda)) {
filDots.push_back(math_util::integer_divide_ceil(fil, tilda));
}
SmallVector<int64_t, 5> outTilda;
for (const auto &[out, dilation, fil, stride] :
zip(convDims.out, dilations, convDims.fil, strides)) {
outTilda.push_back(
out + math_util::integer_divide_ceil(dilation * (fil - 1), stride));
}
SmallVector<int64_t, 5> iTildaLeft;
SmallVector<int64_t, 5> iTildaRight;
for (const auto &[padindex, dilation, tilda, stride] :
enumerate(dilations, filTilda, strides)) {
iTildaLeft.push_back(math_util::integer_divide_floor(
std::max((int64_t)0, pads[2 * padindex] - dilation * (tilda - 1)),
stride));
}
for (const auto &[padindex, out, in, stride] :
enumerate(outTilda, convDims.in, strides)) {
iTildaRight.push_back(std::min(
out,
math_util::integer_divide_ceil(pads[2 * padindex] + in - 1, stride) +
1));
}
// i2tilda = kernelid % filtilda[2]
// i1tilda = (kernelid % (filtilda[2] * filtilda[1])) / filtilda[2]
// i0tilda = kernelid / (filtilda[2] * filtilda[1])
// get-backward-kernel-count or similar
int64_t kernelId = kernelIdAttr.getInt();
SmallVector<int64_t, 3> iTilda;
SmallVector<int64_t, 3> iDotSlice;
int64_t product = 1;
for (size_t i = 1; i < convDims.fil.size(); i++)
product *= filTilda[i];
int64_t divisor = 1;
iTilda.resize(convDims.fil.size());
switch (convDims.fil.size()) {
default:
llvm_unreachable("Only 2-D and 3-D have been implemented.");
break;
case 3:
divisor = filTilda[2];
iTilda[2] = kernelId % divisor;
[[fallthrough]];
case 2:
iTilda[1] = (kernelId % product) / divisor;
iTilda[0] = kernelId / product;
}
for (size_t i = 0; i < convDims.fil.size(); i++)
iDotSlice.push_back(math_util::integer_divide_ceil(
convDims.fil[i] - iTilda[i], filTilda[i]));
// backward data only, it's igemm v4r1 algo
// c is input channels , k is output channels
// n is batch , yDotSlice,xDotSlice computed in above
Value gemmFilter, gemmInput, gemmOutput;
// Transform filter tensor.
{
// Embed y/x into {y/x}dot and {y/x}tilda (Why the
// particular embed coefficients is in a presentation somewhere)
llvm::StringMap<SmallVector<StringRef, 2>> expansions;
for (size_t i = 0; i < convDims.fil.size(); i++) {
StringAttr key = b.getStringAttr(Twine(i));
StringAttr val1 = b.getStringAttr(Twine(i) + "dot");
StringAttr val2 = b.getStringAttr(Twine(i) + "tilda");
expansions.insert({key, {val1, val2}});
}
llvm::StringMap<uint32_t> embedDims =
expandNamesInPlace(filterNames, expansions);
BottomUpTMBuilder embedTransform(b, filterNames, filterShape, loc);
BottomUpTMTopDimsWrapper embedWrap(embedTransform, std::move(embedDims));
// array of smallstring?
embedWrap.passThrough({"g", "k", "c"});
for (size_t i = 0; i < convDims.fil.size(); i++) {
StringAttr upper1 = b.getStringAttr(Twine(i) + "dot");
StringAttr upper2 = b.getStringAttr(Twine(i) + "tilda");
StringAttr lower = b.getStringAttr(Twine(i));
embedWrap.embed({upper1, upper2}, {filDots[i], filTilda[i]}, lower,
{strides[i] / gcdStrideDilations[i], 1});
}
TransformMapAttr embedTransformAttr = embedTransform.get();
Value embeddedFilter =
b.create<TransformOp>(loc, op.getFilter(), embedTransformAttr);
// Take slices in the ydot, ytilda, xdot, and xtilda dimensions
// to reflect which kernel we're performing
auto sliceTransform =
BottomUpTMBuilder::above(embedTransform, embedTransformAttr);
sliceTransform.passThrough({"g", "k", "c"});
llvm::SmallVector<StringRef, 2> uppers;
llvm::SmallVector<StringRef, 2> lowers;
for (size_t i = 0; i < convDims.in.size(); i++) {
uppers.push_back(b.getStringAttr(Twine(i) + "dotslice"));
lowers.push_back(b.getStringAttr(Twine(i) + "dot"));
}
sliceTransform.slice(uppers, lowers, {0, 0}, iDotSlice);
uppers.clear();
lowers.clear();
for (size_t i = 0; i < convDims.fil.size(); i++) {
uppers.push_back(b.getStringAttr(Twine(i) + "tildaslice"));
lowers.push_back(b.getStringAttr(Twine(i) + "tilda"));
}
llvm::SmallVector<int64_t, 3> iTildasPlusOne;
for (size_t i = 0; i < convDims.fil.size(); i++)
iTildasPlusOne.push_back(iTilda[i] + 1);
sliceTransform.slice(uppers, lowers, iTilda, iTildasPlusOne);
TransformMapAttr sliceTransformAttr = sliceTransform.get();
Value slicedFilter =
b.create<TransformOp>(loc, embeddedFilter, sliceTransformAttr);
// Set up gemm by passing g -> gemmG, merging
// [k, ydotslice, xdotslice] to gemmK, and [c, ytildaslice, xtildaslice]
// to gemmM
auto gemmFilterTransform =
BottomUpTMBuilder::above(sliceTransform, sliceTransformAttr);
gemmFilterTransform.passThrough({"gemmG"}, {0}, {"g"});
lowers.clear();
lowers.push_back("k");
for (size_t i = 0; i < convDims.fil.size(); i++)
lowers.push_back(b.getStringAttr(Twine(i) + "dotslice"));
gemmFilterTransform.merge("gemmK", 1, lowers);
lowers.clear();
lowers.push_back("c");
for (size_t i = 0; i < convDims.fil.size(); i++)
lowers.push_back(b.getStringAttr(Twine(i) + "tildaslice"));
gemmFilterTransform.merge("gemmM", 2, lowers);
TransformMapAttr gemmFilterTransformAttr = gemmFilterTransform.get();
gemmFilter =
b.create<TransformOp>(loc, slicedFilter, gemmFilterTransformAttr);
}
// Transform input tensor
{
BottomUpTMBuilder padInputTransform(b, inputNames, inputShape, loc);
padInputTransform.passThrough({"gi", "ni", "ci"});
llvm::SmallVector<uint32_t, 2> padDims;
llvm::SmallVector<StringRef, 2> outs;
llvm::SmallVector<StringRef, 2> ins;
for (size_t i = 0; i < convDims.in.size(); i++) {
padDims.push_back(padInputTransform.startIndex(std::to_string(i) + "i"));
outs.push_back(b.getStringAttr(Twine(i) + "ipad"));
ins.push_back(b.getStringAttr(Twine(i) + "i"));
}
padInputTransform.pad(outs, padDims, ins, pads);
TransformMapAttr padTransformAttr = padInputTransform.get();
Value paddedInput =
b.create<TransformOp>(loc, op.getInput(), padTransformAttr);
// Split 0ipad, 1ipad into 0ftilda, 0itilda, 1ftilda, 1itilda
llvm::StringMap<SmallVector<StringRef, 2>> expansions;
for (size_t i = 0; i < convDims.in.size(); i++) {
StringAttr key = b.getStringAttr(Twine(i) + "ipad");
StringAttr val1 = b.getStringAttr(Twine(i) + "ftilda");
StringAttr val2 = b.getStringAttr(Twine(i) + "itilda");
expansions.insert({key, {val1, val2}});
}
llvm::StringMap<uint32_t> embedDims =
expandNamesInPlace(padInputTransform, expansions);
auto tildaEmbedTransform =
BottomUpTMBuilder::above(padInputTransform, padTransformAttr);
BottomUpTMTopDimsWrapper tildaEmbedWrap(tildaEmbedTransform,
std::move(embedDims));
tildaEmbedWrap.passThrough({"gi", "ni", "ci"});
for (size_t i = 0; i < convDims.fil.size(); i++) {
StringAttr upper1 = b.getStringAttr(Twine(i) + "ftilda");
StringAttr upper2 = b.getStringAttr(Twine(i) + "itilda");
StringAttr lower = b.getStringAttr(Twine(i) + "ipad");
tildaEmbedWrap.embed({upper1, upper2}, {filTilda[i], outTilda[i]}, lower,
{dilations[i], strides[i]});
}
TransformMapAttr tildaEmbedTransformAttr = tildaEmbedTransform.get();
Value tildaEmbedded =
b.create<TransformOp>(loc, paddedInput, tildaEmbedTransformAttr);
// Slice all the tilda dimensions: ytilda and xtilda get slices of length
// 1 while htilda and wtilda have slice indices computed above
auto sliceTransform =
BottomUpTMBuilder::above(tildaEmbedTransform, tildaEmbedTransformAttr);
sliceTransform.passThrough({"gi", "ni", "ci"});
llvm::SmallVector<StringRef, 2> uppers;
llvm::SmallVector<StringRef, 2> lowers;
for (size_t i = 0; i < convDims.in.size(); i++) {
uppers.push_back(b.getStringAttr(Twine(i) + "slice"));
lowers.push_back(b.getStringAttr(Twine(i) + "ftilda"));
}
llvm::SmallVector<int64_t, 3> iTildasPlusOne;
for (size_t i = 0; i < convDims.fil.size(); i++)
iTildasPlusOne.push_back(iTilda[i] + 1);