-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Transform] Modify FuseTIR pass to propagate buffer attributes #17075
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
Changes from all 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 |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import pytest | ||
|
|
||
| import tvm | ||
| import tvm.testing | ||
| from tvm import relax, topi | ||
|
|
@@ -2314,5 +2316,131 @@ def take( | |
| _check(Before, Before) | ||
|
|
||
|
|
||
| def test_fuse_with_axis_separators(): | ||
| @I.ir_module | ||
| class Before: | ||
| @T.prim_func(private=True) | ||
| def add(a: T.handle, b: T.handle, c: T.handle): | ||
| A = T.match_buffer(a, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
| B = T.match_buffer(b, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
| C = T.match_buffer(c, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
|
|
||
| for iters in T.grid(T.int64(16), T.int64(32)): | ||
| with T.block("compute"): | ||
| i, j = T.axis.remap("SS", iters) | ||
| C[i, j] = A[i, j] + B[i, j] | ||
|
|
||
| @R.function(private=True) | ||
| def fused_function( | ||
| x: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| y: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| z: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| ) -> R.Tensor([T.int64(16), T.int64(32)], dtype="float32"): | ||
| R.func_attr({"Primitive": 1}) | ||
| cls = Before | ||
| with R.dataflow(): | ||
| w = R.call_tir( | ||
| cls.add, [x, y], out_sinfo=R.Tensor([T.int64(16), T.int64(32)], "float32") | ||
|
Contributor
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. Can we add a test case for incompatible usage of a single Relax var? As currently written, we could have a single Relax variable that is used in two separate (Ideally, that should never happen, but this would be the last point at which we'd have enough information to catch this failure mode at compile-time.)
Contributor
Author
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. I've added the test case to check possible inconsistencies. |
||
| ) | ||
| out = R.call_tir( | ||
| cls.add, [w, z], out_sinfo=R.Tensor([T.int64(16), T.int64(32)], "float32") | ||
| ) | ||
| R.output(out) | ||
| return out | ||
|
|
||
| @R.function | ||
| def main( | ||
| x: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| y: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| z: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| ) -> R.Tensor([T.int64(16), T.int64(32)], dtype="float32"): | ||
| cls = Before | ||
| with R.dataflow(): | ||
| gv = cls.fused_function(x, y, z) | ||
| R.output(gv) | ||
| return gv | ||
|
|
||
| @I.ir_module | ||
| class Expected: | ||
| @T.prim_func(private=True) | ||
| def fused_function(x: T.handle, y: T.handle, z: T.handle, c: T.handle): | ||
| T.func_attr({"tir.noalias": True}) | ||
| X = T.match_buffer(x, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
| Y = T.match_buffer(y, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
| Z = T.match_buffer(z, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
| C = T.match_buffer(c, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
| Temp = T.alloc_buffer(X.shape, "float32", axis_separators=[1]) | ||
| for iters in T.grid(*X.shape): | ||
| with T.block("compute_Y"): | ||
| i, j = T.axis.remap("SS", iters) | ||
| Temp[i, j] = X[i, j] + Y[i, j] | ||
|
|
||
| for iters in T.grid(*X.shape): | ||
| with T.block("compute_Z"): | ||
| i, j = T.axis.remap("SS", iters) | ||
| C[i, j] = Temp[i, j] + Z[i, j] | ||
|
|
||
| @R.function | ||
| def main( | ||
| x: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| y: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| z: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| ) -> R.Tensor([T.int64(16), T.int64(32)], dtype="float32"): | ||
| cls = Expected | ||
| with R.dataflow(): | ||
| gv = R.call_tir( | ||
| cls.fused_function, | ||
| [x, y, z], | ||
| out_sinfo=R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| ) | ||
| R.output(gv) | ||
| return gv | ||
|
|
||
| _check(Before, Expected) | ||
|
|
||
|
|
||
| def test_fuse_with_axis_separators_inconsistent_buffer_mapping(): | ||
| @I.ir_module | ||
| class Before: | ||
| @T.prim_func(private=True) | ||
| def mul(a: T.handle, b: T.handle, c: T.handle): | ||
| A = T.match_buffer(a, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
| B = T.match_buffer(b, [T.int64(16), T.int64(32)], "float32", axis_separators=[]) | ||
| C = T.match_buffer(c, [T.int64(16), T.int64(32)], "float32", axis_separators=[1]) | ||
|
|
||
| for iters in T.grid(T.int64(16), T.int64(32)): | ||
| with T.block("compute"): | ||
| i, j = T.axis.remap("SS", iters) | ||
| C[i, j] = A[i, j] * B[i, j] | ||
|
|
||
| @R.function(private=True) | ||
| def fused_function( | ||
| x: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| ) -> R.Tensor([T.int64(16), T.int64(32)], dtype="float32"): | ||
| R.func_attr({"Primitive": 1}) | ||
| cls = Before | ||
| with R.dataflow(): | ||
| out = R.call_tir( | ||
| cls.mul, [x, x], out_sinfo=R.Tensor([T.int64(16), T.int64(32)], "float32") | ||
| ) | ||
| R.output(out) | ||
| return out | ||
|
|
||
| @R.function | ||
| def main( | ||
| x: R.Tensor([T.int64(16), T.int64(32)], "float32"), | ||
| ) -> R.Tensor([T.int64(16), T.int64(32)], dtype="float32"): | ||
| cls = Before | ||
| with R.dataflow(): | ||
| gv = cls.fused_function(x) | ||
| R.output(gv) | ||
| return gv | ||
|
|
||
| with pytest.raises( | ||
| tvm.TVMError, match=r"Inconsistent buffers.*and.*mapped to the same relax var:.*" | ||
| ): | ||
| relax.transform.FuseTIR()(Before) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tvm.testing.main() | ||
Uh oh!
There was an error while loading. Please reload this page.