core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor#
Distributed-checkpoint chunk metadata for Megatron-FSDP’s packed parameter buffers.
An :class:~.parameter_group.FsdpParameterGroup packs several parameters into one flat
- class:
~.dbuffer.DBufferwith least-common-multiple row padding, so a parameter’s per-rank shard does not tile the way torch’s canonicalShard(0)does: a rank may own several rows of one parameter and none of the next. :meth:~.dbuffer.DBuffer.get_dtensorpresents each parameter as a plainShard(0)DTensor, which cannot express those offsets, so DCP’s default planner would mis-place every packed shard and silently corrupt the checkpoint.
DCP lets a local tensor override that placement through the __create_chunk_list__ and
__create_write_items__ hooks, which :class:~torch.distributed.tensor.DTensor forwards to its
local tensor. This module derives each shard’s true position from the
- class:
~.layout.GlobalLayoutthat already backs the buffer – it records every logical tensor’s global element offset, and the buffer records this rank’s owned element range – and attaches those hooks. The derivation is pure rank-local arithmetic, so it needs no collective.
Module Contents#
Functions#
Return the chunk of logical tensor |
|
Return this rank’s chunk of every Megatron-FSDP parameter in |
|
Attach Megatron-FSDP chunk metadata to the sharded DTensors DCP will save or load. |
|
Build chunk metadata for a dim-0 row range of a tensor of shape |
|
Attach chunk metadata to the DTensors of a parameter-FQN-keyed state dict. |
|
Override a DTensor’s DCP placement with |
Data#
API#
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor.__all__#
[‘attach_uneven_dtensor_metadata’, ‘chunk_metadata_by_fqn’]
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor._OPTIMIZER_STATE_KEY#
‘state’
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor._dbuffer_chunk_metadata(
- buffer: core.distributed.fsdp.src.megatron_fsdp.experimental.dbuffer.DBuffer,
- index: int,
Return the chunk of logical tensor
indexthis rank holds inbuffer.Flat placements shard the flat buffer, and every logical tensor is row-aligned within it, so a rank always owns whole dim-0 rows and the chunk is a dim-0 range of the global tensor.
- Parameters:
buffer – Buffer holding the logical tensor.
index – Index of the logical tensor within
buffer.
- Returns:
This rank’s chunk offset and size, in global tensor coordinates.
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor.chunk_metadata_by_fqn(
- model: torch.nn.Module,
Return this rank’s chunk of every Megatron-FSDP parameter in
model, keyed by FQN.The keys are the parameter FQNs that torch’s DCP state-dict helpers use for both the model state dict and the per-parameter entries of the optimizer state dict.
- Parameters:
model – A module tree that has been sharded with :func:
~.fully_shard.fully_shard.- Returns:
Chunk metadata for each sharded parameter, and nothing for parameters FSDP does not own. Tied parameters appear once per FQN, all sharing the one chunk of the buffer entry that backs them.
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor.attach_uneven_dtensor_metadata(
- model: torch.nn.Module,
- model_state_dict: dict[str, Any],
- optimizer_state_dict: dict[str, Any],
Attach Megatron-FSDP chunk metadata to the sharded DTensors DCP will save or load.
Optimizer state is allocated as
zeros_likeof a sharded parameter, so it shares that parameter’s chunk.- Parameters:
model – The sharded module tree the state dicts were taken from.
model_state_dict –
Model state dict from
- func:
~torch.distributed.checkpoint.state_dict.get_model_state_dict.
optimizer_state_dict –
Optimizer state dict from
- func:
~torch.distributed.checkpoint.state_dict.get_optimizer_state_dict.
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor._build_chunk_metadata(
- row_offset: int,
- rows: int,
- shape: torch.Size,
Build chunk metadata for a dim-0 row range of a tensor of shape
shape.
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor._attach_by_fqn(
- state_dict: dict[str, Any],
- metadata_by_fqn: dict[str, torch.distributed.checkpoint.metadata.ChunkStorageMetadata],
Attach chunk metadata to the DTensors of a parameter-FQN-keyed state dict.
Each value is either a parameter’s tensor, as in a model state dict, or a mapping of that parameter’s state tensors, as in the per-parameter entries of an optimizer state dict.
- core.distributed.fsdp.src.megatron_fsdp.experimental.uneven_dtensor._attach_dcp_hooks(
- dtensor: torch.distributed.tensor.DTensor,
- chunk: torch.distributed.checkpoint.metadata.ChunkStorageMetadata,
Override a DTensor’s DCP placement with
chunk.