Repository navigation
Localize output-size arguments of sharded ops as DTensor does - #601
schmidt-jake wants to merge 1 commit into
Conversation
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
|
Hi @schmidt-jake! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
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'ssizeandstride, andslice_backward'sinput_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 inApplyShardingInterpreter. It applied that rewrite only for the shape factories and views, so aShard(...)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_stridedis priced at its global size, then lowered to a global-size tensor on every rank. The next op fails withAttempting 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'sop_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 aShardplacement, 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 withcompute_local_stride. Ops in_VIEW_OPSkeep 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_outputlowersslice_backward,select_backwardand_upsample_bilinear2d_aa_backwardwithShard(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_outputchecks the local size and stride.tests/test_compute_estimation.py:test_sharded_output_size_arg_is_priced_at_local_sizechecks that aShard(0)strategy forslice_backwardand_upsample_bilinear2d_aa_backwardcosts the same as the op at its local size.All six new cases fail on
mainand 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 againstmain: the same 255 failures and 9 errors on both trees, mostlyTorch 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, whichmainreports too (propagation_rules.py:60,_op_rulesneeds an annotation).This PR was authored with Claude (Claude Code).
🤖 Generated with Claude Code
https://claude.ai/code/session_01UwthrTQxLK9UDaYTG4tAie