Skip to navigation

nemo_automodel.components.models.deepseek_v3.model

View as MarkdownOpen in Claude

Module Contents

Classes

Data

ModelClass

API

class nemo_automodel.components.models.deepseek_v3.model.Block(
layer_idx: int,
config: transformers.models.deepseek_v3.configuration_deepseek_v3.DeepseekV3Config,
)

Bases: Module

input_layernorm
is_moe_layer
= layer_idx >= config.first_k_dense_replace
mlp
post_attention_layernorm
self_attn
= MLA(config, backend, latent_norm_eps=1e-06)
nemo_automodel.components.models.deepseek_v3.model.Block._mlp(
x: torch.Tensor,
padding_mask: torch.Tensor
) -> torch.Tensor
nemo_automodel.components.models.deepseek_v3.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 = {}
) -> tuple[torch.Tensor, torch.Tensor | None]

Forward pass for the Transformer block.

Parameters:

x
torch.Tensor

Input tensor.

freqs_cis
torch.Tensor

Precomputed complex exponential values for rotary embeddings.

padding_mask
torch.TensorDefaults to None

Boolean tensor indicating padding positions.

Returns: torch.Tensor

torch.Tensor: Output tensor after block computation.

nemo_automodel.components.models.deepseek_v3.model.Block.init_weights(
buffer_device: torch.device
)
class nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM(
config: transformers.models.deepseek_v3.configuration_deepseek_v3.DeepseekV3Config,
logits_dtype: torch.dtype | None = None,
moe_overrides: dict | None = None
)

Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin

_keep_in_fp32_modules_strict
= ['e_score_correction_bias']
lm_head
model
state_dict_adapter
tie_word_embeddings_support
TieSupport = TieSupport.UNTIED_ONLY
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.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; context parallelism adds cp_size and cp_rank.

Returns: CausalLMOutputWithPast

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

nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.from_config(
config: transformers.models.deepseek_v3.configuration_deepseek_v3.DeepseekV3Config,
kwargs = {}
)
classmethod
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.from_pretrained(
pretrained_model_name_or_path: str,
model_args = (),
kwargs = {}
)
classmethod
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.get_input_embeddings()
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.get_output_embeddings()
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.initialize_weights(
buffer_device: torch.device | None = None,
dtype: torch.dtype = torch.bfloat16
) -> None
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.set_input_embeddings(
value
)
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.set_output_embeddings(
new_embeddings
)
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3ForCausalLM.update_moe_gate_bias() -> None

Update correction biases only for gates with adaptation enabled.

class nemo_automodel.components.models.deepseek_v3.model.DeepseekV3Model(
config: transformers.models.deepseek_v3.configuration_deepseek_v3.DeepseekV3Config,
moe_overrides: dict | None = None
)

Bases: Module

embed_tokens
layers
= torch.nn.ModuleDict()
max_seq_len
= config.max_position_embeddings
moe_config
= moe_config or MoEConfig(**moe_defaults)
norm
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3Model.forward(
input_ids: torch.Tensor | None = None,
inputs_embeds: torch.Tensor | None = None,
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. Callers that already squeezed packed input for their own preprocessing pass [tokens] layouts, which the squeeze leaves unchanged.

Parameters:

input_ids
torch.Tensor | NoneDefaults to None

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].

inputs_embeds
torch.Tensor | NoneDefaults to None

Optional embeddings of shape [batch, sequence, hidden] used instead of input_ids, or packed [tokens, hidden] from a caller that already squeezed packed input.

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.deepseek_v3.model.DeepseekV3Model.init_weights(
buffer_device: torch.device | None = None
) -> None
nemo_automodel.components.models.deepseek_v3.model.DeepseekV3Model.update_moe_gate_bias() -> None

Update correction biases only for gates with adaptation enabled.

nemo_automodel.components.models.deepseek_v3.model.ModelClass = DeepseekV3ForCausalLM