Skip to content
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

[Relax][Bugfix] Bind symbolic variables in R.match_cast #17023

Closed
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
22 changes: 16 additions & 6 deletions src/relax/ir/expr_functor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ RELAX_EXPR_VISITOR_VISIT_BINDING_IMPL(DataTypeImmNode);

void ExprVisitor::VisitBinding_(const MatchCastNode* binding) {
this->VisitExpr(binding->value);
this->VisitExprDepStructInfoField(binding->struct_info);
this->VisitVarDef(binding->var);
}

Expand Down Expand Up @@ -690,16 +691,25 @@ void ExprMutator::ReEmitBinding(const VarBindingNode* binding, Expr new_value) {
}

void ExprMutator::VisitBinding_(const MatchCastNode* binding) {
Var new_var = this->VisitVarDef(binding->var);
Expr new_value = this->VisitExpr(binding->value);
StructInfo new_struct_info = this->VisitExprDepStructInfoField(binding->struct_info);

// re-emit old binding if nothing changes
if (new_var.same_as(binding->var) && new_value.same_as(binding->value)) {
Var new_var = this->VisitVarDef(binding->var);

if (new_var.same_as(binding->var) && new_value.same_as(binding->value) &&
new_struct_info.same_as(binding->struct_info)) {
// re-emit old binding if nothing changes
builder_->EmitNormalized(GetRef<MatchCast>(binding));
} else {
new_value = builder_->NormalizeArgument(new_value);
builder_->EmitNormalized(MatchCast(new_var, new_value, binding->struct_info, binding->span));
return;
}

new_value = builder_->NormalizeArgument(new_value);
new_var = WithStructInfo(new_var, new_struct_info);

var_remap_[binding->var->vid] = new_var;
var_remap_[new_var->vid] = new_var;

builder_->EmitNormalized(MatchCast(new_var, new_value, new_struct_info, binding->span));
}

BindingBlock ExprMutator::VisitBindingBlock_(const BindingBlockNode* block) {
Expand Down
22 changes: 22 additions & 0 deletions tests/python/relax/test_bind_symbolic_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,27 @@ def expected(A: R.Tensor(["M", 32])):
tvm.ir.assert_structural_equal(expected, after)


def test_bind_inside_match_cast():
"""Symbolic variables may occur within R.match_cast"""

@R.function(private=True)
def before(A: R.Tensor(["M", "N"]), B: R.Tensor(ndim=2)):
M = T.int64()
N = T.int64()
C = R.match_cast(B, R.Tensor([M, N]))
D = R.add(A, C)
return D

@R.function(private=True)
def expected(A: R.Tensor(["M", 32]), B: R.Tensor(ndim=2)):
M = T.int64()
C = R.match_cast(B, R.Tensor([M, 32]))
D = R.add(A, C)
return D

after = before.bind_symbolic_vars({"N": 32})
tvm.ir.assert_structural_equal(expected, after)


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