Skip to content

[FIX][Relax] Preserve transform correctness edge cases - #20512

Open
Yuhx141 wants to merge 8 commits into
apache:mainfrom
Yuhx141:yuhx/fix-relax-transform-safety-20261001
Open

Yuhx141 wants to merge 8 commits into
apache:mainfrom
Yuhx141:yuhx/fix-relax-transform-safety-20261001

Conversation

@Yuhx141

@Yuhx141 Yuhx141 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

This fixes three correctness issues in Relax transformation passes:

  • preserve tuple outputs when a private callee is referenced by make_closure;
  • decode tensor_to_shape constants using their actual int16/int32/int64 storage dtype in FoldConstant;
  • propagate closure identity through a local function alias in LambdaLift and choose the invoke op from the function's purity.

The changes are local to the affected passes and retain the existing behavior for supported controls.

Closes #20493
Closes #20495
Closes #20497

The original AdjustMatmulOrder change and its test are omitted because that pass was removed upstream in #20712. This branch includes current main (c14bc80e).

Validation:

  • Release build with LLVM 18 completed.
  • pytest tests/python/relax/test_transform_remove_unused_outputs.py tests/python/relax/test_transform_fold_constant.py tests/python/relax/test_transform_lambda_lift.py -q: 36 passed.
  • Pre-commit passed on all six PR files.

@Yuhx141

Yuhx141 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

@tlopex Merged current main and resolved the conflicts, keeping the upstream removal of AdjustMatmulOrder (#20712). The three remaining fixes are preserved. The Release/LLVM build, all 36 related tests, and pre-commit checks pass locally. Could you take a look when convenient? Thanks!

@Yuhx141

Yuhx141 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

@tqchen @mshr-h

Could one of you take a look at these Relax transformation correctness fixes when you have a chance, or point me to the right reviewer? I am keen to help get these fixes into TVM and happy to address feedback, add further regression tests, rebase, or split the changes if that would make review easier.

The GitHub Actions checks also seem to need maintainer help with fork-workflow approval or a restart. I would appreciate any guidance on getting the appropriate checks running. Thank you for your time!

@mshr-h
mshr-h requested a max review from Copilot November 16, 2026 06:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
What changed in this PR

Fixes correctness edge cases in Relax transformation passes (RemoveUnusedOutputs, FoldConstant, LambdaLift) around closures and constant decoding.

Changes:

  • Prevent RemoveUnusedOutputs from trimming tuple outputs when a callee is referenced via relax.make_closure.
  • Fix FoldConstant to decode tensor_to_shape constants according to their stored integer dtype (int16/int32/int64).
  • Improve LambdaLift handling for closures called through local aliases and select invoke_(pure_)closure appropriately.
File Description
tests/​python/​relax/​test_transform_remove_unused_outputs.py Adds regression test for tuple output preservation when make_closure references a private callee.
tests/​python/​relax/​test_transform_lambda_lift.py Adds regression test for alias-called closures after LambdaLift.
tests/​python/​relax/​test_transform_fold_constant.py Adds regression test for folding tensor_to_shape from int32 constants.
src/​relax/​transform/​remove_unused_outputs.cc Marks all outputs as used when a function is referenced by relax.make_closure.
src/​relax/​transform/​lambda_lift.cc Tracks closures through var-alias bindings and adjusts purity selection logic.
src/​relax/​transform/​fold_constant.cc Decodes tensor_to_shape constant buffers based on actual integer bit-width.

Review effort: Lite

Comment thread src/relax/transform/lambda_lift.cc Resolved
Comment thread src/relax/transform/lambda_lift.cc Resolved
@mshr-h
mshr-h requested a max review from Copilot November 16, 2026 10:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread src/relax/transform/lambda_lift.cc Resolved
Comment on lines +398 to +409
switch (ndarray.dtype().bits) {
case 16:
append_values(static_cast<const int16_t*>(ndarray.data_ptr()));
break;
case 32:
append_values(static_cast<const int32_t*>(ndarray.data_ptr()));
break;
case 64:
append_values(static_cast<const int64_t*>(ndarray.data_ptr()));
break;
default:
return post_call;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added int16 and int64 cases by parameterizing the existing regression over all three signed integer dtypes. All 50 related tests pass locally.

Comment thread src/relax/transform/lambda_lift.cc Resolved
@Yuhx141

Yuhx141 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

All eight checks pass on the latest revision. Could someone take a look at the remaining Relax transform fixes when they have a chance?

@mshr-h mshr-h self-assigned this Nov 18, 2026
@mshr-h
mshr-h requested a max review from Copilot November 18, 2026 13:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Unresolved recursive and aliased closure handling, output pruning, and invocation-purity bugs can produce invalid IR or remove side effects, while unaligned constant reads can cause undefined behavior or crashes.

1 open issue
6 previously missed issues

In code that hasn't changed since last review

High severity Avoid unaligned tensor data dereferences

src/​relax/​transform/​fold_constant.cc:410

Contiguous imported CPU tensors can have an unaligned data pointer even when byte_offset() is zero. Dereferencing data[i] then has undefined behavior for these int16/int32 paths and may crash on strict-alignment CPUs. Use byte-wise copies as vm.builtin.tensor_to_shape does, or leave unaligned constants unfolded.

High severity Rewrite aliases using in-progress recursive closure mappings

src/​relax/​transform/​lambda_lift.cc:268

With alias = inner inside a recursive noncapturing inner, new_value is a lifted global that has not been registered yet, so IsClosure fails its module lookup even if the alias is unused. For a capturing recursive function, the alias instead retains the unlifted self variable and misses nested_closure_map_, leaving a free variable in the lifted body. Handle in-progress recursive mappings when rewriting aliases, including captured arguments, and add regressions for both cases.

High severity Keep aliases of plain function references as ordinary calls

src/​relax/​transform/​lambda_lift.cc:383

With a noncapturing outer_func that returns a captured function, alias = outer_func; f = alias(x) is now rewritten to invoke_pure_closure(alias, ...), although alias holds a plain function reference. IsClosure also recognizes globals whose return value is a closure, so it cannot by itself decide whether the called value is a closure object. Follow the alias chain to its producer and retain the CallObj guard, so aliases of plain function references remain ordinary calls.

High severity Use the called closure's purity for invocation

src/​relax/​transform/​lambda_lift.cc:386

For f = maker(x); f(y), where a pure maker returns an impure captured function, this still uses the maker's purity and emits invoke_pure_closure for the impure invocation. That mislabels side effects as removable. Use the called variable's original FuncType purity for every closure invocation, and add a pure-factory/impure-closure regression.

High severity Preserve outputs when a GlobalVar is used as a value

src/​relax/​transform/​remove_unused_outputs.cc:110

The GlobalVar-only check still misses f = Before.outputs; closure = R.make_closure(f, (x,)), so the callee's outputs are pruned while the closure retains its old signature. The same issue occurs when a private function is called through an alias, returned, or stored in a tuple. Conservatively preserve all outputs whenever a tracked GlobalVar is used as a value rather than as a direct callee, and add an aliased-callee regression.

Medium severity Avoid quadratic closure checks across variable aliases

src/​relax/​transform/​lambda_lift.cc:265

This runs IsClosure for every variable alias, including tensor aliases. For a1 = x; a2 = a1; ..., each check recursively follows all preceding bindings without caching negative results, so N aliases require quadratic work and linear recursion depth even in a module with no local functions. Skip closure analysis for statically noncallable aliases, or cache negative results to avoid repeatedly traversing the same chains.

5 resolved since last review

Review effort: Max

…ions

Signed-off-by: Yuhx141 <155034285+Yuhx141@users.noreply.github.com>
@Yuhx141

Yuhx141 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Hi @MasterJH5574, could you take a look at the fixes for constant folding, lambda lifting, and unused outputs when you have a chance? All eight checks passed. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants