Skip to content

[dtensor] fixing index single_dim_strategies for contigous indices - #178350

Closed
anshul-si wants to merge 9 commits into
gh/anshul-si/113/basefrom
gh/anshul-si/113/head
Closed

anshul-si wants to merge 9 commits into
gh/anshul-si/113/basefrom
gh/anshul-si/113/head

Conversation

@anshul-si

@anshul-si anshul-si commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary: When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

image Example of How Current Code is Wrong

Test Cases

  1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
  2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
  3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Stack from ghstack (oldest at bottom):

@pytorch-bot

pytorch-bot Bot commented Mar 25, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 10b19ae with merge base 9bd2eff (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added ciflow/dtensor Run DTensor specific tests ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests release notes: distributed (dtensor) release notes category labels Mar 25, 2026
anshul-si added a commit that referenced this pull request Mar 25, 2026
@anshul-si anshul-si added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 25, 2026
**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly. 

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation 
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation




[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 25, 2026
@anshul-si

Copy link
Copy Markdown
Contributor Author

@claude review this pr

@claude

claude Bot commented Mar 25, 2026 •

Copy link
Copy Markdown

Claude finished @anshul-si's task in 1m 40s —— View job


PR Review: fixing index single_dim_strategies for contiguous indices

  • Read the PR diff and changed files
  • Read surrounding context in the implementation file
  • Read surrounding context in the test file
  • Verify correctness of the dim-mapping logic
  • Post review feedback

Summary

This PR fixes an incorrect values-dim-to-self-dim mapping in index_put_single_dim_strategy that caused wrong sharding strategies for aten.index_add and aten.index_copy (which decompose to aten.index_put with a leading None in the indices tuple).

The core insight is correct: PyTorch's advanced indexing has two different behaviors for where the broadcast shape appears in the output:

  • Contiguous indexed dims: broadcast shape replaces the indexed block in-place → (*non_indexed_before, *broadcast, *non_indexed_after)
  • Non-contiguous indexed dims: broadcast shape moves to the front → (*broadcast, *non_indexed_dims)

The old code always assumed the non-contiguous layout, which broke when index_add(self, dim=1, idx, source) decomposed to index_put(self, (None, idx), source).

Correctness Verification

I traced through the key scenarios to verify the new mapping logic:

Example 1: index_add(self[4,8], dim=1, idx[2], source[4,2])

  • Decomposes to index_put(self, (None, idx), source)
  • indexed_dims={1}, non_indexed_dims=[0], broadcast_ndim=1
  • Contiguous ✓, first_indexed=1
  • self_dim=0 < first_indexed=1 → values_dim=0 ✓ (maps self dim 0 to source dim 0)

Example 2: index_put(self[4,6,8], (idx[2],), values[2,6,8])

  • indexed_dims={0}, non_indexed_dims=[1,2], broadcast_ndim=1
  • Contiguous ✓, first_indexed=0
  • self_dim=1 >= 0 → values_dim = 1 - 1 + 1 = 1 ✓
  • self_dim=2 >= 0 → values_dim = 2 - 1 + 1 = 2 ✓

Example 3: index_put(self[4,6,8,10], (None, idx0[2], idx1[2], None), values[4,2,10])

  • indexed_dims={1,2}, non_indexed_dims=[0,3], broadcast_ndim=1, n_indexed=2
  • Contiguous ✓, first_indexed=1
  • self_dim=0 < 1 → values_dim=0 ✓
  • self_dim=3 >= 1 → values_dim = 3 - 2 + 1 = 2 ✓

All cases check out.

Code Review

Implementation (torch/distributed/tensor/_ops/_tensor_ops.py)

The logic at lines 1138-1154 is sound. A few observations:

  1. Contiguity check (line 1139-1141): The check len(indexed_dims_sorted) <= 1 or (last - first + 1 == len) correctly identifies contiguous blocks of indexed dimensions. The <= 1 handles single-dim and empty cases.

  2. Dim mapping (lines 1144-1154): The iteration changes from for values_dim in range(broadcast_ndim, values_ndim) to for i, self_dim in enumerate(non_indexed_dims) — this is a cleaner approach since it iterates over the non-indexed self dims directly rather than computing them indirectly from values dims.

  3. Edge case — empty indexed_dims_sorted (line 1145): The and indexed_dims_sorted guard correctly falls through to the else-branch when there are no indexed dims, preserving existing behavior.

  4. Minor nit: The variable n_indexed (line 1122) was already computed before the diff; the PR removes the now-unused values_ndim variable. Clean.

Tests (test/distributed/tensor/test_tensor_ops.py)

  1. test_index_ops_leading_none_dim_mapping (line 945): This is the key regression test. It constructs the exact scenario from the bug diagram — Shard(0) on self and Shard(1) on source with indexing on dim 1. With the old code, the strategy would accept this without redistribution, producing wrong results. The test verifies numerical correctness by comparing against the non-distributed reference.

  2. _test_index_op_partial_propagation (line 902): Tests that the Partial placement rule works correctly for index_copy and index_add. The reduce_op parametrization covers sum, avg, max, and min for index_copy, and sum/avg for index_add.

  3. instantiate_parametrized_tests(DistTensorOpsTest) (line 1374): Required for @parametrize to work. Placed before create_local_tensor_test_class — ordering matters here since the class needs to be instantiated before it's passed to create_local_tensor_test_class.

Potential Concerns

  1. Multi-dim contiguous indexing is untested: The fix handles multi-dim contiguous indexing (e.g., (None, idx0, idx1)), but the tests only cover single-dim cases. This is fine for now since the docstring TODO at line 1110-1113 notes that non-contiguous indexed dims are blocked by an infra bug, and index_add/index_copy always index a single dim. But if multi-dim contiguous support is needed later, adding a test for that case would be valuable.

  2. The reduce_op parametrization for test_index_add_partial_propagation only covers ["sum", "avg"] while test_index_copy_partial_propagation covers ["sum", "avg", "max", "min"]. This is presumably because index_add only supports additive accumulation — worth a brief comment in the test to explain why, but not a blocker.

Verdict

The fix is correct, well-motivated, and the test coverage targets the exact failure mode described in the PR. The code is clean and the comments explain the contiguous vs. non-contiguous distinction well. No blocking issues.


**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly. 

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation 
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation




[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 25, 2026
**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly. 

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation 
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation




[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 25, 2026
@anshul-si
anshul-si requested review from pianpwk and wconstab and removed request for wconstab March 25, 2026 21:10
@anshul-si anshul-si changed the title fixing index single_dim_strategies for contigous indices [dtensor] fixing index single_dim_strategies for contigous indices Mar 26, 2026

@wconstab wconstab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

first i want to understand the testing gap. how was the old index_put strategy landed without catching this? Is it because it didn't go through strategy_validation / opinfo testing, or becuase those tests didn't have sufficient test vectors?

Then, when did we start using decomps for index_add? What testing happened at the time this path was enabled?

The exception is broadcasted value dimensions (size 1) - we require Replicate, but can shard self.
- Additionally, we allow the full Partial rule on non-indexing tensors.

TODO(pianpwk): support non-contiguous indexed dims (None gaps in indices tuple,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

uh, is this TODO related, is it fixed now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This TODO is unrelated, my fix has to do with problems in contiguous indexed dims, where None is in front of broadcasted dims.

# values shape = (*non_indexed_before, *broadcast_shape, *non_indexed_after)
# Non-contiguous (e.g., (idx0, None, idx1)): broadcast goes to front.
# values shape = (*broadcast_shape, *non_indexed_dim_sizes)
indexed_dims_sorted = sorted(indexed_dims)

@wconstab wconstab Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i was confused why we need to sort indexed_dims. i think it's just because it's a set, and we want it in the order we built it in as if it were a list

@anshul-si

Copy link
Copy Markdown
Contributor Author

first i want to understand the testing gap. how was the old index_put strategy landed without catching this? Is it because it didn't go through strategy_validation / opinfo testing, or becuase those tests didn't have sufficient test vectors?

Then, when did we start using decomps for index_add? What testing happened at the time this path was enabled?

index_put has different OpInfo than index_add, so strategy validation passes on it. OpInfo for index_put always uses index = 0, whereas index_add has atleast one example where index = 1, exposing this issue

… indices"

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly. 

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation 
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation




[ghstack-poisoned]
… indices"

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly. 

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation 
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation




[ghstack-poisoned]
@anshul-si

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

… indices"

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly. 

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation 
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation




[ghstack-poisoned]
@anshul-si

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

pytorchmergebot pushed a commit that referenced this pull request Apr 7, 2026
…178456)

**Summary:** Added strategy for index_reduce which has no decompositions. index_fill does have a decomposition that fails because part of its decomposition includes scalar_tensor, a factory function. See #171652 for a deeper explanation on why these fail.

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_fill
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_reduce

Pull Request resolved: #178456
Approved by: https://github.com/wconstab
ghstack dependencies: #178350
aditvenk added a commit that referenced this pull request Apr 7, 2026
The strategy computation for aten.index_put had two remaining issues
blocking models like Falcon-7b that use list indexing
(e.g. tensor[..., [-1], :]):

1. aten.index_put_.default (in-place variant, generated by autograd in the
   backward of aten.index.Tensor) was not registered in the single-dim
   strategy system.

2. _get_num_tensor_inputs counted None children in TupleStrategy, but
   args_strategy drops them, causing a count mismatch for ops with
   List[Optional[Tensor]] arguments.

The values-dim-to-self-dim mapping fix for consecutive indexed dims was
already landed in #178350; this commit adds the missing op registration,
the infra bug fix, updated docstrings, and additional test coverage for
None indices, in-place index_put_, non-contiguous indexed dims, and
mid-tensor indexing.

Authored with Claude.

Fixes meta-pytorch/autoparallel#348

<!-- ps-id: 3fc0ba49-8b82-4c1d-b532-2ba111099dc3 -->
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Pull Request resolved: pytorch#178350
Approved by: https://github.com/wconstab
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
…dices (pytorch#178350)"

This reverts commit 684f506.

Reverted pytorch#178350 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#178350 (comment)))
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Pull Request resolved: pytorch#178350
Approved by: https://github.com/wconstab
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
…ytorch#178456)

**Summary:** Added strategy for index_reduce which has no decompositions. index_fill does have a decomposition that fails because part of its decomposition includes scalar_tensor, a factory function. See pytorch#171652 for a deeper explanation on why these fail.

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_fill
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_reduce

Pull Request resolved: pytorch#178456
Approved by: https://github.com/wconstab
ghstack dependencies: pytorch#178350
aditvenk added a commit that referenced this pull request Apr 8, 2026
The strategy computation for aten.index_put had two remaining issues
blocking models like Falcon-7b that use list indexing
(e.g. tensor[..., [-1], :]):

1. aten.index_put_.default (in-place variant, generated by autograd in the
   backward of aten.index.Tensor) was not registered in the single-dim
   strategy system.

2. _get_num_tensor_inputs counted None children in TupleStrategy, but
   args_strategy drops them, causing a count mismatch for ops with
   List[Optional[Tensor]] arguments.

The values-dim-to-self-dim mapping fix for consecutive indexed dims was
already landed in #178350; this commit adds the missing op registration,
the infra bug fix, updated docstrings, and additional test coverage for
None indices, in-place index_put_, non-contiguous indexed dims, and
mid-tensor indexing.

Authored with Claude.

Fixes meta-pytorch/autoparallel#348

<!-- ps-id: 3fc0ba49-8b82-4c1d-b532-2ba111099dc3 -->
pytorchmergebot pushed a commit that referenced this pull request Apr 8, 2026
The strategy computation for aten.index_put had two remaining issues blocking models like Falcon-7b that use list indexing (e.g. tensor[..., [-1], :]):

1. aten.index_put_.default (in-place variant, generated by autograd in the backward of aten.index.Tensor) was not registered in the single-dim strategy system.

2. _get_num_tensor_inputs counted None children in TupleStrategy, but args_strategy drops them, causing a count mismatch for ops with List[Optional[Tensor]] arguments.

The values-dim-to-self-dim mapping fix for consecutive indexed dims was already landed in #178350; this commit adds the missing op registration, the infra bug fix, updated docstrings, and additional test coverage for None indices, in-place index_put_, non-contiguous indexed dims, and mid-tensor indexing.

Authored with Claude.

Fixes meta-pytorch/autoparallel#348
Pull Request resolved: #179217
Approved by: https://github.com/pianpwk
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 9, 2026
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Pull Request resolved: pytorch#178350
Approved by: https://github.com/wconstab
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 9, 2026
…dices (pytorch#178350)"

This reverts commit 684f506.

Reverted pytorch#178350 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#178350 (comment)))
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 10, 2026
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Pull Request resolved: pytorch#178350
Approved by: https://github.com/wconstab
bobrenjc93 pushed a commit to bobrenjc93/pytorch that referenced this pull request Apr 10, 2026
…dices (pytorch#178350)"

This reverts commit 684f506.

Reverted pytorch#178350 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#178350 (comment)))
@github-actions
github-actions Bot deleted the gh/anshul-si/113/head branch May 8, 2026 02:26
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Pull Request resolved: pytorch#178350
Approved by: https://github.com/wconstab
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…dices (pytorch#178350)"

This reverts commit 684f506.

Reverted pytorch#178350 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#178350 (comment)))
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Pull Request resolved: pytorch#178350
Approved by: https://github.com/wconstab
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ytorch#178456)

**Summary:** Added strategy for index_reduce which has no decompositions. index_fill does have a decomposition that fails because part of its decomposition includes scalar_tensor, a factory function. See pytorch#171652 for a deeper explanation on why these fail.

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_fill
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_reduce

Pull Request resolved: pytorch#178456
Approved by: https://github.com/wconstab
ghstack dependencies: pytorch#178350
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…79217)

The strategy computation for aten.index_put had two remaining issues blocking models like Falcon-7b that use list indexing (e.g. tensor[..., [-1], :]):

1. aten.index_put_.default (in-place variant, generated by autograd in the backward of aten.index.Tensor) was not registered in the single-dim strategy system.

2. _get_num_tensor_inputs counted None children in TupleStrategy, but args_strategy drops them, causing a count mismatch for ops with List[Optional[Tensor]] arguments.

The values-dim-to-self-dim mapping fix for consecutive indexed dims was already landed in pytorch#178350; this commit adds the missing op registration, the infra bug fix, updated docstrings, and additional test coverage for None indices, in-place index_put_, non-contiguous indexed dims, and mid-tensor indexing.

Authored with Claude.

Fixes meta-pytorch/autoparallel#348
Pull Request resolved: pytorch#179217
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
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

Pull Request resolved: pytorch#178350
Approved by: https://github.com/wconstab
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…dices (pytorch#178350)"

This reverts commit 684f506.

Reverted pytorch#178350 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#178350 (comment)))
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ytorch#178350)

**Summary:** When running strategy_validator on aten.index_add and aten.index_copy, it found incorrect rules produced by their decomposition to aten.index_put. The old index_put strategy assumes the broadcast shape always occupies the leading dimensions of values, which is only true when indexed dims are non-contiguous. When indexed dims are contiguous (as they always are for index_add/index_copy, which index a single dim), PyTorch replaces them in-place, so values layout is (*non_indexed_before, *broadcast, *non_indexed_after). The old code skips the first broadcast_ndim dims of values, misidentifying the actual non-indexed dims as broadcast and vice versa, which produces strategies that allow sharding the broadcast dim of values (e.g. S(0), R, S(1) -> S(0)). At runtime this causes a shape mismatch since each rank holds a slice of index selections rather than a slice of data. The fix checks whether indexed dims are contiguous and computes the correct values-dim-to-self-dim mapping accordingly.

<img width="972" height="269" alt="image" src="https://github.com/user-attachments/assets/1a57679a-9d7d-4f0a-8eea-b731ab18f410" />
Example of How Current Code is Wrong

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_ops_leading_none_dim_mapping
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_copy_partial_propagation
3. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_add_partial_propagation

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

**Summary:** Added strategy for index_reduce which has no decompositions. index_fill does have a decomposition that fails because part of its decomposition includes scalar_tensor, a factory function. See pytorch#171652 for a deeper explanation on why these fail.

**Test Cases**
1. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_fill
2. pytest test/distributed/tensor/test_tensor_ops.py -k test_index_reduce

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

The strategy computation for aten.index_put had two remaining issues blocking models like Falcon-7b that use list indexing (e.g. tensor[..., [-1], :]):

1. aten.index_put_.default (in-place variant, generated by autograd in the backward of aten.index.Tensor) was not registered in the single-dim strategy system.

2. _get_num_tensor_inputs counted None children in TupleStrategy, but args_strategy drops them, causing a count mismatch for ops with List[Optional[Tensor]] arguments.

The values-dim-to-self-dim mapping fix for consecutive indexed dims was already landed in pytorch#178350; this commit adds the missing op registration, the infra bug fix, updated docstrings, and additional test coverage for None indices, in-place index_put_, non-contiguous indexed dims, and mid-tensor indexing.

Authored with Claude.

Fixes meta-pytorch/autoparallel#348
Pull Request resolved: pytorch#179217
Approved by: https://github.com/pianpwk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/dtensor Run DTensor specific tests ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (dtensor) release notes category Reverted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants