-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathfusion.cpp
More file actions
1365 lines (1181 loc) · 40.2 KB
/
fusion.cpp
File metadata and controls
1365 lines (1181 loc) · 40.2 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 <fusion.h>
#include <type.h>
#include <iterator>
#include <ostream>
#include <ranges>
#include <codegen.h>
#include <debug.h>
#include <device_lower/analysis/bank_conflict.h>
#include <device_lower/lower2device.h>
#include <disjoint_set.h>
#include <fusion_segmenter.h>
#include <host_ir/container.h>
#include <instrumentation.h>
#include <ir/all_nodes.h>
#include <ir/builder.h>
#include <ir/cloner.h>
#include <ir/internal_nodes.h>
#include <ir/printer.h>
#include <ir/utils.h>
#include <iter_visitor.h>
#include <kernel.h>
#include <ops/alias.h>
#include <ops/arith.h>
#include <runtime/executor_params.h>
#include <transform_replay.h>
namespace nvfuser {
size_t Fusion::hash() const {
size_t hash = 0;
for (const Val* val : inputs()) {
hashCombine(hash, val->hash());
}
for (const Expr* expr : exprs()) {
hashCombine(hash, expr->hash());
}
for (const Val* val : outputs()) {
hashCombine(hash, val->hash());
}
return hash;
}
namespace {
//! Check if the alias info is the same between different Fusion objects
bool checkAliasInfo(
const AliasInfo& this_alias_info,
const AliasInfo& other_alias_info) {
if (this_alias_info.type != other_alias_info.type) {
return false;
}
if (this_alias_info.visibility != other_alias_info.visibility) {
return false;
}
if (this_alias_info.aliased_io == nullptr) {
return other_alias_info.aliased_io == nullptr;
}
return this_alias_info.aliased_io->sameDefinition(
other_alias_info.aliased_io);
}
} // namespace
bool Fusion::sameDefinition(const Fusion& other) const {
if (inputs().size() != other.inputs().size()) {
return false;
}
for (auto&& [input, other_input] : zip(inputs(), other.inputs())) {
if (!input->sameDefinition(other_input)) {
return false;
}
}
if (outputs().size() != other.outputs().size()) {
return false;
}
// Call sameDefinition on each output traverses the entire Fusion DAG.
// First the output is checked, then the output definition, and on to the
// definition's inputs. This repeats until fusion inputs are reached.
const auto& this_output_aliases = getOutputAliases();
const auto& other_output_aliases = other.getOutputAliases();
for (auto&& [output, other_output] : zip(outputs(), other.outputs())) {
if (!output->sameDefinition(other_output)) {
return false;
}
if (!checkAliasInfo(
this_output_aliases.get(output),
other_output_aliases.get(other_output))) {
return false;
}
}
return true;
}
void Fusion::swap(Fusion& a, Fusion& b) {
FUSER_PERF_SCOPE("Fusion swap");
if (&a == &b) {
return;
}
NVF_ERROR(
a.ir_container_ != nullptr, "Fusion::swap: a has null ir_container_");
NVF_ERROR(
b.ir_container_ != nullptr, "Fusion::swap: b has null ir_container_");
// Collect statements owned by each Fusion BEFORE swap so we can update
// Statement::ir_container_ pointers afterward.
std::vector<Val*> a_owned_vals, b_owned_vals;
std::vector<Expr*> a_owned_exprs, b_owned_exprs;
const auto& av = a.ir_container_->valsOwnedBy(&a);
const auto& ae = a.ir_container_->exprsOwnedBy(&a);
a_owned_vals.assign(av.begin(), av.end());
a_owned_exprs.assign(ae.begin(), ae.end());
const auto& bv = b.ir_container_->valsOwnedBy(&b);
const auto& be = b.ir_container_->exprsOwnedBy(&b);
b_owned_vals.assign(bv.begin(), bv.end());
b_owned_exprs.assign(be.begin(), be.end());
// Transfer Fusion registrations between containers before pointer swap.
// After swap, a will own b's container and b will own a's container.
if (a.ir_container_.get() != b.ir_container_.get()) {
a.ir_container_->transferFusion(&a, &b);
b.ir_container_->transferFusion(&b, &a);
}
// Swap container pointers
std::swap(a.ir_container_, b.ir_container_);
// Swap all Fusion-level members
std::swap(a.inputs_, b.inputs_);
std::swap(a.outputs_, b.outputs_);
std::swap(a.io_alias_, b.io_alias_);
std::swap(a.all_tv_uses_valid_, b.all_tv_uses_valid_);
std::swap(a.is_during_update_uses_, b.is_during_update_uses_);
std::swap(a.managed_data_, b.managed_data_);
std::swap(a.managed_named_data_, b.managed_named_data_);
std::swap(a.expected_dynamic_smem_bytes_, b.expected_dynamic_smem_bytes_);
std::swap(a.all_tvs_ptr_, b.all_tvs_ptr_);
std::swap(a.zero_val_, b.zero_val_);
std::swap(a.one_val_, b.one_val_);
std::swap(a.true_val_, b.true_val_);
std::swap(a.false_val_, b.false_val_);
std::swap(a.magic_zero_val_, b.magic_zero_val_);
std::swap(a.axioms_, b.axioms_);
std::swap(a.metadata_, b.metadata_);
std::swap(a.val_type_name_map_, b.val_type_name_map_);
std::swap(a.expr_name_counter_, b.expr_name_counter_);
// Update Statement::ir_container_ pointers: a's old statements now belong
// to b, and b's old statements now belong to a
for (auto* val : a_owned_vals) {
val->ir_container_ = &b;
}
for (auto* expr : a_owned_exprs) {
expr->ir_container_ = &b;
}
for (auto* val : b_owned_vals) {
val->ir_container_ = &a;
}
for (auto* expr : b_owned_exprs) {
expr->ir_container_ = &a;
}
// Update per-Fusion tracking keys in containers. At this point, both
// a and b are guaranteed to have non-null ir_container_ (verified above).
if (a.ir_container_.get() == b.ir_container_.get()) {
// Same container: directly swap per-Fusion tracking entries
auto* c = a.ir_container_.get();
std::swap(c->per_fusion_vals_[&a], c->per_fusion_vals_[&b]);
std::swap(c->per_fusion_exprs_[&a], c->per_fusion_exprs_[&b]);
} else {
// Different containers: rename tracking keys to match new owners
a.ir_container_->transferStatementOwnership(&b, &a);
b.ir_container_->transferStatementOwnership(&a, &b);
}
}
std::unique_ptr<SegmentedFusion> Fusion::segment(
const KernelArgumentHolder& args) {
FUSER_PERF_SCOPE("Segment Fusion");
return SegmentCandidateFinder::segment(this, args);
}
IrCloner Fusion::copy(const Fusion* from, Fusion* to) {
to->clear();
IrCloner ir_cloner(to);
// Clone from's vals in insertion order
for (auto val : from->deterministic_vals()) {
ir_cloner.clone(val);
}
// Wire up definitions and uses on cloned vals in deterministic order
// to ensure exprs are inserted into exprs_up_ deterministically
for (auto val : from->deterministic_vals()) {
ir_cloner.clone(val)->setDefinition(ir_cloner.clone(val->definition_));
ir_cloner.clone(val)->setUses(ir_cloner.clone(val->uses_));
}
// Sync per-Fusion name counters from source to dest.
// Must be AFTER all cloning (vals and exprs) so that registerVal/registerExpr
// increments during cloning do not inflate the final counter values.
// During cloning, registerVal increments the dest Fusion's counter for each
// val, then IrBuilder::clone overrides the name with setName(src->name()).
// If source names are non-sequential (e.g., {0..10, 22..27} from segmenter
// creating intermediate TVs), the dest counter ends up at N (number of vals)
// instead of max(name)+1. Copying the source's counter state ensures new
// vals created post-copy won't collide with existing names.
to->val_type_name_map_ = from->val_type_name_map_;
to->expr_name_counter_ = from->expr_name_counter_;
// Remap cached special val pointers
if (from->zero_val_) {
to->zero_val_ = ir_cloner.clone(from->zero_val_);
}
if (from->one_val_) {
to->one_val_ = ir_cloner.clone(from->one_val_);
}
if (from->true_val_) {
to->true_val_ = ir_cloner.clone(from->true_val_);
}
if (from->false_val_) {
to->false_val_ = ir_cloner.clone(from->false_val_);
}
if (from->magic_zero_val_) {
to->magic_zero_val_ =
ir_cloner.clone(from->magic_zero_val_)->as<NamedScalar>();
}
to->inputs_ = ir_cloner.clone(from->inputs_);
to->outputs_ = ir_cloner.clone(from->outputs_);
for (auto inp : to->inputs_) {
inp->setIsFusionInput(true);
}
for (auto out : to->outputs_) {
out->setIsFusionOutput(true);
}
for (Val* out : from->outputs_) {
const AliasInfo& alias = from->io_alias_.get(out);
if (alias.type == AllocationType::New) {
continue;
}
Val* copied_out = ir_cloner.clone(out);
Val* copied_in = ir_cloner.clone(alias.aliased_io);
to->io_alias_.add(copied_out, copied_in, alias.type, alias.visibility);
}
to->all_tv_uses_valid_ = from->all_tv_uses_valid_;
to->is_during_update_uses_ = from->is_during_update_uses_;
for (const auto& i : from->managed_data_) {
if (i.first.has_value()) {
to->managed_data_.emplace_back(i.second(ir_cloner, i.first), i.second);
} else {
to->managed_data_.emplace_back(i.first, i.second);
}
}
for (const auto& [k, v] : from->managed_named_data_) {
if (v.first.has_value()) {
to->managed_named_data_.insert(std::make_pair(
k, std::make_pair(v.second(ir_cloner, v.first), v.second)));
}
}
to->expected_dynamic_smem_bytes_ = from->expected_dynamic_smem_bytes_;
if (from->axioms_ != nullptr) {
to->axioms_ = std::make_unique<std::vector<Val*>>();
to->axioms_->reserve(from->axioms_->size());
for (auto pred : *from->axioms_) {
to->axioms_->push_back(ir_cloner.clone(pred));
}
}
for (auto& [key, val_expr] : from->metadata_) {
to->metadata_[ir_cloner.clone(key)] = std::make_pair(
ir_cloner.clone(val_expr.first), ir_cloner.clone(val_expr.second));
}
if (from->all_tvs_ptr_ != nullptr) {
to->all_tvs_ptr_ = std::make_unique<std::vector<TensorView*>>();
to->all_tvs_ptr_->reserve(from->all_tvs_ptr_->size());
for (TensorView* from_tv : *from->all_tvs_ptr_) {
to->all_tvs_ptr_->push_back(ir_cloner.clone(from_tv)->as<TensorView>());
}
}
return ir_cloner;
}
// Default constructor
Fusion::Fusion() : ir_container_(std::make_shared<IrContainer>()) {
ir_container_->addFusion(this);
}
// Copy constructor -- shares the source's container
Fusion::Fusion(const Fusion& other) : ir_container_(other.ir_container_) {
FUSER_PERF_SCOPE("Fusion copy");
ir_container_->addFusion(this);
Fusion::copy(&other, this);
}
// Move constructor
// Not marked noexcept: Fusion::swap allocates local std::vectors to collect
// statement ownership before the swap, which can throw. Since Fusions are not
// expected to be moved into containers, the performance trade-off is
// acceptable.
// NOLINTNEXTLINE(cppcoreguidelines-noexcept-move-operations)
Fusion::Fusion(Fusion&& other) : Fusion() {
FUSER_PERF_SCOPE("Fusion move");
swap(*this, other);
}
// Copy Assignment -- shares the source's container
Fusion& Fusion::operator=(const Fusion& other) {
FUSER_PERF_SCOPE("Fusion copy assign");
if (this != &other) {
Fusion copy(other);
clear();
swap(*this, copy);
}
return *this;
}
// Not marked noexcept: See move constructor above.
// NOLINTNEXTLINE(cppcoreguidelines-noexcept-move-operations)
Fusion& Fusion::operator=(Fusion&& other) {
FUSER_PERF_SCOPE("Fusion move assign");
if (this != &other) {
clear();
swap(*this, other);
}
return *this;
}
Fusion::~Fusion() {
clear();
if (ir_container_) {
ir_container_->removeFusion(this);
}
}
void Fusion::clear() noexcept {
// Perf scope isn't safe here as this function could be called by
// the Fusion destructor and the scope initializer could call the
// constructor of Trace, which could throw an exception.
// FUSER_PERF_SCOPE("Fusion clear");
if (ir_container_) {
ir_container_->removeStatementsOwnedBy(this);
}
inputs_.clear();
outputs_.clear();
io_alias_.clear();
managed_data_.clear();
managed_named_data_.clear();
zero_val_ = nullptr;
one_val_ = nullptr;
true_val_ = nullptr;
false_val_ = nullptr;
magic_zero_val_ = nullptr;
axioms_.reset();
metadata_.clear();
val_type_name_map_.clear();
expr_name_counter_ = 0;
invalidateTvsAndUses();
is_during_update_uses_ = false;
}
void Fusion::removeExpr(Expr* expr) {
assertInContainer(expr, "Cannot remove expr ");
// If we hit this error too frequently, we could lighten the restrictions so
// that removing something that doesn't exist simply does nothing. For now,
// we're going with the strictest model which errors.
for (auto* out : expr->outputs()) {
if (out->isA<TensorView>()) {
invalidateTvsAndUses();
}
out->setDefinition(nullptr);
}
// Remove uses in inputs
for (auto* inp : expr->inputs()) {
// Note that if inp is a TensorView, this may call invalidateTvsAndUses
inp->removeUse(expr);
if (inp->isA<TensorView>()) {
invalidateTvsAndUses();
}
}
auto* c = ir_container();
auto expr_in_deque = std::ranges::find_if(
c->exprs_up_,
[expr](std::unique_ptr<Expr>& expr_up) { return expr_up.get() == expr; });
NVF_ERROR(
expr_in_deque != c->exprs_up_.end(),
"Wanted to remove an expression but its unique ptr is missing.");
c->per_fusion_exprs_[this].erase(expr);
c->exprs_.erase(expr);
c->exprs_up_.erase(expr_in_deque);
}
void Fusion::removeVal(Val* val) {
assertInContainer(val, "Cannot remove val ");
// Don't remove cached special vals — they are lazily created singletons
if (val == zero_val_ || val == one_val_ || val == true_val_ ||
val == false_val_ || val == magic_zero_val_) {
return;
}
NVF_CHECK(
!val->isFusionInput(),
"Cannot remove val as it is an input of the fusion.");
NVF_CHECK(
!val->isFusionOutput(),
"Cannot remove val as it is an output of the fusion.");
if (Expr* orig = val->definition()) {
removeExpr(orig);
}
// We previously first looped over val->uses() and removed them all from the
// Fusion. This seems correct at first glance, but it is incomplete since
// `val->uses()` actually only gives all live uses. When there is dead code in
// the Fusion that includes some uses of a val that is to be removed, we can
// wind up with an expression that holds an invalid pointer to the removed
// value in its inputs(). In https://github.com/NVIDIA/Fuser/issues/1270 this
// caused a segfault when the fusion was cloned since that will clone not only
// live objects but also these dangerous dangling dead ones.
//
// IMPORTANT: We must use unordered_exprs() instead of exprs() here.
// exprs() only returns Exprs reachable from terminating outputs, which means
// dead Exprs that still reference the Val won't be found and removed.
// This causes use-after-free when copying the Fusion later.
std::vector<Expr*> exprs_to_remove;
for (Expr* e : unordered_exprs()) {
if (!inContainer(e)) {
continue;
}
if (std::find(e->inputs().begin(), e->inputs().end(), val) !=
e->inputs().end()) {
// Avoid removing until after we've looped through exprs_
exprs_to_remove.push_back(e);
}
}
for (auto e : exprs_to_remove) {
removeExpr(e);
}
auto* c = ir_container();
auto val_in_deque = std::ranges::find_if(
c->vals_up_,
[val](std::unique_ptr<Val>& val_up) { return val_up.get() == val; });
NVF_ERROR(
val_in_deque != c->vals_up_.end(),
"Wanted to remove a value but its unique ptr is missing.");
c->per_fusion_vals_[this].erase(val);
c->vals_.erase(val);
c->vals_up_.erase(val_in_deque);
invalidateTvsAndUses();
}
void Fusion::removeStatementsCreatedAfter(
int64_t num_exprs_before,
int64_t num_vals_before) {
auto* c = ir_container();
// Remove expressions before values because we need to change Val::uses_.
while (std::ssize(c->exprsOwnedBy(this)) > num_exprs_before) {
// Pop from global deque back — statements created by this Fusion during
// the guard scope are at the tail (LIFO invariant).
Expr* e = c->exprs_up_.back().get();
NVF_ERROR(
c->per_fusion_exprs_[this].count(e) > 0,
"removeStatementsCreatedAfter: tail expr belongs to another Fusion");
for (Val* in : e->inputs()) {
in->removeUse(e);
}
c->per_fusion_exprs_[this].erase(e);
c->exprs_.erase(e);
c->exprs_up_.pop_back();
}
while (numValsExcludingShortcuts() > num_vals_before) {
Val* v = c->vals_up_.back().get();
NVF_ERROR(
c->per_fusion_vals_[this].count(v) > 0,
"removeStatementsCreatedAfter: tail val belongs to another Fusion");
// Null out shortcut caches if they point to vals about to be destroyed
if (v == zero_val_) {
zero_val_ = nullptr;
} else if (v == one_val_) {
one_val_ = nullptr;
} else if (v == true_val_) {
true_val_ = nullptr;
} else if (v == false_val_) {
false_val_ = nullptr;
} else if (v == magic_zero_val_) {
magic_zero_val_ = nullptr;
}
c->per_fusion_vals_[this].erase(v);
c->vals_.erase(v);
c->vals_up_.pop_back();
}
}
void Fusion::addInput(Val* input) {
assertInContainer(input, "Cannot register input ");
if (input->getValType() == ValType::TensorView) {
auto tv = input->as<TensorView>();
if (tv->getMemoryType() != MemoryType::Symmetric) {
tv->setMemoryType(MemoryType::Global);
}
} else if (input->getValType() == ValType::Others) {
NVF_CHECK(
!input->isConst(),
"Immediate scalar value cannot be added as an input. It is not "
"necessary to pass it as an input.");
}
NVF_CHECK(
!input->isFusionInput(),
"Val: ",
input->toString(),
" is already registered as input, duplicated inputs is not allowed");
inputs_.push_back(input);
input->setIsFusionInput(true);
invalidateTvsAndUses();
}
void Fusion::addOutputInternal(Val* output) {
assertInContainer(output, "Cannot register output ");
NVF_CHECK(
output->isA<TensorView>(),
"Non-TensorView outputs are not supported at this point: ",
output->toString());
auto* tv = output->as<TensorView>();
if (tv->getMemoryType() != MemoryType::Symmetric) {
tv->setMemoryType(MemoryType::Global);
}
outputs_.push_back(output);
output->setIsFusionOutput(true);
invalidateTvsAndUses();
}
void Fusion::addOutput(Val* output) {
// Special handling for returning aliased output. We just need to remove its
// existing entry in the outputs_ used for inplace update
if (io_alias_.get(output).type != AllocationType::New) {
AliasInfo& alias = io_alias_.mutable_at(output);
// if previous output is only added for aliasing purpose, we should remove
// the previous entry and add a new one. Otherwise, it may be positioned
// wrong in the output list.
if (alias.visibility == OutputVisibility::kHidden) {
removeOutput(output);
}
// output shouldn't be hidden any more
alias.visibility = OutputVisibility::kVisible;
}
addOutputInternal(output);
}
void Fusion::removeInput(Val* input) {
auto find_input = std::ranges::find(inputs_, input);
if (find_input != inputs_.end()) {
inputs_.erase(find_input);
}
input->setIsFusionInput(false);
invalidateTvsAndUses();
}
void Fusion::removeOutput(Val* output) {
auto find_output = std::ranges::find(outputs_, output);
if (find_output != outputs_.end()) {
outputs_.erase(find_output);
}
output->setIsFusionOutput(false);
invalidateTvsAndUses();
}
void Fusion::replaceOutput(Val* output, Val* replacement) {
auto find_output = std::ranges::find(outputs_, output);
NVF_CHECK(find_output != outputs_.end(), "Unable to find output in Fusion");
if (find_output != outputs_.end()) {
std::ranges::replace_if(
outputs_, [&output](Val* v) { return v == output; }, replacement);
if (replacement->getValType() == ValType::TensorView) {
replacement->setIsFusionOutput(true);
NVF_CHECK(
replacement->as<TensorView>()->getMemoryType() !=
MemoryType::Symmetric,
"Symmetric memory type not supported for replacement: ",
replacement);
replacement->as<TensorView>()->setMemoryType(MemoryType::Global);
}
if (output->getValType() == ValType::TensorView) {
output->setIsFusionOutput(false);
// If `output` is both an input and an output before the replacement,
// don't localize it.
if (!output->isFusionInput()) {
output->as<TensorView>()->setMemoryType(MemoryType::Local);
}
}
// Mark uses invalid so that they will be reset next time uses() is called
invalidateTvsAndUses();
}
// Temporary WAR for issue #1112
// (https://github.com/csarofeen/pytorch/issues/1112)
AliasInfo alias = io_alias_.get(output);
if (alias.type != AllocationType::New) {
io_alias_.erase(output);
io_alias_.add(replacement, alias.aliased_io, alias.type, alias.visibility);
}
}
std::vector<Expr*> Fusion::exprs() const {
return StmtSort::getExprs(this);
}
bool Fusion::isNoOp() {
if (exprs().empty()) {
return true;
}
for (auto out_tv : ir_utils::filterByType<TensorView>(outputs())) {
auto logical_dom = out_tv->getLogicalDomain() | TensorDomain::kNoReductions;
const bool size_zero = std::ranges::any_of(logical_dom, [](IterDomain* id) {
return id->extent()->isConstScalar() &&
id->extent()->evaluate().as<int64_t>() == 0;
});
if (!size_zero) {
return false;
}
}
return true;
}
std::vector<Val*> Fusion::inputsOf(Val* val) {
return InputsOf::output(val);
}
void Fusion::validateInputs() {
std::unordered_set<Val*> all_inputs;
for (Val* out : outputs()) {
for (Val* input : inputsOf(out)) {
all_inputs.insert(input);
}
}
std::unordered_set<Val*> input_dims;
auto inp_tvs = ir_utils::filterByType<TensorView>(inputs());
for (auto tv : inp_tvs) {
for (auto id : tv->getLogicalDomain()) {
input_dims.emplace(id->extent());
}
}
// NOLINTNEXTLINE(bugprone-nondeterministic-pointer-iteration-order)
for (Val* input : all_inputs) {
if (!input->isConstScalar()) {
NVF_CHECK(
input->isFusionInput() ||
// TODO: Switch:
inContainer(input),
// to: input_dims.find(input) != input_dims.end(),
// https://github.com/csarofeen/pytorch/issues/1365
"Could not figure out how ",
input->toString(),
" is generated, however it was not specified as an input.");
}
}
}
std::ostream& Fusion::print(std::ostream& os, bool include_tensor_transforms)
const {
FUSER_PERF_SCOPE("Fusion::print");
os << "Inputs:" << '\n';
for (auto inp : inputs()) {
os << " " << inp << '\n';
}
os << "Outputs:" << '\n';
for (auto out : outputs()) {
os << " " << out << '\n';
}
os << "\n%kernel {\n";
IrPrinter op_exprs(os);
op_exprs.handle(this);
if (include_tensor_transforms) {
os << "\nTransformPrinter : \n";
IrTransformPrinter t_exprs(os);
t_exprs.handle(this);
}
os << "} // %kernel\n";
os << std::flush;
return os;
}
void Fusion::printKernel(const CompileParams& compile_params) {
FUSER_PERF_SCOPE("Fusion::printKernel");
NVF_ERROR(
!this->isA<kir::Kernel>(),
"Cannot \"print kernel\" of a kernel container. ",
"This would require lowering during lowering.");
GpuLower lower(this, compile_params);
lower.run();
debug() << codegen::generateCudaKernel(lower.kernel());
debug() << std::flush;
}
std::unordered_map<
TensorView*,
std::pair<std::vector<int64_t>, std::vector<int64_t>>>
Fusion::bankConflictInfo(const CompileParams& compile_params) {
std::vector<TensorView*> smem_tvs;
for (auto v : usedMathVals()) {
auto tv = dynamic_cast<TensorView*>(v);
if (tv == nullptr) {
continue;
}
if (tv->getMemoryType() == MemoryType::Shared) {
smem_tvs.push_back(tv);
}
}
if (smem_tvs.empty()) {
return {};
}
manage("smem_tvs", smem_tvs);
GpuLower lower(this, compile_params);
lower.run();
auto kernel = lower.kernel();
auto info = getBankConflictInfo(kernel);
// Convert TVs in kernel to TVs in fusion
auto smem_tvs_in_kernel =
kernel->getManaged<std::vector<TensorView*>>("smem_tvs");
NVF_ERROR(smem_tvs_in_kernel.size() == smem_tvs.size());
auto getSmemTvInFusion = [&](Val* v) -> TensorView* {
auto ti = dynamic_cast<kir::TensorIndex*>(v);
if (ti == nullptr) {
return nullptr;
}
auto tv = ti->view();
auto it = std::ranges::find(smem_tvs_in_kernel, tv);
if (it == smem_tvs_in_kernel.end()) {
return nullptr;
}
auto index = std::distance(smem_tvs_in_kernel.begin(), it);
return smem_tvs.at(index);
};
std::unordered_map<
TensorView*,
std::pair<std::vector<int64_t>, std::vector<int64_t>>>
result;
result.reserve(info.size());
for (auto i : info) {
auto expr = i.first;
// Currently only set and load store op are supported
NVF_ERROR(expr->inputs().size() == 1);
NVF_ERROR(expr->outputs().size() == 1);
auto input = getSmemTvInFusion(expr->input(0));
auto output = getSmemTvInFusion(expr->output(0));
if (input == nullptr) {
NVF_ERROR(i.second.first == 0);
} else {
NVF_ERROR(i.second.first != 0);
result[input].first.push_back(i.second.first);
}
if (output == nullptr) {
NVF_ERROR(i.second.second == 0);
} else {
NVF_ERROR(i.second.second != 0);
result[output].second.push_back(i.second.second);
}
}
return result;
}
void Fusion::printMath(bool from_outputs_only) {
FUSER_PERF_SCOPE("Fusion::printMath");
FusionGuard fg(this);
auto exprs_for_print = exprs();
debug() << "Inputs:" << '\n';
for (auto inp : inputs()) {
debug() << " " << inp << '\n';
}
debug() << "Outputs:" << '\n';
for (auto out : outputs()) {
debug() << " " << out << '\n';
}
// If we want everything in the fusion, grab all values without uses to
// traverse from.
if (!from_outputs_only) {
std::vector<Val*> leaf_vals;
for (auto val : deterministic_vals()) {
if (val->uses().empty()) {
leaf_vals.push_back(val);
}
}
exprs_for_print = StmtSort::getExprsTo(leaf_vals);
}
debug() << "\n%kernel_math {\n";
for (auto expr : exprs_for_print) {
debug() << expr;
}
debug() << "} // %kernel_math \n\n";
debug() << std::flush;
}
std::vector<Val*> Fusion::inputsAndCreated() {
auto result = inputs_;
for (auto expr : exprs()) {
auto tv_inputs = ir_utils::filterByType<TensorView>(expr->inputs());
if (tv_inputs.empty()) {
for (auto v : expr->outputs()) {
result.emplace_back(v);
}
}
}
return result;
}
void Fusion::printTransforms() {
FUSER_PERF_SCOPE("Fusion::printTransforms");
FusionGuard fg(this);
IrTransformPrinter t_exprs(debug());
t_exprs.handle(this);
}
Val* Fusion::zeroVal() {
if (!zero_val_) {
zero_val_ = IrBuilder::createInContainer<Val>(this, 0L, DataType::Index);
}
return zero_val_;
}
Val* Fusion::oneVal() {
if (!one_val_) {
one_val_ = IrBuilder::createInContainer<Val>(this, 1L, DataType::Index);
}
return one_val_;
}
Val* Fusion::falseVal() {
if (!false_val_) {
false_val_ = IrBuilder::createInContainer<Val>(this, false, DataType::Bool);
}
return false_val_;
}
Val* Fusion::trueVal() {
if (!true_val_) {
true_val_ = IrBuilder::createInContainer<Val>(this, true, DataType::Bool);
}
return true_val_;
}
NamedScalar* Fusion::magicZeroVal() {
if (!magic_zero_val_) {
magic_zero_val_ = IrBuilder::createInContainer<NamedScalar>(
this, kMagicZeroName, DataType::Index);
}
return magic_zero_val_;
}
Val* Fusion::zeroVal(DataType dtype) {
if (dtype == DataType::Index) {
return zeroVal();
} else if (isBooleanType(dtype)) {
return falseVal();
} else {
// NOTE: this does not cache values
return IrBuilder::createInContainer<Val>(this, 0L, dtype);
}
}
Val* Fusion::oneVal(DataType dtype) {
if (dtype == DataType::Index) {
return oneVal();
} else if (isBooleanType(dtype)) {
return trueVal();
} else {
// NOTE: this does not cache values
return IrBuilder::createInContainer<Val>(this, 1L, dtype);
}
}
Val* Fusion::metadataOf(Val* v) {
if (metadata_.count(v) == 0) {
auto metadata_val =
IrBuilder::createInContainer<Val>(this, metaDataTypeOf(v));
auto metadata_expr =
IrBuilder::createInContainer<GetMetaData>(this, metadata_val, v);
metadata_[v] = std::make_pair(metadata_val, metadata_expr);
}
return metadata_.at(v).first;
}
const std::vector<Val*>& Fusion::axioms() {
if (!axioms_) {
axioms_ = std::make_unique<std::vector<Val*>>();
axioms_->reserve(kParallelTypeThreads.size() * 3);
auto zero = zeroVal();
for (auto p : kParallelTypeThreads) {
auto pidx = NamedScalar::getParallelIndex(p);
auto pdim = NamedScalar::getParallelDim(p);
axioms_->push_back(SimplifyingIrBuilder::geExpr(pidx, zero));
axioms_->push_back(SimplifyingIrBuilder::gtExpr(pdim, zero));
axioms_->push_back(SimplifyingIrBuilder::ltExpr(pidx, pdim));
}
}
return *axioms_;
}
void Fusion::assumePositive(Val* val) {
NVF_ERROR(inContainer(val));
// Lazy init axioms, then add the assumption
axioms();
axioms_->emplace_back(IrBuilder::gtExpr(val, zeroVal()));
}
void Fusion::assumeNonNegative(Val* val) {
NVF_ERROR(inContainer(val));
// Lazy init axioms, then add the assumption
axioms();
axioms_->emplace_back(IrBuilder::geExpr(val, zeroVal()));
}
void Fusion::registerVal(Val* val) {
if (inContainer(val)) {
return;
}
if (val->fusion()) {
NVF_CHECK(
val->fusion() == this, val, " was not found in the active fusion.");
}
auto* c = ir_container();
c->vals_up_.emplace_back(val);
c->vals_.insert(val);
c->per_fusion_vals_[this].insert(val);
val->setName(IrContainerPasskey(), getValName(val->vtype()));
}
void Fusion::registerExpr(Expr* expr) {