Skip to navigation

nemo_automodel.components.models.hy_mt2.model

View as MarkdownOpen in Claude

HyMT2ForCausalLM — Tencent Hy-MT2-30B-A3B (translation MoE) SFT support.

Architecture (from tencent/Hy-MT2-30B-A3B config.json):

  • 48 transformer layers; layer 0 is dense, layers 1-47 are MoE
  • MoE: 128 routed experts + 1 shared expert, top-8 activated
  • Sigmoid routing with expert-bias correction, router_scaling_factor=2.826
  • route_norm = True (normalize top-k weights to sum to 1)
  • GQA: 32 Q heads, 4 KV heads, head_dim=128, hidden_size=2048
  • Per-head Q/K RMSNorm (qk_norm=True) before RoPE
  • 256K context, rope_theta=11158840
  • dense intermediate_size=6912, moe_intermediate_size=expert_hidden_dim=768
  • vocab_size=120832
  • enable_lm_head_fp32 = True (HF reference upcasts lm_head to fp32), so logits_dtype defaults to FP32

Notes vs. components/models/hy_v3 (Hy3-preview 295B):

  • Smaller everywhere (48L / 128 experts / 32+4 heads / hidden=2048).
  • score_func is driven by config.moe_router_use_sigmoid instead of being hard-coded.

Module Contents

Classes

NameDescription
BlockSingle Hy-MT2 transformer block: attention + (dense MLP | MoE) + residual norms.
HyMT2ForCausalLMHy-MT2-30B-A3B causal-LM wrapper.
HyMT2ModelHy-MT2 backbone: token embeddings + transformer blocks + final RMSNorm.

Functions

NameDescription
_resolve_score_funcMap config.moe_router_use_sigmoid to a gate score_func name.

Data

ModelClass

API

class nemo_automodel.components.models.hy_mt2.model.Block(
layer_idx: int,
config: typing.Any,
)

Bases: Module

Single Hy-MT2 transformer block: attention + (dense MLP | MoE) + residual norms.

input_layernorm
mlp
post_attention_layernorm
self_attn
= HyMT2Attention(config, backend)
nemo_automodel.components.models.hy_mt2.model.Block._mlp(
x: torch.Tensor,
padding_mask: torch.Tensor | None
) -> torch.Tensor
nemo_automodel.components.models.hy_mt2.model.Block.forward(
x: torch.Tensor,
freqs_cis: torch.Tensor,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor
nemo_automodel.components.models.hy_mt2.model.Block.init_weights(
buffer_device: torch.device
)
class nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM(
config: typing.Any,
logits_dtype: torch.dtype | None = None,
moe_overrides: dict | None = None
)

Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin

Hy-MT2-30B-A3B causal-LM wrapper.

Mixes in MoEFSDPSyncMixin so EP / FSDP2 expert-gradient sync works out of the box (set distributed.ep_size in the YAML; must divide num_experts=128). The HFCheckpointingMixin provides from_pretrained / save_pretrained over the HF safetensors layout.

_keep_in_fp32_modules_strict
= ['mlp.gate.e_score_correction_bias']
backend
= backend or BackendConfig()
lm_head
model
state_dict_adapter
tie_word_embeddings_support
TieSupport = TieSupport.UNTIED_ONLY
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.forward(
input_ids: torch.Tensor,
position_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
logits_to_keep: typing.Union[int, torch.Tensor] = 0,
output_hidden_states: bool | None = None,
attn_kwargs: typing.Any = {}
) -> transformers.modeling_outputs.CausalLMOutputWithPast

Run the decoder and project the kept positions to vocabulary logits.

Parameters:

input_ids
torch.Tensor

Token IDs of shape [batch, sequence], or packed (THD) IDs of shape [1, tokens], which the decoder squeezes to [tokens]. Later pipeline stages pass hidden states of shape [batch, sequence, hidden] or [1, tokens, hidden].

position_ids
torch.Tensor | NoneDefaults to None

Optional positions of shape [batch, sequence], or [1, tokens] for packed input, which requires them.

attention_mask
torch.Tensor | NoneDefaults to None

Optional Tensor of shape [batch, sequence], 1 at valid tokens and 0 at padding, or, with SDPA and an explicit padding_mask, a dense mask of shape [batch, 1, sequence, sequence]; packed input ignores it.

padding_mask
torch.Tensor | NoneDefaults to None

Optional bool Tensor of shape [batch, sequence], or [1, tokens] when packed, true at padding; when None, the MoE layers derive it from attention_mask.

logits_to_keep
Union[int, torch.Tensor]Defaults to 0

0 keeps every position, a positive int keeps the last N positions, and an integer Tensor of shape [kept] keeps those positions.

output_hidden_states
bool | NoneDefaults to None

Whether the output carries the final hidden states; None reads config.output_hidden_states.

**attn_kwargs
AnyDefaults to {}

Backend metadata for the decoder. Packed input sets qkv_format="thd" and passes int32 cu_seqlens and cu_seqlens_padded of shape [1, documents + 1], padded with -1000, and max_seqlen.

Returns: CausalLMOutputWithPast

CausalLMOutputWithPast whose logits have shape [batch, kept, vocab], or [1, kept, vocab] for packed

nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.from_config(
config: typing.Any,
kwargs = {}
)
classmethod
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.from_pretrained(
pretrained_model_name_or_path: str,
model_args = (),
kwargs = {}
)
classmethod
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.get_input_embeddings()
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.get_output_embeddings()
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.initialize_weights(
buffer_device: torch.device | None = None,
dtype: torch.dtype = torch.bfloat16
) -> None
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.set_input_embeddings(
value
)
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.set_output_embeddings(
new_embeddings
)
nemo_automodel.components.models.hy_mt2.model.HyMT2ForCausalLM.update_moe_gate_bias() -> None
class nemo_automodel.components.models.hy_mt2.model.HyMT2Model(
config: typing.Any,
moe_overrides: dict | None = None
)

Bases: Module

Hy-MT2 backbone: token embeddings + transformer blocks + final RMSNorm.

The MoE / dense split is governed by config.first_k_dense_replace (layer 0 dense, the rest MoE for the published Hy-MT2-30B-A3B). The MoE configuration is assembled from the HF config fields and forwarded to every MoE-bearing Block.

embed_tokens
head_dim
layers
= torch.nn.ModuleDict()
max_seq_len
= config.max_position_embeddings
moe_config
= moe_config or MoEConfig(**moe_defaults)
norm
rotary_emb
nemo_automodel.components.models.hy_mt2.model.HyMT2Model.forward(
input_ids: torch.Tensor,
position_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor

Run the decoder layers on padded or packed (THD) input.

Packed input arrives batched and is squeezed to [tokens, …] for the layers, which TE’s packed kernels take; the hidden states come back batched.

Parameters:

input_ids
torch.Tensor

Token IDs of shape [batch, sequence], or packed token IDs of shape [1, tokens]. Later pipeline stages pass hidden states of shape [batch, sequence, hidden] or [1, tokens, hidden].

position_ids
torch.Tensor | NoneDefaults to None

Optional positions of shape [batch, sequence]; packed input requires them, as [1, tokens].

attention_mask
torch.Tensor | NoneDefaults to None

Optional Tensor of shape [batch, sequence], 1 at valid tokens and 0 at padding, or, with SDPA and an explicit padding_mask, a dense mask of shape [batch, 1, sequence, sequence]; packed input ignores it.

padding_mask
torch.Tensor | NoneDefaults to None

Optional bool mask of shape [batch, sequence] or [1, tokens], true at padding.

**attn_kwargs
AnyDefaults to {}

Backend metadata. Packed input sets qkv_format="thd" and passes int32 cu_seqlens and cu_seqlens_padded of shape [1, documents + 1], padded with -1000, and max_seqlen.

Returns: torch.Tensor

Hidden states of shape [batch, sequence, hidden], or [1, tokens, hidden] for packed input.

nemo_automodel.components.models.hy_mt2.model.HyMT2Model.init_weights(
buffer_device: torch.device | None = None
) -> None
nemo_automodel.components.models.hy_mt2.model._resolve_score_func(
config: typing.Any
) -> str

Map config.moe_router_use_sigmoid to a gate score_func name.

Returns “sigmoid” when the flag is True (Hy-MT2 default) and “softmax” otherwise. The bias-aware variants (“sigmoid_with_bias” / “softmax_with_bias”) are selected at the gate level by the presence of e_score_correction_bias plus expert-group routing, which Hy-MT2 does not use (n_expert_groups=0).

nemo_automodel.components.models.hy_mt2.model.ModelClass = HyMT2ForCausalLM