Skip to content

[Relax][PyTorch] Bind symbolic scalar inputs in from_fx#19964

Open
guan404ming wants to merge 1 commit into
apache:mainfrom
guan404ming:fix-fx-sym-input-placeholder
Open

[Relax][PyTorch] Bind symbolic scalar inputs in from_fx#19964
guan404ming wants to merge 1 commit into
apache:mainfrom
guan404ming:fix-fx-sym-input-placeholder

Conversation

@guan404ming

@guan404ming guan404ming commented Jul 7, 2026

Copy link
Copy Markdown
Member

Why

  • torch.compile(backend=relax_dynamo(), dynamic=True) lifts SymInt scalars as scalar graph inputs
  • from_fx skips these placeholders, so ops referencing one, e.g. view(x.size(0), -1), fail with KeyError

How

  • Bind sym placeholders to the same-named tir.Var from the input tensors' symbolic shapes; skip as before when none exists
  • Add test_relax_dynamo_dynamic_sym_input_reference; fails with KeyError without the fix

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request enables symbolic shape variables lifted by Dynamo as scalar graph inputs to be bound to matching shape variables, allowing operations like view that reference them to resolve correctly. A test case is added to verify this behavior. The feedback recommends explicitly importing tirx locally to prevent potential runtime errors from fragile transitive imports.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +1099 to +1101
sym_vars = {
v.name: v for shape, _ in input_info for v in shape if isinstance(v, tvm.tirx.Var)
}

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.

medium

Relying on transitive imports for submodules (like tvm.tirx) can be fragile and may lead to AttributeError if the import structure of other modules changes. It is safer and more robust to explicitly import tirx locally within the function.

        from tvm import tirx

        sym_vars = {
            v.name: v for shape, _ in input_info for v in shape if isinstance(v, tirx.Var)
        }

@guan404ming guan404ming force-pushed the fix-fx-sym-input-placeholder branch 2 times, most recently from cc2e639 to abc639b Compare July 7, 2026 06:00
@guan404ming guan404ming marked this pull request as ready for review July 7, 2026 11:24
With torch.compile(dynamic=True), dynamo lifts SymInt scalars (e.g. a
symbolic batch size) as scalar graph inputs. from_fx skipped these
placeholders entirely, so any op referencing one directly, such as
view(x.size(0), -1), failed with KeyError during translation.

Bind such placeholders to the tir.Var of the same name already created
for the input tensors' symbolic shapes, so references resolve the same
way sym_size.int results do. Placeholders with no matching shape var
are still skipped as before.
@guan404ming guan404ming force-pushed the fix-fx-sym-input-placeholder branch from abc639b to 5347983 Compare July 7, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant