> This page is for version Nightly (default).
> 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.hy_v4_flash.state_dict_adapter

State-dict conversion between the released `tencent/Hy4-flash` checkpoint and AutoModel.

The released safetensors use the original module names (the transformers reference renames them on load). The
renamed suffixes are listed in `_SUFFIXES`; in addition the post-MLP norm is `mlp.combine_output_norm` (MoE
layers) or `mlp.post_fc2_norm` (dense layer) in the release, and the expert tensors are transposed
(`gate_up_proj` \[E, 2I, latent] -> `gate_and_up_projs` \[E, latent, 2I]; `down_proj` \[E, latent, I] ->
`down_projs` \[E, I, latent]). Every other language-model key is identical. `model.dfly_layers.*` (the
inference-only speculative drafter) is not loaded.

The Engram tables hold exactly `sum(head vocab sizes)` rows; the owner-sharded table pads them to a multiple of
the owner group size, so loading adds zero rows and saving exposes only the logical rows.

## Module Contents

### Classes

| Name                                                                                                                      | Description                                                                           |
| ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| [`HYV4FlashStateDictAdapter`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-HYV4FlashStateDictAdapter) | Bridge the released Hy4-flash language-model tensors and the AutoModel module layout. |

### Functions

| Name                                                                                            | Description                                                                                                |
| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| [`_swap_suffix`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_swap_suffix) | Rename `key` from the released to the native spelling (`released=True`) or back; other keys are unchanged. |

### Data

[`AdapterOption`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-AdapterOption)

[`_ENGRAM_TABLE`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_ENGRAM_TABLE)

[`_EXPERT`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_EXPERT)

[`_LAYER`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_LAYER)

[`_NATIVE_EXPERT`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_NATIVE_EXPERT)

[`_POST_MLP_NORM_NATIVE`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_POST_MLP_NORM_NATIVE)

[`_POST_MLP_NORM_RELEASED`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_POST_MLP_NORM_RELEASED)

[`_SKIPPED`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_SKIPPED)

[`_SUFFIXES`](#nemo_automodel-components-models-hy_v4_flash-state_dict_adapter-_SUFFIXES)

### API

```python
class nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter(
    config: nemo_automodel.components.models.hy_v4_flash.config.HYV4FlashConfig,
    moe_config: nemo_automodel.components.moe.config.MoEConfig,
    backend: nemo_automodel.components.models.common.BackendConfig,
    dtype: torch.dtype = torch.bfloat16
)
```

**Bases:** [StateDictAdapter](/nemo/automodel/nightly/nemo-automodel/nemo_automodel/components/checkpoint/state_dict_adapter#nemo_automodel-components-checkpoint-state_dict_adapter-StateDictAdapter)

Bridge the released Hy4-flash language-model tensors and the AutoModel module layout.

**`_engram_rows`**

---

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter._to_native_key(
    key: str
) -> str
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter._to_released_key(
    key: str
) -> str
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter.convert_single_tensor_to_hf(
    fqn: str,
    tensor: torch.Tensor,
    kwargs: nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.AdapterOption = {}
) -> list[tuple[str, torch.Tensor]]
```

Convert one native tensor (grouped experts \[experts, in, out]; Engram tables \[padded_rows, dim]).

Returns one `(released key, tensor)` pair, or none for keys the release has no counterpart for.

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter.forced_hf_dtype_mapping(
    state_dict: dict[str, torch.Tensor]
) -> dict[str, str]
```

Keep fp32 parameters (hyper-connections, sinks, router, indexer pooling, head) in fp32 on export.

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter.from_hf(
    hf_state_dict: dict[str, torch.Tensor],
    device_mesh: torch.distributed.device_mesh.DeviceMesh | None = None,
    kwargs: nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.AdapterOption = {}
) -> dict[str, torch.Tensor]
```

Convert released tensors to AutoModel names and layouts.

**Parameters:**

**`hf_state_dict`** `dict[str, torch.Tensor]`

Released-name tensors. Stacked experts have shape \[experts, out, in]; they are either
complete CPU tensors (sliced here to this rank's expert range) or already expert-sharded DTensors.

---

**`device_mesh`** `DeviceMesh | None` — default: None

Expert mesh selecting this rank's experts.

---

**Returns:** `dict[str, torch.Tensor]`

Native tensors; grouped experts have shape \[experts, in, out].

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter.to_hf(
    state_dict: dict[str, torch.Tensor],
    exclude_key_regex: str | None = None,
    kwargs: nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.AdapterOption = {}
) -> dict[str, torch.Tensor]
```

Export native tensors under the released names and layouts.

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._swap_suffix(
    key: str,
    released: bool
) -> str
```

Rename `key` from the released to the native spelling (`released=True`) or back; other keys are unchanged.

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.AdapterOption: TypeAlias = 'bool | str | None'
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._ENGRAM_TABLE = re.compile(_LAYER + 'engram_layer\\.multi_head_embedding\\.embedding\\.weight$')
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._EXPERT = re.compile(_LAYER + 'mlp\\.experts\\.(gate_up_proj|down_proj)$')
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._LAYER = 'model\\.layers\\.(\\d+)\\.'
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._NATIVE_EXPERT = re.compile(_LAYER + 'mlp\\.experts\\.(gate_and_up_projs|down_projs)$')
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._POST_MLP_NORM_NATIVE = re.compile('(model\\.layers\\.(\\d+))\\.post_mlp_layernorm\\.weight$')
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._POST_MLP_NORM_RELEASED = re.compile('(model\\.layers\\.\\d+)\\.mlp\\.(?:combine_output_norm|post_fc2_norm...
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._SKIPPED = re.compile('(model\\.dfly_layers\\..*|.*_extra_state)')
```

```python
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._SUFFIXES: tuple[tuple[str, str], ...] = (*((f'.hc_attn_layer.hc_pre.hc_{name}', f'.attn_hc.{name}') for name in ('fn', '...
```