Skip to content

[Inductor] Bind fallback output unbacked symbols - #186255

Closed
sanketpurandare wants to merge 4 commits into
sanketpurandare/stack/13from
sanketpurandare/stack/16
Closed

sanketpurandare wants to merge 4 commits into
sanketpurandare/stack/13from
sanketpurandare/stack/16

Conversation

@sanketpurandare

@sanketpurandare sanketpurandare commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[Inductor] Bind fallback output unbacked symbols

Fallback kernels can produce outputs whose shapes contain fresh unbacked symbols. When those symbols were created under ignore_fresh_unbacked_symbols(), compute_unbacked_bindings() may return no pending bindings, but the generated wrapper can still use the fallback output symbol in size assertions or downstream allocations.

If fallback binding discovery returns no bindings, derive bindings directly from the fallback output structure when it still contains free unbacked symbols. This keeps the fallback output extent bound from the runtime output tensor before later wrapper code references it.

Fixes #185341

Test Plan: python test/inductor/test_unbacked_symints.py -k standalone_compile_binds_ignored_fallback_unbacked_output -v

Test Plan: lintrunner --config=.lintrunner.toml torch/_inductor/ir.py test/inductor/test_unbacked_symints.py

@pytorch-bot

pytorch-bot Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 3 New Failures, 1 Unrelated Failure

As of commit f30ad28 with merge base 8981cf8 (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

sanketpurandare added a commit that referenced this pull request Jun 4, 2026
Fallback kernels can produce outputs whose shapes contain fresh unbacked symbols. When those symbols were created under ignore_fresh_unbacked_symbols(), compute_unbacked_bindings() may return no pending bindings, but the generated wrapper can still use the fallback output symbol in size assertions or downstream allocations.

If fallback binding discovery returns no bindings, derive bindings directly from the fallback output structure when it still contains free unbacked symbols. This keeps the fallback output extent bound from the runtime output tensor before later wrapper code references it.

Fixes #185341

Test Plan: python test/inductor/test_unbacked_symints.py -k standalone_compile_binds_ignored_fallback_unbacked_output -v

Test Plan: lintrunner --config=.lintrunner.toml torch/_inductor/ir.py test/inductor/test_unbacked_symints.py

stack-info: PR: #186255, branch: sanketpurandare/stack/16
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from aecd39e to 4ee12d2 Compare June 4, 2026 19:01
@sanketpurandare
sanketpurandare marked this pull request as draft June 4, 2026 19:06
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/12 to main June 4, 2026 19:07
@pytorch-bot pytorch-bot Bot added ciflow/dtensor Run DTensor specific tests release notes: distributed (pipeline) release notes category release notes: distributed (dtensor) release notes category labels Jun 4, 2026
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from 4ee12d2 to 9344ca0 Compare June 4, 2026 19:07
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/13 June 4, 2026 19:07
@sanketpurandare
sanketpurandare marked this pull request as ready for review June 4, 2026 19:07
@sanketpurandare
sanketpurandare deleted the sanketpurandare/stack/16 branch June 4, 2026 20:53
@sanketpurandare
sanketpurandare restored the sanketpurandare/stack/16 branch June 4, 2026 20:54
@sanketpurandare
sanketpurandare changed the base branch from gh/sanketpurandare/5/head to sanketpurandare/stack/13 June 4, 2026 20:54
@sanketpurandare
sanketpurandare marked this pull request as draft June 4, 2026 22:32
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/13 to main June 4, 2026 22:32
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from 9344ca0 to ba70fec Compare June 4, 2026 22:32
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/13 June 4, 2026 22:32
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/13 June 8, 2026 18:16
@sanketpurandare
sanketpurandare marked this pull request as ready for review June 8, 2026 18:16
pytorchmergebot pushed a commit that referenced this pull request Jun 8, 2026
Stacked PRs:
 * #183838
 * #186255
 * #183839
 * #183840
 * #183837
 * __->__#184911

--- --- ---

### [Inductor][TP] Fuse slice-cat TP collective patterns

Graph-level chunking can expose tensor-parallel collective values through simple slice/cat reassembly before a matmul, or can feed reduce-scatter from an equivalent slice/cat form after a matmul. The existing micro-pipeline TP matcher only recognized the direct all-gather/matmul and matmul/reduce-scatter patterns, so these semantically equivalent graphs missed fusion.

Teach the all-gather and reduce-scatter matchers to recognize slice/cat forms that reassemble a single collective value without changing the TP communication contract. The replacement and erase logic remains scoped to the matched nodes, and the matmul multi-user guard still rejects users outside the matched reassembly.

Fixes #184833

Test Plan:

python test/distributed/tensor/parallel/test_micro_pipeline_tp.py MicroPipelineTPTest.test_find_all_gather_patterns MicroPipelineTPTest.test_find_reduce_scatter_patterns MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat_trim MicroPipelineTPTest.test_fuse_matmul_reduce_scatter_slice_cat -v

lintrunner --config=.lintrunner.toml torch/_inductor/fx_passes/micro_pipeline_tp.py test/distributed/tensor/parallel/test_micro_pipeline_tp.py

Pull Request resolved: #184911
Approved by: https://github.com/pianpwk
Higher-order operator metadata paths sometimes run fake traces only to answer
questions such as aliasing and mutation. Those traces are discarded, so
unbacked symbols created or left pending inside them should not force a
binding in the trace outputs.

FlexAttention hits this through its score_mod alias check and fake/metadata
propagation when tracing with dynamic block-mask inputs. The internal
block-mask symbols are not semantic outputs of the region, but
compute_unbacked_bindings still required every pending symbol to appear in
the returned values and raised PendingUnbackedSymbolNotFound.

Teach the binding check to honor the existing ignore_fresh_unbacked_symbols
contract by treating pending symbols as ignorable while that TLS is active.
Also make the HOP alias/mutation helper reuse the fake mode carried by the
branch inputs, rather than an unrelated ambient tracing context, and run its
diagnostic make_fx trace under the ignore-fresh-unbacked context.

Fixes #183677

Test Plan:

python test/higher_order_ops/test_utils.py -v

python test/functorch/test_control_flow.py TestControlFlowTraced.test_cond_functionalized_nested

lintrunner --config=.lintrunner.toml torch/_higher_order_ops/utils.py torch/fx/experimental/symbolic_shapes.py test/higher_order_ops/test_utils.py

stack-info: PR: #183837, branch: sanketpurandare/stack/11
Inductor layout constraint lowering can need a stride order for tensors whose
strides contain hinted unbacked expressions, for example
256*Max(1, u0//2) after graph-level sequence chunking. Stride ordering is an
optimization/codegen policy decision, but the comparison still should use
normal symbolic reasoning whenever possible.

Use ShapeEnv.evaluate_expr() first so provable symbolic comparisons stay on the
standard symbolic path. Only if that comparison is data-dependent do we compare
the operands through ShapeEnv.optimization_hint(). This removes the
size-oblivious comparison from the sorting path while preserving deterministic
ordering for hinted unbacked strides. The hint is used only for optimization
ordering and does not specialize tensor semantics or emit guards.

Fixes #183834

Test Plan:

python test/inductor/test_unbacked_symints.py TestUnbackedSymintsCPU.test_stride_order_uses_unbacked_optimization_hint_cpu -q

lintrunner --config=.lintrunner.toml torch/_inductor/utils.py test/inductor/test_unbacked_symints.py

TorchTitan graph_trainer H100 integration: aot_fx_trace_deepseek_v3_sdpa_full_inductor_ep_overlap_moe_seq

stack-info: PR: #183840, branch: sanketpurandare/stack/14
Trace tooling serializes FakeTensor storage metadata to JSON. When the
storage size is a hinted SymInt, the trace can have both useful pieces of
information:

- the symbolic storage expression, which preserves provenance for downstream
  trace consumers;
- the optimization hint, which gives diagnostic/policy tooling a concrete
  expected extent when one was explicitly provided.

Keep the existing `size` field symbolic for symbolic storage sizes and add a
separate `size_hint` field only when every free symbol in the storage-size
expression has an explicit optimization hint override. This preserves the
existing symbolic trace contract while exposing concrete policy metadata
without specializing tensor shapes or changing runtime semantics.

Fixes #183835

Test Plan:

python test/test_fake_tensor.py FakeTensorTest.test_meta_storage_trace_uses_hint_for_symbolic_size -q

lintrunner --config=.lintrunner.toml torch/_subclasses/meta_utils.py test/test_fake_tensor.py

stack-info: PR: #183839, branch: sanketpurandare/stack/13
Fallback kernels can produce outputs whose shapes contain fresh unbacked symbols. When those symbols were created under ignore_fresh_unbacked_symbols(), compute_unbacked_bindings() may return no pending bindings, but the generated wrapper can still use the fallback output symbol in size assertions or downstream allocations.

If fallback binding discovery returns no bindings, derive bindings directly from the fallback output structure when it still contains free unbacked symbols. This keeps the fallback output extent bound from the runtime output tensor before later wrapper code references it.

Fixes #185341

Test Plan: python test/inductor/test_unbacked_symints.py -k standalone_compile_binds_ignored_fallback_unbacked_output -v

Test Plan: lintrunner --config=.lintrunner.toml torch/_inductor/ir.py test/inductor/test_unbacked_symints.py

stack-info: PR: #186255, branch: sanketpurandare/stack/16
@sanketpurandare
sanketpurandare marked this pull request as draft June 8, 2026 21:40
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/13 to main June 8, 2026 21:40
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/16 branch from c29c07b to f30ad28 Compare June 8, 2026 21:40
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/13 June 8, 2026 21:40
@sanketpurandare
sanketpurandare marked this pull request as ready for review June 8, 2026 21:41
pytorchmergebot pushed a commit that referenced this pull request Jun 9, 2026
#183837)

Stacked PRs:
 * #183838
 * #186255
 * #183839
 * #183840
 * __->__#183837

--- --- ---

### [HOP][Dynamic Shapes] Ignore discarded unbacked symbols in fake traces

Higher-order operator metadata paths sometimes run fake traces only to answer
questions such as aliasing and mutation. Those traces are discarded, so
unbacked symbols created or left pending inside them should not force a
binding in the trace outputs.

FlexAttention hits this through its score_mod alias check and fake/metadata
propagation when tracing with dynamic block-mask inputs. The internal
block-mask symbols are not semantic outputs of the region, but
compute_unbacked_bindings still required every pending symbol to appear in
the returned values and raised PendingUnbackedSymbolNotFound.

Teach the binding check to honor the existing ignore_fresh_unbacked_symbols
contract by treating pending symbols as ignorable while that TLS is active.
Also make the HOP alias/mutation helper reuse the fake mode carried by the
branch inputs, rather than an unrelated ambient tracing context, and run its
diagnostic make_fx trace under the ignore-fresh-unbacked context.

Fixes #183677

Test Plan:

python test/higher_order_ops/test_utils.py -v

python test/functorch/test_control_flow.py TestControlFlowTraced.test_cond_functionalized_nested

lintrunner --config=.lintrunner.toml torch/_higher_order_ops/utils.py torch/fx/experimental/symbolic_shapes.py test/higher_order_ops/test_utils.py

Pull Request resolved: #183837
Approved by: https://github.com/laithsakka
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/13 branch from 8dd6572 to d392794 Compare June 9, 2026 02:35
@sanketpurandare

Copy link
Copy Markdown
Contributor Author

Closing this because #183840 now includes the fallback-output unbacked symbol binding fix and addresses #185341.

jemitche1 pushed a commit to jemitche1/pytorch that referenced this pull request Jun 13, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * pytorch#184911
 * __->__#183545

--- --- ---

### [DTensor] Use explicit hints for unbacked sharding

DTensor sharding falls back to conservative behavior when a shard size is
unbacked because divisibility and sharding metadata often require Python
decisions. That is correct for arbitrary symbolic sizes, but it is too
conservative for tracing paths that explicitly override optimization hints and
know that a sharded dimension or strict-view split factor is stable.

This change only trusts explicit optimization hint overrides for those Python
decisions. When a hint selects a no-padding or shardable branch, the traced graph
still emits the symbolic torch._check for the correctness predicate, so runtime
values that satisfy the predicate continue to work and values that violate it
fail closed. The hint chooses the trace-time policy branch; it is not baked in as
a concrete tensor extent.

For _StridedShard, view propagation now preserves a symbolic split_factor when
the split factor came from symbolic shape propagation. The C++ placement still
needs an int64_t unroll count for list-based lowering, so the Python wrapper
stores the symbolic split factor separately and uses the explicit hint only as a
guarded unroll count. The equality assertion is deferred instead of directly
calling torch._check(symbol == hint), because the direct check immediately
refines the ShapeEnv replacement and destroys the symbolic split_factor
metadata we need to preserve.

A post-merge revert found additional gaps. First, strict view propagation could
raise for Shard on a non-first flattened input dim through a combined
Split(Flatten(...)) rule. That combined rewrite cannot preserve the exact
intermediate _StridedShard placement, so it now falls back to input replication
for that mesh dim instead of crashing. During compiled DTensor tracing, the same
redistribution path can create temporary local fake symbols while computing
per-rank shard offsets; those symbols are implementation details because DTensor
returns the global output contract through the output spec, so they are ignored
while tracing local redistribution and local compute.

Second, the ignore-fresh-symbol helper must live in this commit, not in a later
stack commit, because PR 183545 CI runs this commit independently. Third, stale
xfails are removed only where the behavior now genuinely works: eager/multithreaded
view and reshape, unbacked view_copy, and unbacked pixel_shuffle/pixel_unshuffle.
Compiled view and reshape remain xfailed because AOTAutograd alias replay still
reaches DTensor as_strided. The view fallback test is also updated to distinguish
comm-free representable cases from deliberate Replicate fallback cases, and the
_StridedShard utility test now mocks the all_gather_single path that the code
actually calls.

Assisted by Codex.

Fixes pytorch#183678

Test Plan:

```bash
pip install -e . -v --no-build-isolation --no-deps
```

```bash
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=3 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_take_along_dim_cpu_float32 -v
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=2 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_linalg_tensorsolve_cpu_float32 -v
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=0 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_nn_functional_instance_norm_cpu_float32 -v
python test/distributed/tensor/test_view_ops.py TestViewOps.test_split_flatten_non_first_shard_falls_back_to_replicate -v
python test/distributed/tensor/test_dtensor_ops.py -k test_unbacked_dtensor_op_db_view_copy_cpu_float32 -v
python test/distributed/tensor/test_dtensor_ops.py -k test_unbacked_dtensor_op_db_nn_functional_pixel_shuffle_cpu_float32 -v
```

```bash
python -m pytest test/distributed/tensor/test_dtensor_ops.py -q -k 'test_compiled_dtensor_op_db_reshape_cpu_float32 or test_dtensor_op_db_reshape_cpu_float32 or test_dtensor_op_db_view_cpu_float32 or test_unbacked_dtensor_op_db_nn_functional_pixel_unshuffle_cpu_float32'
python -m pytest test/distributed/tensor/test_dtensor_ops.py -q -k 'test_compiled_dtensor_op_db_view_cpu_float32 or test_dtensor_op_db_view_cpu_float32'
python test/distributed/tensor/test_view_ops.py TestViewOps.test_dtensor_flatten_split_multi_mesh -v
python test/distributed/tensor/test_utils.py LocalTest.test_strided_shard_to_replicate_preserves_even_unbacked_shape -v
python test/distributed/tensor/test_dtensor_compile.py TestDTensorCompile.test_dtensor_different_gradient_placement -v
python test/distributed/tensor/test_compile_on_one_rank.py TestCompileOnOneRank.test_compiled_dtensor_rng_op_graph_consistency -v
python test/distributed/tensor/debug/test_debug_mode.py TestDTensorDebugMode.test_debug_mode_mm -v
python test/inductor/test_compiled_autograd.py TestCompiledAutograd.test_dtensor_backward_symints_do_not_reuse_native_sharding_cache -v
python test/dynamo/test_fx_graph_runnable.py FxGraphRunnableTest.test_dtensor_compile_redistribute -v
lintrunner -a
```

```bash
python -m pytest -q test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py::TestViewOps::test_view_groups_unbacked_sharding_propagation
```

```bash
lintrunner --config=.lintrunner.toml torch/distributed/tensor/placement_types.py torch/distributed/tensor/_ops/_view_ops.py torch/distributed/tensor/_ops/utils.py test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py
```

Pull Request resolved: pytorch#183545
Approved by: https://github.com/pianpwk
jemitche1 pushed a commit to jemitche1/pytorch that referenced this pull request Jun 13, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * __->__#184911

--- --- ---

### [Inductor][TP] Fuse slice-cat TP collective patterns

Graph-level chunking can expose tensor-parallel collective values through simple slice/cat reassembly before a matmul, or can feed reduce-scatter from an equivalent slice/cat form after a matmul. The existing micro-pipeline TP matcher only recognized the direct all-gather/matmul and matmul/reduce-scatter patterns, so these semantically equivalent graphs missed fusion.

Teach the all-gather and reduce-scatter matchers to recognize slice/cat forms that reassemble a single collective value without changing the TP communication contract. The replacement and erase logic remains scoped to the matched nodes, and the matmul multi-user guard still rejects users outside the matched reassembly.

Fixes pytorch#184833

Test Plan:

python test/distributed/tensor/parallel/test_micro_pipeline_tp.py MicroPipelineTPTest.test_find_all_gather_patterns MicroPipelineTPTest.test_find_reduce_scatter_patterns MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat_trim MicroPipelineTPTest.test_fuse_matmul_reduce_scatter_slice_cat -v

lintrunner --config=.lintrunner.toml torch/_inductor/fx_passes/micro_pipeline_tp.py test/distributed/tensor/parallel/test_micro_pipeline_tp.py

Pull Request resolved: pytorch#184911
Approved by: https://github.com/pianpwk
jemitche1 pushed a commit to jemitche1/pytorch that referenced this pull request Jun 13, 2026
pytorch#183837)

Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * __->__#183837

--- --- ---

### [HOP][Dynamic Shapes] Ignore discarded unbacked symbols in fake traces

Higher-order operator metadata paths sometimes run fake traces only to answer
questions such as aliasing and mutation. Those traces are discarded, so
unbacked symbols created or left pending inside them should not force a
binding in the trace outputs.

FlexAttention hits this through its score_mod alias check and fake/metadata
propagation when tracing with dynamic block-mask inputs. The internal
block-mask symbols are not semantic outputs of the region, but
compute_unbacked_bindings still required every pending symbol to appear in
the returned values and raised PendingUnbackedSymbolNotFound.

Teach the binding check to honor the existing ignore_fresh_unbacked_symbols
contract by treating pending symbols as ignorable while that TLS is active.
Also make the HOP alias/mutation helper reuse the fake mode carried by the
branch inputs, rather than an unrelated ambient tracing context, and run its
diagnostic make_fx trace under the ignore-fresh-unbacked context.

Fixes pytorch#183677

Test Plan:

python test/higher_order_ops/test_utils.py -v

python test/functorch/test_control_flow.py TestControlFlowTraced.test_cond_functionalized_nested

lintrunner --config=.lintrunner.toml torch/_higher_order_ops/utils.py torch/fx/experimental/symbolic_shapes.py test/higher_order_ops/test_utils.py

Pull Request resolved: pytorch#183837
Approved by: https://github.com/laithsakka
@github-actions
github-actions Bot deleted the sanketpurandare/stack/16 branch July 10, 2026 02:25
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * pytorch#184911
 * __->__#183545

--- --- ---

### [DTensor] Use explicit hints for unbacked sharding

DTensor sharding falls back to conservative behavior when a shard size is
unbacked because divisibility and sharding metadata often require Python
decisions. That is correct for arbitrary symbolic sizes, but it is too
conservative for tracing paths that explicitly override optimization hints and
know that a sharded dimension or strict-view split factor is stable.

This change only trusts explicit optimization hint overrides for those Python
decisions. When a hint selects a no-padding or shardable branch, the traced graph
still emits the symbolic torch._check for the correctness predicate, so runtime
values that satisfy the predicate continue to work and values that violate it
fail closed. The hint chooses the trace-time policy branch; it is not baked in as
a concrete tensor extent.

For _StridedShard, view propagation now preserves a symbolic split_factor when
the split factor came from symbolic shape propagation. The C++ placement still
needs an int64_t unroll count for list-based lowering, so the Python wrapper
stores the symbolic split factor separately and uses the explicit hint only as a
guarded unroll count. The equality assertion is deferred instead of directly
calling torch._check(symbol == hint), because the direct check immediately
refines the ShapeEnv replacement and destroys the symbolic split_factor
metadata we need to preserve.

Assisted by Codex.

Fixes pytorch#183678

Test Plan:

```bash
python -m pytest -q test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py::TestViewOps::test_view_groups_unbacked_sharding_propagation
```

```bash
lintrunner --config=.lintrunner.toml torch/distributed/tensor/placement_types.py torch/distributed/tensor/_ops/_view_ops.py torch/distributed/tensor/_ops/utils.py test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py
```

Pull Request resolved: pytorch#183545
Approved by: https://github.com/pianpwk
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * pytorch#184911
 * __->__#183545

--- --- ---

### [DTensor] Use explicit hints for unbacked sharding

DTensor sharding falls back to conservative behavior when a shard size is
unbacked because divisibility and sharding metadata often require Python
decisions. That is correct for arbitrary symbolic sizes, but it is too
conservative for tracing paths that explicitly override optimization hints and
know that a sharded dimension or strict-view split factor is stable.

This change only trusts explicit optimization hint overrides for those Python
decisions. When a hint selects a no-padding or shardable branch, the traced graph
still emits the symbolic torch._check for the correctness predicate, so runtime
values that satisfy the predicate continue to work and values that violate it
fail closed. The hint chooses the trace-time policy branch; it is not baked in as
a concrete tensor extent.

For _StridedShard, view propagation now preserves a symbolic split_factor when
the split factor came from symbolic shape propagation. The C++ placement still
needs an int64_t unroll count for list-based lowering, so the Python wrapper
stores the symbolic split factor separately and uses the explicit hint only as a
guarded unroll count. The equality assertion is deferred instead of directly
calling torch._check(symbol == hint), because the direct check immediately
refines the ShapeEnv replacement and destroys the symbolic split_factor
metadata we need to preserve.

A post-merge revert found additional gaps. First, strict view propagation could
raise for Shard on a non-first flattened input dim through a combined
Split(Flatten(...)) rule. That combined rewrite cannot preserve the exact
intermediate _StridedShard placement, so it now falls back to input replication
for that mesh dim instead of crashing. During compiled DTensor tracing, the same
redistribution path can create temporary local fake symbols while computing
per-rank shard offsets; those symbols are implementation details because DTensor
returns the global output contract through the output spec, so they are ignored
while tracing local redistribution and local compute.

Second, the ignore-fresh-symbol helper must live in this commit, not in a later
stack commit, because PR 183545 CI runs this commit independently. Third, stale
xfails are removed only where the behavior now genuinely works: eager/multithreaded
view and reshape, unbacked view_copy, and unbacked pixel_shuffle/pixel_unshuffle.
Compiled view and reshape remain xfailed because AOTAutograd alias replay still
reaches DTensor as_strided. The view fallback test is also updated to distinguish
comm-free representable cases from deliberate Replicate fallback cases, and the
_StridedShard utility test now mocks the all_gather_single path that the code
actually calls.

Assisted by Codex.

Fixes pytorch#183678

Test Plan:

```bash
pip install -e . -v --no-build-isolation --no-deps
```

```bash
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=3 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_take_along_dim_cpu_float32 -v
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=2 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_linalg_tensorsolve_cpu_float32 -v
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=0 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_nn_functional_instance_norm_cpu_float32 -v
python test/distributed/tensor/test_view_ops.py TestViewOps.test_split_flatten_non_first_shard_falls_back_to_replicate -v
python test/distributed/tensor/test_dtensor_ops.py -k test_unbacked_dtensor_op_db_view_copy_cpu_float32 -v
python test/distributed/tensor/test_dtensor_ops.py -k test_unbacked_dtensor_op_db_nn_functional_pixel_shuffle_cpu_float32 -v
```

```bash
python -m pytest test/distributed/tensor/test_dtensor_ops.py -q -k 'test_compiled_dtensor_op_db_reshape_cpu_float32 or test_dtensor_op_db_reshape_cpu_float32 or test_dtensor_op_db_view_cpu_float32 or test_unbacked_dtensor_op_db_nn_functional_pixel_unshuffle_cpu_float32'
python -m pytest test/distributed/tensor/test_dtensor_ops.py -q -k 'test_compiled_dtensor_op_db_view_cpu_float32 or test_dtensor_op_db_view_cpu_float32'
python test/distributed/tensor/test_view_ops.py TestViewOps.test_dtensor_flatten_split_multi_mesh -v
python test/distributed/tensor/test_utils.py LocalTest.test_strided_shard_to_replicate_preserves_even_unbacked_shape -v
python test/distributed/tensor/test_dtensor_compile.py TestDTensorCompile.test_dtensor_different_gradient_placement -v
python test/distributed/tensor/test_compile_on_one_rank.py TestCompileOnOneRank.test_compiled_dtensor_rng_op_graph_consistency -v
python test/distributed/tensor/debug/test_debug_mode.py TestDTensorDebugMode.test_debug_mode_mm -v
python test/inductor/test_compiled_autograd.py TestCompiledAutograd.test_dtensor_backward_symints_do_not_reuse_native_sharding_cache -v
python test/dynamo/test_fx_graph_runnable.py FxGraphRunnableTest.test_dtensor_compile_redistribute -v
lintrunner -a
```

```bash
python -m pytest -q test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py::TestViewOps::test_view_groups_unbacked_sharding_propagation
```

```bash
lintrunner --config=.lintrunner.toml torch/distributed/tensor/placement_types.py torch/distributed/tensor/_ops/_view_ops.py torch/distributed/tensor/_ops/utils.py test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py
```

Pull Request resolved: pytorch#183545
Approved by: https://github.com/pianpwk
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * __->__#184911

--- --- ---

### [Inductor][TP] Fuse slice-cat TP collective patterns

Graph-level chunking can expose tensor-parallel collective values through simple slice/cat reassembly before a matmul, or can feed reduce-scatter from an equivalent slice/cat form after a matmul. The existing micro-pipeline TP matcher only recognized the direct all-gather/matmul and matmul/reduce-scatter patterns, so these semantically equivalent graphs missed fusion.

Teach the all-gather and reduce-scatter matchers to recognize slice/cat forms that reassemble a single collective value without changing the TP communication contract. The replacement and erase logic remains scoped to the matched nodes, and the matmul multi-user guard still rejects users outside the matched reassembly.

Fixes pytorch#184833

Test Plan:

python test/distributed/tensor/parallel/test_micro_pipeline_tp.py MicroPipelineTPTest.test_find_all_gather_patterns MicroPipelineTPTest.test_find_reduce_scatter_patterns MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat_trim MicroPipelineTPTest.test_fuse_matmul_reduce_scatter_slice_cat -v

lintrunner --config=.lintrunner.toml torch/_inductor/fx_passes/micro_pipeline_tp.py test/distributed/tensor/parallel/test_micro_pipeline_tp.py

Pull Request resolved: pytorch#184911
Approved by: https://github.com/pianpwk
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
pytorch#183837)

Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * __->__#183837

--- --- ---

### [HOP][Dynamic Shapes] Ignore discarded unbacked symbols in fake traces

Higher-order operator metadata paths sometimes run fake traces only to answer
questions such as aliasing and mutation. Those traces are discarded, so
unbacked symbols created or left pending inside them should not force a
binding in the trace outputs.

FlexAttention hits this through its score_mod alias check and fake/metadata
propagation when tracing with dynamic block-mask inputs. The internal
block-mask symbols are not semantic outputs of the region, but
compute_unbacked_bindings still required every pending symbol to appear in
the returned values and raised PendingUnbackedSymbolNotFound.

Teach the binding check to honor the existing ignore_fresh_unbacked_symbols
contract by treating pending symbols as ignorable while that TLS is active.
Also make the HOP alias/mutation helper reuse the fake mode carried by the
branch inputs, rather than an unrelated ambient tracing context, and run its
diagnostic make_fx trace under the ignore-fresh-unbacked context.

Fixes pytorch#183677

Test Plan:

python test/higher_order_ops/test_utils.py -v

python test/functorch/test_control_flow.py TestControlFlowTraced.test_cond_functionalized_nested

lintrunner --config=.lintrunner.toml torch/_higher_order_ops/utils.py torch/fx/experimental/symbolic_shapes.py test/higher_order_ops/test_utils.py

Pull Request resolved: pytorch#183837
Approved by: https://github.com/laithsakka
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * pytorch#184911
 * __->__#183545

--- --- ---

### [DTensor] Use explicit hints for unbacked sharding

DTensor sharding falls back to conservative behavior when a shard size is
unbacked because divisibility and sharding metadata often require Python
decisions. That is correct for arbitrary symbolic sizes, but it is too
conservative for tracing paths that explicitly override optimization hints and
know that a sharded dimension or strict-view split factor is stable.

This change only trusts explicit optimization hint overrides for those Python
decisions. When a hint selects a no-padding or shardable branch, the traced graph
still emits the symbolic torch._check for the correctness predicate, so runtime
values that satisfy the predicate continue to work and values that violate it
fail closed. The hint chooses the trace-time policy branch; it is not baked in as
a concrete tensor extent.

For _StridedShard, view propagation now preserves a symbolic split_factor when
the split factor came from symbolic shape propagation. The C++ placement still
needs an int64_t unroll count for list-based lowering, so the Python wrapper
stores the symbolic split factor separately and uses the explicit hint only as a
guarded unroll count. The equality assertion is deferred instead of directly
calling torch._check(symbol == hint), because the direct check immediately
refines the ShapeEnv replacement and destroys the symbolic split_factor
metadata we need to preserve.

Assisted by Codex.

Fixes pytorch#183678

Test Plan:

```bash
python -m pytest -q test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py::TestViewOps::test_view_groups_unbacked_sharding_propagation
```

```bash
lintrunner --config=.lintrunner.toml torch/distributed/tensor/placement_types.py torch/distributed/tensor/_ops/_view_ops.py torch/distributed/tensor/_ops/utils.py test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py
```

Pull Request resolved: pytorch#183545
Approved by: https://github.com/pianpwk
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * pytorch#184911
 * __->__#183545

--- --- ---

### [DTensor] Use explicit hints for unbacked sharding

DTensor sharding falls back to conservative behavior when a shard size is
unbacked because divisibility and sharding metadata often require Python
decisions. That is correct for arbitrary symbolic sizes, but it is too
conservative for tracing paths that explicitly override optimization hints and
know that a sharded dimension or strict-view split factor is stable.

This change only trusts explicit optimization hint overrides for those Python
decisions. When a hint selects a no-padding or shardable branch, the traced graph
still emits the symbolic torch._check for the correctness predicate, so runtime
values that satisfy the predicate continue to work and values that violate it
fail closed. The hint chooses the trace-time policy branch; it is not baked in as
a concrete tensor extent.

For _StridedShard, view propagation now preserves a symbolic split_factor when
the split factor came from symbolic shape propagation. The C++ placement still
needs an int64_t unroll count for list-based lowering, so the Python wrapper
stores the symbolic split factor separately and uses the explicit hint only as a
guarded unroll count. The equality assertion is deferred instead of directly
calling torch._check(symbol == hint), because the direct check immediately
refines the ShapeEnv replacement and destroys the symbolic split_factor
metadata we need to preserve.

A post-merge revert found additional gaps. First, strict view propagation could
raise for Shard on a non-first flattened input dim through a combined
Split(Flatten(...)) rule. That combined rewrite cannot preserve the exact
intermediate _StridedShard placement, so it now falls back to input replication
for that mesh dim instead of crashing. During compiled DTensor tracing, the same
redistribution path can create temporary local fake symbols while computing
per-rank shard offsets; those symbols are implementation details because DTensor
returns the global output contract through the output spec, so they are ignored
while tracing local redistribution and local compute.

Second, the ignore-fresh-symbol helper must live in this commit, not in a later
stack commit, because PR 183545 CI runs this commit independently. Third, stale
xfails are removed only where the behavior now genuinely works: eager/multithreaded
view and reshape, unbacked view_copy, and unbacked pixel_shuffle/pixel_unshuffle.
Compiled view and reshape remain xfailed because AOTAutograd alias replay still
reaches DTensor as_strided. The view fallback test is also updated to distinguish
comm-free representable cases from deliberate Replicate fallback cases, and the
_StridedShard utility test now mocks the all_gather_single path that the code
actually calls.

Assisted by Codex.

Fixes pytorch#183678

Test Plan:

```bash
pip install -e . -v --no-build-isolation --no-deps
```

```bash
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=3 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_take_along_dim_cpu_float32 -v
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=2 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_linalg_tensorsolve_cpu_float32 -v
PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=0 python test/distributed/tensor/test_dtensor_ops.py TestCompiledDTensorOpsCPU.test_compiled_dtensor_op_db_nn_functional_instance_norm_cpu_float32 -v
python test/distributed/tensor/test_view_ops.py TestViewOps.test_split_flatten_non_first_shard_falls_back_to_replicate -v
python test/distributed/tensor/test_dtensor_ops.py -k test_unbacked_dtensor_op_db_view_copy_cpu_float32 -v
python test/distributed/tensor/test_dtensor_ops.py -k test_unbacked_dtensor_op_db_nn_functional_pixel_shuffle_cpu_float32 -v
```

```bash
python -m pytest test/distributed/tensor/test_dtensor_ops.py -q -k 'test_compiled_dtensor_op_db_reshape_cpu_float32 or test_dtensor_op_db_reshape_cpu_float32 or test_dtensor_op_db_view_cpu_float32 or test_unbacked_dtensor_op_db_nn_functional_pixel_unshuffle_cpu_float32'
python -m pytest test/distributed/tensor/test_dtensor_ops.py -q -k 'test_compiled_dtensor_op_db_view_cpu_float32 or test_dtensor_op_db_view_cpu_float32'
python test/distributed/tensor/test_view_ops.py TestViewOps.test_dtensor_flatten_split_multi_mesh -v
python test/distributed/tensor/test_utils.py LocalTest.test_strided_shard_to_replicate_preserves_even_unbacked_shape -v
python test/distributed/tensor/test_dtensor_compile.py TestDTensorCompile.test_dtensor_different_gradient_placement -v
python test/distributed/tensor/test_compile_on_one_rank.py TestCompileOnOneRank.test_compiled_dtensor_rng_op_graph_consistency -v
python test/distributed/tensor/debug/test_debug_mode.py TestDTensorDebugMode.test_debug_mode_mm -v
python test/inductor/test_compiled_autograd.py TestCompiledAutograd.test_dtensor_backward_symints_do_not_reuse_native_sharding_cache -v
python test/dynamo/test_fx_graph_runnable.py FxGraphRunnableTest.test_dtensor_compile_redistribute -v
lintrunner -a
```

```bash
python -m pytest -q test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py::TestViewOps::test_view_groups_unbacked_sharding_propagation
```

```bash
lintrunner --config=.lintrunner.toml torch/distributed/tensor/placement_types.py torch/distributed/tensor/_ops/_view_ops.py torch/distributed/tensor/_ops/utils.py test/distributed/tensor/test_placement_types.py test/distributed/tensor/test_view_ops.py
```

Pull Request resolved: pytorch#183545
Approved by: https://github.com/pianpwk
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * pytorch#183837
 * __->__#184911

--- --- ---

### [Inductor][TP] Fuse slice-cat TP collective patterns

Graph-level chunking can expose tensor-parallel collective values through simple slice/cat reassembly before a matmul, or can feed reduce-scatter from an equivalent slice/cat form after a matmul. The existing micro-pipeline TP matcher only recognized the direct all-gather/matmul and matmul/reduce-scatter patterns, so these semantically equivalent graphs missed fusion.

Teach the all-gather and reduce-scatter matchers to recognize slice/cat forms that reassemble a single collective value without changing the TP communication contract. The replacement and erase logic remains scoped to the matched nodes, and the matmul multi-user guard still rejects users outside the matched reassembly.

Fixes pytorch#184833

Test Plan:

python test/distributed/tensor/parallel/test_micro_pipeline_tp.py MicroPipelineTPTest.test_find_all_gather_patterns MicroPipelineTPTest.test_find_reduce_scatter_patterns MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat MicroPipelineTPTest.test_fuse_all_gather_matmul_slice_cat_trim MicroPipelineTPTest.test_fuse_matmul_reduce_scatter_slice_cat -v

lintrunner --config=.lintrunner.toml torch/_inductor/fx_passes/micro_pipeline_tp.py test/distributed/tensor/parallel/test_micro_pipeline_tp.py

Pull Request resolved: pytorch#184911
Approved by: https://github.com/pianpwk
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
pytorch#183837)

Stacked PRs:
 * pytorch#183838
 * pytorch#186255
 * pytorch#183839
 * pytorch#183840
 * __->__#183837

--- --- ---

### [HOP][Dynamic Shapes] Ignore discarded unbacked symbols in fake traces

Higher-order operator metadata paths sometimes run fake traces only to answer
questions such as aliasing and mutation. Those traces are discarded, so
unbacked symbols created or left pending inside them should not force a
binding in the trace outputs.

FlexAttention hits this through its score_mod alias check and fake/metadata
propagation when tracing with dynamic block-mask inputs. The internal
block-mask symbols are not semantic outputs of the region, but
compute_unbacked_bindings still required every pending symbol to appear in
the returned values and raised PendingUnbackedSymbolNotFound.

Teach the binding check to honor the existing ignore_fresh_unbacked_symbols
contract by treating pending symbols as ignorable while that TLS is active.
Also make the HOP alias/mutation helper reuse the fake mode carried by the
branch inputs, rather than an unrelated ambient tracing context, and run its
diagnostic make_fx trace under the ignore-fresh-unbacked context.

Fixes pytorch#183677

Test Plan:

python test/higher_order_ops/test_utils.py -v

python test/functorch/test_control_flow.py TestControlFlowTraced.test_cond_functionalized_nested

lintrunner --config=.lintrunner.toml torch/_higher_order_ops/utils.py torch/fx/experimental/symbolic_shapes.py test/higher_order_ops/test_utils.py

Pull Request resolved: pytorch#183837
Approved by: https://github.com/laithsakka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/dtensor Run DTensor specific tests ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: inductor release notes: distributed (dtensor) release notes category release notes: distributed (pipeline) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inductor standalone_compile misses fallback output unbacked symbol binding

1 participant