Skip to content
Open
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
45 changes: 39 additions & 6 deletions src/relax/transform/fuse_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1029,12 +1029,7 @@ class OperatorFusor : public ExprMutator {
// Only check those group defined before.
// Skip the vars from input or groups with single binding.
if (producer_group != cur_group) {
for (Group* depgroup : group_deps_[producer_group]) {
TVM_FFI_ICHECK(depgroup != cur_group)
<< "A cyclic dependency detected between the groups " << binding->var->name
<< " and " << used_var->name << " are in.";
}
group_deps_[cur_group].push_back(producer_group);
AddGroupDep(cur_group, producer_group, binding->var->name, used_var->name);
}

if (auto producer = group2func_.find(producer_group);
Expand All @@ -1051,7 +1046,43 @@ class OperatorFusor : public ExprMutator {
TVM_FFI_ICHECK_NOTNULL(match_cast);
PostOrderVisit(match_cast->value, update_boundary);
}

// Step 3. A symbolic shape var may only appear in the binding's resulting type(e.g. a
// call_tir's out_ty), invisible to the relax::Var traversal above. Depend on the group
// whose MatchCast defined that var.
for (const tvm::Var& used_tir_var : TIRVarsInType(GetType(binding->var))) {
if (auto it = symbolic_var_def_group_.find(used_tir_var);
it != symbolic_var_def_group_.end() && it->second != cur_group) {
AddGroupDep(cur_group, it->second, binding->var->name, used_tir_var->name);
}
}

// Step 4. Record which group first defines each symbolic var a MatchCast introduces, so
// later bindings (Step 3) can depend on it. First write wins: re-matching the same var
// later must not move its defining group.
if (const auto* match_cast = binding.as<MatchCastObj>()) {
for (const tvm::Var& defined_var : DefinableTIRVarsInType(match_cast->ty)) {
symbolic_var_def_group_.emplace(defined_var, cur_group);
}
}
}
}

/*!
* \brief Record that `cur_group` depends on `producer_group`, checking for cycles.
* \param cur_group The group of the binding with the dependency.
* \param producer_group The group being depended on.
* \param consumer_name The name of the dependent binding's var, used for the error message.
* \param producer_name The name of the dependent-on var, used for the error message.
*/
void AddGroupDep(Group* cur_group, Group* producer_group, const ffi::String& consumer_name,
const ffi::String& producer_name) {
for (Group* depgroup : group_deps_[producer_group]) {
TVM_FFI_ICHECK(depgroup != cur_group)
<< "A cyclic dependency detected between the groups " << consumer_name << " and "
<< producer_name << " are in.";
}
group_deps_[cur_group].push_back(producer_group);
}

/*!
Expand Down Expand Up @@ -1146,6 +1177,8 @@ class OperatorFusor : public ExprMutator {
* linear search is OK.
*/
std::unordered_map<Group*, std::vector<Group*>> group_deps_;
/*! \brief A map from a symbolic var to the group of the MatchCast that first defines it. */
std::unordered_map<tvm::Var, Group*> symbolic_var_def_group_;
/*! \brief Whether or not to lift bound constants to parameters of the grouped function. */
bool lift_constants_{true};
};
Expand Down
93 changes: 93 additions & 0 deletions tests/python/relax/test_transform_fuse_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,99 @@ def main(
assert sum(isinstance(param.ty, tvm_tirx.ir.PrimType) for param in fused.params) == 1


def test_fuseops_respects_match_cast_shape_dependency():
"""A call_tir's out_ty may use a symbolic var only an earlier match_cast defines."""

@I.ir_module
class Before:
make_shape_n = T.int64()

@Ts.function(private=True)
def make_shape(
x: T.Tensor((make_shape_n, T.int64(8)), "float32"),
out: T.Tensor((T.int64(2),), "int64"),
):
T.func_attr({"op_pattern": 0, "tirx.noalias": True})
with Ts.sblock("write0"):
out[0] = x.shape[0]
with Ts.sblock("write1"):
out[1] = T.int64(8)

copy_2d_m = T.int64()
copy_2d_n = T.int64()

@Ts.function(private=True)
def copy_2d(
x: T.Tensor((copy_2d_m, T.int64(8)), "float32"),
out: T.Tensor((copy_2d_n, T.int64(8)), "float32"),
):
T.func_attr({"op_pattern": 8, "tirx.noalias": True})
for i, j in T.grid(out.shape[0], T.int64(8)):
with Ts.sblock("copy"):
vi, vj = Ts.axis.remap("SS", [i, j])
out[vi, vj] = T.float32(1.0)

def emit_shape_bound_copy(bb, x, shape_gv, copy_gv, shape_var, name_prefix):
shape_tensor = bb.emit(
relax.call_tir(shape_gv, (x,), ty_args=[relax.TensorType((2,), "int64")]),
name_hint=f"{name_prefix}_shape_tensor",
)
shape = bb.emit(relax.op.tensor_to_shape(shape_tensor), name_hint=f"{name_prefix}_shape")
bb.match_cast(
shape,
relax.ShapeType([shape_var, 8]),
name_hint=f"{name_prefix}_shape_bound",
)
return bb.emit(
relax.call_tir(copy_gv, (x,), ty_args=[relax.TensorType((shape_var, 8), "float32")]),
name_hint=f"{name_prefix}_copy",
)

shape_gv = Before.get_global_var("make_shape")
copy_gv = Before.get_global_var("copy_2d")
bb = relax.BlockBuilder(Before)

m = tvm_tirx.tirx.Var("m", "int64")
d0 = tvm_tirx.tirx.Var("d0", "int64")
s0 = tvm_tirx.tirx.Var("s0", "int64")
s1 = tvm_tirx.tirx.Var("s1", "int64")
x = relax.Var("x", relax.TensorType((m, 8), "float32"))

with bb.function("main", params=[x]):
with bb.dataflow():
# Shape bridge 1: derive s0 from x, then use s0 in a second call_tir's out_ty.
src = emit_shape_bound_copy(bb, x, shape_gv, copy_gv, s0, "first")
source = bb.match_cast(src, relax.TensorType((d0, 8), "float32"))
left0 = bb.match_cast(source, relax.TensorType((d0, 8), "float32"))
keep = bb.emit(relax.Tuple([source, left0]))
left = bb.emit(relax.TupleGetItem(keep, 1))

# Shape bridge 2 (s1), independent of the first. subtract+add below are
# legalized to elemwise call_tir and fused into one group; that group's
# dependency on the bridge's call_tir (which uses s1 in its out_ty) must not
# let FuseOps schedule it ahead of the match_cast that defines s1.
rhs = emit_shape_bound_copy(bb, x, shape_gv, copy_gv, s1, "second")
same = bb.match_cast(rhs, relax.TensorType((d0, 8), "float32"))
right = bb.emit(relax.op.subtract(same, relax.const(0.375, "float32")))
added = bb.emit(relax.op.add(left, right))
gv = bb.emit_output(added)

bb.emit_func_output(gv)

mod = bb.get()
relax.analysis.check_well_formed(mod)

mod = relax.transform.LegalizeOps()(mod)
mod = relax.transform.AnnotateTIROpPattern()(mod)
mod = relax.transform.FoldConstant()(mod)
mod = relax.transform.FuseOps()(mod)

# Guard against the test becoming vacuous: it only exercises the bug if subtract+add
# actually get fused into a group whose dependency call pull the call_tir forward.
assert any(gv.name_hint.startswith("fused_") for gv in mod.get_global_vars())
relax.analysis.check_well_formed(mod)


def test_primitive_call_arg_not_inlined():
@I.ir_module
class Before:
Expand Down
Loading