Skip to content

Conversation

blaine-rister
Copy link
Contributor

@blaine-rister blaine-rister commented Sep 24, 2025

Problem

Inductor sometimes generates unbacked symints to handle things like mismatched branches of torch.cond. This code is represented by pytree.KeyPath, with special codegen logic to convert it to Python and C++. This was not previously supported by the FX backend.

Feature

This PR adds support for unbacked symbol declarations to the FX backend. The implementation is fairly straightforward.

  1. Instead of raw Python/C++, update the wrapper codegen method to emit a new Wrapper IR line called UnbackedSymbolDefsLine. This contains all the information needed to generate the Python and C++ code.
  2. Move the existing Python/C++ codegen to a private method, which is invoked by UnbackedSymbolDefsLine.codegen().
  3. Implement a method to generate FX IR from unbacked symbol definitions. The implementation is based on recursive descent, consuming some keypath entries, emitting an FX IR node, and recursing to the rest of the keypath. It is conceptually identical to the existing algorithm for Python and C++, except it generates FX nodes.
  4. The FX backend currently relies on size hints to generate autotuning arguments, and consequently autotuning does not support unbacked SymInts. At some point, we would like to generalize the autotuning logic to support these. But for now, simply emit a warning and skip autotuning when we see them.
  5. The new test case exposed some tricky issues reconciling Triton call args with constants stored in triton_meta. This PR rewrites the relevant helper function to do this in a more principled way.

Test plan

This PR imports an existing control flow test to the FX backend's test suite. The test uses unbacked symbol definitions to handle mismatched dynamic shapes coming from torch.cond branches.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @chenyang78 @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben

Copy link

pytorch-bot bot commented Sep 24, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/163729

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 3ab437c with merge base 5f90e8c (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@blaine-rister blaine-rister marked this pull request as ready for review September 26, 2025 17:15
call_args = add_constants_to_call_args(call_args, kernel_config)
call_args, grid = tuner._interpret_args_grid(call_args, kernel_config)
call_kwargs = dict(zip(signature, call_args))
assert not any(kwarg in kernel_config.kwargs for kwarg in call_kwargs), (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test case in this PR exposed a very tricky bug related to arg/kwarg handling. Although this should no longer happen with the updated method, this assert guards against that bug if by some off chance we missed something.

@blaine-rister
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 29, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged module: inductor release notes: fx release notes category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants