-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TensorIR][M1c] LowerInitBlock #7806
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* Lower block init stmt into branch stmt | ||
* \file lower_reduction.cc | ||
*/ | ||
#include <tvm/tir/op.h> | ||
#include <tvm/tir/stmt_functor.h> | ||
#include <tvm/tir/transform.h> | ||
|
||
namespace tvm { | ||
namespace tir { | ||
|
||
class InitBlockLower : public StmtMutator { | ||
private: | ||
Stmt VisitStmt_(const BlockNode* block) final { | ||
if (!block->init.defined()) { | ||
return StmtMutator::VisitStmt_(block); | ||
} | ||
Stmt init = DoLowering(block->init.value(), block->iter_vars); | ||
Stmt body = VisitStmt(block->body); | ||
auto n = CopyOnWrite(block); | ||
n->init = NullOpt; | ||
n->body = SeqStmt::Flatten(init, body); | ||
return Block(n); | ||
} | ||
|
||
static Stmt DoLowering(const Stmt& init, const Array<IterVar>& iter_vars) { | ||
std::vector<PrimExpr> conditions; | ||
for (const IterVar& var : iter_vars) { | ||
if (var->iter_type == IterVarType::kCommReduce) { | ||
conditions.push_back(equal(var->var, var->dom->min)); | ||
} | ||
} | ||
// Handle the case where there is no condition | ||
if (conditions.empty()) { | ||
return init; | ||
} | ||
// Concat the conditions with logical and (&&) | ||
PrimExpr cond = conditions[0]; | ||
for (size_t i = 1; i < conditions.size(); ++i) { | ||
cond = logical_and(cond, conditions[i]); | ||
} | ||
return IfThenElse(cond, init); | ||
} | ||
}; | ||
|
||
PrimFunc LowerInitBlock(PrimFunc func) { | ||
auto fptr = func.CopyOnWrite(); | ||
fptr->body = InitBlockLower()(std::move(fptr->body)); | ||
return func; | ||
} | ||
|
||
namespace transform { | ||
|
||
Pass LowerInitBlock() { | ||
auto pass_func = [](PrimFunc f, IRModule m, PassContext ctx) { | ||
return LowerInitBlock(std::move(f)); | ||
}; | ||
return CreatePrimFuncPass(pass_func, 0, "tir.LowerReduction", {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("tir.transform.LowerInitBlock").set_body_typed(LowerInitBlock); | ||
|
||
} // namespace transform | ||
|
||
} // namespace tir | ||
} // namespace tvm |
53 changes: 53 additions & 0 deletions
53
tests/python/unittest/test_tir_transform_lower_init_block.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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. | ||
import tvm | ||
from tvm import tir | ||
from tvm.script import ty | ||
|
||
|
||
@tvm.script.tir | ||
class WithInit: | ||
def main(a: ty.handle, b: ty.handle) -> None: | ||
A = tir.match_buffer(a, [64, 64, 64]) | ||
B = tir.match_buffer(b, [64]) | ||
|
||
with tir.block([64, tir.reduce_axis(0, 64), tir.reduce_axis(32, 64)]) as [i, j, k]: | ||
with tir.init(): | ||
B[i] = tir.float32(0) | ||
B[i] += A[i, j, k] | ||
|
||
|
||
@tvm.script.tir | ||
class WithBranch: | ||
def main(a: ty.handle, b: ty.handle) -> None: | ||
A = tir.match_buffer(a, [64, 64, 64]) | ||
B = tir.match_buffer(b, [64]) | ||
|
||
with tir.block([64, tir.reduce_axis(0, 64), tir.reduce_axis(32, 64)]) as [i, j, k]: | ||
if (j == 0) and (k == 32): | ||
B[i] = tir.float32(0) | ||
B[i] += A[i, j, k] | ||
|
||
|
||
def test_lower_reduction(): | ||
origin_mod = WithInit() | ||
mod = tvm.tir.transform.LowerInitBlock()(origin_mod) | ||
tvm.ir.assert_structural_equal(mod, WithBranch(), True) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_lower_reduction() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note here: per discussion with @Hzfengsy, we need to remove "()" here in future PRs, so that the script parser API is consistent across PrimFunc and IRModule