#20498·tvm

[错误] Gradient 会拒绝返回与 Var 绑定的元组的函数,无论目标索引为何

作者: yunligou711-commits创建于 2026年9月30日更新于 2026年9月30日
标签type: bugneeds-triage

Expected behavior

relax.transform.Gradient("main", target_index=k) should accept a function whose return value is a Var of tuple type and differentiate with respect to the k-th field of that tuple.

This should behave the same way as when the function directly returns a tuple literal.

The following two forms describe the same return value:

return (lv1, lv2, lv3)

and:

res = (lv1, lv2, lv3)
return res

Actual behavior

When the tuple is first bound to a Var, every target_index is rejected.

Two different internal checks are triggered.

For target_index=1:

InternalError: Check failed: target_index == 0 (1 vs. 0):
When the function has only one return value, target_index can only be 0.

For target_index=2:

InternalError: Check failed: target_index == 0 (2 vs. 0):
When the function has only one return value, target_index can only be 0.

For target_index=0:

InternalError: Check failed: (IsScalarTensor(target_ty) && IsFloatTensorType(target_ty)) is false:
The differentiation target must be a float scalar (0-dim Tensor), but the Type of
the given target res is T.Tuple(R.Tensor((3, 3), dtype="float32"),
R.Tensor((), dtype="float32"), R.Tensor((), dtype="float32"))

The function returns three values, but the error messages for target_index=1 and target_index=2 state that it has only one return value.

Both checks come from:

src/relax/transform/gradient.cc:731
src/relax/transform/gradient.cc:753
GradientMutator::CheckAndSetTarget

Environment

  • TVM version: 0.26.dev0

  • Commit: 8312a17f8734ddfd56e5f3977cd5df25b83ec49f

  • OS: Linux x86_64, Ubuntu

  • Kernel: 5.15

  • Python: …