/
lower_sparse_iter.cc
1269 lines (1184 loc) · 50.4 KB
/
lower_sparse_iter.cc
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file lower_sparse_iter.cc
*/
#include <tvm/tir/analysis.h>
#include <tvm/tir/stmt_functor.h>
#include <tvm/tir/transform.h>
#include <set>
#include <utility>
#include "../../support/utils.h"
#include "../schedule/analysis.h"
#include "ir_utils.h"
namespace tvm {
namespace tir {
namespace {
class VarCollector : public StmtExprVisitor {
public:
VarCollector() {}
Array<Var> vars;
std::unordered_set<const VarNode*> var_set;
private:
void VisitExpr_(const VarNode* op) {
if (!var_set.count(op)) {
vars.push_back(GetRef<Var>(op));
var_set.insert(op);
}
}
};
/*!
* \brief Collect the ancestors of the given axis along the path to the root.
* \note self not included.
*/
Array<Axis> CollectAncestors(Axis axis, int max_depth = -1) {
Array<Axis> parents, ret;
Optional<ObjectRef> parent = axis->parent;
while (parent.defined()) {
parents.push_back(Downcast<Axis>(parent.value()));
axis = parents.back();
parent = axis->parent;
if (max_depth >= 0) {
if (static_cast<int>(parents.size()) >= max_depth) {
break;
}
}
}
for (int i = static_cast<int>(parents.size()) - 1; i >= 0; i--) {
ret.push_back(parents[i]);
}
return ret;
}
/*!
* \brief Add indptr and indices buffer-matches to PrimFunc's buffer map.
*/
std::tuple<Map<Axis, SparseBuffer>, Map<Axis, SparseBuffer>, Map<Var, Buffer>, Array<Axis>,
Array<BufferDomain>>
UpdateMetadata(PrimFunc f) {
Map<Var, Buffer> buffer_map = f->buffer_map;
Map<Axis, SparseBuffer> axis_indptr_map;
Map<Axis, SparseBuffer> axis_indices_map;
std::unordered_map<const AxisNode*, Axis> to_dense_map;
Array<Axis> new_sp_axes;
Array<BufferDomain> buf_doms;
for (const Axis& axis : f->sp_axes) {
new_sp_axes.push_back(axis);
// TODO(Zihao): special handle of FlattenedAxis
if (axis->IsVariable()) {
// axis is variable, generate the indptr sparse buffers.
String indptr_name = axis->name + "_indptr";
Var indptr(indptr_name + ".data", PointerType(PrimType(axis->idtype), "global"));
Array<Axis> ancestors = CollectAncestors(axis);
Axis parent = ancestors.back();
if (parent->IsSparse()) {
if (!to_dense_map.count(parent.get())) {
new_sp_axes.push_back(ToDenseAxis(parent));
to_dense_map[parent.get()] = new_sp_axes.back();
}
ancestors.Set(ancestors.size() - 1, to_dense_map[parent.get()]);
}
SparseBuffer sp_buf(indptr, ancestors, axis->idtype, indptr_name, Integer(1));
buf_doms.push_back(BufferDomain(sp_buf, Range::FromMinExtent(Integer(0), axis->nnz)));
buffer_map.Set(axis->indptr.value(), sp_buf);
axis_indptr_map.Set(axis, sp_buf);
}
if (axis->IsSparse()) {
// axis is sparse, generate the indices sparse buffers.
String indices_name = axis->name + "_indices";
Var indices(indices_name + ".data", PointerType(PrimType(axis->idtype), "global"));
Array<Axis> ancestors = CollectAncestors(axis);
if (!to_dense_map.count(axis.get())) {
new_sp_axes.push_back(ToDenseAxis(axis));
to_dense_map[axis.get()] = new_sp_axes.back();
}
ancestors.push_back(to_dense_map[axis.get()]);
SparseBuffer sp_buf(indices, ancestors, axis->idtype, indices_name, NullOpt);
buf_doms.push_back(BufferDomain(sp_buf, Range::FromMinExtent(Integer(0), axis->length)));
buffer_map.Set(axis->indices.value(), sp_buf);
axis_indices_map.Set(axis, sp_buf);
}
}
return {axis_indptr_map, axis_indices_map, buffer_map, new_sp_axes, buf_doms};
}
/*!
* \brief Create an intermediate buffer with specified name and data type
* \param name The specified name
* \param dtype The specified data type
* \return The created buffer
*/
Buffer MakeScratchpad(String name, const DataType& dtype) {
return Buffer(/*ptr=*/Var(name, PointerType(PrimType(dtype), "local")),
/*dtype=*/dtype,
/*shape=*/{Integer(1)},
/*strides=*/{Integer(1)},
/*elem_offset=*/PrimExpr{nullptr},
/*name=*/name,
/*data_alignment=*/0,
/*offset_factor=*/0,
/*buffer_type=*/kDefault);
}
Axis GetAxisBeforeFuse(const Axis& axis) {
if (const FusedAxisNode* fused_axis = axis.as<FusedAxisNode>()) {
return fused_axis->group[fused_axis->index];
} else {
return axis;
}
}
class VarUsedVisitor : public StmtExprVisitor {
public:
VarUsedVisitor() {}
std::unordered_set<const VarNode*> used_var;
private:
void VisitExpr_(const VarNode* op) final { used_var.insert(op); }
};
} // namespace
/*!
* \brief Auxlliary context data structure for lower sparse iter vars.
*/
class LowerSparseIterContext {
public:
/*! \brief Enter a new scope. */
void EnterScope(const std::unordered_map<const VarNode*, arith::IntSet>& base_dom_map) {
stack_.push_back(Info());
top()->dom_map_ = base_dom_map;
}
/*! \brief Exit the current scope. */
void ExitScope() {
/* aggregate dom map. */
for (auto it : top()->dom_map_) {
if (stack_.size() > 1) {
stack_[stack_.size() - 2].dom_map_.insert(it);
}
}
stack_.pop_back();
}
/*! \brief Change the is_collecting_regions flag. */
void CollectRegion(bool is_collecting_regions) {
top()->is_collecting_regions = is_collecting_regions;
}
/*! \brief Whether the visitor is collecting read/write regions or not. */
bool IsCollectingRegions() { return top()->is_collecting_regions; }
/*! \brief Update read regions. */
void UpdateRead(Buffer buffer, const std::vector<arith::IntSet>& region) {
Update(&top()->read_buffers_, &top()->read_regions_, buffer, region);
}
/*! \brief Update write regions. */
void UpdateWrite(Buffer buffer, const std::vector<arith::IntSet>& region) {
Update(&top()->write_buffers, &top()->write_regions_, buffer, region);
}
/*! \brief Return the collected read regions. */
Array<BufferRegion> CollectReadRegions() {
return std::move(CollectRegions(top()->read_buffers_, top()->read_regions_));
}
/*! \brief Return the collected write regions. */
Array<BufferRegion> CollectWriteRegions() {
return std::move(CollectRegions(top()->write_buffers, top()->write_regions_));
}
/*! \brief Update the variable-domain map. */
void AddVarDom(Var var, arith::IntSet dom) { top()->dom_map_[var.get()] = dom; }
/*! \brief Get the variable-domain map. */
std::unordered_map<const VarNode*, arith::IntSet>& GetDomMap() { return top()->dom_map_; }
/*! \brief Add an axis-itervar mapping to the context. */
void AddAxisIterVar(Axis axis, IterVar iter_var) {
top()->axis_itervar_map_[axis.get()] = iter_var;
}
/*! \brief Return the itervar corresponding to an axis. */
Optional<IterVar> GetIterVarFromAxis(Axis axis) {
for (int i = stack_.size() - 1; i >= 0; i--) {
auto it = stack_[i].axis_itervar_map_.find(axis.get());
if (it != stack_[i].axis_itervar_map_.end()) {
return it->second;
}
}
return NullOpt;
}
/*! \brief Add an var-itervar mapping to the context. */
void AddVarIterVar(Var var, SpIterVar iter_var) { top()->var_itervar_map_[var.get()] = iter_var; }
/*! \brief Return the sparse itervar corresponding to a variable. */
Optional<SpIterVar> GetSpIterVarFromVar(Var var) {
for (int i = stack_.size() - 1; i >= 0; i--) {
auto it = stack_[i].var_itervar_map_.find(var.get());
if (it != stack_[i].var_itervar_map_.end()) {
return it->second;
}
}
return NullOpt;
}
/*!
* \brief Clear read/write buffers and regions in the context.
*/
void ClearReadWriteBufferRegions() {
top()->read_buffers_.clear();
top()->read_regions_.clear();
top()->write_buffers.clear();
top()->write_regions_.clear();
}
private:
/*! \brief Data structures storing sparse-iteration local information. */
struct Info {
bool is_collecting_regions = false;
/*! \brief Iteration range for loop_vars */
std::unordered_map<const VarNode*, arith::IntSet> dom_map_;
/*! \brief The buffers that the current block reads */
std::vector<Buffer> read_buffers_;
/*! \brief The buffers that the current block writes */
std::vector<Buffer> write_buffers;
/*! \brief The opaque buffer which is access by buffer.data */
std::vector<std::vector<tvm::arith::IntSet>> read_regions_;
/*! \brief The write regions of the current block */
std::vector<std::vector<tvm::arith::IntSet>> write_regions_;
/*! \brief The map from axis to corresponding iter var. */
std::unordered_map<const AxisNode*, IterVar> axis_itervar_map_;
/*! \brief The map from Var to corresponding Sparse IterVar. */
std::unordered_map<const VarNode*, SpIterVar> var_itervar_map_;
};
/*! \brief Get the information corresponding to the top sparse-iteration in the stack. */
inline Info* top() const { return const_cast<Info*>(&stack_.back()); }
/*! \brief Update buffer regions. */
void Update(std::vector<Buffer>* buffers, std::vector<std::vector<arith::IntSet>>* regions,
Buffer buffer, const std::vector<arith::IntSet>& region) {
ICHECK_EQ(buffers->size(), regions->size())
<< " Expected the buffer and regions to have the same size ";
for (size_t i = 0; i < regions->size(); ++i) {
if ((*buffers)[i].same_as(buffer)) {
ICHECK_EQ((*regions)[i].size(), region.size()) << "Inconsistent buffer dimension";
for (size_t j = 0; j < region.size(); ++j) {
(*regions)[i][j] = arith::Union({(*regions)[i][j], region[j]});
}
return;
}
}
buffers->push_back(std::move(buffer));
regions->push_back(std::move(region));
}
/*! \brief Return the array of BufferRegion's given buffer array and region array. */
Array<BufferRegion> CollectRegions(const std::vector<Buffer>& buffers,
const std::vector<std::vector<tvm::arith::IntSet>>& regions) {
ICHECK_EQ(buffers.size(), regions.size());
Array<BufferRegion> res;
res.reserve(buffers.size());
for (size_t i = 0; i < regions.size(); ++i) {
Array<Range> region;
region.reserve(regions[i].size());
ICHECK_EQ(buffers[i]->shape.size(), regions[i].size());
for (size_t j = 0; j < regions[i].size(); j++) {
const tvm::arith::IntSet& range = regions[i][j];
region.push_back(range.CoverRange(Range::FromMinExtent(0, buffers[i]->shape[j])));
}
res.push_back(BufferRegion(buffers[i], region));
}
return res;
}
/*! The stack of sparse-iteration local informations. */
std::vector<Info> stack_;
};
/*!
* \brief Lower sparse iterations by rewriting AST.
*/
class IterTransformer : public StmtExprMutator {
public:
explicit IterTransformer(Map<Axis, SparseBuffer> axis_indptr_map,
Map<Axis, SparseBuffer> axis_indices_map, const Array<Axis>& sp_axes,
bool check_invalid_binary_search)
: axis_indptr_map_(std::move(axis_indptr_map)),
axis_indices_map_(std::move(axis_indices_map)),
bsearch_blk_counter(0),
check_invalid_binary_search_(check_invalid_binary_search) {
CreateBaseDomMap(sp_axes);
}
struct BinarySearchStructure {
String name;
Stmt body;
Map<Var, SpIterVar> var_map;
Map<SpIterVar, Var> inv_var_map;
Array<Buffer> alloc_buffers;
BufferRegion read;
BufferRegion write;
};
std::vector<BinarySearchStructure> bsearch_structures; // binary search related structures.
Array<Buffer> root_alloc_buffers; // allocated buffers in the root block.
Array<BufferDomain> alloc_buf_doms; // allocated buffer domains.
private:
/*! \brief Create base dom map: each axis parameters should be greater than 0. */
void CreateBaseDomMap(const Array<Axis>& axes) {
for (const Axis& axis : axes) {
const VarNode* var_length = axis->length.as<VarNode>();
if (var_length) {
if (!base_dom_map_.count(var_length)) {
base_dom_map_[var_length] = arith::IntSet::FromMinExtent(Integer(1), axis->length);
}
}
const VarNode* var_nnz = axis->nnz.as<VarNode>();
if (var_nnz) {
if (!base_dom_map_.count(var_nnz)) {
base_dom_map_[var_nnz] = arith::IntSet::FromMinExtent(Integer(1), axis->nnz);
}
}
if (!axis->IsVariable()) {
const VarNode* var_nnz_cols = axis->nnz_cols.value().as<VarNode>();
if (var_nnz_cols) {
if (!base_dom_map_.count(var_nnz_cols)) {
base_dom_map_[var_nnz_cols] =
arith::IntSet::FromMinExtent(Integer(1), axis->nnz_cols.value());
}
}
}
}
}
/*!
* \brief Generated the loop nests for the outside the input body.
* \param body The statement to be wrapped by loop nests.
* \param block_iters The block iterators defined in the outermost block in `body`.
* \param iter_binding The itervar bindings defined in the outermost block in `body`.
* \param block_axes The axes corresponding to itervars defined in the outermost block in `body`.
* \return The outermost generated loop.
*/
Stmt GenerateLoops(Stmt body, const Array<IterVar>& block_iters,
const Array<PrimExpr>& iter_bindings, const Array<Axis>& block_axes) {
int n_iter = static_cast<int>(block_iters.size());
for (int i = n_iter - 1; i >= 0; --i) {
const IterVar& iter_var = block_iters[i];
if (!iter_bindings[i]->IsInstance<VarNode>()) {
// skip if iter_binding is not a var (only happens in fused axis).
continue;
}
const Var& loop_var = Downcast<Var>(iter_bindings[i]);
const Axis& axis = block_axes[i];
const Range& dom = iter_var->dom;
PrimExpr extent = dom->extent;
Optional<SparseBuffer> maybe_indptr_buf;
bool is_attached_axis = false;
if (const AttachedAxisNode* attached_axis = axis.as<AttachedAxisNode>()) {
maybe_indptr_buf = axis_indptr_map_.Get(attached_axis->base);
is_attached_axis = true;
} else {
maybe_indptr_buf = axis_indptr_map_.Get(axis);
}
if (axis->IsVariable()) {
ICHECK(maybe_indptr_buf.defined());
SparseBuffer indptr_buf = maybe_indptr_buf.value();
Array<PrimExpr> indices;
Array<Axis> ancestors =
is_attached_axis ? CollectAncestors(axis, 1) : CollectAncestors(axis);
for (const Axis& anc_axis : ancestors) {
PrimExpr index = ctx_.GetIterVarFromAxis(anc_axis).value()->var;
if (is_attached_axis) {
index = VisitExpr(index); // get coordinate for attached axis.
}
indices.push_back(index);
}
PrimExpr lb = BufferLoad(indptr_buf, indices);
indices.Set(indices.size() - 1, indices.back() + 1);
PrimExpr ub = BufferLoad(indptr_buf, indices);
extent = ub - lb;
} else {
extent = axis->nnz_cols.value();
}
body = For(loop_var, Integer(0), extent, ForKind::kSerial, std::move(body));
}
return body;
}
/*! \brief Visitor of sparse iteration node.
* \return The emitted lowered block corresponding to the original sparse iteration.
*/
Stmt VisitStmt_(const SparseIterationNode* sp_iteration) final {
/*! \brief A class temporarily storing the block signatures and the outer loop variables of the
* blocks to be generated */
struct BlockInfo {
/*! \brief The iterators of the block */
Array<IterVar> block_iters;
/*! \brief The axes appeared in the block */
Array<Axis> block_axes;
/*! \brief The loop vars in the block */
Array<PrimExpr> iter_bindings;
/*! \brief The init statement of the block */
Optional<Stmt> init;
/*!
* \brief Push a new block iterator/iterator binding/axis to this block.
* \param block_iter The block iterator to be pushed.
* \param iter_binding The iterator binding to be pushed.
* \param block_axis The axis to be pushed.
*/
void Push(const IterVar& block_iter, const PrimExpr& iter_binding, const Axis& block_axis) {
block_iters.push_back(block_iter);
iter_bindings.push_back(iter_binding);
block_axes.push_back(block_axis);
}
/*!
* \brief Check whether a new block is needed. We need to create a new block when:
* - the input axis is variable (dense-variable or sparse-variable), and
* - the parent axis of the input axis has corresponding loop variable in the current block.
* \param axis The axis to be checked.
* \return Whether a new block is needed according to the conditions above.
*/
bool NeedCreateNewBlock(LowerSparseIterContext* ctx, Axis axis) {
if (!axis->IsVariable()) {
// is fixed axis.
return false;
}
Axis parent_axis = GetParentAxis(axis);
Optional<IterVar> parent_iter_var = ctx->GetIterVarFromAxis(parent_axis);
CHECK(parent_iter_var.defined())
<< "ValueError: The parent axis of " << axis << " does not appear.";
for (const Axis& blk_axis : block_axes) {
if (GetAxisBeforeFuse(blk_axis).same_as(parent_axis)) {
return true;
}
}
return false;
}
};
bool back_up_check_region = binary_search_vaild_check_region;
if (check_invalid_binary_search_) {
auto valid_check_flag =
sp_iteration->annotations.Get("binary_search_vaild_check").value_or(Bool(true));
if (Downcast<Bool>(valid_check_flag) == false) {
binary_search_vaild_check_region = false;
}
}
int n_iters = static_cast<int>(sp_iteration->sp_iter_vars.size());
Array<Var> loop_vars;
// Enter the context
ctx_.EnterScope(base_dom_map_);
// Create the new loop variables, and update axis_itervar and var_itervar map in the
// context.
Map<Var, PrimExpr> var_map;
Array<IterVar> new_iter_vars;
for (const SpIterVar& sp_iter_var : sp_iteration->sp_iter_vars) {
Var loop_var = sp_iter_var->var;
loop_vars.push_back(loop_var);
IterVar iter_var = sp_iter_var->as_iter_var();
new_iter_vars.push_back(iter_var);
ctx_.AddAxisIterVar(GetAxisBeforeFuse(sp_iter_var->axis), iter_var);
ctx_.AddVarIterVar(iter_var->var, sp_iter_var);
var_map.Set(sp_iter_var->var, iter_var->var);
}
// Mutate the `init` field.
Optional<Stmt> init = sp_iteration->init.defined()
? VisitStmt(Substitute(sp_iteration->init.value(), var_map))
: Optional<Stmt>(NullOpt);
// Gather the information of the blocks to be generated.
std::vector<BlockInfo> block_infos(1);
/* Whether a reduction block iterator has appeared */
bool has_reduction_var = false;
for (int i = 0; i < n_iters; ++i) {
SpIterVar sp_iter_var = sp_iteration->sp_iter_vars[i];
if (block_infos.back().NeedCreateNewBlock(&ctx_, sp_iter_var->axis)) {
// Create a new BlockInfo;
block_infos.emplace_back();
}
// Create loop information
bool remove_loop_var = false;
if (const FusedAxisNode* fused_axis = sp_iter_var->axis.as<FusedAxisNode>()) {
// if it's fused axis, and not the last fused axis, remove the loop var.
if (!fused_axis->IsLastAxis()) {
remove_loop_var = true;
}
}
PrimExpr iter_binding = remove_loop_var ? Integer(0) : PrimExpr(loop_vars[i]);
block_infos.back().Push(new_iter_vars[i], iter_binding, sp_iter_var->axis);
if (!has_reduction_var && sp_iter_var->is_reduction) {
block_infos.back().init = std::move(init);
has_reduction_var = true;
}
}
// Recursively mutate the block body.
Stmt body = VisitStmt(Substitute(sp_iteration->body, var_map));
// Process binary search blocks.
std::vector<std::vector<BlockInfo>> bsearch_block_infos(bsearch_structures.size());
std::vector<Map<Var, PrimExpr>> bsearch_var_maps;
for (size_t j = 0; j < bsearch_structures.size(); ++j) {
BinarySearchStructure& bsearch_structure = bsearch_structures[j];
std::vector<BlockInfo>& bsearch_block_info = bsearch_block_infos[j];
bsearch_block_info.push_back(BlockInfo());
Map<Var, PrimExpr> var_map;
for (int i = 0; i < n_iters; ++i) {
SpIterVar sp_iter_var = sp_iteration->sp_iter_vars[i];
if (bsearch_structure.inv_var_map.count(sp_iter_var)) {
Var old_var = bsearch_structure.inv_var_map.Get(sp_iter_var).value();
IterVar new_iter_var = sp_iter_var->as_iter_var();
auto n = new_iter_var.CopyOnWrite();
n->iter_type = kDataPar; // change iter_type to data parallel
var_map.Set(old_var, new_iter_var->var);
Var loop_var(sp_iter_var->var->name_hint, sp_iter_var->var->dtype);
if (bsearch_block_info.back().NeedCreateNewBlock(&ctx_, sp_iter_var->axis)) {
// Create a new BlockInfo;
bsearch_block_info.emplace_back();
}
// Create loop information
bool remove_loop_var = false;
if (const FusedAxisNode* fused_axis = sp_iter_var->axis.as<FusedAxisNode>()) {
if (!fused_axis->IsLastAxis()) {
remove_loop_var = true;
}
}
PrimExpr iter_binding = remove_loop_var ? Integer(0) : PrimExpr(loop_var);
bsearch_block_info.back().Push(new_iter_var, iter_binding, sp_iter_var->axis);
}
}
bsearch_structure.body = Substitute(bsearch_structure.body, var_map);
bsearch_var_maps.emplace_back(std::move(var_map));
}
// Generate nested blocks and loops from innermost to outermost.
for (int i = static_cast<int>(block_infos.size()) - 1; i >= 0; --i) {
BlockInfo info = std::move(block_infos[i]);
// Collect read/write regions.
ctx_.CollectRegion(true); // update is_collecting_regions flag to true;
Optional<Stmt> init = NullOpt;
if (info.init.defined()) {
init = VisitStmt(info.init.value());
}
VisitStmt(body);
// Update read/writes regions.
Array<BufferRegion> writes_new = ctx_.CollectWriteRegions();
std::unordered_set<const BufferNode*> excluded_buffers;
bool is_reduction = false;
for (const IterVar& iter_var : info.block_iters) {
if (iter_var->iter_type == kCommReduce) {
is_reduction = true;
}
}
if (is_reduction) {
for (const BufferRegion& write_access : writes_new) {
excluded_buffers.insert(write_access->buffer.get());
}
}
Array<BufferRegion> reads = ctx_.CollectReadRegions(), reads_new;
for (const BufferRegion& read_access : reads) {
if (!excluded_buffers.count(read_access->buffer.get())) {
reads_new.push_back(read_access);
}
}
ctx_.ClearReadWriteBufferRegions();
ctx_.CollectRegion(false); // update is_collecting_regions flag to false
// Create new block.
Map<String, ObjectRef> annotations = sp_iteration->annotations;
annotations.Set("sparse", Bool(true));
if (binary_search_vaild_check_region && check_invalid_binary_search_) {
annotations.Set("binary_search_vaild_check", Bool(true));
}
Block block(/*iter_vars=*/info.block_iters,
/*reads=*/reads_new,
/*writes=*/writes_new,
/*name_hint=*/sp_iteration->name + std::to_string(i),
/*body=*/body,
/*init=*/init,
/*alloc_buffers=*/{},
/*match_buffers=*/{},
/*buf_doms=*/{},
/*annotations=*/annotations);
// Update var dom.
for (const IterVar& iter_var : info.block_iters) {
ctx_.AddVarDom(iter_var->var, arith::IntSet::FromRange(iter_var->dom));
}
// Create block realize node.
BlockRealize block_realize(
/*iter_values=*/info.iter_bindings,
/*predicate=*/const_true(),
/*block=*/block);
// Create loops
body = std::move(block_realize);
Stmt loop = GenerateLoops(body, info.block_iters, info.iter_bindings, info.block_axes);
body = std::move(loop);
}
// Wrap binary search with outer blocks and loops.
for (size_t j = 0; j < bsearch_structures.size(); ++j) {
BinarySearchStructure& bsearch_structure = bsearch_structures[j];
const std::vector<BlockInfo>& bsearch_block_info = bsearch_block_infos[j];
if (!bsearch_var_maps[j].empty()) {
// avoid unnecessary nested blocks, especially when there are multiple sparse iterations in
// the program.
for (int i = static_cast<int>(bsearch_block_info.size()) - 1; i >= 0; --i) {
BlockInfo info = std::move(bsearch_block_info[i]);
Map<String, ObjectRef> annotations;
annotations.Set("sparse", Bool(true));
annotations.Set("preprocess", Bool(true));
if (check_invalid_binary_search_) {
annotations.Set("is_binary_search_block", Bool(true));
}
Array<BufferRegion> reads, writes;
if (i == static_cast<int>(bsearch_block_info.size()) - 1) {
// innermost
reads = {bsearch_structure.read};
writes = {bsearch_structure.write};
} else {
ctx_.CollectRegion(true); // update is_collecting_regions flag to true;
VisitStmt(bsearch_structure.body);
// Update read/writes regions.
writes = ctx_.CollectWriteRegions();
reads = ctx_.CollectReadRegions();
ctx_.ClearReadWriteBufferRegions();
ctx_.CollectRegion(false); // update is_collecting_regions flag to false
}
Block block(/*iter_vars=*/info.block_iters,
/*reads=*/reads,
/*writes=*/writes,
/*name_hint=*/bsearch_structure.name + "_" + std::to_string(i),
/*body=*/bsearch_structure.body,
/*init=*/{},
/*alloc_buffers=*/bsearch_structure.alloc_buffers,
/*match_buffers=*/{},
/*buf_doms*/ {},
/*annotations=*/annotations);
bsearch_structure.alloc_buffers = {};
BlockRealize block_realize(
/*iter_values=*/info.iter_bindings,
/*predicate=*/const_true(),
/*block=*/std::move(block));
bsearch_structure.body = Substitute(
GenerateLoops(block_realize, info.block_iters, info.iter_bindings, info.block_axes),
bsearch_var_maps[j]);
// Update var dom.
for (const IterVar& iter_var : info.block_iters) {
ctx_.AddVarDom(iter_var->var, arith::IntSet::FromRange(iter_var->dom));
}
}
}
}
// restore binary search vaild check region
binary_search_vaild_check_region = back_up_check_region;
// Exit the context.
ctx_.ExitScope();
return body;
}
/*! \brief Visitor of block realize node, used to collect read/write regions. */
Stmt VisitStmt_(const BlockRealizeNode* op) final {
if (op->block->name_hint == "root") {
// root block, collect alloc buffers
for (const Buffer& buf : op->block->alloc_buffers) {
root_alloc_buffers.push_back(std::move(buf));
}
return VisitStmt(op->block->body);
}
/*! \note detector will not visit child block recursively, so it will stop here */
for (const BufferRegion& read_access : op->block->reads) {
std::vector<arith::IntSet> relaxed_region;
for (const auto& range : read_access->region) {
relaxed_region.push_back(arith::EvalSet(
arith::IntSet::FromRange(Range::FromMinExtent(range->min, range->extent)),
ctx_.GetDomMap()));
}
ctx_.UpdateRead(read_access->buffer, relaxed_region);
}
for (const BufferRegion& write_access : op->block->writes) {
std::vector<arith::IntSet> relaxed_region;
for (const auto& range : write_access->region) {
relaxed_region.push_back(arith::EvalSet(
arith::IntSet::FromRange(Range::FromMinExtent(range->min, range->extent)),
ctx_.GetDomMap()));
}
ctx_.UpdateWrite(write_access->buffer, relaxed_region);
}
return GetRef<BlockRealize>(op);
}
/*! \brief Visitor of variable node.
* \note return decompressed coodinates for itervars corresponding to sparse axes.
*/
PrimExpr VisitExpr_(const VarNode* op) final {
Var var = GetRef<Var>(op);
if (!ctx_.IsCollectingRegions()) {
// decompress variable
Optional<SpIterVar> maybe_sp_iter_var = ctx_.GetSpIterVarFromVar(GetRef<Var>(op));
if (maybe_sp_iter_var.defined()) {
SpIterVar sp_iter_var = maybe_sp_iter_var.value();
Axis axis = sp_iter_var->axis;
if (const FusedAxisNode* fused_axis = axis.as<FusedAxisNode>()) {
// handle the special case of fused axis.
PrimExpr offset = ctx_.GetIterVarFromAxis(fused_axis->group.back()).value()->var;
for (int i = fused_axis->group.size() - 1; i > fused_axis->index; i--) {
Axis original_axis = GetAxisBeforeFuse(fused_axis->group[i]);
Optional<SparseBuffer> maybe_indptr_buf = axis_indptr_map_.Get(original_axis);
ICHECK(maybe_indptr_buf.defined()) << "Not a variable axis.";
SparseBuffer indptr_buf = maybe_indptr_buf.value();
Array<Axis> ancestors = CollectAncestors(GetParentAxis(original_axis));
Array<PrimExpr> prefix_indices;
for (const Axis& ancestor : ancestors) {
prefix_indices.push_back(ctx_.GetIterVarFromAxis(ancestor).value()->var);
}
offset =
BinarySearch(indptr_buf, prefix_indices, Integer(0),
GetParentAxis(original_axis)->nnz + Integer(1), offset, false, true);
}
Axis original_axis = GetAxisBeforeFuse(fused_axis->group[fused_axis->index]);
if (!original_axis->IsSparse()) {
// if dense, return offset
return offset;
} else {
// if sparse, get indices according to offset.
Optional<SparseBuffer> maybe_indices_buf = axis_indices_map_.Get(original_axis);
ICHECK(maybe_indices_buf.defined()) << "Not a sparse axis.";
SparseBuffer indices_buf = maybe_indices_buf.value();
Array<Axis> ancestors = CollectAncestors(original_axis);
Array<PrimExpr> indices;
for (const Axis& ancestor : ancestors) {
indices.push_back(ctx_.GetIterVarFromAxis(ancestor).value()->var);
}
indices.push_back(offset);
return BufferLoad(indices_buf, indices);
}
} else {
Optional<SparseBuffer> maybe_indices_buf;
bool is_attached_axis = false;
if (const AttachedAxisNode* attached_axis = axis.as<AttachedAxisNode>()) {
maybe_indices_buf = axis_indices_map_.Get(attached_axis->base);
is_attached_axis = true;
} else {
maybe_indices_buf = axis_indices_map_.Get(axis);
}
if (maybe_indices_buf.defined()) {
SparseBuffer indices_buf = maybe_indices_buf.value();
Array<Axis> ancestors =
is_attached_axis ? CollectAncestors(axis, 1) : CollectAncestors(axis);
Array<PrimExpr> indices;
for (const Axis& anc_axis : ancestors) {
indices.push_back(ctx_.GetIterVarFromAxis(anc_axis).value()->var);
}
indices.push_back(var);
return BufferLoad(indices_buf, indices);
} else {
return var;
}
}
} else {
return var;
}
} else {
// do nothing when collecting regions.
return var;
}
}
/*! \brief Get relaxed region of indices including variables. */
std::vector<arith::IntSet> GetRelaxedRegion(Array<PrimExpr> indices) {
std::vector<arith::IntSet> relaxed_region;
for (const PrimExpr& index : indices) {
relaxed_region.push_back(arith::EvalSet(arith::IntSet::Vector(index), ctx_.GetDomMap()));
}
return std::move(relaxed_region);
}
/*!
* \brief Perform binary search inside TIR.
* \param buf The sparse buffer to be searched (must be sorted in ascending order on the last
* dimension).
* \param prefix_indices The prefix indices of the sparse buffer from the first dimension to the
* d-1 dimension (suppose `buf` is d-dimensional).
* \param lb The lower bound (close) of the search range [lb, ub)
* \param ub The upper bound (open) of the search range [lb, ub)
* \param val The value to be searched.
* \param minus_one Whether to minus one to the final result (when used together with
* `left=false`, you will get the rightmost index of the suitable location to maintain
* ascending order).
*/
PrimExpr BinarySearch(SparseBuffer buf, Array<PrimExpr> prefix_indices, PrimExpr lb, PrimExpr ub,
PrimExpr val, bool left, bool minus_one = false) {
/* Algorithm:
* - when left = true
* - pre-condition
* lb < ub, and the last dimension of buf is sorted.
* - loop-invariant
* low <= mid < high, buf[..., lb:low] < val, buf[..., high:ub] >= val
* - post-condition
* low = mid = high, buf[..., lb:low] < val, buf[..., high:ub] >= val
* - when left = false
* - pre-condition
* lb < ub, and the last dimension of buf is sorted.
* - loop-invariant
* low <= mid < high, buf[..., lb:low] <= val, buf[..., high:ub] > val
* - post-condition
* low = mid = high, buf[..., lb:low] <= val, buf[..., high:ub] > val
*/
ICHECK(buf->shape.size() == prefix_indices.size() + 1)
<< "The dimensionality of buffer shoule equal the length of prefix indices plus 1.";
CHECK(buf->axes.back()->sorted)
<< "The last axes of " << buf << " must be sorted to perform binary search on.";
// Check bsearch_map_ to avoid duplicate searches.
Array<ObjectRef> args;
args.push_back(buf);
args.push_back(prefix_indices);
args.push_back(lb);
args.push_back(ub);
args.push_back(val);
args.push_back(Bool(left));
args.push_back(Bool(minus_one));
if (bsearch_map_.count(args)) {
return bsearch_map_[args];
}
DataType dtype = buf->dtype;
Buffer low = MakeScratchpad("low", dtype);
Buffer high = MakeScratchpad("high", dtype);
VarCollector collector;
for (const PrimExpr& idx : prefix_indices) {
collector(idx);
}
collector(lb);
collector(ub);
collector(val);
Array<Axis> axes;
Array<PrimExpr> mid_indices;
Map<Var, SpIterVar> var_map;
Map<SpIterVar, Var> inv_var_map;
std::unordered_set<const AxisNode*> visited;
for (const Var& var : collector.vars) {
Optional<SpIterVar> maybe_sp_iter_var = ctx_.GetSpIterVarFromVar(var);
if (maybe_sp_iter_var.defined()) {
SpIterVar sp_iter_var = maybe_sp_iter_var.value();
Axis axis = sp_iter_var->axis;
if (const FusedAxisNode* fused_axis = axis.as<FusedAxisNode>()) {
for (const Axis& ax : fused_axis->group) {
if (visited.count(ax.get())) {
continue;
}
IterVar iter_var = ctx_.GetIterVarFromAxis(ax).value();
sp_iter_var = ctx_.GetSpIterVarFromVar(iter_var->var).value();
axes.push_back(ax);
mid_indices.push_back(iter_var->var);
var_map.Set(iter_var->var, sp_iter_var);
inv_var_map.Set(sp_iter_var, iter_var->var);
visited.insert(ax.get());
}
} else {
if (visited.count(axis.get())) {
continue;
}
axes.push_back(axis);
mid_indices.push_back(var);
var_map.Set(var, sp_iter_var);
inv_var_map.Set(sp_iter_var, var);
visited.insert(axis.get());
}
}
}
String mid_buf_name = "mid_" + std::to_string(bsearch_blk_counter);
SparseBuffer mid = SparseBuffer(Var(mid_buf_name, PointerType(PrimType(dtype), "global")), axes,
dtype, mid_buf_name, Integer(0));
Stmt low_store = BufferStore(low, lb, {Integer(0)});
Stmt high_store = BufferStore(high, ub, {Integer(0)});
PrimExpr low_val = BufferLoad(low, {Integer(0)}), high_val = BufferLoad(high, {Integer(0)}),
mid_val = BufferLoad(mid, mid_indices);
PrimExpr while_cond = low_val < high_val;
// Two store mid statements, one for init, another one inside while loop.
Stmt mid_store_init = BufferStore(mid, low_val + floordiv(high_val - low_val, 2), mid_indices);
Stmt mid_store_while = BufferStore(mid, low_val + floordiv(high_val - low_val, 2), mid_indices);
Array<PrimExpr> indices = prefix_indices;
indices.push_back(mid_val);
PrimExpr pivot = BufferLoad(buf, indices);
PrimExpr pivot_cmp_cond = left ? (pivot < val) : (pivot > val);
Stmt if_true = left ? BufferStore(low, mid_val + 1, {Integer(0)})
: BufferStore(high, mid_val, {Integer(0)});
Stmt if_false = left ? BufferStore(high, mid_val, {Integer(0)})
: BufferStore(low, mid_val + 1, {Integer(0)});
Stmt if_then_else = IfThenElse(pivot_cmp_cond, if_true, if_false);
SeqStmt while_body({if_then_else, mid_store_while});
Stmt while_ = While(while_cond, while_body);
Array<Stmt> body_stmts({low_store, high_store, mid_store_init, while_});
if (minus_one) {
body_stmts.push_back(
BufferStore(mid, BufferLoad(mid, mid_indices) - Integer(1), mid_indices));
}
if (!binary_search_vaild_check_region && check_invalid_binary_search_) {
Stmt then_stmt = BufferStore(mid, -1, mid_indices);
PrimExpr if_stmt = (pivot != val || mid_val == ub);
body_stmts.push_back(IfThenElse(if_stmt, then_stmt));
}
SeqStmt body(body_stmts);
String name = "binary_search_block_" + std::to_string(bsearch_blk_counter);
bsearch_blk_counter++;
root_alloc_buffers.push_back(mid);
alloc_buf_doms.push_back(
BufferDomain(mid, Range::FromMinExtent(Integer(0), buf->shape.back())));
Array<Range> read_regions, write_regions;
for (const PrimExpr& index : prefix_indices) {
read_regions.push_back(Range::FromMinExtent(index, Integer(1)));
}
read_regions.push_back(Range::FromMinExtent(lb, ub - lb));
for (const PrimExpr& mid_index : mid_indices) {
write_regions.push_back(Range::FromMinExtent(mid_index, Integer(1)));
}
BufferRegion read = BufferRegion(buf, read_regions);
BufferRegion write = BufferRegion(mid, write_regions);
bsearch_structures.push_back(
BinarySearchStructure({name, body, var_map, inv_var_map, {low, high}, read, write}));
bsearch_map_[args] = mid_val;
return mid_val;
}
/*! \brief Return indices viewed in a given buffer. */
Array<PrimExpr> RewriteIndices(Buffer buf, Array<PrimExpr> old_indices) {
Array<PrimExpr> new_indices;
if (const SparseBufferNode* sp_buf = buf.as<SparseBufferNode>()) {
// rewrite indices for a sparse buffer.
std::unordered_map<Axis, PrimExpr, ObjectPtrHash, ObjectPtrEqual> new_indices_map;
std::unordered_map<Axis, bool, ObjectPtrHash, ObjectPtrEqual> match_map;
// compute match map
for (size_t i = 0; i < old_indices.size(); ++i) {
PrimExpr index = old_indices[i];