Files
HubertandGitHub d69dbb8da4 feat(cp): context parallelism for hybrid + VLM SFT (#3099)
* 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.
2026-07-20 23:53:42 -07:00

42 lines
1.3 KiB
Python

import pytest
import torch
from prime_rl.utils.sequence import (
get_cu_seqlens_from_position_ids,
get_cu_seqlens_from_seq_lens,
)
@pytest.mark.parametrize(
("position_ids", "expected_cu_seqlens", "expected_max_seqlen"),
[
(torch.arange(8).unsqueeze(0), [0, 8], 8),
(torch.arange(8, 16).unsqueeze(0), [0, 8], 8),
(torch.tensor([[0, 1, 2, 3, 0, 1, 2]]), [0, 4, 7], 4),
(torch.tensor([[5, 6, 7, 0, 1, 2]]), [0, 3, 6], 3),
],
)
def test_get_cu_seqlens_from_position_ids_is_local_relative(
position_ids: torch.Tensor,
expected_cu_seqlens: list[int],
expected_max_seqlen: int,
) -> None:
cu_seqlens, max_seqlen = get_cu_seqlens_from_position_ids(position_ids)
assert cu_seqlens.dtype == torch.int32
assert cu_seqlens.tolist() == expected_cu_seqlens
assert max_seqlen == expected_max_seqlen
def test_get_cu_seqlens_from_seq_lens():
cu_seqlens, max_seqlen = get_cu_seqlens_from_seq_lens(torch.tensor([4, 3, 2]), total_tokens=9)
assert cu_seqlens.dtype == torch.int32
assert cu_seqlens.tolist() == [0, 4, 7, 9]
assert max_seqlen == 4
def test_get_cu_seqlens_from_seq_lens_rejects_wrong_total():
with pytest.raises(ValueError, match="sum must equal"):
get_cu_seqlens_from_seq_lens(torch.tensor([4, 3]), total_tokens=9)