nemo_automodel.components.models.step5.sparse
nemo_automodel.components.models.step5.sparse
CSA proxy compression and learned region selection for Step-5.
Module Contents
Classes
Functions
Data
_compiled_gather_region_payload
API
Native varlen layout for the valid prefix of one single-batch shard.
Int32 cumulative key counts for those same documents.
Int32 cumulative query counts for the intersected documents.
Physical start of the first queried document’s keys.
Exclusive last queried token; future keys must be omitted.
Native SWA dummy keys before the received halo, used to keep document-relative 96-token K tile origins aligned across CP. They are outside every valid query window.
Largest causal key prefix for one queried document.
Largest local document query segment.
Last document’s first token, if a trailing padding-query suffix must retain the original singleton-key attention semantics.
Query prefix evaluated by the native varlen kernel. Zero identifies a fully padded batch row with a proven padding_key.
Layer-independent CSA layout for one contiguous CP query shard.
Singleton global key for a fully padded, fully selected local CSA shard, or None when the shortcut cannot be established.
The same proof as forward_full_block_prefix, independently enabled for backward mask skipping. Zero is fallback.
Region width when the full-block proof also establishes canonical global token coordinates; zero disables the optional arithmetic mask specialization.
Prefix or all-padding layouts when both B2 rows satisfy native causal attention semantics.
Optional native varlen metadata for this query shard.
Int64 current region IDs [batch, local_queries].
Int32 ascending full-prefix IDs [batch, local_queries, min(topk, regions) + 1]; unused entries contain region_count.
Int64 global document IDs [batch, global_keys].
Initial all-selected query count for opt-in forward full blocks, proven only for unpadded B1 CP1 inputs with positions exactly equal to global token indices. Zero is fallback.
Whether any query on any CP rank needs learned scores. All ranks derive it from the same global layout before collectives.
Int64 document IDs [batch, local_queries].
Number of global regions, also the unused-route sentinel.
Int64 document IDs [batch, regions].
Boolean shape matching region_sources, excluding padding tokens as well as missing slots.
Optional int32 source token indices [batch, regions, region_size], shared across layers. Missing slots contain -1.
Index into summary_ranges for each score tile.
Host tuples (batch, query_start, query_stop, first_region, past_last_region), bounded to 128 local queries each. Only tiles containing a sparse query are included.
Boolean [batch, local_queries], true only when more than topk historical regions in the same document are visible.
Merged (batch, first_region, past_last_region) intervals needed by score tiles. Empty on prefix-only or irregular layouts.
Int64 global region IDs [batch, global_keys].
Optional native SWA layout, including at most window - 1 preceding keys. Independent of whether CSA selection is sparse.
Sparse selection tensors, all local to one CP rank’s queries.
The only allowed global key for a fully padded shard, after proving standard document positions and trailing padding.
Every query in this local shard selects all causal regions in its own document; membership adds no restriction.
The same proof as forward_full_block_prefix, independently enabled for backward mask skipping. Zero is fallback.
Region width when the full-block proof also establishes canonical global token coordinates; zero disables the optional arithmetic mask specialization.
Proven prefix or all-padding layout for both rows of a fully selected B2 shard; None if either row needs the fallback.
Optional compact varlen layout for fully selected, single-batch shards with contiguous document positions.
Number of initial queries proven to select every causal key in one unpadded global B1 document, with CP1 and canonical positions starting at zero. Zero disables the proof.
Int64 tensor of shape [batch, global_keys].
Int64 tensor of shape [batch, local_queries].
Number of global regions; also the unused-route sentinel.
Sorted int32 tensor [batch, groups, local_queries, topk + 1]. Unused slots contain a sentinel larger than any valid region.
Int64 tensor of shape [batch, global_keys].
Bases: LayerNorm
Indexer variance E[x²] - E[x]², evaluated inside the module’s FSDP hooks.
Normalize […, proxy_dim] with BF16-rounded scale/bias and FP32 math.
Bases: Step3p5RMSNorm
Indexer RMSNorm with source-required activation-dtype parameter rounding.
Normalize […, proxy_dim] activations and preserve their dtype.
Bases: Linear
Keep FP32 source weights while matching the reference BF16 projection.
Project […, hidden_size] to signed FP32 indexer head weights.
Bases: Module
Checkpoint-backed, discrete CSA selector; LM loss does not train top-k indices.
Select historical CSA regions, scoring only genuinely sparse queries.
Parameters:
Tensor of shape [batch, local_queries, hidden].
Int64 document-local positions [batch, local_queries].
Projected global index keys [batch, global_keys, proxy_dim], or None when every global query sees at most topk historical regions.
Same shape as keys, or None on that full-prefix path.
Int64 document-local positions [batch, global_keys].
Boolean padding flags [batch, global_keys].
Physical start of the contiguous local query shard.
Shared document/region layout for the current model call. Direct component callers may omit it.
Returns: RegionSelection
Ascending selected region IDs [batch, groups, local_queries, topk + 1]
Compute normalized index keys and per-dimension compression logits.
Parameters:
Tensor of shape [batch, sequence, hidden].
Tensor of shape [batch, sequence].
Returns: tuple[torch.Tensor, torch.Tensor]
Two tensors of shape [batch, sequence, proxy_dim], in the hidden dtype.
Gather existing token values without performing floating-point arithmetic.
Preserve eager FP32 arithmetic and both required quantization roundings.
Inductor can remove the intermediate BF16 rounding when this arithmetic is compiled. Even emulate_precision_casts changes a few FP8 summaries through FP32 arithmetic, so only the integer gather is compiled.
Merge each batch’s needed historical intervals without inspecting GPU data.
Gather only the historical regions needed by this rank’s sparse queries.
Parameters:
Original indexer keys [batch, global_tokens, proxy_dim].
Compression logits, shaped like keys.
Int32 token indices [batch, required_regions, region_size]. Missing slots contain -1.
Boolean shape matching sources, excluding padding and missing slots.
Returns: torch.Tensor
Key and logit tensors [batch, required_regions, region_size, proxy_dim]
Fuse weighted-ReLU scoring into one bounded FP32 output tile.
Parameters:
E4M3-rounded BF16 tensor [batch, queries, groups, heads, dim].
FP32 signed head weights [batch, queries, groups, heads].
E4M3-rounded BF16 region summaries [batch, regions, dim].
First query included in this tile.
Exclusive last query included in this tile.
Returns: torch.Tensor
FP32 scores [batch, groups, stop - start, regions]. No gradients are
Pack selected regions directly without int64 scatter workspaces.
Parameters:
Sorted CUDA int32 tensor [batch, groups, queries, slots]; ids at or above regions are padding. Repeated valid ids are allowed.
Total number of key regions.
Returns: torch.Tensor
CUDA int32 bitmap [batch, groups, queries, ceil(regions / 32)].
Prepare document/region metadata once per model forward.
Parameters:
Int64 document-local positions [batch, global_keys].
Boolean padding flags [batch, global_keys].
Physical start of this rank’s contiguous query shard.
Number of local queries.
Tokens per region, reset at every document boundary.
Maximum number of selected historical regions.
Retain a single-document CP1 proof for FA4 forward mask skipping, using this call’s existing host metadata.
Retain the same proof independently for backward mask skipping, without another host transfer.
Optional native SWA layout width. A globally trailing padded suffix keeps its custom-mask path. No additional host synchronization.
Returns: RegionMetadata
Shared immutable metadata. Only linear token/region storage and a bounded