> This page is for version 0.5.0 · 26.06.
> For other versions, use one of these documentation indexes:
> - Nightly (default): https://docs.nvidia.com/nemo/automodel/nightly/llms.txt
> - Latest: https://docs.nvidia.com/nemo/automodel/latest/llms.txt
> - 0.7.0 · 26.10: https://docs.nvidia.com/nemo/automodel/v0.7/llms.txt
> - 0.5.0 · 26.06: https://docs.nvidia.com/nemo/automodel/v0.5/llms.txt
> - 0.4.0 · 26.04: https://docs.nvidia.com/nemo/automodel/v0.4/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.

# nemo_automodel.components.models.ergo.state_dict_adapter

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

## Module Contents

### Classes

| Name                                                                                                       | Description                                                                         |
| ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| [`ErgoConversionOptions`](#nemo_automodel-components-models-ergo-state_dict_adapter-ErgoConversionOptions) | Options the checkpoint code passes to `to_hf`.                                      |
| [`ErgoStateDictAdapter`](#nemo_automodel-components-models-ergo-state_dict_adapter-ErgoStateDictAdapter)   | Rename between the native module names and the names of the published Ergo weights. |

### Functions

| Name                                                                                           | Description                                                                       |
| ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| [`_native_name`](#nemo_automodel-components-models-ergo-state_dict_adapter-_native_name)       | Map a published tensor name to its native name; the inverse of `_published_name`. |
| [`_published_name`](#nemo_automodel-components-models-ergo-state_dict_adapter-_published_name) | Map a native base-weight name to its published name.                              |

### Data

[`_LAYER_NAMES`](#nemo_automodel-components-models-ergo-state_dict_adapter-_LAYER_NAMES)

[`_NATIVE_LAYER`](#nemo_automodel-components-models-ergo-state_dict_adapter-_NATIVE_LAYER)

[`_NATIVE_LAYER_TENSOR`](#nemo_automodel-components-models-ergo-state_dict_adapter-_NATIVE_LAYER_TENSOR)

[`_NATIVE_TOP_LEVEL`](#nemo_automodel-components-models-ergo-state_dict_adapter-_NATIVE_TOP_LEVEL)

[`_PUBLISHED_LAYER`](#nemo_automodel-components-models-ergo-state_dict_adapter-_PUBLISHED_LAYER)

[`_PUBLISHED_LAYER_TENSOR`](#nemo_automodel-components-models-ergo-state_dict_adapter-_PUBLISHED_LAYER_TENSOR)

[`_PUBLISHED_TOP_LEVEL`](#nemo_automodel-components-models-ergo-state_dict_adapter-_PUBLISHED_TOP_LEVEL)

[`_TOP_LEVEL_NAMES`](#nemo_automodel-components-models-ergo-state_dict_adapter-_TOP_LEVEL_NAMES)

### API

```python
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`

---

```python
class nemo_automodel.components.models.ergo.state_dict_adapter.ErgoStateDictAdapter(
    config: nemo_automodel.components.models.ergo.config.ErgoConfig,
    moe_config: nemo_automodel.components.moe.config.MoEConfig,
    backend: nemo_automodel.components.models.common.BackendConfig,
    dtype: torch.dtype
)
```

**Bases:** [MoESplitExpertsStateDictMixin](/nemo/automodel/v0.5/nemo-automodel/nemo_automodel/components/moe/state_dict_mixin#nemo_automodel-components-moe-state_dict_mixin-MoESplitExpertsStateDictMixin), [StateDictAdapter](/nemo/automodel/v0.5/nemo-automodel/nemo_automodel/components/checkpoint/state_dict_adapter#nemo_automodel-components-checkpoint-state_dict_adapter-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`

---

```python
nemo_automodel.components.models.ergo.state_dict_adapter.ErgoStateDictAdapter.convert_single_tensor_to_hf(
    fqn: str,
    tensor: torch.Tensor,
    kwargs: typing.Unpack[nemo_automodel.components.models.ergo.state_dict_adapter.ErgoConversionOptions] = {}
) -> 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]` — default: \{}

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

```python
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 | None` — default: 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

```python
nemo_automodel.components.models.ergo.state_dict_adapter.ErgoStateDictAdapter.to_hf(
    state_dict: dict[str, torch.Tensor],
    kwargs: typing.Unpack[nemo_automodel.components.models.ergo.state_dict_adapter.ErgoConversionOptions] = {}
) -> 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]` — default: \{}

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

```python
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

```python
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

```python
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...
```

```python
nemo_automodel.components.models.ergo.state_dict_adapter._NATIVE_LAYER = {published: (native, transposed) for native, published, transposed in _LAYER_NAM...
```

```python
nemo_automodel.components.models.ergo.state_dict_adapter._NATIVE_LAYER_TENSOR = re.compile('model\\.(?P<layer>layers\\.\\d+\\.)(?P<leaf>.+)')
```

```python
nemo_automodel.components.models.ergo.state_dict_adapter._NATIVE_TOP_LEVEL = {published: native for native, published in _TOP_LEVEL_NAMES}
```

```python
nemo_automodel.components.models.ergo.state_dict_adapter._PUBLISHED_LAYER = {native: (published, transposed) for native, published, transposed in _LAYER_NAM...
```

```python
nemo_automodel.components.models.ergo.state_dict_adapter._PUBLISHED_LAYER_TENSOR = re.compile('(?P<layer>layers\\.\\d+\\.)(?P<leaf>.+)')
```

```python
nemo_automodel.components.models.ergo.state_dict_adapter._PUBLISHED_TOP_LEVEL = dict(_TOP_LEVEL_NAMES)
```

```python
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', '...
```