Files
trinity-rl/envs/nemotron_guard/nemotron_guard_v1/mixing.py
T
carstenandClaude Opus 5.5 609e832572 Move to current prime-rl, verifiers and renderers; DeepSeek V4.1 support
Bump prime-rl to trinity-dsv41: upstream main, PR #4057 (DeepSeek V4.1
Flash) with the engram port onto the shared n-gram module, and renderers
tracking its trinity-dsv41 branch (v0.2.0 + the Hermes parser).

Port the envs to the current verifiers v1 API, rl.toml to the new config
schema (IPO loss kept as the new default), and bootstrap/launch/prepare
scripts to the new layout.

Decensor now runs DecensorEnv: the simulated user writes reviewed
follow-ups up to each row's target_turns and every assistant turn is
judged. Earlier runs trained on the first turn only.
tests/test_multiturn_env.py drives the full loop against a mock endpoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 14:39:44 +00:00

148 lines
5.9 KiB
Python

"""Per-subtask blend control, shared by the four nemotron category tasksets.
Without this, `load()` concatenates every row of every subtask and the training mix is
dictated by raw source size: `nemotron-reasoning` ran 83% science / 2% math purely because
the science file is 40x larger, and `nemotron-tool-calling` ran 41% of an unlearnable
subtask against 4% of its best-signal one.
The design follows NeMo-RL's `use_multiple_dataloader` / `num_prompts_per_dataloader`
(one cycling iterator per source, a per-step quota, small sources repeat rather than
starve) and NeMo-Gym's integer `num_repeats`. The adaptation: our tasksets build a fixed
pool once in `load()` rather than sampling per step, so a quota over the pool produces the
same blend — the orchestrator draws from the pool uniformly.
"""
from __future__ import annotations
import random
from typing import Iterable, TypeVar
from pydantic import BaseModel, Field, model_validator
T = TypeVar("T")
class SubtaskMix(BaseModel):
"""Blend control for one subtask. All fields are optional; the default is a no-op."""
weight: float | None = None
"""Share of the loaded pool, relative to the other weights. Mutually exclusive with
`max_examples`. Subtasks with fewer rows than their quota are cycled, not truncated."""
max_examples: int | None = Field(default=None, ge=0)
"""Hard cap on rows kept from this subtask, applied after the seeded shuffle so it is a
uniform sample rather than a file-order prefix."""
repeat: int = Field(default=1, ge=1)
"""Integer upsampling applied after the cap."""
enabled: bool = True
"""False drops the subtask entirely — the config-level way to retire a dead slice."""
@model_validator(mode="after")
def _reject_weight_with_cap(self) -> SubtaskMix:
if self.weight is not None and self.max_examples is not None:
raise ValueError("set `weight` or `max_examples`, not both")
if self.weight is not None and self.weight < 0:
raise ValueError("`weight` must be non-negative")
return self
def _quotas(weights: dict[str, float], total: int) -> dict[str, int]:
"""Split `total` across `weights` by largest remainder, so the parts sum exactly."""
scale = sum(weights.values())
if scale <= 0:
return {name: 0 for name in weights}
exact = {name: total * weight / scale for name, weight in weights.items()}
counts = {name: int(value) for name, value in exact.items()}
remainder = total - sum(counts.values())
for name in sorted(exact, key=lambda n: exact[n] - counts[n], reverse=True)[:remainder]:
counts[name] += 1
return counts
def _cycle(rows: list[T], count: int) -> list[T]:
"""Take `count` rows, repeating the pool when it is smaller than the quota."""
if not rows or count <= 0:
return []
out = list(rows) * (count // len(rows))
return out + rows[: count - len(out)]
def apply_mix(
rows_by_subtask: dict[str, list[T]],
mix: dict[str, SubtaskMix],
seed: int,
total: int | None = None,
) -> list[T]:
"""Blend per-subtask row lists into one pool.
With no `mix` entries this is plain concatenation — today's behavior. Entries carrying
`weight` claim a share of `total` (defaulting to the number of rows available); entries
without one keep their rows subject to `max_examples` / `repeat`.
"""
kept = {
name: rows
for name, rows in rows_by_subtask.items()
if rows and mix.get(name, SubtaskMix()).enabled
}
for name, rows in kept.items():
random.Random(seed + hash(name) % 10_000).shuffle(rows)
weighted = {name: m.weight for name in kept if (m := mix.get(name)) and m.weight is not None}
pool: list[T] = []
if weighted:
budget = total if total is not None else sum(len(rows) for rows in kept.values())
for name, count in _quotas(weighted, budget).items():
pool.extend(_cycle(kept[name], count))
for name, rows in kept.items():
if name in weighted:
continue
settings = mix.get(name, SubtaskMix())
capped = rows[: settings.max_examples] if settings.max_examples is not None else rows
pool.extend(capped * settings.repeat)
random.Random(seed).shuffle(pool)
return pool
def mix_summary(rows_by_subtask: dict[str, list[T]], pool: Iterable[T]) -> dict[str, int]:
"""Realized per-subtask counts, for logging what the blend actually produced.
Counts by which input list a row came from, by identity — not by any field on the row.
A taskset's loader key and the `subtask` it stamps on the data need not match (the
instruction-following env loads `structured_v2_direct` and stamps
`structured_v2_direct_generation`), and keying off the field reports phantom zeros.
"""
origin = {id(row): name for name, rows in rows_by_subtask.items() for row in rows}
counts = {name: 0 for name in rows_by_subtask}
for row in pool:
name = origin.get(id(row))
if name is not None:
counts[name] += 1
return counts
def distinct_task_ids(tasks: list[T]) -> list[T]:
"""Give every repeated task a distinct `TaskData.id` so task keys stay unique.
Verifiers keys a task by a hash of its data (minus `idx`), and the trainer's sampler
refuses a taskset whose keys collide. `apply_mix` deliberately repeats rows (cycling a
small subtask up to its quota, `repeat`), and a source can carry identical rows, so
the n-th copy of a row is stamped `#n` — the first keeps its data, and the pool's
size, order and blend are unchanged. Deterministic, since the pool order is.
"""
seen: dict[str, int] = {}
out: list[T] = []
for task in tasks:
key = task.key
copy = seen.get(key, 0)
seen[key] = copy + 1
if copy:
base = task.data.id
task = task.with_data(id=f"{base}#{copy}" if base else f"#{copy}")
out.append(task)
return out