Skip to navigation

nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter

View as MarkdownOpen in Claude

Model-owned extension of the existing whole-state MTP adapter.

Reuses the decoder’s public two-call load session, layouts and distributed export. Shared-body checkpoint copies are independent read destinations and must agree with the decoder destinations before its finishers run. A failed, abandoned or exchanged session makes this adapter unusable. Read copies are compared after conversion to the destination dtype, using value equality: signed zero agrees, NaN does not. This is not an audit of stored file bytes. Ordinary validation and decoder-finisher failures are agreed across rank groups. Control interrupts propagate immediately and require launcher-driven peer termination.

Module Contents

Classes

NameDescription
Ernie5_5MTPCheckpointAdapterLoad in place and save sharded MTP state without duplicate native owners.
_CheckpointDestinationsHF destinations and local runtime state owned by one checkpoint read.
_SessionIdentityUnforgeable identity used only to distinguish active local load sessions.

API

class nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter(
)

Bases: Ernie5_5MTPStateDictAdapter

Load in place and save sharded MTP state without duplicate native owners.

The admitted distributed topology covers all job ranks. Error agreement uses every axis group of the embedding’s mesh, which covers both flattened and two-dimensional diagnostic checkpoint layouts. The production ERNIE parallelizer admits a one-dimensional FSDP mesh. This requires symmetric calls on ranks; KeyboardInterrupt and SystemExit propagate immediately.

_active_session
ReferenceType[_CheckpointDestinations] | None = None
_ended_destinations
list[ReferenceType[Tensor]] = []
_identity
_SessionIdentity | None = None

Copy the adapter without sharing a live session with a copied model.

Parameters:

memo
dict[int, Any]

Python copy.deepcopy memo, whose graph nodes have arbitrary library-owned types. The opaque value type is confined to this standard-library protocol boundary, as in the decoder adapter.

Returns: Ernie5_5MTPCheckpointAdapter

An independent idle adapter, or an unusable adapter when the original

nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter._body_aliases(
decoder: dict[str, torch.Tensor]
) -> dict[str, torch.Tensor]

Return MTP body names pointing at the decoder’s released tensors.

Parameters:

decoder
dict[str, torch.Tensor]

Released decoder destinations/export views, in their released layouts.

Returns: dict[str, torch.Tensor]

Alias names and the same tensor objects, excluding the decoder’s router bias.

nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter._local(
value: torch.Tensor
) -> torch.Tensor
staticmethod

Return the local shard of a global DTensor, or the original plain tensor.

Parameters:

value
torch.Tensor

Tensor in any layout; a DTensor has its declared global shape and placements.

Returns: torch.Tensor

Plain local tensor, without gathering or copying its storage.

nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter._open(
) -> None

Hold the tensor-bearing destination mapping weakly until its paired close.

nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter._owned_hf(
owned: dict[str, torch.Tensor]
) -> dict[str, torch.Tensor]

Return released views of the nine owned tensors.

Parameters:

owned
dict[str, torch.Tensor]

Native owned tensors. The head matrix has shape [streams, streams*hidden].

Returns: dict[str, torch.Tensor]

Released names and views; head shape [streams*hidden, streams], others unchanged.

nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter._raise_together(
error: BaseException | None
) -> None
staticmethod

Agree on failure across all mesh axes before ranks return or raise.

Parameters:

session
_CheckpointDestinations

Released-layout destination mapping and explicit runtime groups/device.

error
BaseException | None

Local failure, or None. No tensor values are exchanged.

Raises:

  • RuntimeError: If another rank rejects the load; the message identifies detecting ranks.
  • BaseException: The original local validation or finisher error is re-raised on its detecting rank.
nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter._split_state(
state: dict[str, torch.Tensor]
) -> tuple[dict[str, torch.Tensor], dict[str, torch.Tensor]]

Split decoder tensors and the nine native MTP values without copying.

Parameters:

state
dict[str, torch.Tensor]

Native names mapped to registered tensors in native layouts; DTensor shapes are global. The owned head is [streams, streams*hidden].

Returns: tuple[dict[str, torch.Tensor], dict[str, torch.Tensor]]

Decoder mapping and owned mapping (without mtp.), both aliasing inputs.

Raises:

  • ValueError: If the native state does not contain exactly eight owned parameters and one router bias.
nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter.from_hf(
hf_state_dict: dict[str, torch.Tensor],
device_mesh: torch.distributed.device_mesh.DeviceMesh | None = None,
kwargs: str | bool | None = {}
) -> dict[str, torch.Tensor]

Verify shared copies, finish the decoder session and return native owners.

Parameters:

hf_state_dict
dict[str, torch.Tensor]

The exact live mapping returned by the load-opening to_hf, with unchanged keys and tensor objects in released layouts. Without a session, a complete unsharded mapping follows the parent contract.

device_mesh
DeviceMesh | NoneDefaults to None

Decoder load compatibility mesh. A plain-state load requires None.

**kwargs
str | bool | NoneDefaults to {}

Decoder compatibility options.

Returns: dict[str, torch.Tensor]

Native tensors with unique decoder and MTP owners; session-owned head matrix

Raises:

  • RuntimeError: If session identity or lifecycle is invalid, or another rank rejects the load.
  • ValueError: If keys, destination identities or shared values violate the load contract.
  • BaseException: Unexpected validation and decoder-finisher errors propagate after rank agreement; process-control interrupts propagate immediately.
nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter.get_hf_state_dict_keys(
state_dict: dict[str, torch.Tensor]
) -> list[str]

Plan released names before sharding, without starting a load session.

Raises:

  • RuntimeError: If a load is active, failed or abandoned.
  • ValueError: If key planning is requested after tensors have been sharded.
nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter.Ernie5_5MTPCheckpointAdapter.to_hf(
state_dict: dict[str, torch.Tensor],
quantization: bool = False,
for_checkpoint_load: bool = False,
v4_compatible: bool = False,
kwargs: str | bool | torch.distributed.device_mesh.DeviceMesh | None = {}
) -> dict[str, torch.Tensor]

Return release-layout export views or one opaque in-place load session.

Parameters:

state_dict
dict[str, torch.Tensor]

Native tensor mapping with decoder model.* and nine mtp.* entries. DTensor global shapes/placements follow the decoder adapter; owned head [streams, streams*hidden], bias [experts]. Bias may be a replicated plain FP32 tensor while the parameters are DTensors.

quantization
boolDefaults to False

Must be False.

for_checkpoint_load
boolDefaults to False

Open a load session requiring an unchanged paired mapping.

v4_compatible
boolDefaults to False

Must be False.

**kwargs
str | bool | DeviceMesh | NoneDefaults to {}

Public decoder-checkpointer compatibility options.

Returns: dict[str, torch.Tensor]

Released tensors, head [streams*hidden, streams]. Export aliases share decoder

Raises:

  • NotImplementedError: If quantization or v4 metadata export is requested.
  • RuntimeError: If a load is active, failed or abandoned.
  • ValueError: If the native ownership or full-job embedding mesh is invalid.
class nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter._CheckpointDestinations()

Bases: dict[str, Tensor]

HF destinations and local runtime state owned by one checkpoint read.

Tensor values use released global layouts (DTensors hold only local shards). The decoder mapping stays strongly referenced and unchanged. Owned values retain their native layouts; the MTP head matrix is [streams, streams*hidden]. Alias pairs hold (independent scratch, decoder destination), both in the same released layout. Their plain local shards are compared after storage casts.

alias_pairs
dict[str, tuple[Tensor, Tensor]]
created
tuple[ReferenceType[Tensor], ...]
decoder_destinations
dict[str, Tensor]
flag_device
device
groups
tuple[ProcessGroup, ...]
original
dict[str, Tensor]
owned_native
dict[str, Tensor]
class nemo_automodel.components.models.ernie5_5.mtp_checkpoint_adapter._SessionIdentity()

Unforgeable identity used only to distinguish active local load sessions.