Expose global_shape on ShardTensor.from_local to unlock the no-comm chunk path [ShardTensor Optimization 2]#1791
Conversation
…hunk path from_local defaults to sharding_shapes="infer", which pays a real all_gather on every call to learn each rank's shard shape -- even when the shard is evenly divided and the shape is derivable locally. The internal spec builder already supports a communication-free "chunk" mode, but it requires a global_shape argument that from_local never exposed, making that mode unreachable from the public API (passing sharding_shapes="chunk" always raised ValueError). Thread an optional global_shape parameter through from_local -> _FromTorchTensor.forward -> _infer_shard_tensor_spec_from_local_chunks, and grow _FromTorchTensor.backward's return tuple by one None for the new non-differentiable input. Fully additive: global_shape defaults to None everywhere, so existing "infer" and dict callers are unaffected. Closes #1780
Greptile SummaryThis PR threads an optional
Important Files Changed
|
coreyjadams
left a comment
There was a problem hiding this comment.
Thanks, looks reasonable!
|
Perf numbers for the new path (4xGB200,
~10x faster wrapper construction once the collective is gone. In the HealDA reshard pattern (#1758) |
|
/ok to test 8d74adc |
PhysicsNeMo Pull Request
Description
from_localdefaults tosharding_shapes="infer", which pays a realall_gatheron every call to learn each rank's shard shape -- even when the shard is evenly divided and the shape is derivable locally. The internal spec builder (_infer_shard_tensor_spec_from_local_chunks) already supports a communication-free"chunk"mode, but it requires aglobal_shapeargument thatfrom_localnever exposed -- so that mode was unreachable from the public API (passingsharding_shapes="chunk"always raisedValueError: If sharding_shapes is 'chunk', global_shape must be provided, with no way to satisfy it).This PR threads an optional
global_shapeparameter throughfrom_local->_FromTorchTensor.forward-> the existing internal helper, and grows_FromTorchTensor.backward's return tuple by oneNonefor the new non-differentiable input. The codebase already uses this exact pattern internally (_resolve_spec_for_dtensorcalls the helper withsharding_shapes="chunk", global_shape=dtensor.shape) -- this just makes it available tofrom_localcallers.Fully additive:
global_shapedefaults toNoneeverywhere, so existing"infer"and dict callers are unaffected._FromTorchTensor.applyhas exactly one caller (from_localitself), so nothing else needed updating.Closes #1780
Resolves NVIDIA/physicsnemo-roadmap#2719
Testing
torchrun --nproc_per_node=4):"chunk"+global_shapeproduces a spec identical to"infer"'s (same global shape, same per-rank sharding shapes), round-trips throughredistribute, flows gradients through the 5-arg autograd function, still raisesValueErrorfor"chunk"withoutglobal_shape, and the default no-kwarg path is unchanged.torchrun --nproc_per_node=4 -m pytest test/domain_parallel/ --multigpu-static -q-- 394 passed, 0 failed on top of the merged ShardTensor Refactor #1556 refactor.pre-commit(ruff check/format, interrogate, license) clean on the changed file.Related
all_to_allinredistribute'sShard->Shardtranspose; this one removes theall_gatherinfrom_local. Both are independent redundant collectives found while profiling the context-parallel reshard benchmark in HealDA v2 Architecture #1758, and they stack.Checklist
Dependencies
None.
Review Process
All PRs are reviewed by the PhysicsNeMo team before merging.
Depending on which files are changed, GitHub may automatically assign a maintainer for review.
We are also testing AI-based code review tools (e.g., Greptile), which may add automated comments with a confidence score.
This score reflects the AI's assessment of merge readiness and is not a qualitative judgment of your work, nor is
it an indication that the PR will be accepted / rejected.
AI-generated feedback should be reviewed critically for usefulness.
You are not required to respond to every AI comment, but they are intended to help both authors and reviewers.
Please react to Greptile comments with 👍 or 👎 to provide feedback on their accuracy.