Skip to content

[Relax][PyTorch] Fix segfault in from_exported_program when model uses index_put_ with tuple output#19488

Open
cchung100m wants to merge 10 commits intoapache:mainfrom
cchung100m:issue-18363
Open

[Relax][PyTorch] Fix segfault in from_exported_program when model uses index_put_ with tuple output#19488
cchung100m wants to merge 10 commits intoapache:mainfrom
cchung100m:issue-18363

Conversation

@cchung100m
Copy link
Copy Markdown
Contributor

@cchung100m cchung100m commented May 1, 2026

Hi Committers,

This PR is trying to fix issues #18363. Any suggestions would be appreciated if you are available.

Root Cause

  • When an ExportedProgram's FX graph output node returns a nested Python tuple (e.g., buffer mutation outputs + user-defined tuple returns), _translate_fx_graph() passes the raw nested structure directly to the Relax FFI Tuple constructor.
  • The C++ Array initializer cannot handle heterogeneous/nested Python containers, causing a segmentation fault at expr.cc.
  • Additionally, index_put_ (in-place write op) did not update self.env to alias the source tensor to the mutated output, causing subsequent FX nodes that read the same tensor to observe stale pre-mutation values.

Solution

  • exported_program_translator.py
    • Added static method _flatten_output_args() that recursively walks any Python tuple/list, collects only relax.Expr leaves, and preserve explicit None outputs as Relax null objects.
    • Replaced the fragile assert isinstance(output_args, tuple | relax.Tuple) guard with a call to _flatten_output_args(), producing a clean flat tuple of relax.Expr before FFI construction.
  • base_fx_graph_translator.py
    • In _index_put(), after emitting the relax.op.index_put(...) call, added an env alias update: self.env[source_node] = output when the target op name starts with index_put_, preserving correct in-place mutation semantics for downstream FX nodes.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 introduces support for in-place index_put_ operations in the Torch FX translator by updating the environment with the operation's result. It also adds a _flatten_output_args utility to the ExportedProgram translator to handle nested output structures that could cause FFI issues. Feedback indicates that the flattening utility is currently defined but not invoked, and suggests a more robust approach for extracting target names from FX nodes to handle cases where the target is a string.

Comment thread python/tvm/relax/frontend/torch/exported_program_translator.py Outdated
Comment thread python/tvm/relax/frontend/torch/base_fx_graph_translator.py Outdated
@cchung100m cchung100m marked this pull request as ready for review May 4, 2026 06:43
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