Add a check to ensure shardings are aligned for ops where it matters - #1959
Add a check to ensure shardings are aligned for ops where it matters#1959coreyjadams wants to merge 2 commits into
Conversation
CODEOWNERS review mapCurrent for commit ⏳ @coreyjadams — 8 file(s)
⏳ @negin513 — 8 file(s)
Comment |
| """ | ||
| _validate_aligned_shard_boundaries(func, args, kwargs) |
There was a problem hiding this comment.
Since DTensor has assumptions about sharding shapes, we do an alignment check before passing to DTensor.
Greptile SummaryThis PR adds metadata-only validation that rejects mismatched uneven shard boundaries before operations pair local tensor elements.
Important Files Changed
Reviews (1): Last reviewed commit: "Add a check to ensure shardings are alig..." | Re-trigger Greptile |
| ) | ||
| if not sharded: | ||
| continue | ||
| cached = getattr(spec, "_sharding_shapes", None) or {} |
There was a problem hiding this comment.
Reading the local shapes along the sharded dimension directly like this prevents any implicit resolution via collectives, which would be performance hit.
| sizes = tuple(s[tensor_dim] for s in cached[mesh_dim]) | ||
| ref_sizes = known.setdefault(mesh_dim, sizes) | ||
| if ref_sizes != sizes: |
There was a problem hiding this comment.
These three lines are the core logic:
- get the current spec's shard shapes
- get the reference spec's shard shapes
- compare
All that's asked here is equality to assure two shard tensors can communicate; sanity checking an individual tensor spec is done at TesnorSpec creation, etc.
peterdsharpe
left a comment
There was a problem hiding this comment.
Looks good, thanks for adding the non-regression test!
PhysicsNeMo Pull Request
This PR targets issue #1943 - Shard tensor can, if users are not careful, accept and happily try to use, sharded tensors where the local sharding is different per tensor yet the global shape is the same. For elementwise operations this would have been sent to DTensor, for example, and silently been wrong in strange patterns, or raised errors in some cases, depending on the local shardings. Either way, not great.
DTensor never checks this, because they have baked in assumptions about how tensors get sharded.
This PR introduces a validation scheme. We keep flexibility, but on ops where the local shapes need to be the same, we can (and do) now apply a validation that checks the local shape is aligned across inputs. I'll annotate the relevant pieces for clarity in the PR.
Description
Checklist
Dependencies
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.