Skip to navigation
ReferenceFull Library ReferenceNemo AutomodelNemo AutomodelComponentsModelsErgonemo_automodel.components.models.ergo.state_dict_adapter

nemo_automodel.components.models.ergo.state_dict_adapter

View as MarkdownOpen in Claude

Read and write Ergo checkpoints in the tensor names and layouts of the published weights.

Module Contents

Classes

NameDescription
ErgoConversionOptionsOptions the checkpoint code passes to to_hf.
ErgoStateDictAdapterRename between the native module names and the names of the published Ergo weights.

Functions

NameDescription
_native_nameMap a published tensor name to its native name; the inverse of _published_name.
_published_nameMap a native base-weight name to its published name.

Data

_LAYER_NAMES

_NATIVE_LAYER

_NATIVE_LAYER_TENSOR

_NATIVE_TOP_LEVEL

_PUBLISHED_LAYER

_PUBLISHED_LAYER_TENSOR

_PUBLISHED_TOP_LEVEL

_TOP_LEVEL_NAMES

API

class nemo_automodel.components.models.ergo.state_dict_adapter.ErgoConversionOptions

Bases: typing.TypedDict

Options the checkpoint code passes to to_hf.

device_mesh
DeviceMesh | None
exclude_key_regex
str
for_checkpoint_load
bool
legacy_paramwrapper_layout
bool
quantization
bool
v4_compatible
bool
class nemo_automodel.components.models.ergo.state_dict_adapter.ErgoStateDictAdapter(
dtype: torch.dtype
)

Bases: MoESplitExpertsStateDictMixin, StateDictAdapter

Rename between the native module names and the names of the published Ergo weights.

The published weights load as downloaded and checkpoints are written under the same names. No tensor is copied or cast. Three matrices are published as [input, output] and are exchanged as transposed views of the module weights: the router gate_kernel and the shared expert’s shared_up_proj and shared_down_proj. The expert projections up_proj [experts, hidden, expert_hidden] and down_proj [experts, expert_hidden, hidden] have the layout of the grouped expert parameters and are exchanged whole, so a rank that holds a shard of the experts reads and writes only its own rows. Expert LoRA weights keep the shared expert LoRA layouts, and other names pass through unchanged.

_expert_path_segment
str
nemo_automodel.components.models.ergo.state_dict_adapter.ErgoStateDictAdapter.convert_single_tensor_to_hf(
fqn: str,
tensor: torch.Tensor,
) -> list[tuple[str, torch.Tensor]]

Rename one native tensor to its published name and layout.

Parameters:

fqn
str

Native state-dict name.

tensor
torch.Tensor

Native tensor of arbitrary shape, optionally a DTensor.

**kwargs
Unpack[ErgoConversionOptions]Defaults to {}

Conversion options; see ErgoConversionOptions.

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

Published name and tensor pairs. A base weight gives one pair whose tensor is the input or its

nemo_automodel.components.models.ergo.state_dict_adapter.ErgoStateDictAdapter.from_hf(
hf_state_dict: dict[str, torch.Tensor],
device_mesh: torch.distributed.device_mesh.DeviceMesh | None = None
) -> dict[str, torch.Tensor]

Rename a state dict with published names to native names and layouts.

Parameters:

hf_state_dict
dict[str, torch.Tensor]

Published names mapped to tensors of arbitrary shape, optionally DTensors: the destinations to_hf handed to a checkpoint read, or tensors read from the files.

device_mesh
DeviceMesh | NoneDefaults to None

Expert-parallel mesh, unused because tensors keep their own placements.

Returns: dict[str, torch.Tensor]

Native names mapped to the input tensors or transposed views of them. Expert LoRA weights are

nemo_automodel.components.models.ergo.state_dict_adapter.ErgoStateDictAdapter.to_hf(
state_dict: dict[str, torch.Tensor],
) -> dict[str, torch.Tensor]

Rename a native state dict to the published names and layouts.

Parameters:

state_dict
dict[str, torch.Tensor]

Native names mapped to tensors of arbitrary shape, optionally DTensors.

**kwargs
Unpack[ErgoConversionOptions]Defaults to {}

Conversion options; see ErgoConversionOptions.

Returns: dict[str, torch.Tensor]

Published names mapped to the input tensors or transposed views of them, so a checkpoint read that

nemo_automodel.components.models.ergo.state_dict_adapter._native_name(
key: str
) -> tuple[str, bool] | None

Map a published tensor name to its native name; the inverse of _published_name.

Parameters:

key
str

Checkpoint tensor name.

Returns: tuple[str, bool] | None

The native state-dict name and whether the published matrix is the transpose of the native one, or

nemo_automodel.components.models.ergo.state_dict_adapter._published_name(
fqn: str
) -> tuple[str, bool] | None

Map a native base-weight name to its published name.

Parameters:

fqn
str

Native state-dict name.

Returns: tuple[str, bool] | None

The published name and whether the published matrix is the transpose of the native one, or None when

nemo_automodel.components.models.ergo.state_dict_adapter._LAYER_NAMES: tuple[tuple[str, str, bool], ...] = (('attention_norm.weight', 'attention_norm.weight', False), ('attention.wqkv.wei...
nemo_automodel.components.models.ergo.state_dict_adapter._NATIVE_LAYER = {published: (native, transposed) for native, published, transposed in _LAYER_NAM...
nemo_automodel.components.models.ergo.state_dict_adapter._NATIVE_LAYER_TENSOR = re.compile('model\\.(?P<layer>layers\\.\\d+\\.)(?P<leaf>.+)')
nemo_automodel.components.models.ergo.state_dict_adapter._NATIVE_TOP_LEVEL = {published: native for native, published in _TOP_LEVEL_NAMES}
nemo_automodel.components.models.ergo.state_dict_adapter._PUBLISHED_LAYER = {native: (published, transposed) for native, published, transposed in _LAYER_NAM...
nemo_automodel.components.models.ergo.state_dict_adapter._PUBLISHED_LAYER_TENSOR = re.compile('(?P<layer>layers\\.\\d+\\.)(?P<leaf>.+)')
nemo_automodel.components.models.ergo.state_dict_adapter._PUBLISHED_TOP_LEVEL = dict(_TOP_LEVEL_NAMES)
nemo_automodel.components.models.ergo.state_dict_adapter._TOP_LEVEL_NAMES: tuple[tuple[str, str], ...] = (('model.embed_tokens.weight', 'tok_embeddings.weight'), ('model.norm.weight', '...