Skip to content

[inductor] Enable nested Inductor configs for invoke_subgraph regions - #189320

Closed
desertfire wants to merge 5 commits into
gh/desertfire/744/basefrom
gh/desertfire/744/head
Closed

desertfire wants to merge 5 commits into
gh/desertfire/744/basefrom
gh/desertfire/744/head

Conversation

@desertfire

@desertfire desertfire commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

The normal Inductor invoke_subgraph path compiled each nested region under the
surrounding graph's Inductor config, so per-region config patches were dropped
and nested options had no effect on the region.

This threads the nested-region config through the invoke_subgraph HOP metadata,
the GraphModule metadata, and the IR Subgraph object, and applies it via
config.patch across the recursive Inductor pass pipeline, lowering, codegen, and
FX cache keying, so each region is compiled, codegened, and cache-keyed under
its own config.

triton.cudagraphs can be set as a nested config and is threaded here, but it
does not yet affect cudagraph capture; that is enabled in a follow-up.

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

Differential Revision: D111943224

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 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 62878bc 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

@claude review this pr

@claude

claude Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Claude finished @desertfire's task in 11m 58s —— View job


PR Review: #189320

One-sentence summary: The plumbing is sound and the functional path is well-tested, but the substantial new cache-keying/bypass logic is untested and there's a confusing cross-module name collision worth cleaning up.

Full review

Summary

Threads a per-region inductor_config_patches dict from NestedCompileRegionOptions through HOP node meta → subgraph GraphModule meta → ir.Subgraph, and applies it via config.patch across the recursive pre/joint/post-grad passes, lowering, codegen, and FX cache keying. The core design is reasonable and the functional behavior is covered, but the new caching logic lacks direct tests and there are a few consistency/wording issues.

Testing

  • The added test test_normal_inductor_hop_subgraph_uses_nested_post_grad_config only exercises the functional path (that the inner post_grad_custom_pre_pass runs on the region and the outer pass runs on the parent). None of the new cache-keying logic is tested, even though this PR adds a non-trivial amount of it:
    • No test that two regions with different inductor_config_patches produce different FX cache keys (FxGraphHashDetails.nested_inductor_config_patches, codecache.py:1490). A regression that dropped nested patches from the key would silently return a stale compiled artifact and this test would not catch it.
    • No test that a callable nested config (or one of the _NESTED_REGION_UNCACHEABLE_CONFIG_KEYS) triggers the cache bypass in _check_nested_region_inductor_config_patches (codecache.py:1149). The added test uses a callable (inner_pass) but never asserts caching was bypassed, so the bypass branch is effectively unverified.
      Please add at least a cache-key differentiation test and a bypass test. Fix this →

Code Quality

  • Name collision across modules. There are now two functions named _get_nested_region_inductor_config_patches with different signatures and semantics:

    • compile_fx.py:516 — returns dict | None for a single gm's own meta.
    • codecache.py:1319 — returns a sorted tuple[(name, tuple[(key, value)])] collected across all submodules and HOP nodes.
      Same name, different return type and purpose. This is a maintenance/readability hazard (easy to grab the wrong one on a future refactor). Rename one, e.g. the codecache one to _collect_nested_region_inductor_config_patches_for_hash (it already delegates to _collect_...). Fix this →
  • Bypass message is inaccurate. codecache.py:1157 always reports "Unsupported nested region inductor callable config", but the branch above (key in _NESTED_REGION_UNCACHEABLE_CONFIG_KEYS and value) also fires for non-callable truthy values. The message will mislead when the bypass was due to an uncacheable key rather than a callable.

API Design / Behavior

  • Nested CustomGraphPass with a valid uuid() still bypasses the cache. _check_nested_region_inductor_config_patches bypasses on any callable(value), whereas the top-level _check_custom_passes (codecache.py:1124) permits a CustomGraphPass with a stable uuid() to remain cacheable. So a nested region is strictly more conservative than the parent graph for the identical pass object. If intentional (safe default), add a one-line comment explaining why nested passes can't reuse the uuid()-based cacheability; if not, consider mirroring the top-level CustomGraphPass/uuid() handling so nested regions with proper passes stay cacheable.

Minor

  • _propagate_invoke_subgraph_nested_region_config(gm) is invoked at the top of each of _recursive_pre_grad_passes, _recursive_joint_graph_passes, _recursive_post_grad_passes, and _compile_fx_inner. It's idempotent (meta.setdefault), so this is correct but redundant on the outer graph across phases. Not blocking; just noting the repeated sweep.

Recommendation

Request Changes — primarily for the missing cache-keying/bypass tests (new functionality/logic without regression coverage). The name collision and bypass-message wording should be fixed alongside; the CustomGraphPass cacheability point warrants either a clarifying comment or a small follow-up.

· branch `gh/desertfire/744/head`

[ghstack-poisoned]
@desertfire
desertfire marked this pull request as ready for review July 10, 2026 23:23
@desertfire
desertfire requested a review from yushangdi July 10, 2026 23:23
@desertfire

Copy link
Copy Markdown
Contributor Author

@claude review this pr

@claude

claude Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

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


PR Review: #189320

One-sentence summary: This revision resolves all the concerns from my earlier review (name collision, bypass-message accuracy, cache-key/bypass tests, CustomGraphPass comment) and I found no blocking issues in the current diff.

Full review

Verification of prior review items

All four points from my previous review are addressed:

  • Name collision — the codecache function was split into _collect_nested_region_inductor_config_patches (raw dict collection) and _collect_nested_region_inductor_config_patches_for_hash (sorted, hashable tuple), distinct from compile_fx.py's _get_nested_region_inductor_config_patches. No more same-name/different-semantics collision.
  • Bypass message accuracy — the callable branch now reports "callable value" and the uncacheable-key branch reports "custom pass" (codecache.py:1158-1164). Messages are distinct and correct because bypass() is NoReturn, so the second if is only reached for non-callable values.
  • CustomGraphPass cacheability — the intentional divergence from _check_custom_passes (no uuid() fallback for nested regions) is now documented in the comment at codecache.py:1150-1154.
  • Missing tests — test_codecache.py now has test_nested_region_config_patches_affect_cache_key (different patches -> different keys, same patches -> same key) and test_nested_region_uncacheable_config_bypasses_cache (callable and uncacheable-key bypass). test_regional_inductor.py adds codegen-level tests (multi_kernel, persistent_reductions) asserting the region and parent are compiled under different configs.

Testing

  • The new cache-key/bypass tests cover the validator and hash-detail layers directly. Consider (non-blocking) also asserting counters["inductor"]["fxgraph_cache_bypass"] increments in the bypass test, since that is the observable production signal, but the assertRaisesRegex on BypassFxGraphCache is adequate coverage.

Correctness note (low confidence — worth a sanity check)

  • _propagate_invoke_subgraph_nested_region_config uses subgraph.meta.setdefault(...) (compile_fx.py). If two invoke_subgraph HOP nodes ever reference the same subgraph GraphModule object but carry different nested_region_config in their node meta, only the first config wins and the second is silently dropped. In practice invoke_subgraph dedups identical subgraphs and the config participates in region identity, so this should not arise — but if a shared-module-with-differing-config path is possible, setdefault would mask it rather than error. Worth confirming this can't happen; if it genuinely can't, the current code is fine.

Minor

  • _propagate_invoke_subgraph_nested_region_config(gm) still runs at the top of _recursive_pre_grad_passes, _recursive_joint_graph_passes, _recursive_post_grad_passes, and _compile_fx_inner. It is idempotent (setdefault), so the repeated sweep on the outer graph is harmless — noting only that a brief comment on why it is re-invoked per phase (each phase may be entered independently) would save a future reader the double-take.

Recommendation

Approve — the prior blocking issues (missing cache-keying/bypass tests, name collision) are resolved, the new logic is exercised by direct unit tests plus end-to-end codegen tests, and the uncacheable-config-key list matches the real config names. The setdefault note and the per-phase-propagation comment are optional follow-ups, not blockers.

· branch `gh/desertfire/744/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.

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 14, 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.

[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.

1 similar comment
@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.

1 similar comment
@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.

2 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
desertfire requested a review from eellison July 17, 2026 13:53
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #190068

pytorchmergebot pushed a commit that referenced this pull request Jul 21, 2026
…oke_subgraph (#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: #190068
Approved by: https://github.com/yushangdi
ghstack dependencies: #189320
youlituao pushed a commit to youlituao/pytorch that referenced this pull request Jul 28, 2026
…pytorch#189320)

The normal Inductor invoke_subgraph path compiled each nested region under the
surrounding graph's Inductor config, so per-region config patches were dropped
and nested options had no effect on the region.

This threads the nested-region config through the invoke_subgraph HOP metadata,
the GraphModule metadata, and the IR Subgraph object, and applies it via
config.patch across the recursive Inductor pass pipeline, lowering, codegen, and
FX cache keying, so each region is compiled, codegened, and cache-keyed under
its own config.

triton.cudagraphs can be set as a nested config and is threaded here, but it
does not yet affect cudagraph capture; that is enabled in a follow-up.

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

Differential Revision: [D111943224](https://our.internmc.facebook.com/intern/diff/D111943224)
Pull Request resolved: pytorch#189320
Approved by: https://github.com/yushangdi
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
…pytorch#189320)

The normal Inductor invoke_subgraph path compiled each nested region under the
surrounding graph's Inductor config, so per-region config patches were dropped
and nested options had no effect on the region.

This threads the nested-region config through the invoke_subgraph HOP metadata,
the GraphModule metadata, and the IR Subgraph object, and applies it via
config.patch across the recursive Inductor pass pipeline, lowering, codegen, and
FX cache keying, so each region is compiled, codegened, and cache-keyed under
its own config.

triton.cudagraphs can be set as a nested config and is threaded here, but it
does not yet affect cudagraph capture; that is enabled in a follow-up.

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

Differential Revision: [D111943224](https://our.internmc.facebook.com/intern/diff/D111943224)
Pull Request resolved: pytorch#189320
Approved by: https://github.com/yushangdi
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
…pytorch#189320)

The normal Inductor invoke_subgraph path compiled each nested region under the
surrounding graph's Inductor config, so per-region config patches were dropped
and nested options had no effect on the region.

This threads the nested-region config through the invoke_subgraph HOP metadata,
the GraphModule metadata, and the IR Subgraph object, and applies it via
config.patch across the recursive Inductor pass pipeline, lowering, codegen, and
FX cache keying, so each region is compiled, codegened, and cache-keyed under
its own config.

triton.cudagraphs can be set as a nested config and is threaded here, but it
does not yet affect cudagraph capture; that is enabled in a follow-up.

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

Differential Revision: [D111943224](https://our.internmc.facebook.com/intern/diff/D111943224)
Pull Request resolved: pytorch#189320
Approved by: https://github.com/yushangdi
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
…pytorch#189320)

The normal Inductor invoke_subgraph path compiled each nested region under the
surrounding graph's Inductor config, so per-region config patches were dropped
and nested options had no effect on the region.

This threads the nested-region config through the invoke_subgraph HOP metadata,
the GraphModule metadata, and the IR Subgraph object, and applies it via
config.patch across the recursive Inductor pass pipeline, lowering, codegen, and
FX cache keying, so each region is compiled, codegened, and cache-keyed under
its own config.

triton.cudagraphs can be set as a nested config and is threaded here, but it
does not yet affect cudagraph capture; that is enabled in a follow-up.

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

Differential Revision: [D111943224](https://our.internmc.facebook.com/intern/diff/D111943224)
Pull Request resolved: pytorch#189320
Approved by: https://github.com/yushangdi
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
@github-actions
github-actions Bot deleted the gh/desertfire/744/head branch August 21, 2026 02:11
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