nemo_automodel.components.models.common.packing
nemo_automodel.components.models.common.packing
Model-derived packing contracts and native metadata normalization.
HF flash attention consumes the public varlen kwargs built by dataset collaters. Native FA4 and recurrent consumers use separate unpadded document metadata. Neither path modifies Transformers globals.
Module Contents
Classes
Functions
Data
API
Typed attention selection carried by a built custom model.
Selected native attention backend.
Model that composes document eligibility with its own attention policy.
Compose selected document attention with a model-owned policy.
Parameters:
Integer selected IDs [batch, sequence], a prepared mask [batch, 1, sequence, sequence], a mapping of such masks, or None for native masking.
Optional modality IDs [batch, sequence].
Optional original membership [batch, sequence], used only by this model policy rather than attention/MTP segmentation.
Returns: torch.Tensor | dict[str, torch.Tensor] | None
A prepared mask or mapping of masks [batch, 1, sequence, sequence],
Model whose attention layers invoke Automodel’s native FA4 callable.
Model with additional constraints beyond a backend’s generic contract.
Reject a backend that cannot preserve this model’s packed policy.
Model that owns masking and consumes compact document IDs.
Model whose attention policy needs metadata at a specific forward entry.
Return the called module that owns packed attention preparation.
Model-derived loader requests and native attention setup information.
packed_mask_type and return_packed_sequence_metadata apply only to
batched packed inputs. THD derives kernel metadata from lengths instead.
return_document_ids requests original membership in either format.
uses_native_fa4 is model setup information, not an emitted batch field.
max_sequence_length is the configured decoder input-token limit per packed
document, before batch flattening; it applies to either output format.
Convert native collator document IDs using the selected model interface.
Parameters:
Integer tensor [batch, sequence], with positive IDs for document membership and zero for padding. CPU collation stays on CPU; model-entry preparation independently handles transfer.
Returns: dict[str, torch.Tensor | int]
An attention mask [batch, 1, sequence, sequence], compact document
Consumer of AutoModel’s gathered-token indices and cumulative lengths.
packed_token_indices selects non-padding tokens; cu_seqlens and
max_seqlen describe that gathered stream, not physical padded storage.
Qwen3.5, Kimi Linear/K3 use these boundaries for recurrent-state processing;
AutoModel’s FA4 path also needs the unpadded layout. This is independent of
the data loader or packing algorithm. The batch-major bundle is normalized
by flatten_packed_sequence_metadata after PP splitting, where needed.
Model whose active attention path dispatches through its HF config.
Whether HF’s dispatch key controls attention rather than backend.attn.
Whether each [B,S] attention-ID map is one eligible prefix plus padding.
Any positive ID is valid, independently in each batch document. Prefix or interior exclusions and distinct subdocuments require an explicit mask. Trailing padding cannot affect earlier causal outputs; its loss is ignored.
Flatten packed metadata once after pipeline microbatch splitting.
Parameters:
Native FA4 model receiving the forward call.
Positional model inputs. Tensor entries retain their model-specific layouts and are returned unchanged.
Model inputs containing packed_token_indices of shape
[batch, sequence] or [tokens] and cu_seqlens of shape
[batch, max_documents + 1] or [documents + 1].
Returns: tuple[tuple[Any, ...], dict[str, Any]] | None
Updated positional and keyword inputs whose packed token indices have
Raises:
ValueError: If pre-packed THD is requested, only one metadata tensor is supplied, or either layout is inconsistent with the current microbatch.
Install the once-per-forward native FA4 metadata normalizer.
Return the packing-relevant attention backend an already-built model runs with.
model.config._attn_implementation is a Transformers dispatch key, whose
vocabulary is wider than the mask layouts packing knows about: when flash
attention is requested but only the kernels package provides it,
Transformers records a kernels-hub id instead of the mainline name. Those ids
are mapped back so a model genuinely running varlen flash attention is packed
as such. Other live string keys are returned unchanged and therefore use
packing’s conservative block-causal default.
Prepare host offsets before FSDP moves the document map to CUDA.
PP has already selected the microbatch. Keep text Flash kwargs bundled until the decoder hook consumes them: VLM vision towers have their own boundaries and must not receive the text offsets through forwarded keyword arguments.
Parameters:
Outer model or pipeline stage, before its FSDP input-transfer hook.
Token IDs [batch, sequence] or hidden states [batch, sequence, hidden].
Forward inputs with CPU attention document IDs [batch, sequence].
Configured FlashAttention backend.
Returns: tuple[tuple[Any, ...], dict[str, Any]] | None
Prepared inputs with device-local document IDs and privately bundled
Materialize the selected document segmentation after PP splitting.
Parameters:
Consumer owning the attention implementation.
Forward inputs, including token IDs [batch, sequence] or hidden states [batch, sequence, hidden].
Forward inputs with integer _packed_seq_ids [batch, sequence]
describing effective attention and MTP boundaries, zero at excluded
slots. CPU IDs allow host Flash offset construction before transfer.
Optional document_ids [batch, sequence] retain original membership
for a model-owned policy. Positions [batch, sequence] or
[position_axes, batch, sequence] are passed through unchanged.
Required attention input representation.
Configured backend, used for mask dtype.
Returns: tuple[tuple[Any, ...], dict[str, Any]] | None
Updated args/kwargs retaining all input layouts. Selected IDs move to
Keep dataset-only NEAT offsets away from a legacy THD consumer.
Parameters:
Model that does not require explicit NEAT metadata.
Positional model inputs, returned unchanged in their original layouts.
Model inputs. packed_token_indices of shape [batch, sequence]
or [tokens] identifies NEAT metadata; its companion cu_seqlens
has shape [batch, max_documents + 1] or [documents + 1].
Returns: tuple[tuple[Any, ...], dict[str, Any]] | None
Inputs without NEAT token indices, cumulative lengths, and integer
Raise if model’s forward cannot receive the typed packing contract.
A model consumes the contract when its forward accepts **kwargs (HF
models thread FlashAttentionKwargs this way), names _packed_seq_ids
(the custom-model document map), or names all four varlen kwargs explicitly.
A forward whose signature cannot be introspected is rejected.
Parameters:
Already-built model, or a DDP wrapper exposing .module.
Configure the model consumer and return its dataset packing contract.
HF attention accepts public masks/varlen kwargs or a CPU batch-major attention document map. Maps are materialized at the model-owned entry point (HF decoder by default) without modifying Transformers globals. Flash offsets are prepared before FSDP input transfer and exposed only to the text consumer. Native FA4 receives batch-major metadata flattened at model entry after microbatch splitting. Other consumers strip unused native metadata so it cannot accidentally select their THD path.
Parameters:
Attention implementation used by the built model.
Optional built model declaring additional packed-metadata needs.
Returns: PackingCapabilities
Structural packed-data requirements for the dataset collater.
Raises:
ValueError: If native FA4 packing lacks a declared compatible consumer.RuntimeError: If HF flash attention cannot consume the public varlen contract.
Validate and install the adapters for batched packed execution.
Always obtain loader requests with get_packing_capabilities_for_models.
Call this setup only for packed batched input. THD validates its length-based
interface separately and must not install these mask/Flash/gather hooks.
Parameters:
Nonempty sequence of built local stages consuming the same batch.
Returns: PackingCapabilities
The same loader requests as the side-effect-free model query.
Raises:
ValueError: If stages require incompatible batched mask layouts.
Normalize batch-major packed metadata to a single flat token stream.
Parameters:
Tensor of shape [batch, sequence] containing
row-local token indices with -1 padding, or a pre-flattened
tensor of shape [tokens] containing indices into [batch, sequence].
Tensor of shape [batch, max_documents + 1] containing
row-local cumulative lengths with -1 padding, or a pre-flattened
tensor of shape [documents + 1].
Batch dimension of the padded token layout.
Sequence dimension of the padded token layout.
Returns: torch.Tensor
Flat token indices of shape [tokens] into the padded [batch, sequence]
Raises:
ValueError: If the layouts, boundaries, or represented token counts are inconsistent.
Return the NEAT/BSHD attention implementation used by a built model.
Custom models expose a typed backend.attn. Hugging Face models record
their resolved dispatch key on model.config; this reflects preload and
fallback decisions that are intentionally absent from the recipe config.
Models mixing native and HF attention declare which dispatch handles BSHD.
A separate pre-packed THD path does not determine the NEAT mask layout.
Parameters:
Constructed model, optionally wrapped in DDP.
Returns: str
The active attention backend or HF dispatch key, normalized for packing.
Raises:
TypeError: If model is not a torch.nn.Module.
Describe loader requests without configuring or validating packed execution.
This cheap query also serves unpacked and THD loaders. It does not install
hooks or invoke model packed-backend validators. Batched packing support is
checked separately by configure_packing; THD has its own validation.
Parameters:
Resolved attention implementation.
Optional built consumer declaring its metadata requirements.
Returns: PackingCapabilities
Loader return requests plus model-owned native FA4 setup information.
Combine loader return requests across local model or pipeline stages.
This query does not configure models or validate an execution format. Batched packed execution separately checks that stage mask layouts agree.
Parameters:
Nonempty sequence of built local stages consuming the same batch.
Returns: PackingCapabilities
The first stage’s mask interface, the union of optional return requests,
Raises:
ValueError: If no model stages were supplied.
Return True iff attention_mask is an Automodel-style indexed packing mask.
The Automodel neat_packed_vlm_collater (and the LLM equivalent) encode
packed-sample boundaries by marking document i (1-based) with the
integer i and using 0 for padding (e.g. [1, 1, 1, 2, 2, 3, 3, 0, 0]).
Any value greater than 1 is therefore a sufficient signal that two or
more documents are packed into the same row. A standard 0/1 attention mask
never has values > 1.
Fail loudly when flash neat packing cannot be consumed by the backend or model.
Neat packing under flash attention relies on the varlen FlashAttention
contract: the collater emits FlashAttentionKwargs (cu_seq_lens_q/
cu_seq_lens_k/max_length_q/max_length_k) which HuggingFace threads
into flash_attn_varlen_func. This function verifies, before training
starts, that (1) the installed Transformers build exposes that contract and
(2) the model’s forward can actually receive it — either as **kwargs,
explicit varlen parameters, or the custom-model _packed_seq_ids map. Any
gap raises here instead of silently dropping the cumulative lengths and
enabling cross-document attention.
Non-flash backends (sdpa / eager) use a 4D block-causal mask built by
the collater and need no validation.
Parameters:
The attention implementation the model runs with.
Optional already-built model (or DDP wrapper) to check for the
ability to receive the typed packing contract. Skipped when None
or when the model’s forward signature cannot be introspected.
Raises:
RuntimeError: If a flash backend is requested but the installed Transformers_flash_attention_forwarddoes not accept the public varlen kwargs, or the model’sforwardcan consume neither the varlen kwargs nor_packed_seq_ids.
Reject THD inputs before loader construction if a local stage cannot consume them.
THD uses the model’s seq_lens path rather than the mask/Flash conversion installed by configure_packing_for_models. Validate the existing model/backend capability without selecting a different backend or disabling packing.