Skip to content

Localize output-size arguments of sharded ops as DTensor does - #601

Open
schmidt-jake wants to merge 1 commit into
meta-pytorch:mainfrom
schmidt-jake:jake/localize-shape-args
Open

schmidt-jake wants to merge 1 commit into
meta-pytorch:mainfrom
schmidt-jake:jake/localize-shape-args

Conversation

@schmidt-jake

Copy link
Copy Markdown

Problem

Fixes #600.

Some ops take the size of their output as an argument, such as the upsample backward ops' input_size, new_empty_strided's size and stride, and slice_backward's input_sizes. When such an op's output is sharded, DTensor rewrites these arguments to the local size (ShardingPropagator.op_to_shape_and_stride_idx, _adjust_shape_and_stride_args). AutoParallel runs ops on local tensors, both to estimate their cost and in ApplyShardingInterpreter. It applied that rewrite only for the shape factories and views, so a Shard(...) node of any other listed op ran at its global size:

  • F.interpolate(..., antialias=True) under data parallelism fails in cost estimation (Expected grad_output to have the same shape as output).
  • new_empty_strided is priced at its global size, then lowered to a global-size tensor on every rank. The next op fails with Attempting to broadcast a dimension of length N.

Change

  • shardings/dtensor_sharding_helpers.get_shape_and_stride_arg_indices(op) reads the argument positions from DTensor's op_to_shape_and_stride_idx, so AutoParallel picks up new entries in that table without a list of its own.
  • ApplyShardingInterpreter._redistribute_and_adjust_args: when the op's output has a Shard placement, it replaces the size argument with _localize_shape_arg (the helper the factory ops use; SymInt sizes in dynamic mode are kept) and the stride argument with compute_local_stride. Ops in _VIEW_OPS keep their existing handling.
  • cost_models.compute_estimation._shard_args_for_node: the same rewrite from _get_sharded_shape_stride(strategy.output_specs), so these ops are priced at their local size and their meta functions see consistent shapes.

Replicate and Partial outputs are not touched. DTensor's strategies for these ops never shard a dim the size argument can't simply be divided along. slice_backward, for example, isn't sharded along its slice dim, and the upsample ops shard only N or C.

Tests

  • tests/test_apply_sharding.py:
    • test_static_shape_arg_is_localized_for_sharded_output lowers slice_backward, select_backward and _upsample_bilinear2d_aa_backward with Shard(0) on a 2-rank CPU mesh and compares the result with the matching slice of the unsharded result.
    • test_static_new_empty_strided_is_localized_for_sharded_output checks the local size and stride.
  • tests/test_compute_estimation.py: test_sharded_output_size_arg_is_priced_at_local_size checks that a Shard(0) strategy for slice_backward and _upsample_bilinear2d_aa_backward costs the same as the op at its local size.

All six new cases fail on main and pass with the change. The repro in the issue lowers both models with the change. Tested on CPU only: torch 2.16.0.dev20261128 (CPU build), macOS arm64. The tests that need CUDA or Triton could not run here, and I compared every other test file against main: the same 255 failures and 9 errors on both trees, mostly Torch not compiled with CUDA enabled, and 6 more passes with the change (150 vs 144), which are the new tests. Black 22.3.0, flake8 6.1.0 and isort 5.12.0 (the pre-commit pins) pass on the changed files. mypy reports one error, which main reports too (propagation_rules.py:60, _op_rules needs an annotation).

This PR was authored with Claude (Claude Code).

🤖 Generated with Claude Code

https://claude.ai/code/session_01UwthrTQxLK9UDaYTG4tAie

Ops such as the upsample backward ops, new_empty_strided, slice_backward
and select_backward take their output size as an argument. DTensor
rewrites it to the local size when the output is sharded
(ShardingPropagator.op_to_shape_and_stride_idx), but AutoParallel runs
ops on local tensors and only localized the shape factories and views.
A Shard(...) node of any other listed op ran at its global size: the
cost model raised on F.interpolate(..., antialias=True) under data
parallelism, and new_empty_strided lowered to a global-size tensor on
each rank.

Read the argument positions from DTensor's table and localize the size
(and stride) in both ApplyShardingInterpreter and _shard_args_for_node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UwthrTQxLK9UDaYTG4tAie
@meta-cla

meta-cla Bot commented Nov 28, 2026

Copy link
Copy Markdown

Hi @schmidt-jake!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ops that take their output size as an argument run with the global size on sharded inputs (e.g. F.interpolate(..., antialias=True), new_empty_strided)

1 participant