bridge.models.deepseek.deepseek_v4_step#

DSv4-specific training step with contiguous CP partition support.

DSv4 hybrid attention uses a CSA (Compressed Sparse Attention) compressor that exchanges boundary hidden states between adjacent CP ranks. This requires contiguous token assignment (each rank gets a consecutive slice), unlike the default zigzag interleaved assignment used by standard causal models.

At CP>1, this step requires packed THD metadata and both native model fields attention_cp_layout=linear_cp_layout=’contiguous’. Non-packed batches and zigzag layouts are rejected; there is no fallback to the standard zigzag CP partitioner. A legacy dev-only cp_partition_mode setting is not sufficient. Use –step-func dsv4_step with a packed dataset. MCore’s native DSv4 packed-CP support validates the supported model topology.

Module Contents#

Functions#

_packed_metadata_for_forward

Extract global boundaries, preserving native DSv4 router-padding semantics.

_get_dsv4_packed_seq_params

Build packed-sequence parameters without the SSM sequence index.

_partition_packed_batch_contiguous

Slice a consecutive [start, end) token window for this CP rank.

_stage_batch_fields

Return the batch fields the DSv4 get_batch reads on this PP/VPP stage.

_reads_batch_on_skip

Return whether the DSv4 step reads the data iterator on this rank.

get_batch

get_batch with DSv4 contiguous CP partition support.

forward_step

Forward training step for DSv4 with contiguous CP partition support.

Data#

API#

bridge.models.deepseek.deepseek_v4_step.logger#

‘getLogger(…)’

bridge.models.deepseek.deepseek_v4_step._DSV4_CURRENT_PACKED_SEQ_PARAM_KEYS#

(‘cu_seqlens_q’, ‘cu_seqlens_kv’, ‘cu_seqlens_q_padded’, ‘cu_seqlens_kv_padded’, ‘max_seqlen_q’, ‘ma…

bridge.models.deepseek.deepseek_v4_step._DSV4_LEGACY_PACKED_SEQ_PARAM_KEYS#

(‘cu_seqlens’, ‘cu_seqlens_unpadded’, ‘cu_seqlens_argmin’, ‘max_seqlen’, ‘cu_seqlens_unpadded_argmin…

bridge.models.deepseek.deepseek_v4_step._packed_metadata_for_forward(
batch: dict,
*,
include_padding_mask: bool = False,
) → dict | None#

Extract global boundaries, preserving native DSv4 router-padding semantics.

Only the fixed THD benchmark requests padding_mask forwarding. Normal native packed batches continue counting alignment padding in router statistics.

bridge.models.deepseek.deepseek_v4_step._get_dsv4_packed_seq_params(
batch: dict,
) → megatron.core.packed_seq_params.PackedSeqParams#

Build packed-sequence parameters without the SSM sequence index.

bridge.models.deepseek.deepseek_v4_step._SEQLEN_KEYS#

‘frozenset(…)’

bridge.models.deepseek.deepseek_v4_step._TOKEN_KEYS#

(‘tokens’, ‘labels’, ‘loss_mask’, ‘position_ids’, ‘padding_mask’)

bridge.models.deepseek.deepseek_v4_step._partition_packed_batch_contiguous(
batch: dict[str, torch.Tensor],
cp_size: int,
cp_rank: int | None = None,
) → dict[str, torch.Tensor]#

Slice a consecutive [start, end) token window for this CP rank.

Only tokens, labels, loss_mask, position_ids, and padding_mask are sliced. Sequence-length metadata (cu_seqlens, max_seqlen, …) is intentionally kept at global values — the DSv4 CSA compressor needs global sequence boundaries to correctly exchange boundary hidden states between adjacent CP ranks. This mirrors how zigzag mode leaves cu_seqlens untouched.

The packed sequence length must be divisible by cp_size — ensure packed_sequence_size = N * cp_size when running pack_sft_data.

bridge.models.deepseek.deepseek_v4_step._stage_batch_fields(
cfg: megatron.bridge.training.config.ConfigContainer,
*,
pg_collection,
use_mtp: bool,
vp_stage: int | None,
) → megatron.bridge.training.gpt_step._StageBatchFields#

Return the batch fields the DSv4 get_batch reads on this PP/VPP stage.

bridge.models.deepseek.deepseek_v4_step._reads_batch_on_skip(
cfg: megatron.bridge.training.config.ConfigContainer,
pg_collection,
) → bool#

Return whether the DSv4 step reads the data iterator on this rank.

train() uses it on skipped iterations (train.iterations_to_skip) to advance the same ranks that read batches in a real iteration.

bridge.models.deepseek.deepseek_v4_step.get_batch(
data_iterator: Iterable,
cfg: megatron.bridge.training.config.ConfigContainer,
use_mtp: bool = False,
*,
pg_collection,
vp_stage: int | None = None,
)#

get_batch with DSv4 contiguous CP partition support.

Native attention_cp_layout selects contiguous partitioning. Every CP pipeline stage consumes the global packed metadata before token slicing.

bridge.models.deepseek.deepseek_v4_step.forward_step(
state: megatron.bridge.training.state.GlobalState,
data_iterator: Iterable,
model: megatron.core.models.gpt.GPTModel,
return_schedule_plan: bool = False,
)#

Forward training step for DSv4 with contiguous CP partition support.