Skip to content

[DCP] Checkpoint BlockShard DTensors as multiple chunks per rank - #199235

Draft
weifengpy wants to merge 5 commits into
gh/weifengpy/206/basefrom
gh/weifengpy/206/head
Draft

weifengpy wants to merge 5 commits into
gh/weifengpy/206/basefrom
gh/weifengpy/206/head

Conversation

@weifengpy

@weifengpy weifengpy commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

This PR was authored with an AI assistant (Claude Code). The description below is AI-generated and is pending author review.

A BlockShard rank owns a contiguous row range of the merged view
(prod(shape[:k]), *shape[k:]) (of its expert-parallel Shard(0) slice,
if any). When that range crosses a boundary inside shape[:k], it is up to
2k - 1 boxes of the global tensor, not one, so the single-chunk DTensor
checkpoint path can't describe it. Checkpoints keep using the global
(unmerged) coordinates, so the serialized format doesn't depend on the
placement.

The DCP core already handles several chunks per tensor per rank
(ShardedTensor and CheckpointableTensor use it): write items carry
MetadataIndex(fqn, offset, index), and find_tensor_shard resolves each
chunk on load. Only the DTensor hooks assume one chunk. For BlockShard,
__create_chunk_list__ and __create_write_items__ now emit one chunk per
box (BlockShard._local_boxes, offset by the EP slice), and
__get_tensor_shard__ returns local[row_start:row_stop].view(box_size).
The rows are contiguous in the local tensor, so the view is writable and
loads land in place. Other placements keep the single-chunk path. Replicated
mesh dims (HSDP) produce identical MetadataIndex entries, which the global
save plan already dedups.

Two helpers bypassed the hooks and are fixed:
_create_default_metadata_only_plan now goes through
DTensor.__create_write_items__, and _distribute_tensors (used by
set_model_state_dict(..., broadcast_from_rank0=True)) takes the
merged-row chunk (of the EP slice) for BlockShard instead of slicing the
full tensor by one offset.

CheckpointableTensor can't be used for this directly: its validation
requires the local tensor to have as many dims as the global tensor, while a
BlockShard local tensor has the merged-view shape.

Test Plan

python test/distributed/tensor/test_block_shard.py
python test/distributed/checkpoint/test_dtensor_checkpoint.py
python test/distributed/checkpoint/test_state_dict_utils.py
python test/distributed/checkpoint/test_planner.py

The new tests check box decomposition, chunk lists and write items (1D and
expert-parallel layouts), saving BlockShard tensors with rows crossing
expert boundaries and loading them as BlockShard (1D, HSDP, EP),
Replicate, and Shard(1), loading a Shard(1) checkpoint into
BlockShard, and _distribute_tensors.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit 1b8f705 with merge base 381cc3e (image):

NEW FAILURE - The following job has failed:

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

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant