nemo_automodel.components.models.deepseek_v4.cp
nemo_automodel.components.models.deepseek_v4.cp
Context-parallel helpers for the DeepSeek V4 custom model.
This implements the Miles-style training path: each CP rank owns a contiguous query shard, while K/V and compressed K/V are all-gathered with autograd-aware collectives before DSV4 sparse attention consumes them.
Module Contents
Functions
Data
API
Insert DSV4 compression-safe padding into packed BSHD rows before CP slicing.
The generic packed dataset may pad each packed sequence only for TE CP. DSV4
compression additionally needs document boundaries to align to compressor
windows; CSA then uses packed_seq_ids to reset the previous-window overlap.
This routine rebuilds each row from real sequence spans, pads every span to
pad_multiple, and appends row-level pack padding with sequence ID 0. When
requested for HybridEP, the final physical length is max-reduced across ranks
before CP slicing so every rank in a flattened DP x CP expert group is uniform.
Build local-query/global-key additive mask for Miles-style DSV4 CP.
position_ids are the local query positions after contiguous CP slicing.
Keys are in global sequence order because DSV4 gathers K/V along sequence.
padding_mask follows the internal convention True=padding.
Build local-query/global-key additive mask for packed DSV4 CP.
packed_seq_ids is local to the CP rank. The function all-gathers sequence
IDs and document-local positions for the key side, then applies same-document,
causal, padding, and optional sliding-window constraints.
Build per-token packed sequence IDs from padded packed lengths.
IDs are 1-based within each batch row; 0 marks trailing pack padding that
belongs to no sequence. seq_lens_padded may be right-padded with
padding_value to make rows rectangular.
All-gather activation tensors across CP ranks and concatenate on dim.
The distributed.nn functional collective preserves autograd, so backward routes gradients for gathered remote slices back to their owning ranks.
All-gather non-differentiable metadata such as padding masks.
Return whether a real CP process group is active.
Required per-CP-rank sequence-length multiple for DSV4 Miles-style CP.
Compress-ratio layers constrain how the sequence may be split across CP ranks:
a ratio-R layer needs each local shard divisible by R, and ratio-4 layers use
cross-window overlap so they need 2*R. The returned value is the LCM across
all configured compress_ratios (1 when none are configured).
Return this rank’s index in the DSV4 CP group, or 0 without CP.
Return the DSV4 CP group size, or 1 without CP.
Contiguously shard a batch for DeepSeek V4 Miles-style context parallelism.
Exposed as ContextParallelSharder.shard_batch (via functools.partial to bind
pad_multiple) and invoked by the CP dispatch. HybridEP can
first max-reduce packed lengths so every rank contributes a uniform token count.
Each CP rank then keeps one seq_start:seq_end slice; DSV4 attention all-gathers
K/V across CP ranks during forward.
pad_multiple is the required per-CP-rank shard multiple (from
dsv4_cp_local_seq_multiple); the global sequence is padded so it is divisible
by cp_size and each local shard is divisible by pad_multiple (>= 2).
At CP size one, the native THD route keeps the original tensors and packing
metadata and exposes physical document IDs to the generic loss contract.
Parameters:
Context-parallel mesh; size one selects native preparation.
Tensor-parallel mesh passed to the shared sharder.
Mutable mapping with input_ids [batch, global_sequence] or inputs_embeds [batch, global_sequence, hidden], labels and position_ids [batch, global_sequence], and optional attention_mask [batch, global_sequence] or [batch, 1, global_sequence, global_sequence]. Native packed seq_lens and seq_lens_padded are integer tensors [batch, documents] of real and physical lengths; -1000 pads unused document entries. Existing _packed_seq_ids [batch, global_sequence] take precedence. At CP1, physical lengths produce row-local IDs starting at one, with zero for trailing pack padding.
Optional tensor [batch, global_sequence], used as labels if absent, otherwise kept as a separate sharded loss mask.
Token value used for added input padding.
Required per-rank sequence-length multiple.
Whether to synchronize packed lengths before sharding.
Returns:
Context-manager factory, the mutated batch mapping, and a ShardLayout