Files
prime-rl/tests/unit/test_configs.py
T
55fc08db46 feat(rl): pack multimodal samples with context parallelism (#3103)
* 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.
2026-07-22 20:09:45 -07:00

644 lines
23 KiB
Python

from pathlib import Path
from typing import Annotated, Literal
import pytest
import tomli_w
from pydantic import BaseModel, Field, ValidationError
from pydantic_config import ConfigFileError
from prime_rl.configs.inference import InferenceConfig
from prime_rl.configs.orchestrator import OrchestratorConfig
from prime_rl.configs.rl import RLConfig
from prime_rl.configs.sft import SFTConfig
from prime_rl.configs.trainer import ModelConfig as TrainerModelConfig
from prime_rl.configs.trainer import TrainerConfig
from prime_rl.utils.config import BaseConfig, cli, to_toml_dict
# All config config classes
CONFIG_CLASSES = [
RLConfig,
TrainerConfig,
SFTConfig,
OrchestratorConfig,
InferenceConfig,
]
def get_config_files() -> list[Path]:
"""Any TOML file inside `configs/` or `examples/`."""
config_files = list(Path("configs").rglob("*.toml"))
example_files = list(Path("examples").rglob("*.toml"))
return config_files + example_files
@pytest.mark.parametrize("config_file", get_config_files(), ids=lambda x: x.as_posix())
def test_load_configs(config_file: Path):
"""Tests that all config files can be loaded by at least one config class."""
could_parse = []
for config_cls in CONFIG_CLASSES:
try:
cli(config_cls, args=["@", config_file.as_posix()])
could_parse.append(True)
except (ValidationError, ConfigFileError, SystemExit):
could_parse.append(False)
assert any(could_parse), f"No config class could be parsed from {config_file}"
class NestedConfig(BaseConfig):
lr: float = 1e-4
weight_decay: float = 0.01
name: str = "default"
class VariantA(BaseModel):
type: Literal["a"] = "a"
alpha: float = 0.1
shared: int = 1
class VariantB(BaseModel):
type: Literal["b"] = "b"
beta: float = 0.2
shared: int = 1
VariantType = Annotated[VariantA | VariantB, Field(discriminator="type")]
class DummyConfig(BaseConfig):
name: str = "experiment"
seed: int = 42
nested: NestedConfig = NestedConfig()
variant: VariantType = VariantA()
def write_toml(path: Path, data: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "wb") as f:
tomli_w.dump(data, f)
def test_defaults():
"""All defaults are applied when no TOML or CLI args are given."""
config = cli(DummyConfig, args=[])
assert config.name == "experiment"
assert config.seed == 42
assert config.nested.lr == 1e-4
assert config.nested.weight_decay == 0.01
assert config.variant.type == "a"
assert config.variant.alpha == 0.1
def test_toml_partial_nested_override(tmp_path):
"""Partially overriding a nested model preserves unset field defaults."""
write_toml(tmp_path / "cfg.toml", {"nested": {"lr": 3e-4}})
config = cli(DummyConfig, args=["@", str(tmp_path / "cfg.toml")])
assert config.nested.lr == 3e-4
assert config.nested.weight_decay == 0.01
assert config.nested.name == "default"
def test_toml_discriminated_union_default_type(tmp_path):
"""Overriding a discriminated union field without 'type' uses the default variant."""
write_toml(tmp_path / "cfg.toml", {"variant": {"alpha": 0.9}})
config = cli(DummyConfig, args=["@", str(tmp_path / "cfg.toml")])
assert config.variant.type == "a"
assert config.variant.alpha == 0.9
assert config.variant.shared == 1
def test_toml_discriminated_union_switch_variant(tmp_path):
"""Providing an explicit 'type' switches to that variant."""
write_toml(tmp_path / "cfg.toml", {"variant": {"type": "b"}})
config = cli(DummyConfig, args=["@", str(tmp_path / "cfg.toml")])
assert config.variant.type == "b"
assert config.variant.beta == 0.2
def test_toml_discriminated_union_override_switch_variant(tmp_path):
"""Providing an explicit 'type' overrides the default variant."""
write_toml(tmp_path / "cfg.toml", {"variant": {"type": "b", "beta": 0.5}})
config = cli(DummyConfig, args=["@", str(tmp_path / "cfg.toml")])
assert config.variant.type == "b"
assert config.variant.beta == 0.5
def test_cli_overrides_defaults():
"""CLI args override defaults."""
config = cli(DummyConfig, args=["--name", "my-run", "--seed", "7"])
assert config.name == "my-run"
assert config.seed == 7
assert config.nested.lr == 1e-4
def test_toml_overrides_defaults(tmp_path):
"""TOML overrides defaults."""
write_toml(tmp_path / "cfg.toml", {"name": "my-run", "seed": 7, "nested": {"lr": 3e-4}})
config = cli(DummyConfig, args=["@", str(tmp_path / "cfg.toml")])
assert config.name == "my-run"
assert config.seed == 7
assert config.nested.lr == 3e-4
def test_cli_overrides_toml(tmp_path):
"""CLI args override TOML."""
write_toml(tmp_path / "cfg.toml", {"seed": 1, "nested": {"lr": 3e-4}})
config = cli(DummyConfig, args=["@", str(tmp_path / "cfg.toml"), "--seed", "99", "--nested.lr", "5e-5"])
assert config.seed == 99
assert config.nested.lr == 5e-5
# TOML value not overridden by CLI should still be applied (not reverted to class default)
assert config.nested.weight_decay == 0.01
def test_removed_fused_lm_head_chunk_size_field_is_rejected():
with pytest.raises(ValidationError, match="fused_lm_head_chunk_size"):
TrainerModelConfig.model_validate({"fused_lm_head_chunk_size": "auto"})
def test_to_toml_dict_roundtrips_explicit_none(tmp_path):
"""An explicit None override survives the write/re-parse round-trip used by SLURM launches."""
config = cli(TrainerConfig, args=["--model.compile", "None", "--optim.max_norm", "None"])
assert config.model.compile is None
assert config.optim.max_norm is None
write_toml(tmp_path / "cfg.toml", to_toml_dict(config))
reloaded = cli(TrainerConfig, args=["@", str(tmp_path / "cfg.toml")])
assert reloaded.model.compile is None
assert reloaded.optim.max_norm is None
assert reloaded == config
# Unset None fields stay dropped, so defaults still resolve on re-parse
assert "max_steps" not in to_toml_dict(cli(TrainerConfig, args=[]))
def test_env_algo_overrides_top_level():
config = OrchestratorConfig.model_validate(
{
"renderer": {"name": "qwen3"}, # echo needs the renderer's role attribution
"algo": {"type": "echo"},
"train": {"env": [{"id": "a", "algo": {"type": "grpo"}}, {"id": "b"}]},
}
)
env_a, env_b = config.train.env
# Env a sets its own algorithm; only env b inherits the top-level echo algorithm.
assert env_a.algo is not None and env_a.algo.type == "grpo"
assert env_b.algo is not None and env_b.algo.type == "echo"
# Resolved configs round-trip.
dumped = config.model_dump(exclude_none=True)
reloaded = OrchestratorConfig.model_validate(dumped)
assert reloaded.train.env[0].algo is not None and reloaded.train.env[0].algo.type == "grpo"
def test_trainer_enable_token_export_cli_flag():
assert not cli(TrainerConfig, args=[]).enable_token_export
assert cli(TrainerConfig, args=["--enable-token-export"]).enable_token_export
def test_single_node_auto_inference_client_dp_rank_count_matches_local_dp():
config = RLConfig.model_validate(
{
"trainer": {},
"orchestrator": {},
"inference": {"parallel": {"tp": 1}},
"deployment": {
"type": "single_node",
"gpus_per_node": 4,
"num_train_gpus": 2,
"num_infer_gpus": 2,
},
}
)
assert config.inference is not None
assert config.inference.parallel.dp == 2
assert config.orchestrator.model.client.dp_rank_count == 2
def test_multi_node_auto_inference_client_dp_rank_count_uses_router_url():
config = RLConfig.model_validate(
{
"trainer": {},
"orchestrator": {},
"inference": {"parallel": {"tp": 4}},
"deployment": {
"type": "multi_node",
"gpus_per_node": 8,
"num_train_nodes": 1,
"num_infer_nodes": 2,
},
"slurm": {},
}
)
assert config.inference is not None
assert config.inference.data_parallel_size_local == 2
assert config.inference.parallel.dp == 2
assert config.orchestrator.model.client.dp_rank_count == 1
def test_orchestrator_vlm_requires_renderer():
with pytest.raises(ValidationError, match="renderer"):
OrchestratorConfig.model_validate(
{
"model": {
"name": "Qwen/Qwen3-VL-4B-Instruct",
"vlm": {
"vision_encoder_attr": "model.visual",
"language_model_attr": "model.language_model",
},
},
"renderer": None,
}
)
config = OrchestratorConfig.model_validate(
{
"model": {
"name": "Qwen/Qwen3-VL-4B-Instruct",
"vlm": {
"vision_encoder_attr": "model.visual",
"language_model_attr": "model.language_model",
},
},
}
)
assert config.renderer is not None
def test_trainer_rejects_vlm_cp_with_ring():
config = {
"model": {
"cp": 2,
"impl": "custom",
"optimization_dtype": "bfloat16",
"reduce_dtype": "bfloat16",
"vlm": {
"vision_encoder_attr": "model.visual",
"language_model_attr": "model.language_model",
},
},
}
with pytest.raises(ValidationError, match="cp_style='ulysses'"):
TrainerConfig.model_validate(config)
def test_selective_activation_checkpointing_requires_custom_impl():
with pytest.raises(ValidationError, match="Selective activation checkpointing requires model.impl='custom'"):
TrainerModelConfig.model_validate({"impl": "hf", "ac": {"mode": "selective"}})
def test_shared_model_name_propagates_to_subconfigs():
model_name = "PrimeIntellect/test-model"
config = RLConfig.model_validate(
{
"model": {"name": model_name},
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}},
"inference": {},
}
)
assert config.trainer.model.name == model_name
assert config.orchestrator.model.name == model_name
assert config.inference is not None and config.inference.model.name == model_name
assert config.trainer.tokenizer.name == model_name
assert config.orchestrator.tokenizer.name == model_name
def test_shared_tokenizer_propagates_when_subconfigs_unset():
config = RLConfig.model_validate(
{
"model": {"name": "my-model"},
"tokenizer": {"name": "my-tokenizer"},
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}},
}
)
assert config.trainer.tokenizer.name == "my-tokenizer"
assert config.orchestrator.tokenizer.name == "my-tokenizer"
def test_shared_and_sub_tokenizer_name_conflict_raises():
"""Setting tokenizer.name in both [tokenizer] and [trainer.tokenizer]
is a config conflict — the sub-config would silently win, and any later
CLI override of [tokenizer].name would silently no-op for the trainer."""
with pytest.raises(ValidationError, match=r"tokenizer.name.*trainer.tokenizer.name"):
RLConfig.model_validate(
{
"model": {"name": "my-model"},
"tokenizer": {"name": "shared-tok"},
"trainer": {"tokenizer": {"name": "trainer-tok"}},
"orchestrator": {"renderer": {"name": "default"}},
}
)
def test_tokenizer_name_falls_back_to_model_name_when_unset():
config = RLConfig.model_validate(
{
"model": {"name": "my-model"},
"tokenizer": {"trust_remote_code": True},
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}},
}
)
assert config.trainer.tokenizer.name == "my-model"
assert config.orchestrator.tokenizer.name == "my-model"
assert config.trainer.tokenizer.trust_remote_code is True
assert config.orchestrator.tokenizer.trust_remote_code is True
def test_explicit_subconfig_tokenizer_name_survives_shared_model_propagation():
"""Regression: shared ``[model] name = "M"`` must propagate model names but
must NOT clobber an explicit ``[orchestrator.tokenizer] name = "T"``.
This is the case that the old RL-level ``auto_setup_tokenizer`` fix-up got
wrong: it unconditionally re-derived ``orchestrator.tokenizer.name`` from
``orchestrator.model.name`` after propagation, silently overriding
the user's explicit value. The ``mode="before"`` ``auto_setup_shared_configs``
propagator fixes this because it propagates the model name into the raw
dict before sub-configs are built, so ``OrchestratorConfig``'s own
``auto_setup_tokenizer`` (mode=after) sees the resolved name *and* the
explicit user-set tokenizer name, and the ``fill``-if-absent semantic
leaves the explicit value alone.
"""
config = RLConfig.model_validate(
{
"model": {"name": "M"},
"trainer": {},
"orchestrator": {
"renderer": {"name": "default"},
"tokenizer": {"name": "explicit-orch-tok"},
},
}
)
# Shared model.name reached every sub-config that didn't override it.
assert config.trainer.model.name == "M"
assert config.orchestrator.model.name == "M"
# Trainer didn't specify a tokenizer, so it falls back to the propagated model name.
assert config.trainer.tokenizer.name == "M"
# Orchestrator's explicit tokenizer name survived.
assert config.orchestrator.tokenizer.name == "explicit-orch-tok"
def test_tokenizer_chat_template_mismatch_raises():
with pytest.raises(ValidationError, match="chat_template"):
RLConfig.model_validate(
{
"trainer": {"tokenizer": {"chat_template": "A"}},
"orchestrator": {"renderer": {"name": "default"}, "tokenizer": {"chat_template": "B"}},
}
)
def test_shared_seq_len_propagates_to_subconfigs():
config = RLConfig.model_validate(
{
"seq_len": 4096,
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}},
}
)
assert config.trainer.model.seq_len == 4096
assert config.orchestrator.seq_len == 4096
def test_shared_and_sub_seq_len_conflict_raises():
"""Setting seq_len at the shared level and on a sub-config is a conflict —
forces the user to pick one place to express the value rather than
relying on the silent 'sub wins' rule."""
with pytest.raises(ValidationError, match=r"seq_len.*trainer.model.seq_len"):
RLConfig.model_validate(
{
"seq_len": 4096,
"trainer": {"model": {"seq_len": 8192}},
"orchestrator": {"renderer": {"name": "default"}},
}
)
def test_shared_and_sub_model_name_conflict_raises():
"""Setting model.name at the shared level and on a sub-config is a conflict."""
with pytest.raises(ValidationError, match=r"model.name.*trainer.model.name"):
RLConfig.model_validate(
{
"model": {"name": "X"},
"trainer": {"model": {"name": "Y"}},
"orchestrator": {"renderer": {"name": "default"}},
}
)
def test_shared_and_sub_max_steps_conflict_raises():
"""Top-level scalar shared fields also participate in the mutex check."""
with pytest.raises(ValidationError, match=r"max_steps.*orchestrator.max_steps"):
RLConfig.model_validate(
{
"max_steps": 100,
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}, "max_steps": 200},
}
)
def test_trainer_chat_template_cascades_to_inference():
"""``[trainer.tokenizer] chat_template`` set directly (no shared
``[tokenizer] chat_template``) must still reach
``inference.model.chat_template`` so vLLM's ``--chat-template`` is wired
up. Regression: the original ``auto_setup_tokenizer`` cascaded this; the
refactored propagator must keep doing it."""
config = RLConfig.model_validate(
{
"model": {"name": "Qwen/Qwen3-0.6B"},
"trainer": {"tokenizer": {"chat_template": "TPL"}},
"orchestrator": {"renderer": {"name": "default"}, "tokenizer": {"chat_template": "TPL"}},
"inference": {},
}
)
assert config.trainer.tokenizer.chat_template == "TPL"
assert config.orchestrator.tokenizer.chat_template == "TPL"
assert config.inference is not None
assert config.inference.model.chat_template == "TPL"
def test_shared_wandb_fields_propagate_to_subconfigs():
"""Every ``SharedWandbConfig`` leaf (project, entity, name, group, tags,
offline) propagates to both trainer.wandb and orchestrator.wandb. Regression
for a miss in the inline propagator."""
config = RLConfig.model_validate(
{
"model": {"name": "Qwen/Qwen3-0.6B"},
"wandb": {
"project": "shared-proj",
"entity": "shared-entity",
"name": "shared-name",
"group": "shared-group",
"tags": ["a", "b"],
"offline": False,
},
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}},
}
)
for component in (config.trainer.wandb, config.orchestrator.wandb):
assert component is not None
assert component.project == "shared-proj"
assert component.entity == "shared-entity"
assert component.name == "shared-name"
assert component.group == "shared-group"
assert component.tags == ["a", "b"]
assert component.offline is False
def test_empty_shared_ckpt_block_does_not_conflict_with_subconfig_ckpt():
"""An empty shared [ckpt] block is a presence-only signal, not a field
setting — it should not conflict with a non-empty [trainer.ckpt]."""
config = RLConfig.model_validate(
{
"ckpt": {}, # empty block, no field set
"trainer": {"ckpt": {"interval": 50}},
"orchestrator": {"renderer": {"name": "default"}, "ckpt": {"interval": 50}},
}
)
assert config.trainer.ckpt is not None
assert config.trainer.ckpt.interval == 50
def test_shared_and_subconfig_disjoint_fields_coexist():
"""Per-field mutex only forbids conflicts on the SAME field — disjoint
fields in [model] vs [trainer.model] are fine."""
config = RLConfig.model_validate(
{
"model": {"name": "Qwen/Qwen3-0.6B"},
"trainer": {"model": {"impl": "custom"}},
"orchestrator": {"renderer": {"name": "default"}},
}
)
assert config.trainer.model.name == "Qwen/Qwen3-0.6B"
assert config.trainer.model.impl == "custom"
def test_shared_output_dir_propagates_through_cli(tmp_path):
"""Shared output_dir from CLI reaches sub-configs even when tyro constructs sub-configs before the before-validator."""
toml_path = tmp_path / "cfg.toml"
write_toml(
toml_path,
{
"max_steps": 1,
"seq_len": 128,
"model": {"name": "Qwen/Qwen3-0.6B"},
"trainer": {},
"orchestrator": {"batch_size": 16, "group_size": 1},
"inference": {},
},
)
shared_out = tmp_path / "shared"
config = cli(RLConfig, args=["@", str(toml_path), "--output-dir", str(shared_out)])
assert config.trainer.output_dir == shared_out
assert config.orchestrator.output_dir == shared_out / "run_default"
def test_orchestrator_renderer_auto_rejects_unmapped_model():
"""Default ``renderer`` (AutoRendererConfig) must reject models not in MODEL_RENDERER_MAP."""
with pytest.raises(ValidationError, match="silently fall back to DefaultRenderer"):
OrchestratorConfig.model_validate({"model": {"name": "not-a-real-org/not-a-real-model"}})
def test_orchestrator_renderer_auto_accepts_mapped_model():
"""The default Qwen model is in MODEL_RENDERER_MAP and should validate cleanly."""
config = OrchestratorConfig.model_validate({"model": {"name": "Qwen/Qwen3-0.6B"}})
assert config.renderer is not None
assert config.renderer.name == "auto"
def test_sft_renderer_auto_accepts_prime_qwen_model():
config = SFTConfig.model_validate({"model": {"name": "PrimeIntellect/Qwen3-0.6B"}})
assert config.renderer.name == "auto"
def test_sft_rejects_default_renderer_for_real_data():
with pytest.raises(ValidationError, match="requires a typed renderer"):
SFTConfig.model_validate({"renderer": {"name": "default"}})
def test_sft_allows_unused_default_renderer_for_fake_data():
config = SFTConfig.model_validate(
{
"data": {"type": "fake"},
"renderer": {"name": "default"},
}
)
assert config.renderer.name == "default"
def test_orchestrator_explicit_renderer_skips_unmapped_check():
"""Explicit renderer.name bypasses the auto-resolution check — user opted in."""
config = OrchestratorConfig.model_validate(
{
"model": {"name": "not-a-real-org/not-a-real-model"},
"renderer": {"name": "qwen3"},
}
)
assert config.renderer is not None
assert config.renderer.name == "qwen3"
def test_orchestrator_renderer_none_rejected():
"""A renderer is required (training is renderer-only): the non-optional type rejects None."""
with pytest.raises(ValidationError, match="renderer"):
OrchestratorConfig.model_validate(
{
"model": {"name": "not-a-real-org/not-a-real-model"},
"renderer": None,
}
)
def test_orchestrator_explicit_default_renderer_with_unmapped_model():
"""renderer.name='default' is an explicit opt-in to DefaultRenderer and must pass."""
config = OrchestratorConfig.model_validate(
{
"model": {"name": "not-a-real-org/not-a-real-model"},
"renderer": {"name": "default", "tool_parser": "qwen3"},
}
)
assert config.renderer is not None
assert config.renderer.name == "default"
assert config.renderer.tool_parser == "qwen3"
def test_shared_model_name_resolves_inference_parsers():
"""Shared [model] name must reach inference.model BEFORE ModelConfig's after-validator
runs auto_resolve_parsers — i.e. the parsers resolve from the propagated name, not
from an empty default.
"""
config = RLConfig.model_validate(
{
"model": {"name": "Qwen/Qwen3-Coder-30B-A3B-Instruct"},
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}},
"inference": {},
}
)
assert config.inference is not None
assert config.inference.model.name == "Qwen/Qwen3-Coder-30B-A3B-Instruct"
assert config.inference.model.tool_call_parser == "qwen3_coder"
def test_explicit_inference_parser_wins_over_auto():
"""Explicit inference.model.tool_call_parser is preserved even when the shared model
name would otherwise auto-resolve to something else."""
config = RLConfig.model_validate(
{
"model": {"name": "Qwen/Qwen3-Coder-30B-A3B-Instruct"},
"trainer": {},
"orchestrator": {"renderer": {"name": "default"}},
"inference": {"model": {"tool_call_parser": "hermes"}},
}
)
assert config.inference is not None
assert config.inference.model.tool_call_parser == "hermes"