Skip to content

Commit

Permalink
[TensorIR][M2a] Compute-At (apache#8943)
Browse files Browse the repository at this point in the history
This PR is part of the TensorIR upstreaming effort (apache#7527), which adds the following schedule primitives:
* `compute-at`
* `reverse-compute-at`

Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>
Co-authored-by: Siyuan Feng <Hzfengsy@sjtu.edu.cn>
  • Loading branch information
6 people authored and ylc committed Jan 13, 2022
1 parent 524787b commit 08c911b
Show file tree
Hide file tree
Showing 30 changed files with 2,526 additions and 343 deletions.
11 changes: 9 additions & 2 deletions include/tvm/arith/int_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,24 @@ class IntSet : public ObjectRef {
* \return The result set containing the indices in the vector.
*/
static IntSet Vector(PrimExpr vec);
/*!
* \brief Construct a set representing a range [min, min + extent).
* \param min The minimum of the range range
* \param extent The extent of the range.
* \return The constructed set.
*/
static IntSet FromMinExtent(PrimExpr min, PrimExpr extent);
/*!
* \brief Construct a set representing a range.
* \param r The range
* \return constructed set.
* \return The constructed set.
*/
static IntSet FromRange(tvm::Range r);
/*!
* \brief Construct a set representing a interval.
* \param min The minimum value of the interval.
* \param max The maximum value of the interval.
* \return constructed set.
* \return The constructed set.
*/
static IntSet Interval(PrimExpr min, PrimExpr max);

Expand Down
35 changes: 35 additions & 0 deletions include/tvm/tir/schedule/schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,41 @@ class ScheduleNode : public runtime::Object {
virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
const String& storage_scope) = 0;
/******** Schedule: Compute location ********/
/*!
* \brief Move a producer block under the specific loop, and regenerate the
* loops induced by the block so that the buffer region produced by the producer block could
* cover those regions consumed by its consumer blocks under the given loop. It requires:
* 1) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
* 2) The scope block has stage-pipeline property
* 3) The subtree of the scope block, where the given block is in, satisfies the compact dataflow
* condition. i.e. all the blocks in the scope block's subtree must be either complete block or
* reduction block
* 4) The block is not an output block with regard to the scope block, i.e. the buffers written by
* the block are allocated under the scope block
* 5) All the consumers of the block are under the given loop
* \param block_rv The block to be moved
* \param loop_rv The loop where the block to be moved under
* \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
*/
virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
bool preserve_unit_loops) = 0;
/*!
* \brief Move a consumer block under the specific loop, and regenerate the
* loops induced by the block so that the buffer region consumed by the consumer block could
* cover those regions produced by its producer blocks under the given loop. It requires:
* 1) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
* 2) The scope block has stage-pipeline property
* 3) The subtree of the scope block, where the given block is in, satisfies the compact dataflow
* condition. i.e. all the blocks in the scope block's subtree must be either complete block or
* reduction block
* 4) All the producers of the block are under the given loop
*
* \param block_rv The block to be moved
* \param loop_rv The loop where the block to be moved under
* \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
*/
virtual void ReverseComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
bool preserve_unit_loops) = 0;
/*!
* \brief Inline a block into its consumer(s). It requires:
* 1) The block is a complete non-root block, which only produces one buffer
Expand Down
5 changes: 0 additions & 5 deletions include/tvm/tir/schedule/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ class ScheduleStateNode : public Object {
*/
TVM_DLL void Replace(const tir::StmtSRef& src_sref, const Stmt& tgt_stmt,
const Map<Block, Block>& block_sref_reuse);
/*!
* \brief Recalculate the `affine_binding` flag of the scope block info.
* \param scope_sref The sref to the interested scope block.
*/
TVM_DLL void UpdateAffineFlag(const StmtSRef& scope_sref);
/*!
* \brief Trigger the verification according to the `debug_mask` bitmask.
* 1) If the bitmask `kVerifySRefTree` is on, verify the correctness of the sref tree.
Expand Down
186 changes: 184 additions & 2 deletions python/tvm/tir/schedule/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,183 @@ def after_cache_write(a: ty.handle, b: ty.handle) -> None:

########## Schedule: Compute location ##########

def compute_at(
self,
block: BlockRV,
loop: LoopRV,
preserve_unit_loops: bool = False,
) -> None:
"""Compute-At. Move a producer block under the specific loop, and regenerate the
loops induced by the block so that the buffer region produced by the producer block could
cover those regions consumed by its consumer blocks under the given loop. It requires:
1) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
2) The scope block has stage-pipeline property
3) The subtree of the scope block, where the given block is in, satisfies the compact
dataflow condition. i.e. all the blocks in the scope block's subtree must be either
complete block or reduction block
4) The block is not an output block with regard to the scope block, i.e. the buffers written
by the block are allocated under the scope block
5) All the consumers of the block are under the given loop
Parameters
----------
block : BlockRV
The block to be moved
loop: LoopRV
The loop where the block to be moved under
preserve_unit_loops: bool
Whether to keep the trivial loops whose extents are 1
Examples
--------
Before compute-at, in TensorIR, the IR is:
.. code-block:: python
@tvm.script.tir
def before_compute_at(a: ty.handle, c: ty.handle) -> None:
A = tir.match_buffer(a, (128, 128), "float32")
B = tir.alloc_buffer((128, 128), "float32")
C = tir.match_buffer(c, (128, 128), "float32")
with tir.block([128, 128], "B") as [vi, vj]:
B[vi, vj] = A[vi, vj] * 2.0
with tir.block([128, 128], "C") as [vi, vj]:
C[vi, vj] = B[vi, vj] + 1.0
Create the schedule and do compute-at:
.. code-block:: python
sch = tir.Schedule(before_compute_at)
block = sch.get_block("B")
loop, _ = sch.get_loops(sch.get_block("C"))
sch.compute_at(block, loop, preserve_unit_loops=False)
print(tvm.script.asscript(sch.mod["main"]))
After applying compute-at, the IR becomes:
.. code-block:: python
@tvm.script.tir
def after_compute_at(a: ty.handle, c: ty.handle) -> None:
A = tir.match_buffer(a, (128, 128), "float32")
B = tir.alloc_buffer((128, 128), "float32")
C = tir.match_buffer(c, (128, 128), "float32")
for i in tir.serial(0, 128):
for j in tir.serial(0, 128):
with tir.block([128, 128], "B") as [vi, vj]:
tir.bind(vi, i)
tir.bind(vj, j)
B[vi, vj] = A[vi, vj] * 2.0
for j in tir.serial(0, 128):
with tir.block([128, 128], "C") as [vi, vj]:
tir.bind(vi, i)
tir.bind(vj, j)
C[vi, vj] = B[vi, vj] + 1.0
"""
_ffi_api.ScheduleComputeAt( # type: ignore # pylint: disable=no-member
self,
block,
loop,
preserve_unit_loops,
)

def reverse_compute_at(
self,
block: BlockRV,
loop: LoopRV,
preserve_unit_loops: bool = False,
) -> None:
"""Reverse-Compute-At. Move a consumer block under the specific loop, and regenerate the
loops induced by the block so that the buffer region consumed by the consumer block could
cover those regions produced by its producer blocks under the given loop. It requires:
1) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
2) The scope block has stage-pipeline property
3) The subtree of the scope block, where the given block is in, satisfies the compact
dataflow condition. i.e. all the blocks in the scope block's subtree must be either
complete block or reduction block
4) All the producers of the block are under the given loop
Parameters
----------
block : BlockRV
The block to be moved
loop: LoopRV
The loop where the block to be moved under
preserve_unit_loops: bool
Whether to keep the trivial loops whose extents are 1
Examples
--------
Before reverse-compute-at, in TensorIR, the IR is:
.. code-block:: python
@tvm.script.tir
def before_reverse_compute_at(a: ty.handle, c: ty.handle) -> None:
A = tir.match_buffer(a, (128, 128), "float32")
B = tir.alloc_buffer((128, 128), "float32")
C = tir.match_buffer(c, (128, 128), "float32")
with tir.block([128, 128], "B") as [vi, vj]:
B[vi, vj] = A[vi, vj] * 2.0
with tir.block([128, 128], "C") as [vi, vj]:
C[vi, vj] = B[vi, vj] + 1.0
Create the schedule and do reverse-compute-at:
.. code-block:: python
sch = tir.Schedule(before_reverse_compute_at)
block = sch.get_block("C")
loop, _ = sch.get_loops(sch.get_block("B"))
sch.reverse_compute_at(block, loop, preserve_unit_loops=False)
print(tvm.script.asscript(sch.mod["main"]))
After applying reverse-compute-at, the IR becomes:
.. code-block:: python
@tvm.script.tir
def after_reverse_compute_at(a: ty.handle, c: ty.handle) -> None:
A = tir.match_buffer(a, (128, 128), "float32")
B = tir.alloc_buffer((128, 128), "float32")
C = tir.match_buffer(c, (128, 128), "float32")
for i in tir.serial(0, 128):
for j in tir.serial(0, 128):
with tir.block([128, 128], "B") as [vi, vj]:
tir.bind(vi, i)
tir.bind(vj, j)
B[vi, vj] = A[vi, vj] * 2.0
for j in tir.serial(0, 128):
with tir.block([128, 128], "C") as [vi, vj]:
tir.bind(vi, i)
tir.bind(vj, j)
C[vi, vj] = B[vi, vj] + 1.0
"""
_ffi_api.ScheduleReverseComputeAt( # type: ignore # pylint: disable=no-member
self,
block,
loop,
preserve_unit_loops,
)

def compute_inline(self, block: BlockRV) -> None:
"""Inline a block into its consumer(s). It requires:
Expand Down Expand Up @@ -1189,10 +1366,15 @@ def after_rfactor(a: ty.handle, b: ty.handle) -> None:
"""
return _ffi_api.ScheduleRFactor(self, loop, factor_axis) # type: ignore # pylint: disable=no-member

######## Schedule: Block annotatoin ########
######## Schedule: Block annotation ########

def storage_align( # pylint: disable=too-many-arguments
self, block: BlockRV, buffer_index: int, axis: int, factor: int, offset: int
self,
block: BlockRV,
buffer_index: int,
axis: int,
factor: int,
offset: int,
) -> None:
"""Set alignment requirement for specific dimension such that
stride[axis] == k * factor + offset for some k. This is useful to set memory layout for more
Expand Down
30 changes: 18 additions & 12 deletions src/arith/int_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,13 @@ inline bool ProveEqual(Analyzer* analyzer, PrimExpr lhs, PrimExpr rhs) {
return is_zero(analyzer->Simplify(lhs - rhs));
}

IntSet IntSet::FromMinExtent(PrimExpr min, PrimExpr extent) {
if (is_one(extent)) {
return IntSet::SinglePoint(min);
}
return IntervalSet(min, extent + min - 1);
}

IntSet IntSet::FromRange(Range r) {
// must make sure it can be matched back by MatchRange.
if (is_one(r->extent)) {
Expand Down Expand Up @@ -815,46 +822,45 @@ IntSet EvalSet(Range r, const Map<IterVar, IntSet>& dom_map) {
return EvalSet(r, ConvertDomMap(dom_map));
}

Optional<Array<arith::IntSet>> EstimateRegionLowerBound(const Array<Range>& region,
const Map<Var, Range>& var_dom,
const PrimExpr& predicate,
arith::Analyzer* analyzer) {
Optional<Array<IntSet>> EstimateRegionLowerBound(const Array<Range>& region,
const Map<Var, Range>& var_dom,
const PrimExpr& predicate, Analyzer* analyzer) {
int ndim = region.size();
Array<arith::IterSumExpr> iter_sum_exprs{nullptr};
Array<IterSumExpr> iter_sum_exprs{nullptr};
{
Array<PrimExpr> affine_indices;
affine_indices.reserve(ndim);
for (const Range& range : region) {
affine_indices.push_back(range->min);
}
iter_sum_exprs = arith::DetectIterMap(
iter_sum_exprs = DetectIterMap(
/*indices=*/affine_indices, /*input_iters=*/var_dom,
/*predicate=*/predicate, /*require_bijective=*/false, analyzer);
}
if (iter_sum_exprs.empty()) {
return NullOpt;
}
ICHECK_EQ(iter_sum_exprs.size(), ndim);
Array<arith::IntSet> result;
Array<IntSet> result;
result.reserve(ndim);
for (int i = 0; i < ndim; ++i) {
const arith::IterSumExpr& sum_expr = iter_sum_exprs[i];
const IterSumExpr& sum_expr = iter_sum_exprs[i];
const Range& range = region[i];
if (sum_expr->args.empty()) {
result.push_back(arith::IntSet::Interval(sum_expr->base, sum_expr->base + range->extent));
result.push_back(IntSet::FromMinExtent(sum_expr->base, range->extent));
continue;
}
ICHECK_EQ(sum_expr->args.size(), 1);
const arith::IterSplitExpr& split = sum_expr->args[0];
const IterSplitExpr& split = sum_expr->args[0];
if (!analyzer->CanProve(range->extent >= split->scale)) {
return NullOpt;
}
const PrimExpr& base = sum_expr->base;
// IterSplitExpr: (source // lower_factor) % extent * scale
// where `(source // lower_factor) % extent` is within [0, extent - 1]
// Therefore, the range of `region[i]->min` is `base + [0, (extent - 1) * scale]`
result.push_back(arith::IntSet::Interval(
base, split->extent * split->scale + base + (range->extent - split->scale) - 1));
result.push_back(
IntSet::FromMinExtent(base, split->extent * split->scale + (range->extent - split->scale)));
}
return result;
}
Expand Down
12 changes: 7 additions & 5 deletions src/relay/transforms/fold_scale_axis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ class ForwardPrep : private MixedModeVisitor {
}
}
// Visitor pattern override.
void VisitExpr_(const LetNode* op) {
void VisitExpr_(const TupleGetItemNode* op) final { MixedModeVisitor::VisitExpr_(op); }

void VisitExpr_(const LetNode* op) final {
ExprVisitor::VisitExpr_(op);
// do pass through condition
// by assigning NullValue<Message>
Expand All @@ -256,13 +258,13 @@ class ForwardPrep : private MixedModeVisitor {
flist_.push_back(flazy);
}

void VisitExpr_(const FunctionNode* op) {
void VisitExpr_(const FunctionNode* op) final {
ExprVisitor::VisitExpr_(op);
auto flazy = [this, op] { this->Update(op->body, NullValue<Message>()); };
flist_.push_back(flazy);
}

void VisitExpr_(const CallNode* call) {
void VisitExpr_(const CallNode* call) final {
ExprVisitor::VisitExpr_(call);
// function to be lazily invoked
auto flazy = [this, call]() {
Expand Down Expand Up @@ -292,7 +294,7 @@ class ForwardPrep : private MixedModeVisitor {
flist_.push_back(flazy);
}

void VisitExpr_(const TupleNode* op) {
void VisitExpr_(const TupleNode* op) final {
ExprVisitor::VisitExpr_(op);
// do not support pass scale through tuple for now.
auto flazy = [this, op]() {
Expand All @@ -303,7 +305,7 @@ class ForwardPrep : private MixedModeVisitor {
flist_.push_back(flazy);
}

void VisitExpr_(const IfNode* op) {
void VisitExpr_(const IfNode* op) final {
ExprVisitor::VisitExpr_(op);
// do pass through condition
// by assigning NullValue<Message>
Expand Down
Loading

0 comments on commit 08c911b

Please sign in to comment.