Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/tir/ir/specialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class PrimFuncSpecializer : public StmtExprMutator {
f_ptr->params = std::move(params);
f_ptr->buffer_map = std::move(buffer_map);
f_ptr->body = std::move(body);
f_ptr->struct_info_ = NullOpt;
f_ptr->checked_type_ = Type(nullptr);
}
return f;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/python/tir-base/test_tir_specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# under the License.
# pylint: disable=missing-function-docstring, missing-module-docstring

import pytest

import tvm
from tvm.script import tir as T
from tvm.tir.schedule.testing import assert_structural_equal_ignore_global_symbol
Expand Down Expand Up @@ -324,5 +326,41 @@ def expected(A_data: T.handle("float32")):
tvm.ir.assert_structural_equal(expected, after)


def test_specialization_removes_struct_info():
"""Reset struct info in specialization

While a PrimFunc usually doesn't have a `relax.StructInfo`, the
field can be populated in some edge cases. If that PrimFunc is
Comment on lines +332 to +333
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know of times when we're doing it? It should be something we do consistently or not at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a couple of MLC (example) where the a dynamic PrimFunc is annotated with the relax.FuncStructInfo. This allows the R.call_tir shape inference to work correctly, even after the arguments are mutated.

IIRC, the primary reasons not to add FuncStructInfo to PrimFuncs was that primfuncs didn't follow the relax requirements for functional purity. Since we now have the purity expressed within FuncStructInfo, I think that means we could add FuncStructInfo to every PrimFunc on construction, without making any incorrect statements about the functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always specifying it sounds like a better approach, as long as it would not interfere with anything in TIR.

specialized, the struct info should be reset.
"""

@T.prim_func(private=True)
def before(n: T.int32) -> T.int32:
T.ret(n * 10)

@T.prim_func(private=True)
def expected() -> T.int32:
T.ret(50)

sinfo = tvm.relax.FuncStructInfo(
[tvm.relax.PrimStructInfo("int32")], tvm.relax.PrimStructInfo("int32")
)
tvm.relax.expr._update_struct_info(before, sinfo)

n = before.params[0]
param_map = {n: 5}
after = before.specialize(param_map)

tvm.ir.assert_structural_equal(expected, after)
assert before.struct_info is not None

# PrimFuncs do not expose the `struct_info_` field. Checking the
# `struct_info` field when it isn't set raises an exception. This
# is the desired behavior, since the struct info before
# specialization is no longer valid.
with pytest.raises(tvm.TVMError):
after.struct_info


if __name__ == "__main__":
tvm.testing.main()