Skip to content

[DTensor] lenient handling of view redistributes in decomposition flow - #175194

Closed
pianpwk wants to merge 5 commits into
gh/pianpwk/88/basefrom
gh/pianpwk/88/head
Closed

pianpwk wants to merge 5 commits into
gh/pianpwk/88/basefrom
gh/pianpwk/88/head

Conversation

@pianpwk

@pianpwk pianpwk commented Feb 17, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

#174918 skipped the decomposition flow for CIA ops, because we found it wasn't able to find non-replicate strategies for aten::linear. This turned out to be view handling related; the decomposition flow bans internal redistributes, but the .needs_redistribute semantics is too broad; for views it includes when DTensor dispatch needs to convert from global -> local tensor ops:

# shape and stride args need to be modified for
# view ops and new factory ops, potentially
if op_schema.op in self.op_to_shape_and_stride_idx:
assert isinstance(output_strategy.output_spec, DTensorSpec)
# It happens when the output has the same shape as the input
# and the input placements are not all Replicate().
if any(
isinstance(p, Shard | _StridedShard)
for p in output_strategy.output_spec.placements
):
schema = suggestion_schema or op_schema
assert isinstance(out_tensor_meta, TensorMeta)
suggestion_schema = self._adjust_shape_and_stride_args(
out_tensor_meta, schema, output_strategy.output_spec
)
needs_redistribute = True

For example, DTensor(shape=[8, 8], placements=[Shard(0)]).view(8, 4, 2) on a size-4 mesh, will recalculate and run .view(2, 4, 2) in local compute. The decomposition flow doesn't need this handling, as it always deals with global tensors.

This PR changes redistribution semantics to only be placement based, and reverts the CIA skip, allowing CIA ops to go through the decomposition flow again.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Feb 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (3 Unrelated Failures)

As of commit 8e65853 with merge base 704b8d3 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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

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

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

pianpwk added a commit that referenced this pull request Feb 17, 2026
ghstack-source-id: 5d1076e
Pull-Request: #175194
pianpwk added a commit that referenced this pull request Feb 18, 2026
ghstack-source-id: f018708
Pull Request resolved: #175194
pianpwk added a commit that referenced this pull request Feb 18, 2026
ghstack-source-id: 74157be
Pull Request resolved: #175194
@pianpwk pianpwk changed the title [DTensor] CIA + decomp [DTensor] easier handling of view redistributes in decomposition flow Feb 18, 2026
@pianpwk pianpwk changed the title [DTensor] easier handling of view redistributes in decomposition flow [DTensor] lenient handling of view redistributes in decomposition flow Feb 18, 2026
if output_sharding.needs_redistribute: # pyrefly: ignore [missing-attribute]
raise RuntimeError(f"Decomposition requires redistribution for {func}")
if (
output_sharding.needs_redistribute # pyrefly: ignore [missing-attribute]

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.

asked claude to explain the status-quo of view-ops needs_redistribute handling to me:

View ops don't set needs_redistribute directly. The flow is:

  1. Strategy creation (_view_ops.py): reshape_strategy calls propagate_shape_and_sharding to determine target input/output placements. If a shard can't be preserved through the view (e.g., Shard(1) through a
    flatten), the target input placement becomes Replicate(). The resulting OpSpec records input_specs (desired placements) and redistribute_cost.
  2. Strategy selection (_sharding_prop.py:600-686): ShardingPropagator picks the lowest-cost strategy, then compares each input's actual placements to the strategy's desired placements. If they differ,
    needs_redistribute = True and a suggestion_schema (the redistribute_schema) is built.
  3. Shape/stride adjustment: For view ops specifically, there's a second reason to set needs_redistribute = True — when the output is sharded, the shape/stride arguments (not the tensors) need adjustment to
    reflect local sizes. This sets use_val_from_redistribute_schema = True so the dispatch layer reads non-tensor args from the schema too.
  4. Dispatch (_dispatch.py:275-283): When needs_redistribute is True, redistribute_local_args redistributes tensors whose placements differ and optionally replaces shape/stride args.

redistribute_schema
:= output_sharding.redistribute_schema # pyrefly: ignore [missing-attribute]
)
is not None

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.

according to claude, redistribute_schema is non-none even for the view case, but it is a trivial schema that matches the inputs and incurs a no-op redistribute. is this a lie? should we instead be checking use_val_from_redistribute_schema here?

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.

no, I think that's correct, but we want to allow that path (no-op redistribute, use_val_from_redistribute_schema is True)

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.

My point was that schema!=None should be an invariant anyway so you learn nothing by checking it here. Instead you should be checking the other flag mentioned above?

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.

Oh, never mind. I guess your way works too- checking that the redistribute is a no-op.

My way is a shortcut- the other flag tells us this is the view special case and we can then assume redistribution is a no op. But actually I'm not sure if that flat implies only view- it could apply to reshape too for all I know? Anyway looks fine this way, stamping.

[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Feb 19, 2026
ghstack-source-id: d026756
Pull Request resolved: #175194
…osition flow"


#174918 skipped the decomposition flow for CIA ops, because we found it wasn't able to find non-replicate strategies for aten::linear. This turned out to be view handling related; the decomposition flow bans internal redistributes, but the `.needs_redistribute` semantics is too broad; for views it includes when DTensor dispatch needs to convert from global -> local tensor ops: https://github.com/pytorch/pytorch/blob/c238e6ed935df371f208b72becb206858a0cf087/torch/distributed/tensor/_sharding_prop.py#L638-L653

For example, `DTensor(shape=[8, 8], placements=[Shard(0)]).view(8, 4, 2)` on a size-4 mesh, will recalculate and run `.view(2, 4, 2)` in local compute. The decomposition flow doesn't need this handling, as it always deals with global tensors.

This PR changes redistribution semantics to only be placement based, and reverts the CIA skip, allowing CIA ops to go through the decomposition flow again.


[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Feb 20, 2026
ghstack-source-id: cc9b9f4
Pull Request resolved: #175194
@pianpwk

pianpwk commented Feb 20, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

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

norx1991 pushed a commit that referenced this pull request Feb 24, 2026
#175194)

#174918 skipped the decomposition flow for CIA ops, because we found it wasn't able to find non-replicate strategies for aten::linear. This turned out to be view handling related; the decomposition flow bans internal redistributes, but the `.needs_redistribute` semantics is too broad; for views it includes when DTensor dispatch needs to convert from global -> local tensor ops: https://github.com/pytorch/pytorch/blob/c238e6ed935df371f208b72becb206858a0cf087/torch/distributed/tensor/_sharding_prop.py#L638-L653

For example, `DTensor(shape=[8, 8], placements=[Shard(0)]).view(8, 4, 2)` on a size-4 mesh, will recalculate and run `.view(2, 4, 2)` in local compute. The decomposition flow doesn't need this handling, as it always deals with global tensors.

This PR changes redistribution semantics to only be placement based, and reverts the CIA skip, allowing CIA ops to go through the decomposition flow again.

Pull Request resolved: #175194
Approved by: https://github.com/wconstab
@github-actions
github-actions Bot deleted the gh/pianpwk/88/head branch March 23, 2026 02:24
EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
pytorch#175194)

pytorch#174918 skipped the decomposition flow for CIA ops, because we found it wasn't able to find non-replicate strategies for aten::linear. This turned out to be view handling related; the decomposition flow bans internal redistributes, but the `.needs_redistribute` semantics is too broad; for views it includes when DTensor dispatch needs to convert from global -> local tensor ops: https://github.com/pytorch/pytorch/blob/c238e6ed935df371f208b72becb206858a0cf087/torch/distributed/tensor/_sharding_prop.py#L638-L653

For example, `DTensor(shape=[8, 8], placements=[Shard(0)]).view(8, 4, 2)` on a size-4 mesh, will recalculate and run `.view(2, 4, 2)` in local compute. The decomposition flow doesn't need this handling, as it always deals with global tensors.

This PR changes redistribution semantics to only be placement based, and reverts the CIA skip, allowing CIA ops to go through the decomposition flow again.

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

Labels

ciflow/inductor 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