Skip to navigation

nemo_automodel.components.models.hy_v4_flash.state_dict_adapter

View as MarkdownOpen in Claude

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

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

Functions

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

Data

AdapterOption

_ENGRAM_TABLE

_EXPERT

_LAYER

_NATIVE_EXPERT

_POST_MLP_NORM_NATIVE

_POST_MLP_NORM_RELEASED

_SKIPPED

_SUFFIXES

API

class nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter(
dtype: torch.dtype = torch.bfloat16
)

Bases: StateDictAdapter

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

_engram_rows
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter._to_native_key(
key: str
) -> str
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter._to_released_key(
key: str
) -> str
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.HYV4FlashStateDictAdapter.convert_single_tensor_to_hf(
fqn: str,
tensor: torch.Tensor,
) -> 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.

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.

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,
) -> 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 | NoneDefaults to None

Expert mesh selecting this rank’s experts.

Returns: dict[str, torch.Tensor]

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

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,
) -> dict[str, torch.Tensor]

Export native tensors under the released names and layouts.

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.

nemo_automodel.components.models.hy_v4_flash.state_dict_adapter.AdapterOption: TypeAlias = 'bool | str | None'
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._ENGRAM_TABLE = re.compile(_LAYER + 'engram_layer\\.multi_head_embedding\\.embedding\\.weight$')
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._EXPERT = re.compile(_LAYER + 'mlp\\.experts\\.(gate_up_proj|down_proj)$')
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._LAYER = 'model\\.layers\\.(\\d+)\\.'
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._NATIVE_EXPERT = re.compile(_LAYER + 'mlp\\.experts\\.(gate_and_up_projs|down_projs)$')
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._POST_MLP_NORM_NATIVE = re.compile('(model\\.layers\\.(\\d+))\\.post_mlp_layernorm\\.weight$')
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...
nemo_automodel.components.models.hy_v4_flash.state_dict_adapter._SKIPPED = re.compile('(model\\.dfly_layers\\..*|.*_extra_state)')
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', '...