-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Unity][Frontend] NNModule tensor_ir_op support
#16278
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
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # pylint: disable=missing-docstring, invalid-name | ||
| import tvm | ||
| import tvm.testing | ||
| from tvm import tir | ||
|
|
@@ -508,5 +509,134 @@ def test(x: R.Tensor((10, 10), dtype="float32"), _io: R.Object) -> R.Tuple(R.Ten | |
| tvm.ir.assert_structural_equal(irmodule, Expected) | ||
|
|
||
|
|
||
| def test_tensor_ir_op(): | ||
| num_q_heads, num_kv_heads, head_dim = 8, 8, 16 | ||
|
Member
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. This unittest is a bit more complicated than I expected :)) in the simplest case, we could probably just supply a “B = A + 1”-style TIR |
||
| fused_heads = num_q_heads + num_kv_heads * 2 | ||
| dtype = "float16" | ||
|
|
||
| @T.prim_func(private=True) | ||
| def fused_rope( # pylint: disable=too-many-locals | ||
| var_qkv: T.handle, | ||
| offset: T.int64, | ||
| var_q: T.handle, | ||
| var_k: T.handle, | ||
| var_v: T.handle, | ||
| ): | ||
| batch_size = T.int64() | ||
| seq_len = T.int64() | ||
| qkv = T.match_buffer(var_qkv, (batch_size, seq_len, fused_heads, head_dim), dtype) | ||
| q = T.match_buffer(var_q, (batch_size, seq_len, num_q_heads, head_dim), dtype) | ||
| k = T.match_buffer(var_k, (batch_size, seq_len, num_kv_heads, head_dim), dtype) | ||
| v = T.match_buffer(var_v, (batch_size, seq_len, num_kv_heads, head_dim), dtype) | ||
| T.evaluate(offset) | ||
|
|
||
| class Model(Module): | ||
| def test(self, qkv: Tensor, offset: tir.Var): | ||
| tensor_expr_op_out = op.tensor_ir_op( | ||
| fused_rope, | ||
| "llama_fused_rope", | ||
| args=[qkv, offset], | ||
| out=[ | ||
| Tensor.placeholder((1, 1, num_q_heads, head_dim), dtype), | ||
| Tensor.placeholder((1, 1, num_kv_heads, head_dim), dtype), | ||
| Tensor.placeholder((1, 1, num_kv_heads, head_dim), dtype), | ||
| ], | ||
| ) | ||
| return tensor_expr_op_out | ||
|
|
||
| # fmt: off | ||
| @I.ir_module | ||
| class Expected: | ||
| @T.prim_func(private=True) | ||
| def llama_fused_rope(var_qkv: T.handle, offset: T.int64, var_q: T.handle, var_k: T.handle, var_v: T.handle): | ||
| batch_size, seq_len = T.int64(), T.int64() | ||
| qkv = T.match_buffer(var_qkv, (batch_size, seq_len, 24, 16), "float16") | ||
| q = T.match_buffer(var_q, (batch_size, seq_len, 8, 16), "float16") | ||
| k = T.match_buffer(var_k, (batch_size, seq_len, 8, 16), "float16") | ||
| v = T.match_buffer(var_v, (batch_size, seq_len, 8, 16), "float16") | ||
| T.evaluate(offset) | ||
|
|
||
| @R.function | ||
| def _initialize_effect() -> R.Tuple(R.Object): | ||
| with R.dataflow(): | ||
| _io: R.Object = R.null_value() | ||
| lv: R.Tuple(R.Object) = (_io,) | ||
| gv: R.Tuple(R.Object) = lv | ||
| R.output(gv) | ||
| return gv | ||
|
|
||
| @R.function | ||
| def test(qkv: R.Tensor((1, 1, 24, 16), dtype="float16"), offset: R.Shape(["offset_1"]), _io: R.Object) -> R.Tuple(R.Tuple(R.Tensor((1, 1, 8, 16), dtype="float16"), R.Tensor((1, 1, 8, 16), dtype="float16"), R.Tensor((1, 1, 8, 16), dtype="float16")), R.Tuple(R.Object)): | ||
| offset_1 = T.int64() | ||
| R.func_attr({"num_input": 3}) | ||
| cls = Expected | ||
| with R.dataflow(): | ||
| lv1 = R.call_tir(cls.llama_fused_rope, (qkv,), out_sinfo=[R.Tensor((1, 1, 8, 16), dtype="float16"), R.Tensor((1, 1, 8, 16), dtype="float16"), R.Tensor((1, 1, 8, 16), dtype="float16")], tir_vars=R.shape([offset_1])) | ||
| llama_fused_rope_0: R.Tensor((1, 1, 8, 16), dtype="float16") = lv1[0] | ||
| llama_fused_rope_1: R.Tensor((1, 1, 8, 16), dtype="float16") = lv1[1] | ||
| llama_fused_rope_2: R.Tensor((1, 1, 8, 16), dtype="float16") = lv1[2] | ||
| gv1: R.Tuple(R.Tuple(R.Tensor((1, 1, 8, 16), dtype="float16"), R.Tensor((1, 1, 8, 16), dtype="float16"), R.Tensor((1, 1, 8, 16), dtype="float16")), R.Tuple(R.Object)) = (llama_fused_rope_0, llama_fused_rope_1, llama_fused_rope_2), (_io,) | ||
| R.output(gv1) | ||
| return gv1 | ||
| # fmt: on | ||
|
|
||
| m = Model() | ||
| irmodule, _ = m.export_tvm( | ||
| spec={ | ||
| "test": {"qkv": spec.Tensor([1, 1, fused_heads, head_dim], "float16"), "offset": int} | ||
| }, | ||
| debug=True, | ||
| ) | ||
| tvm.ir.assert_structural_equal(irmodule, Expected) | ||
|
|
||
|
|
||
| def test_extern(): | ||
| class Model(Module): | ||
| def test(self, q: Tensor, k: Tensor, v: Tensor): | ||
| b, s, h_q, d = q.shape | ||
| tensor_expr_op_out = op.extern( | ||
| name="flashinfer.single_decode", | ||
| args=[q, k, v, 0, 0, 1.0, 10000.0], | ||
| out=Tensor.placeholder((b, s, h_q * d), dtype="float16"), | ||
| ) | ||
| return tensor_expr_op_out | ||
|
|
||
| # fmt: off | ||
| @I.ir_module | ||
| class Expected: | ||
| @R.function | ||
| def _initialize_effect() -> R.Tuple(R.Object): | ||
| with R.dataflow(): | ||
| _io: R.Object = R.null_value() | ||
| lv: R.Tuple(R.Object) = (_io,) | ||
| gv: R.Tuple(R.Object) = lv | ||
| R.output(gv) | ||
| return gv | ||
|
|
||
| @R.function | ||
| def test(q: R.Tensor((1, 1, 16, 8), dtype="float32"), k: R.Tensor((64, 16, 8), dtype="float32"), v: R.Tensor((64, 16, 8), dtype="float32"), _io: R.Object) -> R.Tuple(R.Tensor((1, 1, 128), dtype="float16"), R.Tuple(R.Object)): | ||
| R.func_attr({"num_input": 4}) | ||
| with R.dataflow(): | ||
| flashinfer_single_decode = R.call_dps_packed("flashinfer.single_decode", (q, k, v, R.prim_value(0), R.prim_value(0), R.prim_value(T.float64(1)), R.prim_value(T.float64(10000))), out_sinfo=R.Tensor((1, 1, 128), dtype="float16")) | ||
| gv1: R.Tuple(R.Tensor((1, 1, 128), dtype="float16"), R.Tuple(R.Object)) = flashinfer_single_decode, (_io,) | ||
| R.output(gv1) | ||
| return gv1 | ||
| # fmt: on | ||
|
|
||
| batch, seq, t, d, h_q, h_kv = 1, 1, 64, 8, 16, 16 | ||
| m = Model() | ||
| irmodule, _ = m.export_tvm( | ||
| spec={ | ||
| "test": { | ||
| "q": spec.Tensor([batch, seq, h_q, d], "float32"), | ||
| "k": spec.Tensor([t, h_kv, d], "float32"), | ||
| "v": spec.Tensor([t, h_kv, d], "float32"), | ||
| } | ||
| }, | ||
| debug=True, | ||
| ) | ||
| tvm.ir.assert_structural_equal(irmodule, Expected) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tvm.testing.main() | ||
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.
There’s a bit of complication here: if the PrimFunc provided is a public function (has “global_symbol” field in its attrs), Relax is not allowed to rename it, and in this case, it’s not a name hint but a name instead. Therefore, we will have to check symbol duplication and potentially throw an error if it happens.
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.
We could probably leave this logic to future work, but let’s rename name_hint to name to better reflect this point
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.
I agree and thanks for pointing it out. However, the current Python interface
AddFunctionalso treats it asname_hint, which may be renamed if conflicts exist.It would be an independent problem out of the scope of this PR.