-
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
[PASS] Refactor a couple of TIR passes - BindTarget, AnnotateEntryFunc, Filter, LowerInitBlock #11628
[PASS] Refactor a couple of TIR passes - BindTarget, AnnotateEntryFunc, Filter, LowerInitBlock #11628
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 helpers.cc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we might want to find better names other than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. How about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that sounds a reasonable choice! |
||
* \brief Passes that serve as helper functions. | ||
*/ | ||
|
||
#include <tvm/driver/driver_api.h> | ||
#include <tvm/tir/transform.h> | ||
|
||
namespace tvm { | ||
namespace tir { | ||
namespace transform { | ||
transform::Pass BindTarget(Target target) { | ||
auto fpass = [target](tir::PrimFunc f, IRModule m, transform::PassContext ctx) { | ||
return WithAttr(std::move(f), tvm::attr::kTarget, target); | ||
}; | ||
return tir::transform::CreatePrimFuncPass(fpass, 0, "tir.BindTarget", {}); | ||
} | ||
|
||
transform::Pass AnnotateEntryFunc() { | ||
auto fpass = [](tir::PrimFunc f, IRModule m, transform::PassContext ctx) { | ||
ICHECK(m->functions.size() == 1); | ||
return WithAttr(std::move(f), tir::attr::kIsEntryFunc, Bool(true)); | ||
}; | ||
return tir::transform::CreatePrimFuncPass(fpass, 0, "tir.AnnotateEntryFunc", {}); | ||
} | ||
|
||
transform::Pass Filter(runtime::TypedPackedFunc<bool(PrimFunc)> fcond) { | ||
auto fpass = [fcond](tir::PrimFunc f, IRModule m, transform::PassContext ctx) { | ||
if (fcond(f)) { | ||
return f; | ||
} else { | ||
return tir::PrimFunc(nullptr); | ||
} | ||
}; | ||
return tir::transform::CreatePrimFuncPass(fpass, 0, "tir.Filter", {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("tir.transform.BindTarget").set_body_typed(BindTarget); | ||
TVM_REGISTER_GLOBAL("tir.transform.AnnotateEntryFunc").set_body_typed(AnnotateEntryFunc); | ||
TVM_REGISTER_GLOBAL("tir.transform.Filter").set_body_typed(Filter); | ||
|
||
} // namespace transform | ||
} // namespace tir | ||
} // namespace tvm |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# 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 pytest | ||
|
||
import tvm | ||
from tvm.script import tir as T | ||
import tvm.testing | ||
|
||
|
||
def test_annotate_entry_func_single_primfunc(): | ||
@tvm.script.ir_module | ||
class MockModule: | ||
@T.prim_func | ||
def func1(A: T.Buffer[(16,), "float32"]): | ||
for i in T.serial(16): | ||
if i == 5: | ||
if i == 5: | ||
A[i] = 0.0 | ||
|
||
mod = MockModule | ||
assert mod | ||
assert mod["func1"].attrs is None | ||
after = tvm.tir.transform.AnnotateEntryFunc()(mod) | ||
assert ( | ||
after["func1"].attrs | ||
and "tir.is_entry_func" in after["func1"].attrs | ||
and after["func1"].attrs["tir.is_entry_func"] | ||
) | ||
|
||
|
||
# Test module | ||
@tvm.script.ir_module | ||
class MockModule: | ||
@T.prim_func | ||
def func1(A: T.Buffer[(16,), "float32"]): | ||
for i in T.serial(16): | ||
if i == 5: | ||
if i == 5: | ||
A[i] = 0.0 | ||
|
||
@T.prim_func | ||
def func2(A: T.Buffer[(32,), "float32"]): | ||
for i in T.serial(32): | ||
if i == 15: | ||
if i == 15: | ||
A[i] = 0.0 | ||
|
||
|
||
@pytest.mark.xfail | ||
def test_annotate_entry_func_multiple_primfunc(): | ||
mod = MockModule | ||
assert mod | ||
assert mod["func1"].attrs is None | ||
assert mod["func2"].attrs is None | ||
# This should fail | ||
after = tvm.tir.transform.AnnotateEntryFunc()(mod) | ||
|
||
|
||
def test_bind_target(): | ||
mod = MockModule | ||
assert mod | ||
|
||
target = tvm.target.Target("cuda") | ||
assert mod["func1"].attrs is None | ||
assert mod["func2"].attrs is None | ||
after = tvm.tir.transform.BindTarget(target)(mod) | ||
|
||
assert after["func1"].attrs and "target" in after["func1"].attrs | ||
assert after["func1"].attrs["target"] == target | ||
assert after["func2"].attrs and "target" in after["func2"].attrs | ||
assert after["func2"].attrs["target"] == target | ||
|
||
|
||
def test_filter_primfunc(): | ||
mod = MockModule | ||
assert mod | ||
# Annotate each function for testing | ||
mod["func1"] = mod["func1"].with_attr("temp", "test1") | ||
mod["func2"] = mod["func2"].with_attr("temp", "test2") | ||
|
||
# Test condition that does not filter out anything | ||
def checker_filter_out_none(func: tvm.tir.PrimFunc): | ||
return (func.attrs is not None) and ("temp" in func.attrs) | ||
|
||
after = tvm.tir.transform.Filter(checker_filter_out_none)(mod) | ||
assert len(after.functions) == 2 | ||
# Filtered functions should satisfy the given condition. | ||
assert checker_filter_out_none(after["func1"]) | ||
assert checker_filter_out_none(after["func2"]) | ||
|
||
# Test condition that selectively filters out primfuncs | ||
def checker_filter_out_one(func: tvm.tir.PrimFunc): | ||
return (func.attrs is not None) and ("temp" in func.attrs) and func.attrs["temp"] == "test1" | ||
|
||
after = tvm.tir.transform.Filter(checker_filter_out_one)(mod) | ||
assert len(after.functions) == 1 | ||
# Filtered functions should satisfy the given condition. | ||
assert checker_filter_out_one(after["func1"]) | ||
|
||
# Test condition that filters out everything | ||
def checker_filter_out_both(func: tvm.tir.PrimFunc): | ||
return (func.attrs is not None) and ("invalid_attr" in func.attrs) | ||
|
||
after = tvm.tir.transform.Filter(checker_filter_out_both)(mod) | ||
assert len(after.functions) == 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
tvm.testing.main() |
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.
Is it possible that we don't depend on
driver_api.h
?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.
Thanks for pointing out! Changed to
#include <tvm/target/target.h>
which is more accurate.