Skip to content

[dtensor][index_ops] adding index_fill and index_reduce strategies - #178456

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

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

Conversation

@anshul-si

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

Copy link
Copy Markdown
Contributor

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

Stack from ghstack (oldest at bottom):

@pytorch-bot

pytorch-bot Bot commented Mar 26, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 01830a3 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 26, 2026
anshul-si added a commit that referenced this pull request Mar 26, 2026
@anshul-si

Copy link
Copy Markdown
Contributor Author

@claude review this PR

@anshul-si anshul-si added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 26, 2026
@claude

claude Bot commented Mar 26, 2026 •

Copy link
Copy Markdown

Claude finished @anshul-si's task in 4m 29s —— View job


Review of PR #178456: [dtensor][index_ops] adding index_fill and index_reduce strategies

  • Gather context and read changed files
  • Analyze the diff against base branch
  • Review code quality, correctness, and best practices
  • Post review feedback

Summary

This PR adds register_single_dim_strategy implementations for index_fill (both scalar and tensor variants) and index_reduce (including the inplace variant), along with tests. The strategies allow these ops to run on sharded DTensors without falling back to full replication.

The overall approach is sound: shard on all dims except the indexed dim, keep the index tensor replicated, and allow Partial placements where the reduce semantics are compatible.

Issues

1. Missing inplace index_fill_ variants

index_reduce correctly registers both aten.index_reduce.default and aten.index_reduce_.default (line 1263), but index_fill only registers the out-of-place variants (aten.index_fill.int_Scalar and aten.index_fill.int_Tensor). The inplace variants aten.index_fill_.int_Scalar and aten.index_fill_.int_Tensor are missing. Since these have identical signatures and sharding semantics, they should be registered alongside their out-of-place counterparts.

Fix this →

2. Shadowed op variable name in index_fill_scalar_single_dim_strategy (minor/style)

At _tensor_ops.py:1221, the list comprehension for op in ("avg", "max", "min") uses op as the iteration variable, which shadows the function parameter op: OpOverload. Python scoping rules make this work correctly (the comprehension variable is used inside Partial(op)), but it's confusing to readers. Consider renaming the comprehension variable to reduce_op for clarity:

[[Partial(reduce_op), Partial(reduce_op), Replicate()] for reduce_op in ("avg", "max", "min")]

3. @with_comms decorator is unnecessary on the new tests

test_index_fill (line 980) and test_index_reduce (line 998) both use @with_comms, but they're methods of DistTensorOpsTest(DTensorContinuousTestBase). The DTensorContinuousTestBase base class handles process group initialization automatically — no other test in this class uses @with_comms. While @with_comms is a no-op here (it passes through when init_pg isn't present), it's inconsistent with the rest of the class and could mislead future contributors into thinking it's required.

Fix this →

4. xfails in test_dtensor_ops.py not removed

test/distributed/tensor/test_dtensor_ops.py still has:

xfail("index_fill"),          # line 397
xfail("index_reduce", "prod"),  # line 398
xfail("index_reduce", "mean"),  # line 399
xfail("index_reduce", "amax"),  # line 400
xfail("index_reduce", "amin"),  # line 401

If these strategies are now correct, the corresponding xfails should be removed (or converted to skip if there are known remaining issues). Otherwise CI will silently pass even if the strategy produces wrong results — an xfail test that now passes will trigger an XPASS failure in some test frameworks, but it's worth verifying this is intentional to land in a follow-up.

5. Test coverage could be broader

The tests only exercise one sharding pattern: Shard(0) on self with the indexed dim being 1. Consider also testing:

  • Shard(1) with indexed dim 0 (should require redistribution since that's the indexed dim)
  • The int_Tensor variant of index_fill (currently only scalar fill is tested)
  • Partial placement propagation (similar to test_index_copy_partial_propagation which already exists for index_copy)

Correctness Analysis

The strategy logic is correct:

  • index_fill scalar: Correctly excludes Partial("sum") — each rank fills the same scalar v, so sum(v, v, ...) = n*v ≠ v. The avg/max/min reductions are idempotent on identical values, so they're valid.

  • index_fill tensor: Correctly allows all reduce types including sum. The 0-dim value tensor is Partial, meaning each rank holds a partial contribution that reduces to the full value.

  • index_reduce: Correctly shards self and source on the same dim while keeping index replicated. No Partial rules are provided, which is conservative and correct — index_reduce with non-trivial reduce semantics (mean, prod, etc.) makes Partial propagation non-trivial.

  • _index_dim_strategy helper: Clean factoring that correctly iterates over all dims except the indexed dim.

Formatting

The reformatting of contiguous_indexed at line 1138-1139 (joining the condition onto one line) is a whitespace-only change unrelated to the feature. This is fine but worth noting.


Overall this is a solid addition. The main actionable item is adding the missing inplace index_fill_ variant registrations. The other items are minor.

anshul-si added a commit that referenced this pull request Mar 26, 2026
@anshul-si
anshul-si requested review from pianpwk and wconstab March 26, 2026 01:49
…rategies"

**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





[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 26, 2026
return strategies


def _index_dim_strategy(

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.

do we not already have a helper that does this?

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.

We don't have helpers created, each other index op has its own defined strategy, with no shared infra. All of the existing ops have differences that would be annoying to edit them to accommodate the new ops. In the future, it maybe be possible to edit this helper function so that the existing ops use it, but there are fundamental differences, especially with index_put and index, that it should probably remain separate

],
[
[Partial(op), Partial(op), Replicate(), Partial(op)]
for op in ("sum", "avg", "max", "min")

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.

how did you determine which partials to include? could you add that logic in comments?

if this is 'all of them' you can use the constant in Partial class

return _index_dim_strategy(
args_schema,
lambda d: [
_ShardingPlaceholder(d),

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.

inline comments naming these (e.g. self, idx, values, ...) would be nice

…rategies"

**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





[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 27, 2026
…rategies"

**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





[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 31, 2026
…rategies"

**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





[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 31, 2026
…rategies"

**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





[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Mar 31, 2026
…rategies"

**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





[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Apr 2, 2026
…rategies"

**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





[ghstack-poisoned]
anshul-si added a commit that referenced this pull request Apr 6, 2026
@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

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
@github-actions
github-actions Bot deleted the gh/anshul-si/114/head branch May 8, 2026 02:26
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
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
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 ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (dtensor) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants