[Relax][PyTorch] Fix segfault in from_exported_program when model uses index_put_ with tuple output#19488
[Relax][PyTorch] Fix segfault in from_exported_program when model uses index_put_ with tuple output#19488cchung100m wants to merge 10 commits intoapache:mainfrom
Conversation
…s index_put_ with tuple output
There was a problem hiding this comment.
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.
Preserve explicit None outputs by appending Relax null objects.
Flatten output arguments before further processing.
Remove assertion for 3D tensor shape in frontend test.
Add regression test for M4D module's index_put behavior.
tlopex
left a comment
There was a problem hiding this comment.
Thanks for the fix. One correctness case I’d like to clarify before approval: _index_put only updates self.env[node.args[0]]. If the mutated value is an alias of another FX node, later reads through the original node may still see the pre-mutation value.
Could you add a small regression test for mutation through an alias?
Hi Committers,
This PR is trying to fix issues #18363. Any suggestions would be appreciated if you are available.
Root Cause
_translate_fx_graph()passes the raw nested structure directly to the Relax FFI Tuple constructor.expr.cc.Solution
_flatten_output_args()that recursively walks any Pythontuple/list, collects onlyrelax.Exprleaves, and preserve explicit None outputs as Relax null objects.assert isinstance(output_args, tuple | relax.Tuple)guard with a call to_flatten_output_args(), producing a clean flat tuple ofrelax.Exprbefore FFI construction._index_put(), after emitting therelax.op.index_put(...)call, added an env alias update:self.env[source_node] = outputwhen the target op name starts withindex_put_, preserving correct in-place mutation semantics for downstream FX nodes.