-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathtest_reduction.cpp
More file actions
3230 lines (2535 loc) · 95.9 KB
/
test_reduction.cpp
File metadata and controls
3230 lines (2535 loc) · 95.9 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
// clang-format off
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-present NVIDIA CORPORATION & AFFILIATES.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
// clang-format on
#include <algorithm>
#include <iostream>
// fuser and IR parser
#include <ATen/cuda/CUDAContext.h>
#include <ATen/cuda/Exceptions.h>
#include <gtest/gtest.h>
#include <c10/cuda/CUDAStream.h>
#include "codegen.h"
#include "device_lower/lower2device.h"
#include "disjoint_set.h"
#include "exceptions.h"
#include "expr_evaluator.h"
#include "fusion.h"
#include "fusion_segmenter.h"
#include "grouped_reduction.h"
#include "ir/all_nodes.h"
#include "ir/builder.h"
#include "ir/graphviz.h"
#include "ir/iostream.h"
#include "ir/utils.h"
#include "iter_visitor.h"
#include "kernel_ir.h"
#include "logical_domain_map.h"
#include "ops/all_ops.h"
#include "runtime/executor.h"
#include "runtime/executor_params.h"
#include "runtime/fusion_executor_cache.h"
#include "scheduler/all_schedulers.h"
#include "scheduler/reduction_outer_tma.h"
#include "scheduler/reduction_tma.h"
#include "scheduler/reduction_utils.h"
#include "scheduler/tools/inlining.h"
#include "scheduler/utils.h"
#include "tests/cpp/utils.h"
#include "transform_replay.h"
#include "transform_rfactor.h"
#include "validator_utils.h"
namespace nvfuser {
using namespace at::indexing;
namespace {
void validateNoParallelBroadcastExist(kir::Kernel* kernel) {
for (auto expr : KernelExprVisitor::getAllExprs(kernel)) {
BroadcastOp* bc = dynamic_cast<BroadcastOp*>(expr);
if (bc == nullptr) {
auto grid_bc = dynamic_cast<kir::GridBroadcast*>(expr);
if (grid_bc != nullptr) {
std::cerr << "Grid broadcast: " << grid_bc->toString();
bc = grid_bc->broadcast_op();
}
}
if (bc == nullptr) {
continue;
}
NVF_CHECK(
kernel->summary().broadcast_parallel_types.at(bc).none(),
"Parallel broadcast should not exist but was found: ",
bc->toString());
}
}
} // namespace
using ReductionTest = NVFuserTest;
TEST_F(ReductionTest, GridAllreduce1) {
const int nx = 999;
const int tidx = 128;
const int bidx = 4;
if (ceilDiv(nx, tidx) > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(1);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {0});
auto tv2 = broadcast(tv1, {true});
auto tv3 = add(tv0, tv2);
fusion.addOutput(tv3);
tv3->split(0, tidx);
tv3->split(0, bidx);
tv3->split(0, 1); // unswitch
TransformPropagator propagator(tv3);
MaxLogicalDomainInfoSpanningTree(tv3).traverse(&propagator);
tv3->axis(0)->parallelize(ParallelType::BIDy);
tv3->axis(2)->parallelize(ParallelType::BIDx);
tv3->axis(3)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv3);
// Just to make sure fused_reduction and work buffers are allocated
// uniquely
tv1->axis(1)->parallelize(ParallelType::Unswitch);
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn({nx}, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto cg_outputs = ke.run({t0});
auto ref = sum(t0).unsqueeze(0) + t0;
testValidate(&fusion, cg_outputs, {t0}, {ref}, __LINE__, __FILE__);
}
TEST_F(ReductionTest, GridAllreduce2) {
const int nx = 99;
const int tidx = 32;
if (ceilDiv(nx, tidx) > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(1);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {0});
auto tv2 = broadcast(tv1, {true});
auto tv3 = add(tv0, tv2);
fusion.addOutput(tv3);
tv3->split(0, tidx);
TransformPropagator propagator(tv3);
MaxLogicalDomainInfoSpanningTree(tv3).traverse(&propagator);
tv3->axis(0)->parallelize(ParallelType::BIDx);
tv3->axis(1)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv3, {tv2});
// Broadcast on TIDy instead of TIDx. This still uses the fused
// reduction as it's broadcast on BIDx as well. Since TIDy is not
// predicated, the broadcast becomes a set op.
tv1->axis(0)->parallelize(ParallelType::BIDx);
tv1->axis(1)->parallelize(ParallelType::TIDy);
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn({nx}, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto cg_outputs = ke.run({t0});
auto ref = sum(t0).unsqueeze(0) + t0;
testValidate(&fusion, cg_outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Grid reduction with serial non-reduction axis. The global work
// buffer is circular buffered.
TEST_F(ReductionTest, GridAllreduce3) {
const int nx = 100;
const int ny = 5000;
const int tidx = 128;
if (ceilDiv(ny, tidx) > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {1});
auto tv2 = broadcast(tv1, {false, true});
auto tv3 = add(tv0, tv2);
fusion.addOutput(tv3);
tv3->split(1, tidx);
TransformPropagator propagator(tv3);
MaxLogicalDomainInfoSpanningTree(tv3).traverse(&propagator);
tv0->computeAt(tv3, 1);
tv3->axis(1)->parallelize(ParallelType::BIDx);
tv3->axis(2)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv3);
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn({nx, ny}, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto cg_outputs = ke.run({t0});
auto ref = sum(t0, {1}).unsqueeze(-1) + t0;
testValidate(&fusion, cg_outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Indirect reduction and broadcast
TEST_F(ReductionTest, GridAllreduce4) {
const int nx = 999;
const int tidx = 128;
if (ceilDiv(nx, tidx) > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(1);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {0});
auto tv2 = add(tv1, IrBuilder::create<Val>(1.0));
auto tv3 = broadcast(tv2, {true});
auto tv4 = add(tv0, tv3);
fusion.addOutput(tv4);
tv4->split(0, tidx);
TransformPropagator propagator(tv4);
MaxLogicalDomainInfoSpanningTree(tv4).traverse(&propagator);
tv4->axis(0)->parallelize(ParallelType::BIDx);
tv4->axis(1)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv4);
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn({nx}, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto cg_outputs = ke.run({t0});
auto ref = (sum(t0) + 1).unsqueeze(0) + t0;
testValidate(&fusion, cg_outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Unused block dimension in the kernel
TEST_F(ReductionTest, GridAllreduce5) {
const int nx = 999;
const int tidx = 128;
const int iter = 2;
const int bdimx = 9; // One more than required by the reduction
const int bdimy = 3; // Want an unused dimension
// Going to bump the bdimx count for this test, ignor
if (bdimx * bdimy > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
Fusion fusion;
FusionGuard fg(&fusion);
// Didn't setup this test with inlining for register usage, so just leave the
// iter dimension concrete
auto tv0 = makeConcreteTensor({iter, -1});
fusion.addInput(tv0);
auto tv1 = sum(tv0, {1});
auto tv2 = add(tv1, IrBuilder::create<Val>(1.0));
auto tv3 = broadcast(tv2, {false, true});
auto tv4 = add(tv0, tv3);
fusion.addOutput(tv4);
// Dummy op to mess with parallelization
auto tv5 = makeSymbolicTensor(2);
fusion.addInput(tv5);
auto tv6 = set(tv5);
fusion.addOutput(tv6);
// Setup the reduction
tv4->split(1, tidx);
TransformPropagator propagator(tv4);
MaxLogicalDomainInfoSpanningTree(tv4).traverse(&propagator);
tv4->axis(1)->parallelize(ParallelType::BIDx);
tv4->axis(2)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv4);
tv6->axis(0)->parallelize(ParallelType::BIDy);
tv6->axis(1)->parallelize(ParallelType::BIDx);
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn({iter, nx}, options);
auto t5 = at::randn({bdimy, bdimx}, options);
KernelExecutor ke;
ke.compile(&fusion, {t0, t5});
auto cg_outputs = ke.run({t0, t5});
auto ref = (sum(t0, {1}) + 1).unsqueeze(-1) + t0;
testValidate(&fusion, cg_outputs, {t0, t5}, {ref, t5}, __LINE__, __FILE__);
}
TEST_F(ReductionTest, GridAllreduce6) {
Fusion fusion;
FusionGuard fg(&fusion);
std::vector<int64_t> shape({99, 200});
const int vec = 4;
const int tidx = 32;
const int tidy = 8;
const int bdimx = ceilDiv(shape[1], vec * tidx);
const int bdimy = ceilDiv(shape[0], tidy);
if (bdimx * bdimy > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
auto tv0 = makeContigTensor(2);
fusion.addInput(tv0);
auto tv1 = set(tv0);
auto tv2 = sum(tv1, {0});
auto tv3 = broadcast(tv2, {true, false});
auto tv4 = add(tv0, tv3);
fusion.addOutput(tv4);
tv1->split(1, vec);
tv1->split(1, tidx);
tv1->split(0, tidy);
TransformPropagator propagator(tv1);
MaxLogicalDomainInfoSpanningTree(tv1).traverse(&propagator);
tv1->axis(0)->parallelize(ParallelType::BIDy);
tv1->axis(1)->parallelize(ParallelType::TIDy);
tv1->axis(2)->parallelize(ParallelType::BIDx);
tv1->axis(3)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv1);
tv1->axis(4)->parallelize(ParallelType::Vectorize);
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});
auto t0_double = t0.to(at::kDouble);
auto ref = t0_double + t0_double.sum({0}).unsqueeze(0);
testValidate(
ke.compiledKernel()->kernel(), outputs, {t0}, {ref}, __LINE__, __FILE__);
}
TEST_F(ReductionTest, GridAllreduceWelford1) {
const int nx = 999;
const int tidx = 128;
if (ceilDiv(nx, tidx) > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(1);
fusion.addInput(tv0);
auto tvs = Welford(tv0, {0});
auto tv2 = broadcast(tvs.avg, {true});
auto tv3 = broadcast(tvs.var_sum, {true});
auto tv4 = add(tv0, tv2);
auto tv5 = add(tv4, tv3);
fusion.addOutput(tv5);
tv5->split(0, tidx);
TransformPropagator propagator(tv5);
MaxLogicalDomainInfoSpanningTree(tv5).traverse(&propagator);
tv5->axis(0)->parallelize(ParallelType::BIDx);
tv5->axis(1)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv5);
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn({nx}, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto cg_outputs = ke.run({t0});
auto ref =
(t0.mean({0}).unsqueeze(0) + t0) + t0.var({0}, false).unsqueeze(0) * nx;
testValidate(&fusion, cg_outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Grid welford reduction with serial non-reduction axis. The global
// work buffer is circular buffered.
TEST_F(ReductionTest, GridAllreduceWelford2) {
const int nx = 100;
const int ny = 5000;
const int tidx = 128;
if (ceilDiv(ny, tidx) > deviceSMCount()) {
GTEST_SKIP() << "Not enough SMs to run this test";
}
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tvs = Welford(tv0, {1});
auto tv2 = broadcast(tvs.avg, {false, true});
auto tv3 = add(tv0, tv2);
fusion.addOutput(tv3);
tv3->split(1, tidx);
TransformPropagator propagator(tv3);
MaxLogicalDomainInfoSpanningTree(tv3).traverse(&propagator);
tv0->computeAt(tv3, 1);
tv3->axis(1)->parallelize(ParallelType::BIDx);
tv3->axis(2)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv3);
// There must be no parallel broadcast
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn({nx, ny}, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto cg_outputs = ke.run({t0});
auto ref = (sum(t0, {1}) / ny).unsqueeze(-1) + t0;
testValidate(&fusion, cg_outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Persistent batchnorm. Uses the fused reduction for grid welford and
// broadcast.
TEST_F(ReductionTest, FusedReductionBatchnorm) {
const std::vector<int64_t> input_shape{256, 2048, 14, 14};
std::unique_ptr<Fusion> fusion_ptr = std::make_unique<Fusion>();
Fusion& fusion = *fusion_ptr.get();
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(4, DataType::Half);
fusion.addInput(tv0);
auto tv1 = makeSymbolicTensor(1, DataType::Half);
fusion.addInput(tv1);
auto tv2 = makeSymbolicTensor(1, DataType::Half);
fusion.addInput(tv2);
auto tv3 = makeSymbolicTensor(1, DataType::Float);
fusion.addInput(tv3);
auto tv4 = makeSymbolicTensor(1, DataType::Float);
fusion.addInput(tv4);
auto d34 = IrBuilder::create<Val>(1.0);
auto tv5 = castOp(DataType::Float, tv0);
auto tv6 = castOp(DataType::Float, tv1);
auto tv7 = castOp(DataType::Float, tv2);
auto tvs = Welford(tv5, {0, 2, 3});
auto tv8 = tvs.avg;
auto tv9 = tvs.var_sum;
auto tv11 = mul(tv8, IrBuilder::create<Val>(0.1));
auto tv12 = mul(tv3, d34);
auto tv13 = add(tv12, tv11);
auto d43 = IrBuilder::create<Val>(0.5);
auto tv14 = mul(tv9, d43);
auto tv15 = mul(tv14, IrBuilder::create<Val>(0.1));
auto tv16 = mul(tv4, d34);
auto tv17 = add(tv16, tv15);
auto tv18 = broadcast(tv8, {true, false, true, true});
auto tv19 = sub(tv5, tv18);
auto tv20 = mul(tv9, d43);
auto tv21 = add(tv20, IrBuilder::create<Val>(0.0001));
auto tv22 = rsqrt(tv21);
auto tv23 = broadcast(tv22, {true, false, true, true});
auto tv24 = mul(tv19, tv23);
auto tv25 = broadcast(tv6, {true, false, true, true});
auto tv26 = mul(tv24, tv25);
auto tv27 = broadcast(tv7, {true, false, true, true});
auto tv28 = add(tv26, tv27);
auto tv29 = castOp(DataType::Half, tv28);
fusion.addOutput(tv13);
fusion.addOutput(tv17);
fusion.addOutput(tv29);
tv0->cacheAfter();
tv1->cacheAfter();
tv2->cacheAfter();
tv3->cacheAfter();
tv4->cacheAfter();
tv13->cacheBefore();
tv17->cacheBefore();
tv29->cacheBefore();
tv0->split(1, NamedScalar::getParallelDim(ParallelType::BIDx), false);
tv0->split(0, NamedScalar::getParallelDim(ParallelType::BIDy), false);
tv0->split(1, 8, false);
tv0->split(2, 8, false);
tv0->merge(-2, -1);
tv0->split(-1, 2);
tv0->split(-2, 1, false);
tv0->split(-2, 1, false);
tv0->reorder(
{{4, 0},
{5, 1},
{0, 2},
{3, 3},
{8, 4},
{1, 5},
{7, 6},
{2, 7},
{9, 8},
{6, 9}});
TransformPropagator propagator(tv0);
MaxLogicalDomainInfoSpanningTree(tv0).traverse(&propagator);
ir_utils::rFactorHelper(tvs.avg, {-5, -4, -3, -2, -1});
tv0->computeAt(tv29, 2);
tv1->computeAt(tv29, 2);
tv2->computeAt(tv29, 2);
tv3->computeAt(tv13, 2);
tv4->computeAt(tv17, 2);
tv29->axis(0)->parallelize(ParallelType::BIDx);
tv29->axis(2)->parallelize(ParallelType::BIDy);
tv29->axis(3)->parallelize(ParallelType::TIDz);
tv29->axis(4)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv29);
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto options_half = at::TensorOptions().dtype(at::kHalf).device(at::kCUDA, 0);
auto t0 = at::randn(input_shape, options_half);
auto t1 = at::randn(input_shape[1], options_half);
auto t2 = at::randn(input_shape[1], options_half);
auto t3 = at::randn(input_shape[1], options);
auto t4 = at::randn(input_shape[1], options);
KernelArgumentHolder inputs({t0, t1, t2, t3, t4});
GpuLower gpulw(&fusion);
validateNoParallelBroadcastExist(gpulw.run());
KernelExecutor ke;
LaunchParams launch_params(2, 2, -1, -1, -1, -1);
ke.compile(&fusion, inputs, launch_params);
auto cg_outputs = ke.run(inputs, {}, launch_params);
auto t5 = t0.to(at::kFloat);
auto t6 = t1.to(at::kFloat);
auto t7 = t2.to(at::kFloat);
auto t8 = t5.mean({0, 2, 3});
auto t9 = t5.var({0, 2, 3}, false) * input_shape[0] * input_shape[2] *
input_shape[3];
auto t11 = t8 * 0.1;
auto t12 = t3 * 1;
auto t13 = t12 + t11;
auto t14 = t9 * 0.5;
auto t15 = t14 * 0.1;
auto t16 = t4 * 1;
auto t17 = t16 + t15;
auto t18 = t8.unsqueeze(0).unsqueeze(-1).unsqueeze(-1);
auto t19 = t5 - t18;
auto t20 = t9 * 0.5;
auto t21 = t20 + 0.0001;
auto t22 = rsqrt(t21);
auto t23 = t22.unsqueeze(0).unsqueeze(-1).unsqueeze(-1);
auto t24 = t19 * t23;
auto t25 = t6.unsqueeze(0).unsqueeze(-1).unsqueeze(-1);
auto t26 = t24 * t25;
auto t27 = t7.unsqueeze(0).unsqueeze(-1).unsqueeze(-1);
auto t28 = t26 + t27;
auto t29 = t28.to(at::kHalf);
testValidate(
&fusion,
cg_outputs,
inputs,
{t13, t17, t29},
__LINE__,
__FILE__,
"",
launch_params);
}
// Simple grouped reduction
TEST_F(ReductionTest, GroupedReduction1) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {1});
auto tv2 = sum(tv0, {1});
auto tv3 = add(tv1, tv2);
fusion.addOutput(tv3);
groupReductions({tv1, tv2});
tv2->axis(0)->parallelize(ParallelType::BIDx);
tv2->axis(1)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv2);
std::vector<int64_t> shape({99, 999});
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});
auto ref = t0.sum({1}) * 2;
testValidate(
ke.compiledKernel()->kernel(), outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Grouping reductions with different ops
TEST_F(ReductionTest, GroupedReduction2) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = add(tv0, IrBuilder::create<Val>(1.0));
auto tv2 = sum(tv1, {1});
auto tv3 = add(tv0, IrBuilder::create<Val>(2.0));
auto tv4 = max(tv3, {1});
auto tv5 = add(tv2, tv4);
fusion.addOutput(tv5);
groupReductions({tv2, tv4});
tv2->split(1, 128);
TransformPropagator propagator(tv2);
MaxLogicalDomainInfoSpanningTree(tv2).traverse(&propagator);
tv0->computeAt(tv4, -1, ComputeAtMode::MostInlined);
// tv4 is automatically parallelized in the same way
tv2->axis(0)->parallelize(ParallelType::BIDy);
tv2->axis(1)->parallelize(ParallelType::BIDx);
tv2->axis(2)->parallelize(ParallelType::TIDx);
std::vector<int64_t> shape({99, 999});
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});
auto ref = (t0 + 1).sum({1}) + std::get<0>((t0 + 2).max(1));
testValidate(
ke.compiledKernel()->kernel(), outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Grouped reduction with different types
TEST_F(ReductionTest, GroupedReduction3) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {1});
auto tv2 = castOp(DataType::Double, tv0);
auto tv3 = sum(tv2, {1});
auto tv4 = castOp(DataType::Float, tv3);
auto tv5 = add(tv1, tv4);
fusion.addOutput(tv5);
groupReductions({tv1, tv3});
tv1->split(1, 128);
TransformPropagator propagator(tv1);
MaxLogicalDomainInfoSpanningTree(tv1).traverse(&propagator);
tv0->computeAt(tv5, -1, ComputeAtMode::MostInlined);
tv1->axis(0)->parallelize(ParallelType::BIDy);
tv1->axis(1)->parallelize(ParallelType::BIDx);
tv1->axis(2)->parallelize(ParallelType::TIDx);
std::vector<int64_t> shape({99, 999});
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});
auto ref = t0.sum({1}) + t0.to(c10::kDouble).sum({1}).to(c10::kFloat);
testValidate(
ke.compiledKernel()->kernel(), outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Testing validation
TEST_F(ReductionTest, GroupedReduction4) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = makeSymbolicTensor(2);
fusion.addInput(tv1);
auto tv2 = sum(tv0, {1});
auto tv3 = sum(tv1, {1});
auto tv4 = add(tv2, tv3);
fusion.addOutput(tv4);
// Invalid grouping as tv2 and tv3 are not guaranteed to have the
// same shape
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
ASSERT_ANY_THROW(groupReductions({tv2, tv3}));
}
// Testing validation
TEST_F(ReductionTest, GroupedReduction5) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {1});
auto tv2 = sum(tv0, {1});
auto tv3 = add(tv1, tv2);
fusion.addOutput(tv3);
tv1->split(1, 128);
tv2->split(1, 64);
// Invalid grouping as tv1 and tv2 don't have the same
// transformations
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
ASSERT_ANY_THROW(groupReductions({tv1, tv2}));
}
// Grouping 3 reductions
TEST_F(ReductionTest, GroupedReduction6) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = add(tv0, IrBuilder::create<Val>(1.0));
auto tv2 = sum(tv1, {1});
auto tv3 = add(tv0, IrBuilder::create<Val>(2.0));
auto tv4 = sum(tv3, {1});
auto tv5 = add(tv0, IrBuilder::create<Val>(3.0));
auto tv6 = sum(tv5, {1});
auto tv7 = add(add(tv2, tv4), tv6);
fusion.addOutput(tv7);
groupReductions({tv2, tv4, tv6});
// There's no runtime grid reduction function that can take more
// than 2 inputs, yet.
tv2->axis(0)->parallelize(ParallelType::BIDx);
tv2->axis(1)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv2);
std::vector<int64_t> shape({99, 999});
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});
testValidate(
ke.compiledKernel()->kernel(), outputs, {t0}, __LINE__, __FILE__);
}
TEST_F(ReductionTest, GroupedReduction7) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {1});
auto tv2 = broadcast(tv1, {false, true});
auto tv3 = add(tv0, tv2);
auto tv4 = sum(tv3, {1});
fusion.addOutput(tv4);
// Invalid grouping as tv3 depends on tv1
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
ASSERT_ANY_THROW(groupReductions({tv1, tv4}));
}
// Grouping rfactor'ed reductions
TEST_F(ReductionTest, GroupedReductionRfactor1) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(1);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {0});
auto tv2 = sum(tv0, {0});
auto tv3 = add(tv1, tv2);
fusion.addOutput(tv3);
const int64_t gdimx = 10;
const int64_t bdimx = 128;
tv1->split(0, gdimx, false);
tv1->split(1, bdimx);
auto tv1_rf = tv1->rFactor({1});
tv2->split(0, gdimx, false);
tv2->split(1, bdimx);
auto tv2_rf = tv2->rFactor({1});
groupReductions({tv1_rf, tv2_rf});
groupReductions({tv1, tv2});
tv1_rf->axis(0)->parallelize(ParallelType::BIDx);
tv1_rf->axis(2)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv1_rf);
std::vector<int64_t> shape({12345});
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});
auto ref = t0.sum({0}) * 2;
testValidate(
ke.compiledKernel()->kernel(), outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Rfactoring grouped reductions
TEST_F(ReductionTest, GroupedReductionRfactor2) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(1);
fusion.addInput(tv0);
auto tv1 = sum(tv0, {0});
auto tv2 = sum(tv0, {0});
auto tv3 = add(tv1, tv2);
fusion.addOutput(tv3);
groupReductions({tv1, tv2});
const int64_t gdimx = 10;
const int64_t bdimx = 128;
tv1->split(0, gdimx, false);
tv1->split(1, bdimx);
// This should rfactor tv2 as well
auto rf_tvs = tv1->rFactor({1}, {tv1, tv2});
auto tv1_rf = rf_tvs.at(0);
tv1_rf->axis(0)->parallelize(ParallelType::BIDx);
tv1_rf->axis(2)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv1_rf);
std::vector<int64_t> shape({12345});
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});
auto ref = t0.sum({0}) * 2;
testValidate(
ke.compiledKernel()->kernel(), outputs, {t0}, {ref}, __LINE__, __FILE__);
}
// Group reductions of tensors that have computeAt positions set
TEST_F(ReductionTest, GroupedReductionAfterComputeAt) {
Fusion fusion;
FusionGuard fg(&fusion);
auto tv0 = makeSymbolicTensor(2);
fusion.addInput(tv0);
auto tv1 = add(tv0, IrBuilder::create<Val>(1.0));
auto tv2 = sum(tv1, {1});
auto tv3 = sum(tv1, {1});
auto tv4 = add(tv2, tv3);
fusion.addOutput(tv4);
const int64_t bdimx = 128;
tv2->split(1, bdimx);
auto tv2_rf = tv2->rFactor({1});
tv2_rf->reorder({{1, 2}});
tv3->split(1, bdimx);
auto tv3_rf = tv3->rFactor({1});
tv3_rf->reorder({{1, 2}});
tv0->computeAt(tv4, -1, ComputeAtMode::MostInlined);
groupReductions({tv2_rf, tv3_rf});
groupReductions({tv2, tv3});
tv2->axis(1)->parallelize(ParallelType::TIDx);
scheduler_utils::parallelizeAllLike(tv2);
std::vector<int64_t> shape({3, 1234});
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
auto t0 = at::randn(shape, options);
KernelExecutor ke;
ke.compile(&fusion, {t0});
auto outputs = ke.run({t0});