Skip to navigation

nemo_automodel.components.models.qwen3_8_flash_next.model

View as MarkdownOpen in Claude

Trainable Qwen3.8-Flash-Next conditional-generation model.

This implementation uses the checkpoint’s compressed-block QSA router and FlexAttention or optional SM90 CuTe sparse GQA for CUDA BF16 long-sequence SFT, with a PyTorch oracle for CPU and numerical parity. Pipeline parallelism splits the decoder layers into stages that exchange the flattened HyperConnection state [batch, sequence, hc_count * hidden]; the final stage owns the HC mixer and lm_head. Tensor parallelism remains unsupported. Context parallelism uses a model-owned contiguous sequence shard for QSA, GDN, and PLE, and composes with sequence packing; it has not been validated together with pipeline parallelism.

Module Contents

Classes

NameDescription
Qwen3_8_FlashNextCausalLMOutputCausal-LM output with optional per-layer HC states for parity capture.
Qwen3_8_FlashNextForConditionalGenerationTrainable language-only Qwen3.8-Flash-Next causal-LM wrapper.
Qwen3_8_FlashNextModelLanguage-only Qwen3.8-Flash-Next decoder wrapper.
Qwen3_8_FlashNextTextModelBackendQwen3.8-Flash-Next text decoder with four HC streams and one PLE layer.

Functions

NameDescription
_qwen3_8_flash_next_backendReturn a typed backend with the model’s unfused rotary path.
_resolve_model_dtype-

Data

ModelClass

API

class nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextCausalLMOutput()
Dataclass

Bases: CausalLMOutputWithPast

Causal-LM output with optional per-layer HC states for parity capture.

class nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration(
engram_process_group: torch.distributed.ProcessGroup | None = None,
logits_dtype: torch.dtype | None = None,
moe_overrides: dict[str, typing.Any] | None = None
)

Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin

Trainable language-only Qwen3.8-Flash-Next causal-LM wrapper.

_cp_enabled
bool = False
_keep_in_fp32_modules_strict
= ['_fp32_params']
_packed_cp_attn_backends
= ('flex', 'fa4')
_pp_keep_self_forward
bool = True
backend
= _qwen3_8_flash_next_backend(backend)
generation_config
= generation_config_from_model_config(config)
lm_head
model
moe_config
= self.model.language_model.moe_config
pad_token_id
state_dict_adapter
tie_word_embeddings_support
TieSupport = TieSupport.UNTIED_ONLY
vocab_size
= config.text_config.vocab_size
nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration._nemo_prepare_model_owned_dtensors(
fsdp_mesh: torch.distributed.device_mesh.DeviceMesh
) -> set[torch.nn.Parameter]

Convert PLE owner shards to global DTensors before FSDP wrapping.

Under pipeline parallelism this runs per stage on that stage’s mesh, after the stage split, so only the stage that holds the PLE layer binds and shards the table.

Parameters:

fsdp_mesh
DeviceMesh

Flattened data/context shard mesh of this pipeline stage. A pending table binds its group as the owner group; an explicitly bound table requires the identical rank order.

Returns: set[nn.Parameter]

The exact registered parameter identities that FSDP must ignore

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.customize_pipeline_stage_modules(
module_names_per_stage: list[list[str]],
layers_prefix: str,
text_model: torch.nn.Module | None = None
) -> list[list[str]]

Keep the final HyperConnection mixer on the last pipeline stage.

The generic stage planner only knows embed_tokens, layers, norm, rotary_emb and lm_head. The HC mixer that collapses the hc_count streams before lm_head lives next to it.

Parameters:

module_names_per_stage
list[list[str]]

Planner output, one module FQN list per stage.

layers_prefix
str

FQN prefix of the text decoder, ending in ..

text_model
nn.Module | NoneDefaults to None

Unused; the mixer location is fixed by this class.

Returns: list[list[str]]

Copied per-stage FQN lists with the mixer appended to the last stage.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.forward(
input_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
position_ids: torch.Tensor | None = None,
inputs_embeds: torch.Tensor | None = None,
labels: torch.Tensor | None = None,
past_key_values: transformers.cache_utils.Cache | None = None,
use_cache: bool | None = None,
logits_to_keep: int | torch.Tensor = 0,
output_hidden_states: bool | None = None,
kwargs: typing.Any = {}

Run language-only generation and project final HC-mixed states.

Parameters:

input_ids
torch.Tensor | NoneDefaults to None

Raw tokenizer IDs of shape [batch, sequence]. On a non-first pipeline stage, where embed_tokens is absent, the schedule passes the previous stage’s floating-point HC state [batch, sequence, hc_count * hidden_size] here instead; it is routed as inputs_embeds.

attention_mask
torch.Tensor | NoneDefaults to None

Optional token-validity mask of shape [batch, sequence] or backend-specific attention mask.

position_ids
torch.Tensor | NoneDefaults to None

Optional positions of shape [batch, sequence] or [axes, batch, sequence].

inputs_embeds
torch.Tensor | NoneDefaults to None

Optional embeddings of shape [batch, sequence, hidden_size].

labels
torch.Tensor | NoneDefaults to None

Optional labels of shape [batch, sequence]. They are accepted for recipe compatibility; loss is computed externally.

past_key_values
Cache | NoneDefaults to None

Cache state; unsupported.

use_cache
bool | NoneDefaults to None

Cache request; True is unsupported.

logits_to_keep
int | torch.TensorDefaults to 0

0 for all positions, a positive trailing count, or an integer tensor of shape [kept_sequence] containing explicit sequence indices.

output_hidden_states
bool | NoneDefaults to None

Explicit True returns the embedding, every per-layer HC state, and the final state. When omitted, config.output_hidden_states=True returns only the final state for fused linear cross entropy without retaining every decoder activation.

_qwen3_8_flash_next_cp_context
Qwen3_8_FlashNextCPContext | NoneDefaults to None

Internal contiguous CP metadata with replicated raw-ID/padding tensors of shape [batch, global_sequence].

**kwargs
AnyDefaults to {}

Text-attention backend metadata.

Returns: Qwen3_8_FlashNextCausalLMOutput

Causal-LM output with logits [batch, kept_sequence, vocab_size].

classmethod

Construct from a parsed Qwen3.8-Flash-Next configuration.

classmethod

Construct architecture from a local/HF config before checkpoint load.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.get_input_embeddings() -> torch.nn.Module

Return the text token embedding module.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.get_output_embeddings() -> torch.nn.Module

Return the untied LM projection.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.get_pipeline_stage_metas(
is_first: bool,
microbatch_size: int,
seq_len: int,
dtype: torch.dtype
) -> tuple[tuple[torch.Tensor, ...], tuple[torch.Tensor, ...]]

Return static input/output meta tensors for this pipeline stage.

Stages exchange the flattened HC state, so a non-first stage receives [batch, sequence, hc_count * hidden] rather than [batch, sequence, hidden]. The last stage collapses it through the HC mixer and emits logits.

Parameters:

is_first
bool

Whether this stage holds embed_tokens.

microbatch_size
int

Pipeline micro-batch size.

seq_len
int

Sequence length of the padded batch.

dtype
torch.dtype

Activation dtype of the exchanged tensors.

Returns: tuple[torch.Tensor, ...]

(inputs_meta, outputs_meta) tuples of meta tensors. The first-stage

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.initialize_weights(
buffer_device: torch.device | None = None,
dtype: torch.dtype = torch.bfloat16
) -> None

Initialize checkpoint-free model weights.

Parameters:

buffer_device
torch.device | NoneDefaults to None

Target device for backend initializers.

dtype
torch.dtypeDefaults to torch.bfloat16

Final model parameter dtype, excluding intrinsic fp32 GDN state.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.prepare_model_inputs_for_cp(
batch: dict[str, typing.Any],
num_chunks: int = 1
) -> dict[str, typing.Any]

Return Qwen3.8-Flash-Next’s contiguous model-owned CP batch sharder.

Parameters:

batch
dict[str, Any]

Full-sequence batch. Token-aligned tensors have shape [batch, global_sequence, ...] and remain unchanged until the returned sharder is invoked.

num_chunks
intDefaults to 1

Accepted for the framework hook contract; Qwen3.8-Flash-Next shards each non-packed batch directly and does not use it.

Returns: dict[str, Any]

Mapping containing one unresolved cp_sharder. Once invoked it

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.set_input_embeddings(
value: torch.nn.Module
) -> None

Replace the text token embedding module.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextForConditionalGeneration.set_output_embeddings(
value: torch.nn.Module
) -> None

Replace the untied LM projection.

class nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextModel(
moe_overrides: dict[str, typing.Any] | None = None,
engram_process_group: torch.distributed.ProcessGroup | None = None,
)

Bases: Module

Language-only Qwen3.8-Flash-Next decoder wrapper.

language_model
nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextModel.forward(
input_ids: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
position_ids: torch.Tensor | None = None,
inputs_embeds: torch.Tensor | None = None,
past_key_values: transformers.cache_utils.Cache | None = None,
output_hidden_states: bool | None = None,
kwargs: typing.Any = {}
) -> transformers.modeling_outputs.BaseModelOutputWithPast

Run the language-only Qwen3.8-Flash-Next decoder.

Parameters:

input_ids
torch.Tensor | NoneDefaults to None

Raw IDs of shape [batch, sequence]; required on the stage that holds the PLE layer.

attention_mask
torch.Tensor | NoneDefaults to None

Optional token-validity mask of shape [batch, sequence] or backend-specific attention mask.

position_ids
torch.Tensor | NoneDefaults to None

Optional positions of shape [batch, sequence] or [axes, batch, sequence].

inputs_embeds
torch.Tensor | NoneDefaults to None

Optional embeddings of shape [batch, sequence, hidden_size], or the incoming HC state [batch, sequence, hc_count * hidden_size] on a non-first pipeline stage.

past_key_values
Cache | NoneDefaults to None

Cache state; unsupported for training.

output_hidden_states
bool | NoneDefaults to None

Capture decoder HC states.

_qwen3_8_flash_next_cp_context
Qwen3_8_FlashNextCPContext | NoneDefaults to None

Internal contiguous CP metadata with replicated raw-ID/padding tensors of shape [batch, global_sequence].

**kwargs
AnyDefaults to {}

Text-attention backend arguments.

Returns: BaseModelOutputWithPast

Base-model output whose final text states have shape

class nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextTextModelBackend(
moe_overrides: dict[str, typing.Any] | None = None,
engram_process_group: torch.distributed.ProcessGroup | None = None,
)

Bases: Module

Qwen3.8-Flash-Next text decoder with four HC streams and one PLE layer.

Tensor layout

Token embeddings start as [batch, sequence, hidden_size]. Decoder layers retain flattened HC state [batch, sequence, hc_count * hidden_size]. The final learned HC read collapses it back to [batch, sequence, hidden_size].

Parameters:

config
Qwen3_8_FlashNextTextConfig

Text architecture configuration.

backend
BackendConfig

Native attention, linear, and MoE backend configuration.

moe_config
MoEConfig | NoneDefaults to None

Optional native MoE configuration override.

moe_overrides
dict[str, Any] | NoneDefaults to None

Optional fields merged into the default MoE config.

engram_process_group
dist.ProcessGroup | NoneDefaults to None

Explicit group that owns contiguous PLE table row shards. When omitted in a distributed run the table binds the FSDP (data/context shard) mesh of the pipeline stage that holds it during parallelization, so the owner group is the stage’s EP/FSDP group rather than the world. Only the MoE parallelizer binds it, so PLE layers require ep_size > 1.

engram_table_config
Qwen3_8_FlashNextEngramTableConfig | NoneDefaults to None

Optional tiny-table override for unit tests. The released checkpoint uses [320001536, 160] globally.

_cp_enabled
bool = False
embed_tokens
hyper_connection_mixer
layers
= nn.ModuleDict()
model_dtype
= _resolve_model_dtype(config)
moe_config
= moe_config or MoEConfig(**moe_defaults)
padding_idx
= config.pad_token_id
rotary_emb
vocab_size
= config.vocab_size
nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextTextModelBackend.forward(
input_ids: torch.Tensor | None = None,
inputs_embeds: torch.Tensor | None = None,
attention_mask: torch.Tensor | None = None,
position_ids: torch.Tensor | None = None,
padding_mask: torch.Tensor | None = None,
past_key_values: transformers.cache_utils.Cache | None = None,
use_cache: bool | None = None,
output_hidden_states: bool | None = None,
attn_kwargs: typing.Any = {}
) -> transformers.modeling_outputs.BaseModelOutputWithPast

Run the HC decoder and final HC mixer.

Parameters:

input_ids
torch.Tensor | NoneDefaults to None

Raw tokenizer IDs of shape [batch, sequence]. They remain required when inputs_embeds is supplied and this stage holds the PLE layer, because PLE hashes the raw IDs.

inputs_embeds
torch.Tensor | NoneDefaults to None

Optional precomputed token/vision embeddings of shape [batch, sequence, hidden_size], or on a non-first pipeline stage the incoming flattened HC state of shape [batch, sequence, hc_count * hidden_size].

attention_mask
torch.Tensor | NoneDefaults to None

Optional mask of shape [batch, sequence] or a backend-specific attention mask.

position_ids
torch.Tensor | NoneDefaults to None

Optional positions of shape [batch, sequence] or [axes, batch, sequence].

padding_mask
torch.Tensor | NoneDefaults to None

Optional mask [batch, sequence] where True is padding.

past_key_values
Cache | NoneDefaults to None

KV/SSM cache; unsupported by this training backend.

use_cache
bool | NoneDefaults to None

Cache request; True is unsupported.

output_hidden_states
bool | NoneDefaults to None

Include embedding, per-layer HC, and final collapsed states for parity diagnostics.

_qwen3_8_flash_next_cp_context
Qwen3_8_FlashNextCPContext | NoneDefaults to None

Internal contiguous CP metadata. Its replicated raw-ID and padding tensors have shape [batch, global_sequence]; model activations remain local [batch, sequence, hidden] tensors.

**attn_kwargs
AnyDefaults to {}

Attention backend metadata.

Returns: BaseModelOutputWithPast

A base-model output whose last_hidden_state has shape

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextTextModelBackend.get_input_embeddings() -> torch.nn.Module

Return the raw-token embedding table.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextTextModelBackend.init_weights(
buffer_device: torch.device
) -> None

Initialize decoder weights for training from scratch.

Parameters:

buffer_device
torch.device

Device used by layer initializers.

nemo_automodel.components.models.qwen3_8_flash_next.model.Qwen3_8_FlashNextTextModelBackend.set_input_embeddings(
value: torch.nn.Module
) -> None

Replace the raw-token embedding table.

Parameters:

value
nn.Module

Module mapping [batch, sequence] IDs to [batch, sequence, hidden_size] embeddings.

nemo_automodel.components.models.qwen3_8_flash_next.model._qwen3_8_flash_next_backend(

Return a typed backend with the model’s unfused rotary path.

nemo_automodel.components.models.qwen3_8_flash_next.model._resolve_model_dtype(
) -> torch.dtype
nemo_automodel.components.models.qwen3_8_flash_next.model.ModelClass = Qwen3_8_FlashNextForConditionalGeneration