Skip to content

[inductor] Support separate forward/backward inductor configs for invoke_subgraph - #190068

Closed
desertfire wants to merge 4 commits into
gh/desertfire/747/basefrom
gh/desertfire/747/head
Closed

desertfire wants to merge 4 commits into
gh/desertfire/747/basefrom
gh/desertfire/747/head

Conversation

@desertfire

@desertfire desertfire commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

get_invoke_subgraph_compile_options now takes fw_inductor_config_patches (the
forward config, renamed from inductor_config_patches) and a keyword-only
bw_inductor_config_patches. When bw_inductor_config_patches is set, the backward
subgraph of a nested_compile_region is compiled under it, independently of the
forward -- a full replacement, not merged over the forward patches, mirroring
aot_autograd's separate fw_compiler/bw_compiler. When unset, the backward reuses
the forward config.

For example, to max-autotune the forward but not the backward:

opts = get_invoke_subgraph_compile_options(
    fw_inductor_config_patches={"max_autotune": True},
    bw_inductor_config_patches={"max_autotune": False},
)

@torch.compiler.nested_compile_region(options=opts)
def region(x):
    return torch.matmul(x, x)

The backward config is applied where the partitioned backward subgraph is created
(run_joint_graph_passes_on_hops), not on the pre-partition backward graph: the
partitioner restitches a fresh backward subgraph module whose meta otherwise
inherits the forward config, so stamping the pre-partition graph is discarded.
get_backward_nested_region_config derives the backward variant from the forward
region config, and it is stamped onto the partitioned backward module only when the
region requested a distinct backward config, leaving the common path unchanged.

This change was authored with the assistance of an AI coding agent.

Differential Revision: D112152398

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit f121f20 with merge base f30b6ce (image):
💚 Looks good so far! There are no failures yet. 💚

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

@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 15, 2026
[ghstack-poisoned]
@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

3 similar comments
@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@desertfire

Copy link
Copy Markdown
Contributor Author

@claude review this pr

@claude

claude Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Claude finished @desertfire's task in 7m 6s —— View job


PR Review: #190068

Summary: Adds bw_inductor_config_patches so an invoke_subgraph nested region can compile its backward under a separate inductor config from its forward; the implementation is sound and well-tested, with only minor cleanup opportunities below.

Full review details

I traced both compilation paths that consume the region config: the regional pass (torch/fx/passes/regional_inductor_invoke_subgraph.py, which selects fw_compiler/bw_compiler per direction) and the normal inductor lowering path (ir.py/graph.py/codecache.py, which read nested_region_config.inductor_config_patches from module meta). The PR correctly addresses both: the bw_compiler partial carries the backward patches for the regional path, and stamping new_bw_hop_gm.meta in run_joint_graph_passes_on_hops covers the lowering path. Cache-key collection (_collect_nested_region_inductor_config_patches) reads the stamped meta, so the new backward config participates in the cache key — the codecache test confirms this. No correctness or BC issues found (all new parameters are keyword-compatible; inductor_config_patches was only renamed to a new leading positional fw_inductor_config_patches, and no in-tree caller passed it positionally).

The findings below are cleanup only.

1. Redundant guard duplication — torch/_functorch/_aot_autograd/graph_compile.py:1243-1251

The caller re-implements the exact condition (isinstance(...) and bw_inductor_config_patches is not None) that already lives inside get_backward_nested_region_config, and only to decide whether to stamp. Since the helper returns fw_config unchanged (same object) when there is no distinct backward config, the caller can lean on that identity contract instead — which also drops the NestedCompileRegionOptions import added at line 861:

fw_region_config = fw_node.meta.get("custom", {}).get("nested_region_config")
bw_region_config = get_backward_nested_region_config(fw_region_config)
if bw_region_config is not fw_region_config:
    new_bw_hop_gm.meta["nested_region_config"] = bw_region_config

This is exactly the usage the helper's docstring says it was designed for ("callers rely on this identity to detect a distinct backward config"). Fix this →

2. Docstring "identity" contract is currently unused — torch/_higher_order_ops/invoke_subgraph.py:117-123

The docstring states "callers rely on this identity to detect a distinct backward config," but the only production caller (finding #1) does not use identity — it re-checks the condition manually. Either adopt the identity-based caller above (preferred) or reword the docstring so it does not describe a contract no caller actually uses.

3. Derived backward config still carries bw_inductor_config_patches — torch/_higher_order_ops/invoke_subgraph.py:128-131

replace() only overrides inductor_config_patches, so the returned backward config keeps a non-None bw_inductor_config_patches. It's harmless today (nothing re-derives a backward-of-a-backward), but a stray bw_inductor_config_patches on a config that is itself the backward is a latent footgun if this ever recurses. Consider clearing it in the replace() call for hygiene.

Recommendation: Approve — no correctness, BC, or test-coverage blockers; the items above are optional cleanups.
(branch: gh/desertfire/747/head)

[ghstack-poisoned]
@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

[ghstack-poisoned]
@desertfire

Copy link
Copy Markdown
Contributor Author

@claude review this pr

@claude

claude Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Claude finished @desertfire's task in 1m 40s —— View job


PR Review: #190068

Summary: Adds bw_inductor_config_patches so an invoke_subgraph nested region can compile its backward under a separate inductor config from its forward; the implementation is correct, well-tested, and the three cleanup items from the prior review round have all been addressed. No blockers found.

Full review details

I re-traced both compilation paths that consume the region config:

  1. Regional pass (torch/fx/passes/regional_inductor_invoke_subgraph.py:38-41): selects fw_compiler/bw_compiler per direction based on partitioner_tag. Both compiler partials already bake the correct config patches (fw_compiler→fw_inductor_config_patches, bw_compiler→bw_patches), so this path is correct regardless of which region config object is stamped.
  2. Normal inductor lowering path (compile_fx.py / graph.py / codecache.py / ir.py): reads the single inductor_config_patches off nested_region_config and does not distinguish fw/bw. This is why the PR stamps a derived backward config (with inductor_config_patches replaced by the backward patches) onto new_bw_node.meta["custom"] in run_joint_graph_passes_on_hops — _propagate_invoke_subgraph_nested_region_config then mirrors it onto the partitioned backward subgraph module, and _collect_nested_region_inductor_config_patches folds it into the cache key. The codecache test confirms the backward config participates in the hash.

Verification of prior-round findings (all resolved):

  • Identity-based caller — graph_compile.py:1242-1257 now calls get_backward_nested_region_config and stamps only when bw_region_config is not fw_region_config, leaning on the helper's identity contract rather than re-implementing the condition. The redundant NestedCompileRegionOptions import is gone.
  • Docstring contract now accurate — invoke_subgraph.py:114-131 docstring's identity claim now matches the sole production caller.
  • Derived backward config cleared — replace(..., bw_inductor_config_patches=None) at invoke_subgraph.py:128-131 prevents a stray bw_inductor_config_patches from riding along on a config that is itself the backward.

BC: inductor_config_patches was renamed to a new leading positional fw_inductor_config_patches; all in-tree callers pass it by keyword, and bw_inductor_config_patches is keyword-only, so no caller breaks. The validation loop at invoke_subgraph.py correctly validates both raw patch dicts.

Testing: Adequate. test_nested_region_separate_fw_bw_inductor_config and test_nested_region_fw_bw_inductor_config_both_set cover the runtime codegen behavior (fw persistent vs bw looped reductions), and test_nested_region_bw_config_patches_affect_cache_key covers cache-key participation including the replace-not-merge semantics.

No correctness, thread-safety, or performance concerns found.

Recommendation: Approve — implementation is correct, prior review comments are resolved, and coverage is adequate.
(branch: gh/desertfire/747/head)

@desertfire

Copy link
Copy Markdown
Contributor Author

@desertfire has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@desertfire
desertfire requested review from eellison and yushangdi July 16, 2026 20:57
@desertfire

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

youlituao pushed a commit to youlituao/pytorch that referenced this pull request Jul 28, 2026
…oke_subgraph (pytorch#190068)

get_invoke_subgraph_compile_options now takes fw_inductor_config_patches (the
forward config, renamed from inductor_config_patches) and a keyword-only
bw_inductor_config_patches. When bw_inductor_config_patches is set, the backward
subgraph of a nested_compile_region is compiled under it, independently of the
forward -- a full replacement, not merged over the forward patches, mirroring
aot_autograd's separate fw_compiler/bw_compiler. When unset, the backward reuses
the forward config.

For example, to max-autotune the forward but not the backward:

    opts = get_invoke_subgraph_compile_options(
        fw_inductor_config_patches={"max_autotune": True},
        bw_inductor_config_patches={"max_autotune": False},
    )

    @torch.compiler.nested_compile_region(options=opts)
    def region(x):
        return torch.matmul(x, x)

The backward config is applied where the partitioned backward subgraph is created
(run_joint_graph_passes_on_hops), not on the pre-partition backward graph: the
partitioner restitches a fresh backward subgraph module whose meta otherwise
inherits the forward config, so stamping the pre-partition graph is discarded.
get_backward_nested_region_config derives the backward variant from the forward
region config, and it is stamped onto the partitioned backward module only when the
region requested a distinct backward config, leaving the common path unchanged.

This change was authored with the assistance of an AI coding agent.

Differential Revision: [D112152398](https://our.internmc.facebook.com/intern/diff/D112152398)

Pull Request resolved: pytorch#190068
Approved by: https://github.com/yushangdi
ghstack dependencies: pytorch#189320
youlituao pushed a commit to youlituao/pytorch that referenced this pull request Jul 28, 2026
…oke_subgraph (pytorch#190068)

get_invoke_subgraph_compile_options now takes fw_inductor_config_patches (the
forward config, renamed from inductor_config_patches) and a keyword-only
bw_inductor_config_patches. When bw_inductor_config_patches is set, the backward
subgraph of a nested_compile_region is compiled under it, independently of the
forward -- a full replacement, not merged over the forward patches, mirroring
aot_autograd's separate fw_compiler/bw_compiler. When unset, the backward reuses
the forward config.

For example, to max-autotune the forward but not the backward:

    opts = get_invoke_subgraph_compile_options(
        fw_inductor_config_patches={"max_autotune": True},
        bw_inductor_config_patches={"max_autotune": False},
    )

    @torch.compiler.nested_compile_region(options=opts)
    def region(x):
        return torch.matmul(x, x)

The backward config is applied where the partitioned backward subgraph is created
(run_joint_graph_passes_on_hops), not on the pre-partition backward graph: the
partitioner restitches a fresh backward subgraph module whose meta otherwise
inherits the forward config, so stamping the pre-partition graph is discarded.
get_backward_nested_region_config derives the backward variant from the forward
region config, and it is stamped onto the partitioned backward module only when the
region requested a distinct backward config, leaving the common path unchanged.

This change was authored with the assistance of an AI coding agent.

Differential Revision: [D112152398](https://our.internmc.facebook.com/intern/diff/D112152398)

Pull Request resolved: pytorch#190068
Approved by: https://github.com/yushangdi
ghstack dependencies: pytorch#189320
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…oke_subgraph (pytorch#190068)

get_invoke_subgraph_compile_options now takes fw_inductor_config_patches (the
forward config, renamed from inductor_config_patches) and a keyword-only
bw_inductor_config_patches. When bw_inductor_config_patches is set, the backward
subgraph of a nested_compile_region is compiled under it, independently of the
forward -- a full replacement, not merged over the forward patches, mirroring
aot_autograd's separate fw_compiler/bw_compiler. When unset, the backward reuses
the forward config.

For example, to max-autotune the forward but not the backward:

    opts = get_invoke_subgraph_compile_options(
        fw_inductor_config_patches={"max_autotune": True},
        bw_inductor_config_patches={"max_autotune": False},
    )

    @torch.compiler.nested_compile_region(options=opts)
    def region(x):
        return torch.matmul(x, x)

The backward config is applied where the partitioned backward subgraph is created
(run_joint_graph_passes_on_hops), not on the pre-partition backward graph: the
partitioner restitches a fresh backward subgraph module whose meta otherwise
inherits the forward config, so stamping the pre-partition graph is discarded.
get_backward_nested_region_config derives the backward variant from the forward
region config, and it is stamped onto the partitioned backward module only when the
region requested a distinct backward config, leaving the common path unchanged.

This change was authored with the assistance of an AI coding agent.

Differential Revision: [D112152398](https://our.internmc.facebook.com/intern/diff/D112152398)

Pull Request resolved: pytorch#190068
Approved by: https://github.com/yushangdi
ghstack dependencies: pytorch#189320
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
…oke_subgraph (pytorch#190068)

get_invoke_subgraph_compile_options now takes fw_inductor_config_patches (the
forward config, renamed from inductor_config_patches) and a keyword-only
bw_inductor_config_patches. When bw_inductor_config_patches is set, the backward
subgraph of a nested_compile_region is compiled under it, independently of the
forward -- a full replacement, not merged over the forward patches, mirroring
aot_autograd's separate fw_compiler/bw_compiler. When unset, the backward reuses
the forward config.

For example, to max-autotune the forward but not the backward:

    opts = get_invoke_subgraph_compile_options(
        fw_inductor_config_patches={"max_autotune": True},
        bw_inductor_config_patches={"max_autotune": False},
    )

    @torch.compiler.nested_compile_region(options=opts)
    def region(x):
        return torch.matmul(x, x)

The backward config is applied where the partitioned backward subgraph is created
(run_joint_graph_passes_on_hops), not on the pre-partition backward graph: the
partitioner restitches a fresh backward subgraph module whose meta otherwise
inherits the forward config, so stamping the pre-partition graph is discarded.
get_backward_nested_region_config derives the backward variant from the forward
region config, and it is stamped onto the partitioned backward module only when the
region requested a distinct backward config, leaving the common path unchanged.

This change was authored with the assistance of an AI coding agent.

Differential Revision: [D112152398](https://our.internmc.facebook.com/intern/diff/D112152398)

Pull Request resolved: pytorch#190068
Approved by: https://github.com/yushangdi
ghstack dependencies: pytorch#189320
torchxpubot pushed a commit to intel/torch-xpu-ops that referenced this pull request Aug 14, 2026
Fixes issues reported in:
#4438

Test cases fixed:
```
dynamo.test_misc_xpu.MiscTests,test_recompile_on_global_state_change
dynamo.test_regional_inductor_xpu.RegionalInductorInvokeSubgraphTests,test_max_autotune_no_cudagraphs_serialize_False
dynamo.test_regional_inductor_xpu.RegionalInductorInvokeSubgraphTests,test_invalid_inductor_config
```
`test_misc_xpu` changed upstream in
pytorch/pytorch#185236
`test_regional_inductor_xpu` changed upstream in
pytorch/pytorch#190068
@github-actions
github-actions Bot deleted the gh/desertfire/747/head branch August 21, 2026 02:12
minmingzhu pushed a commit to minmingzhu/torch-xpu-ops that referenced this pull request Sep 15, 2026
Fixes issues reported in:
intel#4438

Test cases fixed:
```
dynamo.test_misc_xpu.MiscTests,test_recompile_on_global_state_change
dynamo.test_regional_inductor_xpu.RegionalInductorInvokeSubgraphTests,test_max_autotune_no_cudagraphs_serialize_False
dynamo.test_regional_inductor_xpu.RegionalInductorInvokeSubgraphTests,test_invalid_inductor_config
```
`test_misc_xpu` changed upstream in
pytorch/pytorch#185236
`test_regional_inductor_xpu` changed upstream in
pytorch/pytorch#190068
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants