nemo_rl.models.megatron.draft.dflash#

Module Contents#

Classes#

DFlashBodyConfig

Checkpoint-compatible DFlash body dimensions.

QwenRMSNorm

Qwen RMS normalization with FP32 variance accumulation.

_ColumnParallelProjection

MCore column projection with a tensor-only forward contract.

_RowParallelProjection

MCore row projection with a tensor-only forward contract.

_ShardedModule

Small MCore-checkpoint-aware module used by the DFlash hierarchy.

_DFlashAttention

_DFlashMLP

_DFlashDecoderLayer

DFlashBody

DFlash decoder body with caller-provided embeddings and no output head.

Functions#

Data#

API#

nemo_rl.models.megatron.draft.dflash._ShardedOffsets#

None

nemo_rl.models.megatron.draft.dflash._INPUT_ERROR_MESSAGES#

(‘target_taps shape does not match the DFlash plan’, ‘block_embeddings shape does not match the DFla…

nemo_rl.models.megatron.draft.dflash._INPUT_TYPE_ERROR_CODES#

‘frozenset(…)’

class nemo_rl.models.megatron.draft.dflash.DFlashBodyConfig#

Checkpoint-compatible DFlash body dimensions.

The defaults pin the public z-lab/Qwen3-8B-DFlash-b16 artifact (revision 9b41424b7109f9c5413454f481b09a82b85333f4); the schema is asserted in tests/unit/models/megatron/test_dflash_checkpoint_schema.py. Every dimension listed in the constructor’s partitioned-dimension check must stay divisible by the tensor-parallel size – num_key_value_heads (8) is the binding constraint, so the pinned artifact supports at most TP=8 (there is no KV-head-duplication fallback).

hidden_size: int#

4096

intermediate_size: int#

12288

num_attention_heads: int#

32

num_key_value_heads: int#

8

head_dim: int#

128

num_hidden_layers: int#

5

num_target_taps: int#

5

rope_theta: float#

1000000.0

rms_norm_eps: float#

1e-06

initializer_range: float#

0.02

__post_init__() → None#
class nemo_rl.models.megatron.draft.dflash.QwenRMSNorm(
hidden_size: int,
*,
eps: float,
params_dtype: torch.dtype,
gradient_reduce_group: torch.distributed.ProcessGroup | None = None,
)#

Bases: torch.nn.Module

Qwen RMS normalization with FP32 variance accumulation.

Initialization

static _sum_gradient(
gradient: torch.Tensor,
*,
group: torch.distributed.ProcessGroup | None,
) → torch.Tensor#
forward(hidden_states: torch.Tensor) → torch.Tensor#
class nemo_rl.models.megatron.draft.dflash._ColumnParallelProjection#

Bases: megatron.core.tensor_parallel.layers.ColumnParallelLinear

MCore column projection with a tensor-only forward contract.

__call__(hidden_states: torch.Tensor) → torch.Tensor#
_save_to_state_dict(
destination: dict[str, Any],
prefix: str,
keep_vars: bool,
) → None#
forward(hidden_states: torch.Tensor) → torch.Tensor#
class nemo_rl.models.megatron.draft.dflash._RowParallelProjection#

Bases: megatron.core.tensor_parallel.layers.RowParallelLinear

MCore row projection with a tensor-only forward contract.

__call__(hidden_states: torch.Tensor) → torch.Tensor#
_save_to_state_dict(
destination: dict[str, Any],
prefix: str,
keep_vars: bool,
) → None#
forward(hidden_states: torch.Tensor) → torch.Tensor#
class nemo_rl.models.megatron.draft.dflash._ShardedModule(*, tp_group: torch.distributed.ProcessGroup | None)#

Bases: torch.nn.Module

Small MCore-checkpoint-aware module used by the DFlash hierarchy.

Initialization

sharded_state_dict(
prefix: str = '',
sharded_offsets: nemo_rl.models.megatron.draft.dflash._ShardedOffsets = (),
metadata: dict[str, Any] | None = None,
) → megatron.core.dist_checkpointing.mapping.ShardedStateDict#
class nemo_rl.models.megatron.draft.dflash._DFlashAttention(
config: nemo_rl.models.megatron.draft.dflash.DFlashBodyConfig,
*,
parallel_config: megatron.core.model_parallel_config.ModelParallelConfig,
tp_group: torch.distributed.ProcessGroup | None,
init_method: collections.abc.Callable[[torch.Tensor], torch.Tensor],
)#

Bases: nemo_rl.models.megatron.draft.dflash._ShardedModule

class nemo_rl.models.megatron.draft.dflash._DFlashMLP(
config: nemo_rl.models.megatron.draft.dflash.DFlashBodyConfig,
*,
parallel_config: megatron.core.model_parallel_config.ModelParallelConfig,
tp_group: torch.distributed.ProcessGroup | None,
init_method: collections.abc.Callable[[torch.Tensor], torch.Tensor],
)#

Bases: nemo_rl.models.megatron.draft.dflash._ShardedModule

forward(hidden_states: torch.Tensor) → torch.Tensor#
class nemo_rl.models.megatron.draft.dflash._DFlashDecoderLayer(
config: nemo_rl.models.megatron.draft.dflash.DFlashBodyConfig,
*,
parallel_config: megatron.core.model_parallel_config.ModelParallelConfig,
tp_group: torch.distributed.ProcessGroup | None,
init_method: collections.abc.Callable[[torch.Tensor], torch.Tensor],
)#

Bases: nemo_rl.models.megatron.draft.dflash._ShardedModule

nemo_rl.models.megatron.draft.dflash._rotate_half(hidden_states: torch.Tensor) → torch.Tensor#
nemo_rl.models.megatron.draft.dflash._build_rope_table(
positions: torch.Tensor,
*,
head_dim: int,
theta: float,
dtype: torch.dtype,
) → tuple[torch.Tensor, torch.Tensor]#
nemo_rl.models.megatron.draft.dflash._apply_rope(
hidden_states: torch.Tensor,
*,
cosine: torch.Tensor,
sine: torch.Tensor,
) → torch.Tensor#
nemo_rl.models.megatron.draft.dflash._live_context_parallel_size() → int#
class nemo_rl.models.megatron.draft.dflash.DFlashBody(
config: nemo_rl.models.megatron.draft.dflash.DFlashBodyConfig | None = None,
*,
tp_group: torch.distributed.ProcessGroup | None = None,
parallel_config: megatron.core.model_parallel_config.ModelParallelConfig | None = None,
)#

Bases: nemo_rl.models.megatron.draft.dflash._ShardedModule

DFlash decoder body with caller-provided embeddings and no output head.

Constructed by the DFlash training provider added in PR #3757.

Initialization

_validate_inputs(
*,
target_taps: torch.Tensor,
block_embeddings: torch.Tensor,
plan: nemo_rl.models.megatron.draft.block_plan.DFlashBatchPlan,
) → None#
forward(
*,
target_taps: torch.Tensor,
block_embeddings: torch.Tensor,
plan: nemo_rl.models.megatron.draft.block_plan.DFlashBatchPlan,
) → torch.Tensor#

Update anchored block embeddings from target hidden-state taps.

sharded_state_dict(
prefix: str = '',
sharded_offsets: nemo_rl.models.megatron.draft.dflash._ShardedOffsets = (),
metadata: dict[str, Any] | None = None,
) → megatron.core.dist_checkpointing.mapping.ShardedStateDict#

Return MCore shards with public names and global checkpoint shapes.

nemo_rl.models.megatron.draft.dflash.__all__#

[‘DFlashBody’, ‘DFlashBodyConfig’, ‘QwenRMSNorm’]