Model State Mapper
About
The d9d.model_state.mapper package is a declarative, graph-based framework for transforming model states. You describe which checkpoint keys map to which model keys, and d9d runs the transformation while it streams the checkpoint.
Core Concept
Loading a large model is rarely a 1-to-1 key match. Common problems are:
- Naming mismatches: Hugging Face uses
model.layers.0, your model usestransformer.h.0. - Shape mismatches: The checkpoint stores
q,kandvseparately, but your model expects one stackedqkvtensor. - Scale: The checkpoint takes hundreds of GiB. You cannot load the whole dictionary on every GPU to process it.
A mapper does not loop over tensors and modify them. It treats the transformation as a directed acyclic graph (DAG). Each mapper declares its dependency groups: which input keys produce which output keys. d9d uses these groups to load, transform and save a checkpoint in a stream, without holding the whole checkpoint in memory.
Mappers come in three kinds:
- Leaf mappers (
d9d.model_state.mapper.leaf) transform individual tensors: rename, stack, chunk, transpose, distribute. - Composite mappers (
d9d.model_state.mapper.compose) combine other mappers: in parallel, in sequence, under a key prefix or restricted to one shard. - Adapters (
d9d.model_state.mapper.adapters) build identity mappers from a module or from another mapper.
Usage
Pass-Through Mapping for a PyTorch Module
Use identity_mapper_from_module when checkpoint keys match the model's state dict keys, as in standard load_state_dict. You still get d9d's streaming and sharding.
Leaf Mappers
This example merges separate query, key and value tensors into a single tensor.
Composing Pipelines
A full model conversion processes many keys in parallel and can chain operations, e.g. rename, then stack.
API Reference
d9d.model_state.mapper
Core components of the model state mapping system.
ModelStateMapper
Bases: ABC
Abstract base class for all model state transformations.
A mapper separates the declaration of a transformation from its execution:
- Declarative (topology):
state_dependency_groups()announces what the mapper does without touching data. This lets the system build execution graphs, validate chains, detect collisions and shard work before it allocates memory. - Imperative (execution):
apply()runs the PyTorch operations on model states.
apply(group)
abstractmethod
Executes the transformation on the tensors of one dependency group.
The caller guarantees that group contains all keys listed in inputs of the active StateGroup.
Implementations must return all keys listed in its outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
dict[str, Tensor]
|
The source tensors. Keys match |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
The transformed tensors. Keys must exactly match |
state_dependency_groups()
abstractmethod
Returns the set of independent dependency groups this mapper handles.
Returns:
| Type | Description |
|---|---|
frozenset[StateGroup]
|
A frozenset of |
frozenset[StateGroup]
|
renames ten independent tensors returns ten distinct groups, so they can be sharded or processed |
frozenset[StateGroup]
|
individually. |
StateGroup
dataclass
Atomic unit of dependency in the model state transformation graph.
A StateGroup binds a set of input keys (source) to a set of output keys (destination).
Attributes:
| Name | Type | Description |
|---|---|---|
inputs |
frozenset[str]
|
The complete set of keys required from the source state dictionary to satisfy this dependency. |
outputs |
frozenset[str]
|
The complete set of keys that will be produced as a result of this transformation. |
d9d.model_state.mapper.adapters
Adapters that build simple ModelStateMapper instances from PyTorch modules or other mappers.
identity_mapper_from_mapper_outputs(mapper)
Creates an identity mapper for every output key of the given mapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
ModelStateMapper
|
The mapper whose |
required |
Returns:
| Type | Description |
|---|---|
ModelStateMapper
|
A composite mapper that passes through every key produced by |
identity_mapper_from_module(module)
Creates an identity mapper for every key in the state dict of a PyTorch module.
Use it when the checkpoint keys exactly match the module's state dict keys, as in standard
load_state_dict behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The instantiated PyTorch module to inspect. |
required |
Returns:
| Type | Description |
|---|---|
ModelStateMapper
|
A composite identity mapper for the module's state. |
d9d.model_state.mapper.compose
ModelStateMapper implementations that compose other mappers.
ModelStateMapperParallel
Bases: ModelStateMapper
Mapper that executes a list of state mappers independently of each other.
No two mappers can consume the same input key or produce the same output key. apply() routes each
group to the mapper that declared it.
__init__(mappers)
Constructs the ModelStateMapperParallel object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mappers
|
Sequence[ModelStateMapper]
|
The mappers to run side by side. Mappers without inputs and outputs are dropped. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If two mappers share an input key or an output key. |
ModelStateMapperPrefixScope
Bases: ModelStateMapper
Mapper that runs a child mapper under key prefixes.
Use it to apply a mapper written for a submodule (e.g. one operating on "in_proj") to the state dict of
a parent module. Input (source) and output (target) prefixes are independent.
__init__(mapper, source_prefix='', target_prefix='')
Constructs the ModelStateMapperPrefixScope object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapper
|
ModelStateMapper
|
The child mapper to run within the scope. |
required |
source_prefix
|
str
|
The prefix added to the input keys of |
''
|
target_prefix
|
str
|
The prefix added to the output keys of |
''
|
ModelStateMapperSequential
Bases: ModelStateMapper
Mapper that executes a list of mappers one after another.
Intermediate states stay hidden: the mapper exposes only the inputs of the first stage and the outputs of the last stage.
- Gap filling: If a tensor must pass a stage unchanged to reach a later stage or the final output, an identity mapper is added for it.
- Group merging: The mapper reports the net dependency graph. If stage A maps
xtoyand stage B mapsytoz, the mapper reports a single group{x} -> {z}.
__init__(mappers)
Constructs the ModelStateMapperSequential object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mappers
|
list[ModelStateMapper]
|
The stages in execution order. Mappers without inputs and outputs are dropped. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no mapper with inputs or outputs is given. |
ModelStateMapperShard
Bases: ModelStateMapper
Mapper that restricts another mapper to one shard of its dependency groups.
Use it to split model loading across processes. Give each process a different current_shard so that
each one loads only its part of the checkpoint.
__init__(sub_mapper, total_shards, current_shard)
Constructs the ModelStateMapperShard object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub_mapper
|
ModelStateMapper
|
The mapper whose dependency groups are split. |
required |
total_shards
|
int
|
The total number of shards. |
required |
current_shard
|
int
|
The index of the shard this mapper handles, in |
required |
filter_empty_mappers(mappers)
Filters out mappers that have no effect (no inputs and no outputs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mappers
|
Sequence[ModelStateMapper]
|
The list of mappers to filter. |
required |
Returns:
| Type | Description |
|---|---|
list[ModelStateMapper]
|
A new list containing only active mappers. |
d9d.model_state.mapper.leaf
Leaf mappers that transform individual tensors.
ModelStateMapperChunkTensors
Bases: ModelStateMapper
Mapper that chunks a single input tensor into multiple output tensors along a specified dimension.
__init__(source_name, target_names, dim)
ModelStateMapperConcatenateTensors
Bases: ModelStateMapper
Mapper that concatenates multiple input tensors into a single output tensor along a specified dimension.
It is the inverse of ModelStateMapperChunkTensors.
__init__(source_names, target_name, dim)
Constructs the ModelStateMapperConcatenateTensors object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_names
|
list[str]
|
Names of the input tensors to read. |
required |
target_name
|
str
|
Name of the resulting concatenated output tensor. |
required |
dim
|
int
|
Dimension along which to concatenate the tensors. |
required |
ModelStateMapperDistribute
Bases: ModelStateMapper
Mapper that converts a single full local tensor into a DTensor.
Every rank must hold the same full tensor. No communication happens.
__init__(name, device_mesh, placements)
Constructs the ModelStateMapperDistribute object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the tensor to distribute. |
required |
device_mesh
|
DeviceMesh | None
|
The device mesh of the resulting |
required |
placements
|
Sequence[Placement] | None
|
The placements of the resulting |
required |
ModelStateMapperGatherFullTensor
Bases: ModelStateMapper
Mapper that gathers a single DTensor into a full local tensor.
__init__(name)
Constructs the ModelStateMapperGatherFullTensor object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the tensor to gather. |
required |
ModelStateMapperIdentity
Bases: ModelStateMapper
Mapper that passes a single state tensor through unchanged.
__init__(name)
Constructs the ModelStateMapperIdentity object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the tensor to pass through. |
required |
ModelStateMapperRename
Bases: ModelStateMapper
Mapper that renames a single state tensor.
ModelStateMapperSelectChildModules
Bases: ModelStateMapper
Mapper that selects the keys of a child module and strips the module prefix.
It is a batch rename that moves parameters from a submodule scope to the current scope.
__init__(base_names, parent_name)
ModelStateMapperSqueeze
Bases: ModelStateMapper
Mapper that squeezes an input tensor along a specified dimension or all dimensions of size 1.
ModelStateMapperStackTensors
Bases: ModelStateMapper
Mapper that stacks multiple input tensors into a single output tensor along a new dimension.
__init__(source_names, target_name, dim)
ModelStateMapperTranspose
Bases: ModelStateMapper
Mapper that transposes an input tensor along two specified dimensions.
ModelStateMapperUnsqueeze
Bases: ModelStateMapper
Mapper that unsqueezes an input tensor along a specified dimension.
ModelStateMapperUnstackTensors
Bases: ModelStateMapper
Mapper that unstacks a single input tensor into multiple output tensors along a specified dimension.