* feat(rl): pack multimodal samples
Multimodal RL samples no longer force one-sample micro-batches: eager
image samples pack with text spans and compatible mm_kwargs samples from
the same run/LoRA, with per-sample boundaries kept in seq_lens. Packing
is always on — VLM training is custom-implementation-only and the custom
Qwen3.5 models advertise supports_packed_multimodal_training; a config
validator requires varlen flash attention for VLM training and
validate_multi_modal_pack fails loudly at startup for models without
packed-MM support.
Ported from #2889 minus its gating (pack_multimodal knob, resolve_pack_multimodal)
and its tokenizer packer params (removed on main since).
(cherry picked from commit 027edc2c510c35ab5666e8f9cbf160f58b1fba48)
* fix: fail loudly when RL micro batches carry multimodal data without [model.vlm]
Mirrors the SFT-side check: transported mm samples would otherwise flow
into forward unvalidated (no packability, attn, or freeze policy checks).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(rl): preserve multimodal packing across workers
Keep multimodal bins intact during worker balancing and reject malformed multimodal samples before they can stop shared multi-run packing.
* feat(cp): context parallelism for hybrid + VLM SFT
Unify model CP setup and make packed-boundary metadata CP-aware:
- setup_model_cp replaces setup_hybrid_cp/setup_nemotron_h_cp: models
owning linear-attention/Mamba layers expose
set_context_parallel_attributes (dense + MoE Qwen3.5, NemotronH) and
wire their own layers; hybrid models without the hook are rejected
instead of silently misconfigured.
- seq_lens stays global under CP: documents can straddle the shard
cut, so input_ids/position_ids shard per rank while seq_lens keeps
the full pre-shard boundaries, flagged via seq_lens_are_global
through forward() / prime_forward_kwargs into both Qwen3.5 models.
A cu_seqlens_are_global provenance flag rides along to fla's
GatedDeltaNet instead of reading cu_seqlens back (which cost one
CPU-GPU sync per linear-attention layer per microbatch); the CP
conv path switches to fla's causal_conv1d with a CP context so conv
state passes between ranks with document-boundary resets.
- VLM CP defers sharding to the model: the vision encoder and the
image-embed merge see the full sequence, then the root forward
shards embeds/positions (shard_position_ids_for_cp handles 3D
MRoPE) and updates ulysses attention params from global seq_lens.
The SFT trainer skips pre-sharding for MRoPE batches; the vision
encoder is pinned to SDPA under CP.
- shard_for_cp validates divisibility (uneven shards deadlock ulysses
all-to-all) and CatDataset's seq_len padding guarantees it; the
LoRA token count follows the sharded loss_mask.
- Qwen3.5 CP requires the custom implementation (the HF impl has no
set_context_parallel_attributes); rejected in get_model.
* feat(sft): multimodal (VLM) SFT
Renderer-produced multimodal samples flow end to end through SFT:
- SFTDataset keeps the renderer's MultiModalData: image-safe truncation
(never cuts inside a placeholder run, drops truncated-out images, and
skips samples whose EOS was truncated away), mm_token_type_ids kept
aligned through EOS append and causal shift, video inputs rejected
explicitly. _flatten_mm_items folds per-image processor outputs into
model-forward kwargs (pixel_values, image_grid_thw, ...).
- Cat packing concatenates mm_kwargs across samples in a pack and
represents text-only spans as zeros in mm_token_type_ids; stack
bucketing emits multimodal samples solo. Collates move mm tensors to
CUDA and keep packed rows batch-size 1.
- Local data_files normalization wraps string message content into
typed content blocks under a multimodal flag so Arrow can unify
text and image rows.
- setup_processor loads the AutoProcessor and attaches it to the
renderer; VLM SFT fails fast without one; weight checkpoints save
the processor alongside the tokenizer.
- Dense Qwen3.5 gains the composite VLM body (HF vision encoder +
custom text model) with MRoPE positions from mm_token_type_ids and
registers in _CUSTOM_VLM_MAPPING; VLM training requires a custom
PrimeRL implementation. Both dense and MoE VLM bodies always run
the vision encoder (dummy input on text-only microbatches, kept in
the backward graph) for FSDP/EP collective symmetry.
- Config: VLM SFT requires micro_batch_size=1; unfreezing the vision
encoder is incompatible with LoRA; the renderer-vs-VLM rejection is
gone (renderers own multimodal tokenization now).
* feat: use seq_lens as the packed-sample boundary contract
- Thread seq_lens through transport, packer, trainer forward, and all model forward methods
- Add get_cp_local_seq_lens() for CP sharding of document boundaries
- Replace get_cu_seqlens_from_position_ids with get_cu_seqlens_from_seq_lens in models
- Remove StackDataset and pack_function config (cat is the only packing strategy)
- Fix pending_sample preservation across resume in CatDataset
- Change padding to extend last segment rather than creating a new one
- Remove SDPA/eager guards — flash attention is the only path (consistent with PR #3057)
- Update all model tests to pass seq_lens
- Add seq_lens and padding_len to MicroBatch transport type
* refactor: remove redundant packing metadata
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Sami Jaghouar <sami@primeintellect.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
* feat(sft): renderer-only SFT tokenization
SFT tokenization now goes exclusively through the renderers library:
- build_training_sample with a hand-coded renderer replaces the
incremental apply_chat_template masking path, which corrupted loss
masks under position-dependent chat templates (e.g. Qwen3 stripping
past thinking blocks across user turns). DefaultRenderer is rejected
in setup_renderer; fake data skips renderer construction entirely.
- [renderer] config defaults to auto-resolution from the tokenizer;
template controls (e.g. enable_thinking) are set run-wide via the
typed renderer config.
- Non-assistant roles opt into the loss via the renderer's body-only
path (content_sft_roles).
- _drop_null_fields strips Arrow phantom nulls before tool-call
deserialization so genuine nulls inside argument strings survive.
- Null-check rather than key-check for messages vs prompt/completion
resolution (Arrow schema union adds messages: null to prompt/
completion rows).
- seq_len default 256 (FakeDataConfig keeps 128); loss_impl defaults
to liger_fused; CI reverse-text configs pin [renderer] name=qwen3.