Run this model with HF Jobs
Run Codeseys/composer-replication-framework on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/Codeseys/composer-replication-framework. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/Codeseys/composer-replication-framework on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use Codeseys/composer-replication-framework with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Codeseys/composer-replication-framework with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Codeseys/composer-replication-framework", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Wave 14: close every Wave 13 review finding + 4 documentation files; Wave 14b: real PRIME-RL parity + multi-process DiLoCo convergence
Browse filesPHASE A: 4 parallel impl subagents closed every Wave 13 cross-model review item.
T1 \u2014 compose_loss integration (closes W13 BLOCKER 2):
- Added `dpo_variant: 'dpo'|'simpo'`, `sdpo_wrapper: 'none'|'taid'|'entropy_opd'`,
`taid_schedule_step`, `taid_total_steps` plus 5 tuning kwargs
- 11 new integration tests in composer_replication/tests/test_compose_loss_integration.py
- Bit-exact reproduction of legacy compose_loss output when defaults preserved
- All 38 spike-005 tests still pass
T2 \u2014 replaysim DJ adapter reshape (closes W13 Suggestion 3):
- _dpo_pair_to_dj_record now emits BOTH flat strings AND chat-messages
for dual-shape compatibility with data-juicer's text ops
- _dj_record_to_normalized round-trips both shapes
- default.yaml fixed: text_keys plural \u2192 text_key singular (caught a
related bug in the same op set)
- Real data-juicer e2e test runs DefaultExecutor on a 3-record fixture
- 15 tests (was 9)
T3 \u2014 MockManager DiLoCo integration (closes W13 Suggestion 4):
- Audited torchft Manager surface; added 6 missing methods:
current_step, disallow_state_dict_read, allow_state_dict_read,
register_state_dict_fn, _use_async_quorum, is_leader
- _ImmediateWork wraps allreduce return so DiLoCo can call .wait()
- New integration test runs make_diloco_outer_loop(MockManager(store), nn.Linear)
end-to-end and verifies model parameters change
T4 \u2014 PRIME-RL real GRPO (initial, BUGGY \u2014 see Wave 14b for fix):
- Wave 14 first attempt got the formula wrong; documented in
docs/research/WAVE_14_FINAL_REVIEW.md and re-fixed in Wave 14b.
PHASE B: 4 parallel doc subagents produced 3,930 lines:
- docs/USER_GUIDE.md (670 lines) \u2014 8-section end-to-end narrative
- docs/API_REFERENCE.md (1471 lines) \u2014 every public symbol with
signature + params + return + raises + example, marked tested vs
\u26a0\ufe0f untested vs \ud83d\udfe1 skeleton
- docs/TROUBLESHOOTING.md (797 lines) \u2014 11 failure modes with
SYMPTOM/DIAGNOSIS/FIX/VERIFICATION
- docs/INTEGRATION_RECIPES.md (993 lines) \u2014 5 recipes (TRL/VeRL/
PRIME-RL/serverless/Monarch) with 7-part template + comparison matrix
PHASE C: cross-model adversarial review (Opus 4.7 sub-agent) cloned
PRIME-RL upstream and verified T4's implementation. Found 1 BLOCKER:
T4 thought it matched PRIME-RL but didn't:
- Mask gate was on log_ratio, should be on probs_diff (probability-space)
- Missing importance_ratio multiplication (was REINFORCE)
- Missing advantage-sign-conditioned mask
- Missing KL term
- Wrong defaults (4.0/-4.0 vs PRIME-RL's actual 0.2/0.2)
- Plus 4 SUGGESTIONs.
PHASE C2 (Wave 14b): 2 parallel subagents closed everything.
Subagent 1 re-implemented PRIME-RL composer_loss against upstream:
- Verified formula in /tmp/prime-rl-clone/src/prime_rl/trainer/rl/loss.py
default_loss_fn (lines 116-165) and DefaultLossConfig (412-425)
- Now byte-for-byte matches PRIME-RL: probs_diff masking,
importance_ratio multiplication, advantage-sign-conditioned mask, KL term
- Defaults corrected: dppo_mask_high=0.2, dppo_mask_low=0.2,
adv_tau=1.0, kl_tau=1e-3
- 16 tests including parity test that imports PRIME-RL's default_loss_fn
(skip-marked when prime-rl not installed)
- Updated docstring + USER_GUIDE / API_REFERENCE / TROUBLESHOOTING /
prime_rl_recipe.md / prime_rl_config.yaml all repaired
- FLAGGED for Wave 15: PRIME-RL's setup_loss_fns expects LossOutputs(loss,
metrics) return shape, not bare scalar \u2014 separate adapter-level issue
Subagent 2 closed 3 doc/test SUGGESTIONs:
- ADR-007 updated to reflect Wave 14 closure of compose_loss integration
- INTEGRATION_RECIPES.md: 4 ModalExecutor/HFJobsExecutor dead-code
constructor calls fixed (substituted LocalProcessExecutor)
- New multi-process MockManager+DiLoCo convergence test:
spawns 2 replicas, runs 1 outer round, asserts both replicas converge
to identical weights end-to-end. Test design corrected after initial
attempt: shared-init-seed + rank-specific-data is canonical DiLoCo
setup (not rank-specific-init which is divergent by design).
DOCUMENTATION INDEX (post-Wave-14b):
- README.md (Wave 13 expansion section + roadmap)
- docs/USER_GUIDE.md (start-here narrative)
- docs/API_REFERENCE.md (every public symbol)
- docs/INTEGRATION_RECIPES.md (5 recipes)
- docs/TROUBLESHOOTING.md (11 failure modes + bug-report template)
- docs/V1_V8_COVERAGE.md (brief coverage matrix)
- docs/V3_SUBSTRATE_COVERAGE.md (substrate matrix, 8/8 covered)
- docs/VISION_VALIDATION.md (10-point scorecard)
- docs/ALTERED_MINDS_TIE_IN.md (workstream bridge)
- docs/adrs/ADR-001..007 (7 architectural decisions)
- docs/research/WAVE_7_10_FINAL_REVIEW.md (Wave 11 cross-model review)
- docs/research/WAVE_13_FINAL_REVIEW.md (Wave 13 cross-model review)
- docs/research/WAVE_14_FINAL_REVIEW.md (Wave 14 + 14b cross-model review)
- docs/research/{DILOCO_RECONNAISSANCE, DILOCO_SERVERLESS_RECONNAISSANCE,
MODAL_RECONNAISSANCE, REPLAYSIM_NORMALIZATION_RECONNAISSANCE,
RL_FRAMEWORKS_LANDSCAPE, SELF_DISTILLATION_LANDSCAPE,
TRACE_SOURCE_RECONNAISSANCE}.md (primary-source recons, ~3,300 lines)
TESTS: 130 passing + 1 skip-marked (PRIME-RL parity test runs when
prime-rl installed). Was 93 at end of Wave 13.
NO REGRESSIONS: every prior wave's tests still pass. New code is
either purely additive (distillation, replaysim, serverless DiLoCo) or
backward-compatible (compose_loss kwargs default to legacy behavior).
Files changed (18)
- composer_replication/diloco/serverless/allreduce.py+122-10
- composer_replication/diloco/serverless/tests/test_serverless_diloco_integration.pyAdded+352
- composer_replication/diloco/serverless/tests/test_serverless_local.py+15-4
- composer_replication/loss.py+209-25
- composer_replication/recipes/prime_rl/composer_loss.py+213-73
- composer_replication/recipes/prime_rl/prime_rl_config.yaml+17-3
- composer_replication/recipes/prime_rl/prime_rl_recipe.md+90-22
- composer_replication/recipes/prime_rl/tests/test_composer_loss.pyAdded+484
- composer_replication/recipes/replaysim/default.yaml+49-15
- composer_replication/replaysim/normalize.py+53-7
- composer_replication/replaysim/tests/test_replaysim.py+191-2
- composer_replication/tests/test_compose_loss_integration.pyAdded+416
- docs/API_REFERENCE.mdAdded+1484
- docs/INTEGRATION_RECIPES.mdAdded+998
- docs/TROUBLESHOOTING.mdAdded+823
- docs/USER_GUIDE.mdAdded+683
- docs/adrs/ADR-007-self-distillation-losses.md+46-22
- docs/research/WAVE_14_FINAL_REVIEW.mdAdded+264
175 unmodified lines1761771781791801816 unmodified lines188189190191192193194195196197198199200201202203204205206207208209210211212213214175 unmodified lines# ---------------------------------------------------------------------class MockManager:"""Drop-in replacement for `torchft.Manager` that delegates allreduceto `ObjectStoreAllReduce`.6 unmodified linesReference: `make_diloco_outer_loop` in`composer_replication/diloco/__init__.py` accepts an optional`manager=` kwarg; pass a `MockManager` to enable serverless DiLoCo."""def __init__(self, store: ObjectStoreAllReduce) -> None:self._store = store# torchft Manager attributes that DiLoCo consultsself.num_participants = store.world_sizeself.rank = store.rankdef allreduce(self, tensor: torch.Tensor, **_kwargs: Any) -> torch.Tensor:return self._store.allreduce(tensor)# torchft.Manager has additional methods (`should_commit`, `start_quorum`,# etc.) that are no-ops for our coarse-grained sync. The `DiLoCo` class# only requires `allreduce`, but the others may be probed.def should_commit(self) -> bool:return Truedef start_quorum(self) -> None:passdef wait_quorum(self) -> int:return self.num_participants__all__ = ["MockManager", "ObjectStoreAllReduce"]175 unmodified lines1761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162176 unmodified lines224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326175 unmodified lines# ---------------------------------------------------------------------class _ImmediateWork:"""Work-shaped wrapper for an already-completed allreduce.`torchft.Manager.allreduce` returns a `torch.distributed.Work` (or`torchft.work._DummyWork`) which DiLoCo calls `.wait()` on inside`_StreamingDiLoCoFragment.perform_sync`. Our `ObjectStoreAllReduce`is synchronous — by the time it returns, the average is already inthe tensor — so `.wait()` is a no-op.We deliberately don't subclass `torch.distributed._Work` to keep thismodule importable in environments without a full torch distributedbuild; DiLoCo only does `work.wait()`, nothing more."""__slots__ = ("_tensor",)def __init__(self, tensor: torch.Tensor) -> None:self._tensor = tensordef wait(self, *_args: Any, **_kwargs: Any) -> bool:return Truedef get_future(self) -> Any:# Torch >=2.x sometimes calls Work.get_future(); provide a satisfied# future so callers don't crash. We only need to be defensive here;# DiLoCo itself doesn't call this.try:import torch.futures as _ffut = _f.Future()fut.set_result(self._tensor)return futexcept Exception: # pragma: no cover — defensive onlyreturn Noneclass MockManager:"""Drop-in replacement for `torchft.Manager` that delegates allreduceto `ObjectStoreAllReduce`.6 unmodified linesReference: `make_diloco_outer_loop` in`composer_replication/diloco/__init__.py` accepts an optional`manager=` kwarg; pass a `MockManager` to enable serverless DiLoCo.torchft.Manager surface audited from``torchft/local_sgd.py`` (DiLoCo + _StreamingDiLoCoFragment paths) and``torchft/manager.py``. Methods/attributes DiLoCo touches:* ``allreduce(tensor, should_quantize=...) -> Work`` — must return anobject with ``.wait()`` (DiLoCo calls ``work.wait()`` in``perform_sync``).* ``should_commit() -> bool`` — gates the outer-optimizer step.* ``start_quorum()`` — called once per outer round, before``prepare_sync``.* ``current_step() -> int`` — used to pick the streaming-DiLoCofragment for this round (``step % len(fragments)``).* ``disallow_state_dict_read()`` / ``allow_state_dict_read()`` —called every inner step from the optimizer pre/post hooks.* ``register_state_dict_fn(key, load_fn, save_fn)`` — called onceper fragment from ``DiLoCo.__init__``.* ``_use_async_quorum`` (attribute) — DiLoCo's constructor refusesto start if this is truthy. Must exist and be False.* ``num_participants`` / ``rank`` — read by upstream callers."""def __init__(self, store: ObjectStoreAllReduce) -> None:self._store = store# torchft Manager attributes that DiLoCo consults at construction time# or in user code paths.self.num_participants = store.world_sizeself.rank = store.rank# DiLoCo.__init__ raises if this is truthy (line 622 of# torchft/local_sgd.py). Object-store sync is synchronous → False.self._use_async_quorum: bool = False# Mirror the upstream Manager's monotonic step counter. DiLoCo reads# this via current_step() to decide which fragment to sync each round.# Bumped from start_quorum() so it advances exactly once per outer round.self._step: int = 0# State-dict-fn registry: torchft uses this for fault-tolerant# checkpoint restore. We're single-shot serverless — record but never# invoke. Tests can introspect this dict to confirm registration.self._state_dict_fns: dict[str, tuple[Any, Any]] = {}# ---- Core collective ------------------------------------------------def allreduce(self, tensor: torch.Tensor, **_kwargs: Any) -> _ImmediateWork:# DiLoCo expects a Work-like return value (it stores it in a list# then calls .wait() later). Object-store all-reduce is synchronous,# so the tensor is already averaged when we hand back the wrapper.averaged = self._store.allreduce(tensor)return _ImmediateWork(averaged)# ---- Quorum / commit lifecycle -------------------------------------def should_commit(self) -> bool:# No fault-tolerance failover in serverless mode: every quorum# always commits. Replica failure is handled by the orchestration# layer (HF Jobs / Modal restart), not by DiLoCo skipping a round.return Truedef start_quorum(self) -> None:# The upstream Manager bumps its step counter inside the quorum# bookkeeping. Do the same so current_step() advances per round# and DiLoCo's fragment-rotation math matches across replicas.self._step += 1def wait_quorum(self) -> int:return self.num_participants# ---- Step counter ---------------------------------------------------def current_step(self) -> int:return self._step# ---- State-dict read gating ----------------------------------------# torchft uses these to make checkpoint restore thread-safe. In a# single-process serverless mock there's no concurrent reader, so they# are no-ops — but they MUST exist (DiLoCo's pre/post optimizer hooks# call them on every inner step).def allow_state_dict_read(self) -> None:passdef disallow_state_dict_read(self) -> None:pass# ---- Checkpoint hook registry --------------------------------------def register_state_dict_fn(self,key: str,load_fn: Any,save_fn: Any,) -> None:# DiLoCo registers one (load, save) pair per fragment so torchft can# checkpoint the outer-optimizer state and original-parameter backup.# In serverless mode we capture the registration so tests can verify# it happened, but never invoke it — there's no HA failover.self._state_dict_fns[key] = (load_fn, save_fn)# ---- Convenience ----------------------------------------------------def is_leader(self) -> bool:# Not strictly required by DiLoCo but referenced in some torchft# integrations / our own code that may swap MockManager in.return self.rank == 0__all__ = ["MockManager", "ObjectStoreAllReduce", "_ImmediateWork"]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352"""End-to-end MockManager × torchft.DiLoCo integration test.
Closes the Wave 13 cross-model adversarial-review gap (Suggestion 4):the original MockManager was advertised as a drop-in for torchft.Managerbut only stubbed `.allreduce / .should_commit / .start_quorum`. DiLoCo'sreal call surface (audited from `torchft/local_sgd.py` v2026-spring) alsoincludes `current_step()`, `disallow_state_dict_read()`,`allow_state_dict_read()`, `register_state_dict_fn()`, and the`_use_async_quorum` attribute — plus `allreduce()` must return a Work-likeobject with `.wait()`, not a raw tensor.
This test runs ONE full DiLoCo outer round (sync_every inner steps + thesync) against a tiny `nn.Linear(4, 4)` with `world_size=1` so theobject-store rendezvous is trivial. It verifies:
1. Construction does not raise.2. Running through one full outer round does not raise AttributeError (which is what the old MockManager would have hit at `current_step()`).3. The model parameters change after the outer step fires (proving the outer SGD path actually executed end-to-end, not just that the inner-step hooks ran).4. The MockManager's step counter advanced exactly once (one outer round ⇒ one start_quorum bump).5. DiLoCo registered a state-dict fn per fragment."""from __future__ import annotations
import pytestimport torch
torchft = pytest.importorskip( "torchft.local_sgd", reason="torchft must be installed to run the DiLoCo integration test",)
from composer_replication.diloco import make_diloco_outer_loopfrom composer_replication.diloco.serverless.allreduce import ( MockManager, ObjectStoreAllReduce, _ImmediateWork,)
def _make_store(tmp_path) -> ObjectStoreAllReduce: return ObjectStoreAllReduce( uri=str(tmp_path), rank=0, world_size=1, timeout_s=10.0, poll_interval_s=0.05, )
def test_mockmanager_has_full_diloco_call_surface(tmp_path): """Audited methods/attrs from torchft/local_sgd.py DiLoCo path must exist.""" mgr = MockManager(_make_store(tmp_path)) # Methods DiLoCo invokes for attr in ( "allreduce", "should_commit", "start_quorum", "current_step", "disallow_state_dict_read", "allow_state_dict_read", "register_state_dict_fn", "wait_quorum", "is_leader", ): assert callable(getattr(mgr, attr)), f"MockManager missing method: {attr}" # Attributes DiLoCo reads at construction / runtime assert hasattr(mgr, "_use_async_quorum") assert mgr._use_async_quorum is False # DiLoCo.__init__ rejects True assert hasattr(mgr, "num_participants") assert hasattr(mgr, "rank")
def test_mockmanager_allreduce_returns_workshaped(tmp_path): """DiLoCo stores the allreduce return in a list and calls `.wait()` later.""" mgr = MockManager(_make_store(tmp_path)) work = mgr.allreduce(torch.zeros(2, 2)) # It must look like torch.distributed.Work / torchft._DummyWork assert hasattr(work, "wait"), "allreduce return must have .wait() (DiLoCo calls it)" assert callable(work.wait) # No-op .wait() must not raise on a synchronous mock. assert work.wait() is True # Defensive: get_future() should also work (some torch paths probe it). fut = work.get_future() assert fut is None or hasattr(fut, "wait") # Concrete type assert isinstance(work, _ImmediateWork)
def test_mockmanager_diloco_outer_round_completes(tmp_path): """Run one full inner+outer DiLoCo round and verify params change.
With world_size=1 + MockManager → ObjectStoreAllReduce(file://), the rendezvous is single-process, so this test runs synchronously. We use `sync_every=4` and run exactly 4 inner-optimizer steps; at the 4th step DiLoCo's post-hook fires `prepare_sync` then `perform_sync`, exercising the entire MockManager surface. """ torch.manual_seed(0) model = torch.nn.Linear(4, 4, bias=False) initial_params = model.weight.detach().clone()
inner_optim = torch.optim.SGD(model.parameters(), lr=0.1)
store = _make_store(tmp_path) manager = MockManager(store)
diloco = make_diloco_outer_loop( manager=manager, model_fragments=[model], inner_optimizer=inner_optim, outer_lr=0.7, outer_momentum=0.9, nesterov=True, sync_every=4, fragment_sync_delay=0, fragment_update_alpha=0.0, )
# Sanity: DiLoCo registered a state-dict fn for our single fragment. assert len(manager._state_dict_fns) == 1, ( f"expected 1 fragment registration, got {list(manager._state_dict_fns)}" )
x = torch.randn(2, 4) target = torch.randn(2, 4)
with diloco: for _ in range(4): # exactly sync_every inner steps → one outer round inner_optim.zero_grad() loss = ((model(x) - target) ** 2).mean() loss.backward() # Must NOT raise AttributeError on current_step / state_dict_read / # register_state_dict_fn / etc. The original MockManager would have # crashed here on the very first step's _step_pre_hook calling # disallow_state_dict_read. inner_optim.step()
# After exactly one outer round, the MockManager's step counter # should have advanced exactly once (start_quorum is called once). assert manager.current_step() == 1, ( f"expected current_step()==1 after one outer round, got {manager.current_step()}" )
# The outer SGD step actually fired ⇒ params differ from initial. final_params = model.weight.detach().clone() assert not torch.allclose(initial_params, final_params), ( "model params unchanged after outer round — outer optimizer never ran" )
def _diloco_replica_one_outer_round( rendezvous_uri: str, world_size: int, sync_every: int,) -> dict: """Top-level entry — must be importable for multiprocessing 'spawn'.
Each replica: 1. seeds torch with a SHARED seed for model init (DiLoCo's standard assumption: all replicas start with identical weights — DiLoCo only averages pseudo-gradients, not absolute weights, so divergent inits would never reconcile). 2. builds nn.Linear(4, 4, bias=False) + SGD inner optimizer. 3. trains on RANK-SPECIFIC data so each replica's inner-trained weights diverge during the inner loop (this is what gives the pseudo-gradient real cross-rank variance — without it, the averaging is observationally a no-op). 4. runs `sync_every` inner steps inside `make_diloco_outer_loop` — this fires exactly one outer round. 5. returns the final flattened weight vector and the pre-outer (purely-inner) weights.
The test then asserts both ranks' final weights are identical (allclose), which proves the cross-replica allreduce of the pseudo-gradient ran end-to-end. The pre-outer weights MUST differ across ranks (proving rank-specific data drove divergence in the inner loop) — otherwise the convergence assertion is vacuous. """ import os as _os import torch as _torch import torch.nn as _nn
from composer_replication.diloco import make_diloco_outer_loop from composer_replication.diloco.serverless.allreduce import ( MockManager, ObjectStoreAllReduce, )
rank = int(_os.environ["REPLICA_RANK"])
# SHARED init seed — both replicas start with identical weights, as # DiLoCo assumes. (DiLoCo averages pseudo-gradients, not weights, so # divergent inits would never reconcile and the convergence claim # would be incorrect.) _torch.manual_seed(0) model = _nn.Linear(4, 4, bias=False) initial = model.weight.detach().clone()
inner_optim = _torch.optim.SGD(model.parameters(), lr=0.1)
store = ObjectStoreAllReduce( rendezvous_uri, rank=rank, world_size=world_size, timeout_s=120.0, poll_interval_s=0.05, ) manager = MockManager(store)
diloco = make_diloco_outer_loop( manager=manager, model_fragments=[model], inner_optimizer=inner_optim, sync_every=sync_every, )
# RANK-SPECIFIC data so the inner-trained weights diverge before the # outer sync — this is what makes "post-sync convergence" a real # property to verify rather than a tautology. _torch.manual_seed(100 + rank) x = _torch.randn(2, 4) target = _torch.randn(2, 4)
with diloco: for _ in range(sync_every): inner_optim.zero_grad() loss = ((model(x) - target) ** 2).mean() loss.backward() inner_optim.step()
final = model.weight.detach().clone() return { "rank": rank, "initial": initial.flatten().tolist(), "final": final.flatten().tolist(), "current_step": manager.current_step(), }
def test_mockmanager_diloco_multi_process_weights_converge(tmp_path): """Wave 14 (Suggestion 4): cross-replica weight convergence after one outer round.
Spawns n_replicas=2 subprocesses with IDENTICAL initial weights (DiLoCo's standard assumption — it averages pseudo-gradients, not absolute weights) but RANK-SPECIFIC training data. After exactly one DiLoCo outer round, both replicas must end with IDENTICAL weights, because:
pseudo_grad_i = init - inner_trained_i # per-rank, differ avg_pseudo = mean_i(pseudo_grad_i) # same on all ranks final = init - outer_lr * avg_pseudo # same on all ranks
This catches averaging-direction bugs that the world_size=1 single-process test silently misses (a single-rank allreduce is a no-op and can hide bugs in the multi-rank averaging arithmetic, the file-staging round-id increment, or the weight redistribution after the outer SGD step). """ import os as _os import tempfile as _tempfile
from composer_replication.diloco.serverless import LocalProcessExecutor
n_replicas = 2 sync_every = 2 with _tempfile.TemporaryDirectory() as td: rendezvous = _os.path.join(td, "diloco-multiproc-run") executor = LocalProcessExecutor() handles = executor.launch_replicas( n_replicas=n_replicas, entrypoint=f"{__name__}._diloco_replica_one_outer_round", entrypoint_args={ "rendezvous_uri": rendezvous, "world_size": n_replicas, "sync_every": sync_every, "rank_env": "REPLICA_RANK", }, timeout=180, ) results = executor.collect(handles, timeout=180)
# Diagnostic-friendly failure: surface per-rank error if any replica died. statuses = {r["rank"]: r["status"] for r in results} for rank in range(n_replicas): assert statuses[rank] == "succeeded", ( f"rank {rank} failed: " f"{next(r for r in results if r['rank'] == rank).get('error')}" )
payloads = sorted([r["result"] for r in results], key=lambda d: d["rank"]) rank0, rank1 = payloads[0], payloads[1]
# Sanity: each replica really did fire exactly one outer round. assert rank0["current_step"] == 1, rank0 assert rank1["current_step"] == 1, rank1
# Sanity: replicas STARTED with identical weights (DiLoCo assumption). assert rank0["initial"] == rank1["initial"], ( "replicas started with different initial weights — DiLoCo only " "averages pseudo-gradients, not weights, so this would prevent " "convergence even with a perfectly correct allreduce" )
# The actual property: after one full outer round both replicas must # have the SAME final weights. Tight tolerance because the only # arithmetic between them is SGD + a single allreduce-mean. final0 = torch.tensor(rank0["final"]) final1 = torch.tensor(rank1["final"]) if not torch.allclose(final0, final1, atol=1e-5, rtol=1e-5): max_abs_diff = (final0 - final1).abs().max().item() pytest.fail( "Multi-process DiLoCo did NOT converge to identical weights " "after one outer round.\n" f" rank0 final = {final0.tolist()}\n" f" rank1 final = {final1.tolist()}\n" f" max|diff| = {max_abs_diff}\n" "This indicates a real cross-replica-averaging bug " "(averaging direction, round-id desync, or weight redistribution)." )
def test_mockmanager_diloco_two_outer_rounds_step_counter(tmp_path): """Two outer rounds must bump current_step() to 2 (fragment rotation safety).""" torch.manual_seed(1) model = torch.nn.Linear(4, 4, bias=False) inner_optim = torch.optim.SGD(model.parameters(), lr=0.05)
manager = MockManager(_make_store(tmp_path))
diloco = make_diloco_outer_loop( manager=manager, model_fragments=[model], inner_optimizer=inner_optim, sync_every=2, )
x = torch.randn(2, 4) target = torch.randn(2, 4)
with diloco: for _ in range(4): # 2 outer rounds at sync_every=2 inner_optim.zero_grad() (((model(x) - target) ** 2).mean()).backward() inner_optim.step()
assert manager.current_step() == 2, ( f"expected current_step()==2 after two outer rounds, got {manager.current_step()}" )224 unmodified lines225226227228229230231232233234235236237238239224 unmodified lineswith tempfile.TemporaryDirectory() as td:store = ObjectStoreAllReduce(td, rank=0, world_size=1, timeout_s=10.0)mgr = MockManager(store)# torchft.Manager surfaceassert hasattr(mgr, "allreduce")assert hasattr(mgr, "should_commit")assert hasattr(mgr, "start_quorum")assert hasattr(mgr, "wait_quorum")assert mgr.num_participants == 1assert mgr.rank == 0assert mgr.should_commit() is True# Single-replica allreduce is a passthrought = torch.tensor([1.0, 2.0])out = mgr.allreduce(t.clone())torch.testing.assert_close(out, t, atol=1e-6, rtol=1e-6)224 unmodified lines225226227228229230231232233234235236237238239240241242243244245246247248249250224 unmodified lineswith tempfile.TemporaryDirectory() as td:store = ObjectStoreAllReduce(td, rank=0, world_size=1, timeout_s=10.0)mgr = MockManager(store)# torchft.Manager surface (audited from torchft/local_sgd.py DiLoCo path)assert hasattr(mgr, "allreduce")assert hasattr(mgr, "should_commit")assert hasattr(mgr, "start_quorum")assert hasattr(mgr, "wait_quorum")assert hasattr(mgr, "current_step")assert hasattr(mgr, "disallow_state_dict_read")assert hasattr(mgr, "allow_state_dict_read")assert hasattr(mgr, "register_state_dict_fn")assert hasattr(mgr, "_use_async_quorum")assert mgr._use_async_quorum is Falseassert mgr.num_participants == 1assert mgr.rank == 0assert mgr.should_commit() is True# Single-replica allreduce: averaging is a passthrough, but the return# must be a Work-shaped object (DiLoCo calls .wait() on it). The# tensor itself is mutated in place by ObjectStoreAllReduce.t = torch.tensor([1.0, 2.0])buf = t.clone()work = mgr.allreduce(buf)assert hasattr(work, "wait") and callable(work.wait)assert work.wait() is Truetorch.testing.assert_close(buf, t, atol=1e-6, rtol=1e-6)
20 unmodified lines21222324252627282930313229 unmodified lines6263646566675 unmodified lines73747576777879808182838 unmodified lines9293949596976 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251 unmodified line12712812913013113213313413513613713813914014114214314463 unmodified lines20820921021120 unmodified lines- lm_ce: standard cross-entropy on assistant-response tokens (GRPO stub)- sdpo_jsd: generalized JSD between student and hint-conditioned-teacher logits- trace_replay_dpo: DPO loss over (chosen, rejected) teacher-disagreement pairs"""from __future__ import annotationsimport sysfrom dataclasses import dataclassfrom pathlib import Pathimport torchimport torch.nn.functional as F29 unmodified linessdpo_token_clip: float | None = None,replay_dpo_beta: float = 0.1,lm_ce_label_smoothing: float = 0.0,) -> LossComponents:"""Compute total = lm_ce + alpha * sdpo_jsd + beta * trace_replay_dpo.5 unmodified linesSDPO:- ctx_teacher_input_ids: (B, T_t) hint-conditioned context- sdpo_loss_mask: (B, T_t) 1 at error-turn tokensDPO:- dpo_chosen_input_ids, dpo_chosen_response_mask- dpo_rejected_input_ids, dpo_rejected_response_mask- dpo_chosen_ref_logprobs, dpo_rejected_ref_logprobs (precomputed)"""device = _device_of(model)# ------------------------------------------------------------------8 unmodified lines# ------------------------------------------------------------------# Channel 2 (SDPO): generalized JSD on hint-conditioned forward# ------------------------------------------------------------------sdpo_jsd = _zero(device)if (6 unmodified linesteacher_logits = model(input_ids=inputs["ctx_teacher_input_ids"]).logitsif student_logits.shape == teacher_logits.shape:sdpo_jsd = generalized_jsd_loss(student_logits=student_logits,teacher_logits=teacher_logits,labels=inputs.get("sdpo_loss_mask"),beta=sdpo_jsd_beta,temperature=sdpo_temperature,token_clip=sdpo_token_clip,reduction="batchmean",)# else: silently zero — the data collator is responsible for shape# alignment in production. For the smoke we accept misalignment and# exercise the fallback path.# ------------------------------------------------------------------# Channel 3 (trace-replay DPO): standard DPO loss on teacher-disagreement# pairs.# ------------------------------------------------------------------trace_replay_dpo = _zero(device)if (1 unmodified lineand "dpo_chosen_input_ids" in inputsand inputs["dpo_chosen_input_ids"].numel() > 0):chosen_lp = _sequence_logprobs(model, inputs["dpo_chosen_input_ids"], inputs["dpo_chosen_response_mask"])rejected_lp = _sequence_logprobs(model, inputs["dpo_rejected_input_ids"], inputs["dpo_rejected_response_mask"])ref_chosen = inputs["dpo_chosen_ref_logprobs"]ref_rejected = inputs["dpo_rejected_ref_logprobs"]dpo_logits = replay_dpo_beta * ((chosen_lp - ref_chosen) - (rejected_lp - ref_rejected))trace_replay_dpo = -F.logsigmoid(dpo_logits).mean()total = lm_ce + alpha_sdpo * sdpo_jsd + beta_replay * trace_replay_dpo63 unmodified linesreturn masked.sum(dim=-1)__all__ = ["compose_loss", "LossComponents"]20 unmodified lines21222324252627282930313233343536373839404142434445464729 unmodified lines77787980818283848586878889909192939495965 unmodified lines1021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401418 unmodified lines1501511521531541551566 unmodified lines1631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192201 unmodified line22222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226363 unmodified lines32732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439520 unmodified lines- lm_ce: standard cross-entropy on assistant-response tokens (GRPO stub)- sdpo_jsd: generalized JSD between student and hint-conditioned-teacher logits- trace_replay_dpo: DPO loss over (chosen, rejected) teacher-disagreement pairsADR-007 extensions------------------Three pluggable distillation losses can swap the default DPO/SDPO channels:- ``dpo_variant="simpo"`` — channel 3 uses SimPO (reference-free DPO withmargin) instead of standard DPO. Reference logprobs are no longer required.- ``sdpo_wrapper="taid"`` — channel 2 wraps SDPO with TAID (TemporallyAdaptive Interpolated Distillation). Requires ``taid_schedule_step`` and``taid_total_steps`` plus either ``inputs["student_init_logits"]`` or``inputs["student_init_input_ids"]`` for the frozen-init forward pass.- ``sdpo_wrapper="entropy_opd"`` — channel 2 uses Entropy-Aware OPD, aper-token gated forward/reverse KL.All three default to off; passing the new kwargs at their defaults isbit-exact equivalent to the legacy 3-channel composition."""from __future__ import annotationsfrom dataclasses import dataclassfrom typing import Literalimport torchimport torch.nn.functional as F29 unmodified linessdpo_token_clip: float | None = None,replay_dpo_beta: float = 0.1,lm_ce_label_smoothing: float = 0.0,# ADR-007 extensions ------------------------------------------------dpo_variant: Literal["dpo", "simpo"] = "dpo",sdpo_wrapper: Literal["none", "taid", "entropy_opd"] = "none",taid_schedule_step: int | None = None,taid_total_steps: int | None = None,# SimPO knobs (only used when dpo_variant="simpo") ------------------simpo_beta: float = 2.0,simpo_gamma: float = 1.0,# TAID knobs (only used when sdpo_wrapper="taid") -------------------taid_schedule: str = "linear",taid_alpha_min: float = 0.0,taid_alpha_max: float = 1.0,# Entropy-Aware OPD knobs (only used when sdpo_wrapper="entropy_opd")entropy_opd_h_max: float | None = None,) -> LossComponents:"""Compute total = lm_ce + alpha * sdpo_jsd + beta * trace_replay_dpo.5 unmodified linesSDPO:- ctx_teacher_input_ids: (B, T_t) hint-conditioned context- sdpo_loss_mask: (B, T_t) 1 at error-turn tokensDPO (dpo_variant="dpo"):- dpo_chosen_input_ids, dpo_chosen_response_mask- dpo_rejected_input_ids, dpo_rejected_response_mask- dpo_chosen_ref_logprobs, dpo_rejected_ref_logprobs (precomputed)SimPO (dpo_variant="simpo"):- dpo_chosen_input_ids, dpo_chosen_response_mask- dpo_rejected_input_ids, dpo_rejected_response_mask(reference logprobs not required and silently ignored)TAID (sdpo_wrapper="taid"):- student_init_logits: (B, T_t, V) precomputed frozen init logits, OR- student_init_input_ids: (B, T_t) frozen student snapshot — a frozenforward pass through `model` produces the init logits (this assumes`model` has not yet drifted from init; production callers shouldprefer the precomputed path with a saved init snapshot)."""if dpo_variant not in ("dpo", "simpo"):raise ValueError(f"dpo_variant must be 'dpo' or 'simpo', got {dpo_variant!r}")if sdpo_wrapper not in ("none", "taid", "entropy_opd"):raise ValueError(f"sdpo_wrapper must be 'none', 'taid', or 'entropy_opd', "f"got {sdpo_wrapper!r}")if sdpo_wrapper == "taid":if taid_schedule_step is None:raise ValueError("sdpo_wrapper='taid' requires taid_schedule_step (int)")if taid_total_steps is None:raise ValueError("sdpo_wrapper='taid' requires taid_total_steps (int)")device = _device_of(model)# ------------------------------------------------------------------8 unmodified lines# ------------------------------------------------------------------# Channel 2 (SDPO): generalized JSD on hint-conditioned forward# Optionally wrapped by TAID or replaced by Entropy-Aware OPD.# ------------------------------------------------------------------sdpo_jsd = _zero(device)if (6 unmodified linesteacher_logits = model(input_ids=inputs["ctx_teacher_input_ids"]).logitsif student_logits.shape == teacher_logits.shape:if sdpo_wrapper == "none":sdpo_jsd = generalized_jsd_loss(student_logits=student_logits,teacher_logits=teacher_logits,labels=inputs.get("sdpo_loss_mask"),beta=sdpo_jsd_beta,temperature=sdpo_temperature,token_clip=sdpo_token_clip,reduction="batchmean",)elif sdpo_wrapper == "taid":from composer_replication.distillation import taid_lossstudent_init_logits = _resolve_student_init_logits(model, inputs, expected_shape=teacher_logits.shape)# taid_schedule_step / taid_total_steps validated non-None above.assert taid_schedule_step is not Noneassert taid_total_steps is not Nonesdpo_jsd = taid_loss(student_logits=student_logits,teacher_logits=teacher_logits,student_init_logits=student_init_logits,schedule_step=int(taid_schedule_step),total_steps=int(taid_total_steps),schedule=taid_schedule,alpha_min=taid_alpha_min,alpha_max=taid_alpha_max,jsd_beta=sdpo_jsd_beta,temperature=sdpo_temperature,reduction="batchmean",)elif sdpo_wrapper == "entropy_opd":from composer_replication.distillation import (entropy_aware_opd_loss,)sdpo_jsd = entropy_aware_opd_loss(student_logits=student_logits,teacher_logits=teacher_logits,labels=inputs.get("sdpo_loss_mask"),h_max=entropy_opd_h_max,temperature=sdpo_temperature,reduction="batchmean",)# else: silently zero — the data collator is responsible for shape# alignment in production. For the smoke we accept misalignment and# exercise the fallback path.# ------------------------------------------------------------------# Channel 3 (trace-replay DPO): standard DPO loss on teacher-disagreement# pairs. With dpo_variant="simpo", swap to SimPO (reference-free).# ------------------------------------------------------------------trace_replay_dpo = _zero(device)if (1 unmodified lineand "dpo_chosen_input_ids" in inputsand inputs["dpo_chosen_input_ids"].numel() > 0):if dpo_variant == "dpo":chosen_lp = _sequence_logprobs(model,inputs["dpo_chosen_input_ids"],inputs["dpo_chosen_response_mask"],)rejected_lp = _sequence_logprobs(model,inputs["dpo_rejected_input_ids"],inputs["dpo_rejected_response_mask"],)ref_chosen = inputs["dpo_chosen_ref_logprobs"]ref_rejected = inputs["dpo_rejected_ref_logprobs"]dpo_logits = replay_dpo_beta * ((chosen_lp - ref_chosen) - (rejected_lp - ref_rejected))trace_replay_dpo = -F.logsigmoid(dpo_logits).mean()else: # dpo_variant == "simpo"from composer_replication.distillation import simpo_losschosen_avg_lp = _avg_sequence_logprobs(model,inputs["dpo_chosen_input_ids"],inputs["dpo_chosen_response_mask"],)rejected_avg_lp = _avg_sequence_logprobs(model,inputs["dpo_rejected_input_ids"],inputs["dpo_rejected_response_mask"],)trace_replay_dpo = simpo_loss(chosen_avg_logprobs=chosen_avg_lp,rejected_avg_logprobs=rejected_avg_lp,beta=simpo_beta,gamma=simpo_gamma,)total = lm_ce + alpha_sdpo * sdpo_jsd + beta_replay * trace_replay_dpo63 unmodified linesreturn masked.sum(dim=-1)def _avg_sequence_logprobs(model: torch.nn.Module,input_ids: torch.Tensor,response_mask: torch.Tensor,) -> torch.Tensor:"""Per-sequence AVERAGE next-token logprob over response tokens.SimPO accounting: divide the sum by the number of response tokens solong sequences aren't penalized for length."""outputs = model(input_ids=input_ids)logits = outputs.logits[:, :-1, :]targets = input_ids[:, 1:]log_probs = F.log_softmax(logits, dim=-1)token_lp = log_probs.gather(-1, targets.unsqueeze(-1)).squeeze(-1)mask = response_mask[:, 1:].float()masked = token_lp * maskn_tokens = mask.sum(dim=-1).clamp_min(1.0)return masked.sum(dim=-1) / n_tokensdef _resolve_student_init_logits(model: torch.nn.Module,inputs: dict[str, torch.Tensor],*,expected_shape: torch.Size,) -> torch.Tensor:"""Return frozen student-init logits for TAID.Preferred path: caller pre-saves a snapshot at training step 0 and passesit via ``inputs['student_init_logits']``. Fallback path (only valid earlyin training before the model has drifted): pass``inputs['student_init_input_ids']`` and we run a no-grad forward through``model``. Always returns a tensor on the same device as ``model``."""if "student_init_logits" in inputs and inputs["student_init_logits"].numel() > 0:student_init = inputs["student_init_logits"]if student_init.shape != expected_shape:raise ValueError(f"inputs['student_init_logits'] shape {tuple(student_init.shape)} "f"does not match teacher logits shape {tuple(expected_shape)}")return student_init.detach()if ("student_init_input_ids" in inputsand inputs["student_init_input_ids"].numel() > 0):with torch.no_grad():init_logits = model(input_ids=inputs["student_init_input_ids"]).logitsif init_logits.shape != expected_shape:raise ValueError(f"frozen forward on student_init_input_ids gave shape "f"{tuple(init_logits.shape)} which does not match teacher "f"logits shape {tuple(expected_shape)}")return init_logitsraise ValueError("sdpo_wrapper='taid' requires either inputs['student_init_logits'] ""(precomputed) or inputs['student_init_input_ids'] (frozen forward ""fallback) to be present.")__all__ = ["compose_loss", "LossComponents"]
123456789101112131415161718192021223 unmodified lines2627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104"""PRIME-RL composer loss adapter — SKELETON for v0.Per ADR-006, PRIME-RL exposes a `CustomLossConfig` that takes animportable function. This module supplies that function: a thin adapterthat maps PRIME-RL's `LossInputs` struct onto the framework's 3-channelloss composition.Status: SKELETON. The full implementation requires a runtime spike withprime-rl installed; this file documents the contract and provides aworking stub that returns a finite scalar so PRIME-RL can be configuredend-to-end without yet having all three channels wired up.Reference:- PRIME-RL `LossInputs` shape (verified via DeepWiki audit, Wave 13):- trainer_logprobs: Tensor (B, T) — student log-probs of generated tokens- inference_logprobs: Tensor (B, T) — log-probs from inference engine- teacher_logprobs: Tensor (B, T) | None — optional teacher channel- advantages: Tensor (B, T) — GRPO advantages- loss_mask: Tensor (B, T) — response-token mask"""from __future__ import annotations3 unmodified linesdef loss_fn(inputs: Any, # PRIME-RL's LossInputs — typed as Any to avoid hard import*,alpha_sdpo: float = 0.5,beta_dpo: float = 0.3,epsilon: float = 1e-6,) -> Any: # Returns a torch.Tensor (scalar)"""Composer 3-channel loss adapted to PRIME-RL's LossInputs struct.Channels (per `composer_replication.compose_loss`):1. GRPO policy-gradient: -(advantages * trainer_logprobs * mask).mean()2. SDPO / OPSD: generalized_jsd_loss(student_logits, teacher_logits)3. Trace-replay DPO: standard DPO on (chosen, rejected) pairsFor PRIME-RL adaptation:- Channel 1 reads from `advantages` + `trainer_logprobs` directly.(Note: this is REINFORCE-with-advantage, not full GRPO. FullGRPO would use `inference_logprobs` for the importance-samplingratio + PPO clipping. See Wave 13 review Finding 6.)- Channel 2 (SDPO) is **DEFERRED** for v0 because PRIME-RL v0.5exposes log-probs not logits, and SDPO needs the full vocabdistribution. Setting alpha_sdpo>0 raises NotImplementedError(Wave 13 review Finding 1 — earlier draft was silently degenerate).- Channel 3 (DPO) is OUT OF SCOPE for the PRIME-RL recipe in v0— it would require modifying PRIME-RL's data path to pass`(chosen, rejected)` pairs alongside the rollout, which is aseparate integration effort. v0 emits beta_dpo=0 with awarning if non-zero.Args:inputs: PRIME-RL `LossInputs` (duck-typed)alpha_sdpo: weight on channel 2 (SDPO)beta_dpo: weight on channel 3 (DPO) — currently must be 0epsilon: numerical stability for log/divisionReturns:Scalar torch.Tensor; PRIME-RL's trainer takes care of `.backward()`."""import torch # lazyfrom composer_replication.opsd import generalized_jsd_loss# Channel 1: GRPOadvantages = inputs.advantagestrainer_lp = inputs.trainer_logprobsmask = inputs.loss_maskif mask.dtype != advantages.dtype:mask = mask.to(advantages.dtype)grpo_loss = -(advantages * trainer_lp * mask).sum() / mask.sum().clamp_min(epsilon)total = grpo_loss# Channel 2: SDPO/OPSD — DEFERRED in PRIME-RL recipe v0.## Wave 13 cross-model review (docs/research/WAVE_13_FINAL_REVIEW.md# Finding 1) caught that an earlier draft of this code applied# `unsqueeze(-1)` to (B, T) log-prob tensors before passing them to# generalized_jsd_loss, which calls log_softmax(dim=-1). Softmax of a# 1-element vector is exactly 1.0; its log is 0. So the SDPO term was# mathematically degenerate (always 0), silently disabling channel 2# while reporting alpha_sdpo>0 in the config.## The right path forward depends on PRIME-RL exposing full logits, not# just log-probs. Until that lands upstream, refuse to fake the channel:teacher_lp = getattr(inputs, "teacher_logprobs", None)if teacher_lp is not None and alpha_sdpo > 0:raise NotImplementedError("SDPO channel in the PRIME-RL recipe is deferred. PRIME-RL v0.5 ""exposes (B, T) log-probs through LossInputs but not full logits, ""and SDPO/OPSD requires the full distribution over vocabulary. ""Set alpha_sdpo=0.0 to silence this and use channel 1 (GRPO) only. ""See docs/research/WAVE_13_FINAL_REVIEW.md Finding 1.")# Channel 3: not supported in PRIME-RL recipe v0if beta_dpo != 0.0:import warningswarnings.warn("PRIME-RL recipe v0 does not support DPO channel; ""set beta_dpo=0.0 to silence this warning.",1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889903 unmodified lines949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244"""PRIME-RL composer loss adapter.Per ADR-006, PRIME-RL exposes a ``CustomLossConfig`` that takes animportable function. This module supplies that function: a thin adapterthat maps PRIME-RL's ``LossInputs`` struct onto the framework's 3-channelloss composition.Channel status (v0):1. **DPPO + KL on the importance-sampling ratio** — implemented tomatch PRIME-RL's upstream ``default_loss_fn`` byte-for-byte.2. **SDPO / OPSD** — deferred (raises ``NotImplementedError`` whenenabled). PRIME-RL v0.5 exposes log-probs, not full logits, andSDPO requires the full vocabulary distribution.3. **Trace-replay DPO** — out of scope for this recipe; emits awarning if ``beta_dpo != 0``.LossInputs shape (verified against PrimeIntellect-ai/prime-rl``src/prime_rl/trainer/rl/loss.py`` lines 13-22):.. code-block:: python@dataclassclass LossInputs:trainer_logprobs: Float[Tensor, ' seq'] # current policy log-probsinference_logprobs: Float[Tensor, ' seq'] # rollout-time policy log-probsteacher_logprobs: Float[Tensor, ' seq'] | Noneadvantages: Float[Tensor, ' seq'] # per-token advantageloss_mask: Bool[Tensor, ' seq'] # which tokens countPRIME-RL calls the loss function once per sample, not on a batched``(B, T)`` tensor.PRIME-RL's ``default_loss_fn`` (upstream)-----------------------------------------Verbatim from ``prime_rl/trainer/rl/loss.py`` lines 116-165 and the``DefaultLossConfig`` defaults at``packages/prime-rl-configs/src/prime_rl/configs/trainer.py`` lines412-425::def default_loss_fn(inputs, loss_config):# line 133-135log_importance_ratio = trainer_logprobs - inference_logprobsimportance_ratio = exp(log_importance_ratio)mismatch_kl = importance_ratio - log_importance_ratio - 1# line 137: NOTE — probability-space diff, not log-ratioprobs_diff = exp(trainer_logprobs) - exp(inference_logprobs)# lines 138-139dppo_invalid_mask_high = probs_diff > loss_config.dppo_mask_highdppo_invalid_mask_low = probs_diff < -loss_config.dppo_mask_low# lines 140-142: sign-of-advantage gatepositive_advantages = advantages > 0dppo_invalid_mask = where(positive_advantages,dppo_invalid_mask_high,dppo_invalid_mask_low)# lines 147-148drop_mask = loss_mask & dppo_invalid_maskkeep_mask = loss_mask & ~dppo_invalid_mask# lines 150-153advantages = loss_config.adv_tau * advantagespg_loss = keep_mask * advantages * importance_ratiokl_loss = loss_mask * log_importance_ratio**2loss = (-pg_loss + loss_config.kl_tau * kl_loss).sum()Defaults: ``dppo_mask_low=0.2``, ``dppo_mask_high=0.2``,``adv_tau=1.0``, ``kl_tau=1e-3`` — all ``Field(..., ge=0)``.Three things this differs from a textbook PPO-clip:1. The mask gate is on **probability-space** ``probs_diff``, not onthe log-ratio. ``-loss_config.dppo_mask_low`` flips the sign so``dppo_mask_low`` is itself non-negative.2. The policy-gradient term is multiplied by ``importance_ratio``(= ``exp(trainer_lp - inference_lp)``), giving a proper IS-correctedgradient — not a plain REINFORCE on ``trainer_lp``.3. The mask is **conditioned on advantage sign**: a positive-advantagetoken is dropped when ``probs_diff`` exceeds ``dppo_mask_high``(we'd be upweighting it too aggressively); a negative-advantagetoken is dropped when ``probs_diff`` falls below ``-dppo_mask_low``(we'd be downweighting it too aggressively). Zero-advantage tokensare never DPPO-masked.The reduction is a plain ``sum()`` (PRIME-RL's outer ``compute_loss``divides by ``loss_scale``); we mirror that.License: MIT (matches the rest of the framework). PRIME-RL is Apache-2;we reference its algorithm and convention but vendor no code."""from __future__ import annotations3 unmodified linesdef loss_fn(inputs: Any, # PRIME-RL's LossInputs — typed as Any to avoid hard import*,alpha_sdpo: float = 0.0,beta_dpo: float = 0.0,dppo_mask_high: float = 0.2,dppo_mask_low: float = 0.2,adv_tau: float = 1.0,kl_tau: float = 1e-3,) -> Any: # Returns a torch.Tensor (scalar) matching PRIME-RL's contract"""Composer 3-channel loss adapted to PRIME-RL's ``LossInputs`` struct.Channel 1 mirrors PRIME-RL's ``default_loss_fn`` exactly so configsfrom PRIME-RL's own examples translate. Channels 2 and 3 aredeferred — see module docstring.Args:inputs: PRIME-RL ``LossInputs`` (duck-typed). All tensor fieldsare expected to be 1-D with shape ``(seq,)``.alpha_sdpo: weight on channel 2 (SDPO). Must be 0 in v0; >0raises :class:`NotImplementedError`.beta_dpo: weight on channel 3 (DPO). Non-zero emits a warning;channel 3 is not yet wired in this recipe.dppo_mask_high: upper DPPO masking threshold on``exp(trainer_lp) - exp(inference_lp)``. Tokens with**positive advantage** whose ``probs_diff`` exceeds thisvalue are dropped. PRIME-RL default: ``0.2``. Must be >= 0.dppo_mask_low: magnitude of the lower DPPO masking threshold.Tokens with **negative advantage** whose ``probs_diff`` isbelow ``-dppo_mask_low`` are dropped. PRIME-RL default:``0.2``. Must be >= 0 (note: PRIME-RL stores the magnitude;the sign flip is internal to the comparison).adv_tau: temperature on the advantage term. PRIME-RL default``1.0``. Must be >= 0.kl_tau: temperature on the KL term ``log_importance_ratio**2``.PRIME-RL default ``1e-3``. Must be >= 0.Returns:Scalar ``torch.Tensor``. PRIME-RL's outer ``compute_loss``divides by ``loss_scale`` and calls ``.backward()``.Raises:ValueError: if any of ``trainer_logprobs``, ``inference_logprobs``,``advantages``, ``loss_mask`` is not 1-D, or any of``dppo_mask_high``, ``dppo_mask_low``, ``adv_tau``, ``kl_tau``is negative.NotImplementedError: if ``alpha_sdpo > 0`` (channel 2 is deferred)."""import torch # lazy — keep module importable without torch installed# PRIME-RL enforces these via Pydantic Field(..., ge=0); we mirror it.for name, val in (("dppo_mask_high", dppo_mask_high),("dppo_mask_low", dppo_mask_low),("adv_tau", adv_tau),("kl_tau", kl_tau),):if val < 0:raise ValueError(f"{name} must be >= 0 (PRIME-RL config contract); got {val}")advantages = inputs.advantagestrainer_lp = inputs.trainer_logprobsinference_lp = inputs.inference_logprobsloss_mask = inputs.loss_mask# --- Shape validation -------------------------------------------------# PRIME-RL passes per-sample (seq,) tensors. Reject (B, T) explicitly so# callers don't silently get the wrong reduction.for name, t in (("trainer_logprobs", trainer_lp),("inference_logprobs", inference_lp),("advantages", advantages),("loss_mask", loss_mask),):if t.dim() != 1:raise ValueError(f"PRIME-RL loss_fn expects 1-D (seq,) tensors per "f"PRIME-RL's LossInputs contract; got {name} with shape "f"{tuple(t.shape)} (dim={t.dim()}). PRIME-RL calls the loss "f"function once per sample, not on a batched (B, T) tensor.")# --- Channel 1: DPPO + KL on the importance ratio --------------------# Mirrors prime_rl/trainer/rl/loss.py default_loss_fn lines 133-153.log_importance_ratio = trainer_lp - inference_lpimportance_ratio = torch.exp(log_importance_ratio)# NOTE: probability-space diff, NOT log-ratio. This is the key# divergence from a naive PPO-clip implementation.probs_diff = torch.exp(trainer_lp) - torch.exp(inference_lp)dppo_invalid_mask_high = probs_diff > dppo_mask_highdppo_invalid_mask_low = probs_diff < -dppo_mask_lowpositive_advantages = advantages > 0# Sign-of-advantage gate: positive-advantage tokens use the "high"# threshold; negative-advantage tokens use the "low" threshold.# Zero-advantage tokens fall through ``positive_advantages == False``,# so they are gated by the (negative-advantage) low check; in practice# zero-advantage tokens contribute zero to ``pg_loss`` regardless.dppo_invalid_mask = torch.where(positive_advantages, dppo_invalid_mask_high, dppo_invalid_mask_low)# loss_mask may be bool; combine via boolean ops to match upstream# exactly, then cast to the working dtype for the multiply.if loss_mask.dtype != torch.bool:loss_mask_bool = loss_mask.to(torch.bool)else:loss_mask_bool = loss_maskkeep_mask_bool = loss_mask_bool & ~dppo_invalid_maskkeep_mask = keep_mask_bool.to(trainer_lp.dtype)loss_mask_f = loss_mask_bool.to(trainer_lp.dtype)scaled_advantages = adv_tau * advantagespg_loss = keep_mask * scaled_advantages * importance_ratiokl_loss = loss_mask_f * log_importance_ratio**2total = (-pg_loss + kl_tau * kl_loss).sum()# --- Channel 2: SDPO/OPSD — DEFERRED in PRIME-RL recipe v0 -----------## Wave 13 cross-model review caught that an earlier draft applied# `unsqueeze(-1)` to log-prob tensors before generalized_jsd_loss,# which calls log_softmax(dim=-1). Softmax of a 1-element vector is# exactly 1.0; its log is 0. The SDPO term was mathematically# degenerate (always 0), silently disabling channel 2 while reporting# alpha_sdpo>0 in the config. Until PRIME-RL exposes full logits we# refuse to fake the channel:teacher_lp = getattr(inputs, "teacher_logprobs", None)if alpha_sdpo > 0:raise NotImplementedError("SDPO channel in the PRIME-RL recipe is deferred. PRIME-RL ""v0.5 exposes (seq,) log-probs through LossInputs but not ""full vocabulary logits, and SDPO/OPSD requires the full ""distribution. Set alpha_sdpo=0.0 to silence this and use ""channel 1 (DPPO+KL) only. teacher_logprobs is "f"{'present' if teacher_lp is not None else 'absent'} in this ""call but unused. See docs/research/WAVE_13_FINAL_REVIEW.md ""Finding 1.")# --- Channel 3: not supported in PRIME-RL recipe v0 -------------------if beta_dpo != 0.0:import warningswarnings.warn("PRIME-RL recipe v0 does not support DPO channel; ""set beta_dpo=0.0 to silence this warning.",
24 unmodified lines25262728293031323324 unmodified lines# The function MUST return a scalar tensor (PRIME-RL handles backward).import_path: "composer_replication.recipes.prime_rl.composer_loss:loss_fn"kwargs:alpha_sdpo: 0.5beta_dpo: 0.0 # DPO channel out-of-scope for PRIME-RL recipe v0epsilon: 1.0e-6# --- PRIME-RL three-actor split --------------------------------------trainer:24 unmodified lines252627282930313233343536373839404142434445464724 unmodified lines# The function MUST return a scalar tensor (PRIME-RL handles backward).import_path: "composer_replication.recipes.prime_rl.composer_loss:loss_fn"kwargs:# Channel 2 (SDPO/OPSD) is deferred in v0 — set >0 to fail fast# rather than silently no-op until PRIME-RL exposes full logits.alpha_sdpo: 0.0# Channel 3 (DPO) is out-of-scope for PRIME-RL recipe v0.beta_dpo: 0.0# DPPO mask thresholds (PRIME-RL convention, NOT textbook PPO).# Tokens whose probability-space diff# exp(trainer_lp) - exp(inference_lp)# exceeds dppo_mask_high (for positive-advantage tokens) or falls# below -dppo_mask_low (for negative-advantage tokens) are dropped# from the policy-gradient term. Defaults match PRIME-RL's# DefaultLossConfig (Field(..., ge=0), so both must be non-negative).dppo_mask_high: 0.2dppo_mask_low: 0.2# Advantage / KL temperatures from PRIME-RL DefaultLossConfig.adv_tau: 1.0kl_tau: 1.0e-3# --- PRIME-RL three-actor split --------------------------------------trainer:
13 unmodified lines14151617181920212223242526273 unmodified lines31323334353637385 unmodified lines444546474849505152535455565758596061626364656667686929 unmodified lines9910010110210310410510610713 unmodified lines```python@dataclassclass LossInputs:trainer_logprobs: Tensor # student log-probs of generated tokensinference_logprobs: Tensor # log-probs from the inference engine# (importance-sampling ratio numerator)teacher_logprobs: Tensor | None # if the teacher channel is wired inadvantages: Tensor # GRPO advantages (channel 1)loss_mask: Tensor # response-token mask```The user wires this in via a YAML config field — no fork, no Trainersubclass, no monkey-patching:3 unmodified linescustom:import_path: composer_replication.recipes.prime_rl.composer_loss:loss_fnkwargs:alpha_sdpo: 0.5beta_dpo: 0.3```## Step-by-step5 unmodified lines```### 2. Drop in the composer lossThe framework ships `composer_replication.recipes.prime_rl.composer_loss`which adapts the 3-channel `compose_loss` to PRIME-RL's `LossInputs`struct. The signature is fixed by PRIME-RL:```pythondef loss_fn(inputs: LossInputs, *, alpha_sdpo: float, beta_dpo: float) -> Tensor:# channel 1: GRPO (PRIME-RL's default policy gradient)grpo = (inputs.advantages * inputs.trainer_logprobs * inputs.loss_mask).mean()# channel 2: SDPO/OPSD against teacher_logprobssdpo = ...# channel 3: trace-replay DPO via teacher_logprobs disagreementtrace_replay_dpo = ...return -grpo + alpha_sdpo * sdpo + beta_dpo * trace_replay_dpo```Concrete file: `composer_loss.py` in this directory (skeleton; fills inwhen the user does the runtime spike).### 3. PRIME-RL config29 unmodified lines- An actual training run yet — that's a separate spike.- Quality validation against TRL/VeRL — pending Spike 004 A/B.- Hardware autoscaling — that's the Monarch recipe's job (recipes/monarch/).## References- PRIME-RL repo: https://github.com/PrimeIntellect-ai/prime-rl- ADR-006: docs/adrs/ADR-006-rl-frameworks.md- Reconnaissance: docs/research/RL_FRAMEWORKS_LANDSCAPE.md (§ PRIME-RL)13 unmodified lines141516171819202122232425262728293031323 unmodified lines3637383940414243444546475 unmodified lines535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913029 unmodified lines16016116216316416516616716816917017117217317417513 unmodified lines```python@dataclassclass LossInputs:trainer_logprobs: Tensor[' seq'] # current-policy log-probs (per-sample, 1-D)inference_logprobs: Tensor[' seq'] # rollout-time policy log-probs# (importance-sampling-ratio denominator)teacher_logprobs: Tensor[' seq'] | None # if the teacher channel is wired inadvantages: Tensor[' seq'] # GRPO advantages (channel 1)loss_mask: Tensor[' seq'] # response-token mask```> **Shape note.** PRIME-RL calls the loss function **once per sample**;> the tensors above are 1-D ``(seq,)``, *not* batched ``(B, T)``. An> earlier draft of `composer_loss.py` assumed `(B, T)` and was caught> in the Wave 13 cross-model review.The user wires this in via a YAML config field — no fork, no Trainersubclass, no monkey-patching:3 unmodified linescustom:import_path: composer_replication.recipes.prime_rl.composer_loss:loss_fnkwargs:alpha_sdpo: 0.0 # channel 2 deferred in v0 — see belowbeta_dpo: 0.0 # channel 3 out-of-scope in v0dppo_mask_high: 0.2 # PRIME-RL DPPO mask (probability-space)dppo_mask_low: 0.2adv_tau: 1.0kl_tau: 1.0e-3```## Step-by-step5 unmodified lines```### 2. Drop in the composer lossThe framework ships `composer_replication.recipes.prime_rl.composer_loss`which adapts the 3-channel `compose_loss` to PRIME-RL's `LossInputs`struct. Channel 1 mirrors PRIME-RL's upstream `default_loss_fn` exactly(verified in `prime_rl/trainer/rl/loss.py` lines 116-165):```pythondef loss_fn(inputs: LossInputs,*,alpha_sdpo: float = 0.0,beta_dpo: float = 0.0,# PRIME-RL DefaultLossConfig defaults (Field(..., ge=0)):dppo_mask_high: float = 0.2,dppo_mask_low: float = 0.2,adv_tau: float = 1.0,kl_tau: float = 1e-3,) -> Tensor:# ----- Channel 1: DPPO + KL on the importance ratio -----log_ir = trainer_lp - inference_lpir = exp(log_ir)probs_diff = exp(trainer_lp) - exp(inference_lp) # NB: probability spaceinvalid_high = probs_diff > dppo_mask_highinvalid_low = probs_diff < -dppo_mask_lowpos_adv = advantages > 0invalid = where(pos_adv, invalid_high, invalid_low) # advantage-conditionedkeep = loss_mask & ~invalidpg_loss = keep * (adv_tau * advantages) * irkl_loss = loss_mask * log_ir**2loss = (-pg_loss + kl_tau * kl_loss).sum() # SUM, not mean# ----- Channel 2: SDPO/OPSD against teacher_logprobs -----# DEFERRED — PRIME-RL v0.5 exposes log-probs not full logits.# alpha_sdpo > 0 raises NotImplementedError until that lands.# ----- Channel 3: trace-replay DPO -----# Out of scope for PRIME-RL recipe v0.return loss```**DPPO clip semantics — three things to know.** This is *not* atextbook PPO clipped surrogate; it is exactly what PRIME-RL's`default_loss_fn` does:1. The mask gate is on **probability-space**`probs_diff = exp(trainer_lp) - exp(inference_lp)`, **not** on thelog-ratio. (`-dppo_mask_low` flips the sign so the threshold itselfis non-negative; PRIME-RL stores both bounds as `Field(..., ge=0)`.)2. The policy-gradient term is multiplied by`importance_ratio = exp(trainer_lp - inference_lp)`, not by`trainer_lp` directly — this is a proper IS-corrected gradient, notplain REINFORCE.3. The mask is **conditioned on the sign of the advantage**: apositive-advantage token is dropped iff `probs_diff > dppo_mask_high`(we'd be upweighting an already-too-high-probability token); anegative-advantage token is dropped iff `probs_diff < -dppo_mask_low`(we'd be downweighting an already-too-low-probability token). Thegates are *not* OR'd together.The reduction is a plain `sum()`; PRIME-RL's outer `compute_loss`divides by `loss_scale` and aggregates across the packed batch.There is also a per-token KL term `kl_tau * log_importance_ratio**2`(the Kimi-K2.5 KL, see PRIME-RL's docstring on line 119-126), gated by`loss_mask` only — DPPO masking does not affect it.Concrete file: `composer_loss.py` in this directory. Tests:`tests/test_composer_loss.py` (16 cases including a parity test againstPRIME-RL's own `default_loss_fn`, skip-marked when prime-rl is notinstalled).### 3. PRIME-RL config29 unmodified lines- An actual training run yet — that's a separate spike.- Quality validation against TRL/VeRL — pending Spike 004 A/B.- Hardware autoscaling — that's the Monarch recipe's job (recipes/monarch/).- **SDPO/OPSD channel** — deferred until PRIME-RL exposes full logitsthrough `LossInputs` (currently only log-probs).## References- PRIME-RL repo: https://github.com/PrimeIntellect-ai/prime-rl- PRIME-RL upstream `default_loss_fn`: `src/prime_rl/trainer/rl/loss.py`lines 116-165- PRIME-RL `DefaultLossConfig` defaults:`packages/prime-rl-configs/src/prime_rl/configs/trainer.py` lines412-425- ADR-006: docs/adrs/ADR-006-rl-frameworks.md- Reconnaissance: docs/research/RL_FRAMEWORKS_LANDSCAPE.md (§ PRIME-RL)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484"""Unit tests for the PRIME-RL composer-loss adapter.
Verifies parity with PRIME-RL's upstream ``default_loss_fn``(``src/prime_rl/trainer/rl/loss.py`` lines 116-165). Hand-computedexpected values use the upstream formula; the parity test at the bottomimports PRIME-RL itself (skip-marked when not installed) and comparesoutputs end-to-end.
License: MIT."""from __future__ import annotations
import mathfrom dataclasses import dataclassfrom typing import Optional
import pytestimport torch
from composer_replication.recipes.prime_rl.composer_loss import loss_fn
# Try to import PRIME-RL upstream for the parity test; skip-mark if# unavailable. PRIME-RL pulls in heavy deps (jaxtyping, beartype) and# is not part of the framework's own test environment.try: from prime_rl.trainer.rl.loss import ( # type: ignore[import-not-found] LossInputs as PrimeRLLossInputs, default_loss_fn as prime_rl_default_loss_fn, ) from prime_rl.configs.trainer import ( # type: ignore[import-not-found] DefaultLossConfig as PrimeRLDefaultLossConfig, ) _HAS_PRIME_RL = Trueexcept Exception: # noqa: BLE001 — broad: missing module, version skew, etc. _HAS_PRIME_RL = False
# ---------------------------------------------------------------------# Test double — duck-typed stand-in for PRIME-RL's LossInputs# ---------------------------------------------------------------------@dataclassclass FakeLossInputs: trainer_logprobs: torch.Tensor inference_logprobs: torch.Tensor advantages: torch.Tensor loss_mask: torch.Tensor teacher_logprobs: Optional[torch.Tensor] = None
def _make_inputs( seq: int = 8, *, same_logprobs: bool = True, teacher: bool = False, seed: int = 0,) -> FakeLossInputs: """Build a realistic (seq,) LossInputs stand-in.
Uses ``requires_grad`` on ``trainer_logprobs`` so callers can also sanity-check that the loss is differentiable end-to-end. Default log-probs are clamped to a moderate negative range so ``exp(trainer_lp) - exp(inference_lp)`` stays inside the 0.2 PRIME-RL default DPPO band — i.e. tokens are not all DPPO-masked by chance. """ g = torch.Generator().manual_seed(seed) # Negative log-probs in [-2, -0.5] keep exp() in roughly [0.13, 0.6] # so probs_diff differences stay tiny under small perturbation. trainer = -(0.5 + 1.5 * torch.rand(seq, generator=g)) trainer = trainer.detach().clone().requires_grad_(True) if same_logprobs: # Tiny perturbation -> probs_diff ~ 0, no DPPO masking. inference = trainer.detach().clone() + 0.001 * torch.randn( seq, generator=g ) else: inference = -(0.5 + 1.5 * torch.rand(seq, generator=g)) advantages = torch.randn(seq, generator=g) loss_mask = torch.ones(seq, dtype=torch.bool) teacher_lp = torch.randn(seq, generator=g) if teacher else None return FakeLossInputs( trainer_logprobs=trainer, inference_logprobs=inference, advantages=advantages, loss_mask=loss_mask, teacher_logprobs=teacher_lp, )
# ---------------------------------------------------------------------# Reference re-implementation (independent restatement of upstream).# Used by hand-computed expected-value tests so we don't accidentally# encode our own bugs as ground truth.# ---------------------------------------------------------------------def _reference_default_loss( trainer_lp: torch.Tensor, inference_lp: torch.Tensor, advantages: torch.Tensor, loss_mask: torch.Tensor, *, dppo_mask_high: float, dppo_mask_low: float, adv_tau: float, kl_tau: float,) -> torch.Tensor: log_ir = trainer_lp - inference_lp ir = torch.exp(log_ir) probs_diff = torch.exp(trainer_lp) - torch.exp(inference_lp) invalid_high = probs_diff > dppo_mask_high invalid_low = probs_diff < -dppo_mask_low pos_adv = advantages > 0 invalid = torch.where(pos_adv, invalid_high, invalid_low) keep = loss_mask.to(torch.bool) & ~invalid keep_f = keep.to(trainer_lp.dtype) lm_f = loss_mask.to(trainer_lp.dtype) pg = keep_f * (adv_tau * advantages) * ir kl = lm_f * log_ir**2 return (-pg + kl_tau * kl).sum()
# ---------------------------------------------------------------------# Test 1 — finite scalar on realistic (seq,) tensors# ---------------------------------------------------------------------def test_returns_finite_scalar(): inputs = _make_inputs(seq=16) out = loss_fn(inputs, alpha_sdpo=0.0, beta_dpo=0.0)
assert isinstance(out, torch.Tensor) assert out.shape == (), f"expected scalar, got shape {tuple(out.shape)}" assert torch.isfinite(out).item() # Differentiable: gradient flows to trainer_logprobs. out.backward() assert inputs.trainer_logprobs.grad is not None assert torch.isfinite(inputs.trainer_logprobs.grad).all().item()
# ---------------------------------------------------------------------# Test 2 — DPPO mask drops tokens whose probs_diff exceeds dppo_mask_high# (advantage-conditioned: positive advantages use the high gate)# ---------------------------------------------------------------------def test_dppo_mask_high_drops_positive_advantage_outliers(): """Token with positive advantage and probs_diff > dppo_mask_high is dropped.
Build a 4-token sample where token 0 has ``probs_diff`` huge and positive (trainer prob ~ 1, inference prob ~ 0) AND positive advantage. Tokens 1..3 have tiny probs_diff. With the upstream sign-conditioned gate, only token 0 should be dropped. """ # trainer_lp ~ 0 -> exp ~ 1; inference_lp = -10 -> exp ~ 4.5e-5. # probs_diff[0] ~ 1.0 >> dppo_mask_high (0.2). trainer_lp = torch.tensor( [0.0, math.log(0.30), math.log(0.40), math.log(0.50)], requires_grad=True, ) inference_lp = torch.tensor( [-10.0, math.log(0.31), math.log(0.39), math.log(0.51)] ) advantages = torch.tensor([+5.0, +1.0, -1.0, +1.0]) mask = torch.ones(4, dtype=torch.bool)
inputs = FakeLossInputs( trainer_logprobs=trainer_lp, inference_logprobs=inference_lp, advantages=advantages, loss_mask=mask, ) out = loss_fn( inputs, alpha_sdpo=0.0, beta_dpo=0.0, dppo_mask_high=0.2, dppo_mask_low=0.2, adv_tau=1.0, kl_tau=1e-3, )
expected = _reference_default_loss( trainer_lp.detach(), inference_lp, advantages, mask, dppo_mask_high=0.2, dppo_mask_low=0.2, adv_tau=1.0, kl_tau=1e-3, ) assert torch.isclose(out, expected, atol=1e-5), ( f"got {out.item()}, expected {expected.item()}" )
# Token 0 was DPPO-dropped from pg_loss but still contributes to kl_loss # (loss_mask gates KL, not the DPPO mask). The pg gradient on token 0 # should be zero; KL contributes a small grad. We assert the pg path # is masked by checking the gradient magnitude is dominated by the # tiny kl_tau * 2 * log_ir term, not by the +5 advantage. out.backward() g0 = inputs.trainer_logprobs.grad[0].item() # If pg weren't masked, |g0| would be on the order of # advantage * importance_ratio * 1 ~ 5 * exp(10) ~ 1e5. # With pg masked, |g0| is on the order of # 2 * kl_tau * log_ir ~ 2 * 1e-3 * 10 = 0.02. assert abs(g0) < 1.0, ( f"DPPO mask should suppress the pg gradient on token 0; got |g0|={abs(g0)}" )
# ---------------------------------------------------------------------# Test 3 — DPPO mask catches the lower bound on negative-advantage tokens# ---------------------------------------------------------------------def test_dppo_mask_low_drops_negative_advantage_outliers(): """Symmetric coverage: probs_diff < -dppo_mask_low drops a NEGATIVE-adv token.""" # Token 0: trainer prob ~ 0, inference prob ~ 1, so probs_diff ~ -1. # Negative advantage -> the low gate applies -> dropped. trainer_lp = torch.tensor( [-10.0, math.log(0.30), math.log(0.40)], requires_grad=True ) inference_lp = torch.tensor( [0.0, math.log(0.31), math.log(0.39)] ) advantages = torch.tensor([-5.0, +1.0, -1.0]) mask = torch.ones(3, dtype=torch.bool)
inputs = FakeLossInputs( trainer_logprobs=trainer_lp, inference_logprobs=inference_lp, advantages=advantages, loss_mask=mask, ) out = loss_fn(inputs, alpha_sdpo=0.0, beta_dpo=0.0)
expected = _reference_default_loss( trainer_lp.detach(), inference_lp, advantages, mask, dppo_mask_high=0.2, dppo_mask_low=0.2, adv_tau=1.0, kl_tau=1e-3, ) assert torch.isclose(out, expected, atol=1e-5)
# ---------------------------------------------------------------------# Test 4 — sign-conditioning: a positive-advantage token whose probs_diff# is *negative* (and large in magnitude) is NOT dropped, because the# high gate doesn't fire on a negative probs_diff.# ---------------------------------------------------------------------def test_dppo_mask_sign_conditioned_on_advantage(): """A positive-advantage token with probs_diff < -dppo_mask_low survives.
PRIME-RL's gate is ``where(positive_advantages, invalid_high, invalid_low)``. For positive advantages it only checks the upper bound, so ``probs_diff = -0.9`` with a positive advantage is KEPT; with a negative advantage it would be DROPPED. """ # Token 0: probs_diff = exp(-10) - exp(0) ~ -1. Massively negative. trainer_lp_pos = torch.tensor([-10.0], requires_grad=True) inference_lp_pos = torch.tensor([0.0]) adv_pos = torch.tensor([+1.0]) mask = torch.ones(1, dtype=torch.bool)
inputs_pos = FakeLossInputs( trainer_logprobs=trainer_lp_pos, inference_logprobs=inference_lp_pos, advantages=adv_pos, loss_mask=mask, ) out_pos = loss_fn(inputs_pos, alpha_sdpo=0.0, beta_dpo=0.0)
# With positive advantage the LOW bound is not checked; the token is # KEPT. pg = +1 * exp(-10 - 0) = ~4.5e-5; kl = (-10)^2 = 100. # loss = -pg + 1e-3 * 100 ~ 0.1. expected_pos = _reference_default_loss( trainer_lp_pos.detach(), inference_lp_pos, adv_pos, mask, dppo_mask_high=0.2, dppo_mask_low=0.2, adv_tau=1.0, kl_tau=1e-3, ) assert torch.isclose(out_pos, expected_pos, atol=1e-5) # Sanity: token wasn't masked, so kl_tau alone shouldn't dominate to # zero — loss should be ~0.1, definitely not zero. assert out_pos.item() > 0.05
# Same probs_diff but negative advantage -> DROPPED from pg. trainer_lp_neg = torch.tensor([-10.0], requires_grad=True) inputs_neg = FakeLossInputs( trainer_logprobs=trainer_lp_neg, inference_logprobs=inference_lp_pos, advantages=torch.tensor([-1.0]), loss_mask=mask, ) out_neg = loss_fn(inputs_neg, alpha_sdpo=0.0, beta_dpo=0.0) expected_neg = _reference_default_loss( trainer_lp_neg.detach(), inference_lp_pos, torch.tensor([-1.0]), mask, dppo_mask_high=0.2, dppo_mask_low=0.2, adv_tau=1.0, kl_tau=1e-3, ) assert torch.isclose(out_neg, expected_neg, atol=1e-5)
# ---------------------------------------------------------------------# Test 5 — alpha_sdpo=0 must not raise (channel 2 disabled)# ---------------------------------------------------------------------def test_alpha_sdpo_zero_does_not_raise(): inputs = _make_inputs(seq=6, teacher=True) out = loss_fn(inputs, alpha_sdpo=0.0, beta_dpo=0.0) assert torch.isfinite(out).item()
# ---------------------------------------------------------------------# Test 6 — alpha_sdpo>0 still raises NotImplementedError# ---------------------------------------------------------------------def test_alpha_sdpo_nonzero_raises_not_implemented(): inputs = _make_inputs(seq=6, teacher=True) with pytest.raises(NotImplementedError, match="SDPO"): loss_fn(inputs, alpha_sdpo=0.5, beta_dpo=0.0)
def test_alpha_sdpo_nonzero_no_teacher_also_raises(): """Defensive: even without teacher_logprobs, alpha_sdpo>0 must fail rather than silently no-op.""" inputs = _make_inputs(seq=6, teacher=False) with pytest.raises(NotImplementedError): loss_fn(inputs, alpha_sdpo=0.5, beta_dpo=0.0)
# ---------------------------------------------------------------------# Test 7 — shape validation: (seq,) accepted, (B, T) rejected# ---------------------------------------------------------------------def test_advantages_shape_validates_seq_accepted(): inputs = _make_inputs(seq=12) out = loss_fn(inputs, alpha_sdpo=0.0, beta_dpo=0.0) assert out.shape == ()
def test_advantages_shape_validates_bt_rejected(): B, T = 2, 4 bad = FakeLossInputs( trainer_logprobs=torch.zeros(B, T, requires_grad=True), inference_logprobs=torch.zeros(B, T), advantages=torch.zeros(B, T), loss_mask=torch.ones(B, T, dtype=torch.bool), ) with pytest.raises(ValueError, match="1-D"): loss_fn(bad, alpha_sdpo=0.0, beta_dpo=0.0)
# ---------------------------------------------------------------------# Test 8 — beta_dpo != 0 emits a warning but does not raise# ---------------------------------------------------------------------def test_beta_dpo_nonzero_warns(): inputs = _make_inputs(seq=8) with pytest.warns(UserWarning, match="DPO channel"): out = loss_fn(inputs, alpha_sdpo=0.0, beta_dpo=0.3) assert torch.isfinite(out).item()
# ---------------------------------------------------------------------# Test 9 — config-validation knobs match PRIME-RL Field(..., ge=0)# ---------------------------------------------------------------------@pytest.mark.parametrize( "kw", [ {"dppo_mask_high": -0.1}, {"dppo_mask_low": -0.1}, {"adv_tau": -0.1}, {"kl_tau": -0.1}, ],)def test_negative_knobs_rejected(kw): inputs = _make_inputs(seq=4) with pytest.raises(ValueError, match=">= 0"): loss_fn(inputs, alpha_sdpo=0.0, beta_dpo=0.0, **kw)
# ---------------------------------------------------------------------# Test 10 — disabling masking via wide bounds gives plain DPPO+KL on all# tokens. This pins the "pure IS-corrected REINFORCE + KL" baseline.# ---------------------------------------------------------------------def test_dppo_bounds_can_be_disabled(): """Setting bounds to a huge value disables DPPO masking.
At dppo_mask_high=dppo_mask_low=1e6, ``probs_diff`` never exceeds the threshold so ``keep_mask == loss_mask`` and the loss reduces to the plain DPPO+KL on the whole sequence. """ seq = 4 trainer_lp = torch.tensor( [math.log(0.10), math.log(0.30), math.log(0.20), math.log(0.40)], requires_grad=True, ) inference_lp = torch.tensor( [math.log(0.11), math.log(0.31), math.log(0.21), math.log(0.39)] ) advantages = torch.tensor([+1.0, -1.0, +0.5, -0.5]) mask = torch.ones(seq, dtype=torch.bool) inputs = FakeLossInputs( trainer_logprobs=trainer_lp, inference_logprobs=inference_lp, advantages=advantages, loss_mask=mask, )
out = loss_fn( inputs, alpha_sdpo=0.0, beta_dpo=0.0, dppo_mask_high=1e6, dppo_mask_low=1e6, adv_tau=1.0, kl_tau=1e-3, )
expected = _reference_default_loss( trainer_lp.detach(), inference_lp, advantages, mask, dppo_mask_high=1e6, dppo_mask_low=1e6, adv_tau=1.0, kl_tau=1e-3, ) assert torch.isclose(out, expected, atol=1e-6)
# ---------------------------------------------------------------------# Test 11 — PARITY against PRIME-RL upstream's default_loss_fn.# Skip-marked when prime-rl is not installable.# ---------------------------------------------------------------------@pytest.mark.skipif( not _HAS_PRIME_RL, reason="prime-rl not installed; skipping upstream parity test",)def test_parity_with_prime_rl_default_loss_fn(): """Run identical inputs through ours and PRIME-RL's; loss must match.""" seq = 32 g = torch.Generator().manual_seed(42) trainer_lp = -(0.1 + 2.0 * torch.rand(seq, generator=g)).to(torch.float32) inference_lp = (trainer_lp + 0.05 * torch.randn(seq, generator=g)).to(torch.float32) advantages = torch.randn(seq, generator=g, dtype=torch.float32) loss_mask = torch.ones(seq, dtype=torch.bool)
# Use PRIME-RL's defaults (dppo_mask_high=0.2, etc.) directly. cfg = PrimeRLDefaultLossConfig() # type: ignore[name-defined]
upstream_inputs = PrimeRLLossInputs( # type: ignore[name-defined] trainer_logprobs=trainer_lp, inference_logprobs=inference_lp, teacher_logprobs=None, advantages=advantages, loss_mask=loss_mask, ) upstream_out = prime_rl_default_loss_fn(upstream_inputs, cfg) # type: ignore[name-defined]
ours = loss_fn( FakeLossInputs( trainer_logprobs=trainer_lp.clone(), inference_logprobs=inference_lp.clone(), advantages=advantages.clone(), loss_mask=loss_mask.clone(), ), alpha_sdpo=0.0, beta_dpo=0.0, dppo_mask_high=cfg.dppo_mask_high, dppo_mask_low=cfg.dppo_mask_low, adv_tau=cfg.adv_tau, kl_tau=cfg.kl_tau, )
assert torch.isclose(ours, upstream_out.loss, atol=1e-5, rtol=1e-5), ( f"Parity mismatch with PRIME-RL upstream: ours={ours.item()}, " f"upstream={upstream_out.loss.item()}" )7 unmodified lines89101112131415161718192021222324252627283 unmodified lines323334353637381 unmodified line40414243444546474849505152535455565758596061627 unmodified lines## {# "state_id": "...",# "messages": [{"role": "user", "content": "..."}],# "chosen": [{"role": "assistant", "content": "..."}],# "rejected": [{"role": "assistant", "content": "..."}],# "chosen_teacher": "...",# "rejected_teacher": "..."# }## Ops listed in `process` are applied in order. Each op operates on the# full record but typically reads/writes one field. data-juicer's# DPO/preference-pair ops know how to handle the chosen/rejected pair# structure natively.# Project & I/O are filled in by DJNormalizer at runtime; we only# specify the op pipeline here.# --- Op pipeline (applied in order) -----------------------------------process:3 unmodified lines- text_length_filter:min_len: 8max_len: 32000text_keys: ["chosen", "rejected"]# 2. Word-count filter on response.# Drops pairs with absurdly low (< 2 words) or high (> 4096 words)1 unmodified line- words_num_filter:min_num: 2max_num: 4096text_keys: ["chosen", "rejected"]# 3. Special-character filter.# Drops responses where >50% of characters are non-alphabetic# special chars (likely encoding errors or junk).- special_characters_filter:max_ratio: 0.5text_keys: ["chosen", "rejected"]# 4. Per-conversation deduplication.# If the chosen and rejected responses are identical (no real# disagreement), drop the pair.- document_deduplicator:lowercase: trueignore_non_character: truetext_keys: ["chosen"]# data-juicer's per-batch dedup; full corpus dedup is a separate op.# Notes:# - We DO NOT run `pair_preference_mapper` because its default config may7 unmodified lines8910111213141516171819202122232425262728293031323334353637383940414243444546474849503 unmodified lines54555657585960616263641 unmodified line666768697071727374757677787980818283848586878889909192939495967 unmodified lines## {# "state_id": "...",# "messages": [{"role": "user", "content": "..."}], # context# # --- flat-string shape (consumed by length/word/special-char/dedup filters) ---# "chosen": "the chosen response as a plain string",# "rejected": "the rejected response as a plain string",# # --- chat-messages shape (preserved for chat-aware ops + round-trip) ---# "chosen_messages": [{"role": "assistant", "content": "..."}],# "rejected_messages": [{"role": "assistant", "content": "..."}],# "n_teachers_agreeing": 2# }## IMPORTANT — field-key contract:# data-juicer's `text_length_filter`, `words_num_filter`,# `special_characters_filter` and `document_deduplicator` all read a SINGLE# string field named by `text_key` (singular). They expect plain strings.# Pointing them at a list-of-dicts (the chat-messages shape) crashes or# silently no-ops. We therefore keep two parallel representations:# * `chosen` / `rejected` — plain strings, fed to filter ops below.# * `chosen_messages` / `rejected_messages` — chat-messages list, preserved# untouched for downstream chat-aware consumers and the round-trip.## data-juicer caveat: each filter op accepts only ONE `text_key`. To filter# both `chosen` AND `rejected`, we duplicate each op — once with# `text_key: chosen`, once with `text_key: rejected`. The top-level# `text_keys: chosen` below also satisfies data-juicer's dataset-load# validation (the formatter checks the global text_key exists in the dataset).## Ops listed in `process` are applied in order. Each op operates on the# full record but reads/writes one field.# Project & I/O are filled in by DJNormalizer at runtime; we only# specify the op pipeline here.# --- Global text-key contract (see header note) -----------------------# data-juicer validates this exists on the dataset before any op runs, and# uses it as the default text_key for ops that don't specify their own.text_keys: chosen# --- Op pipeline (applied in order) -----------------------------------process:3 unmodified lines- text_length_filter:min_len: 8max_len: 32000text_key: chosen- text_length_filter:min_len: 8max_len: 32000text_key: rejected# 2. Word-count filter on response.# Drops pairs with absurdly low (< 2 words) or high (> 4096 words)1 unmodified line- words_num_filter:min_num: 2max_num: 4096text_key: chosen- words_num_filter:min_num: 2max_num: 4096text_key: rejected# 3. Special-character filter.# Drops responses where >50% of characters are non-alphabetic# special chars (likely encoding errors or junk).- special_characters_filter:max_ratio: 0.5text_key: chosen- special_characters_filter:max_ratio: 0.5text_key: rejected# 4. Per-conversation deduplication.# Within the batch, drop records where the `chosen` field is a# duplicate of another record's `chosen`. (data-juicer's# document_deduplicator is per-batch hashing — full-corpus dedup is# a separate op family.)- document_deduplicator:lowercase: trueignore_non_character: truetext_key: chosen# Notes:# - We DO NOT run `pair_preference_mapper` because its default config may
70 unmodified lines717273747576777879808182838485868788899091929394959670 unmodified linesdef _dpo_pair_to_dj_record(pair: DPOPair | dict[str, Any]) -> dict[str, Any]:"""Convert a DPOPair (or dict-shaped equivalent) into a data-juicerrecord using the messages format."""p = cast(dict[str, Any], pair)return {"state_id": p.get("state_id", ""),"messages": p.get("state_messages", []),"chosen": [{"role": "assistant", "content": p.get("chosen", "")}],"rejected": [{"role": "assistant", "content": p.get("rejected", "")}],"n_teachers_agreeing": p.get("n_teachers_agreeing", 0),}def _dj_record_to_normalized(rec: dict[str, Any]) -> NormalizedDPOPair:"""Inverse — convert a data-juicer record back to NormalizedDPOPair."""return NormalizedDPOPair(state_id=rec.get("state_id", ""),state_messages=rec.get("messages", []),chosen_messages=rec.get("chosen", []),rejected_messages=rec.get("rejected", []),n_teachers_agreeing=rec.get("n_teachers_agreeing", 0),metadata=rec.get("__dj_meta__", {}),)70 unmodified lines717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114270 unmodified linesdef _dpo_pair_to_dj_record(pair: DPOPair | dict[str, Any]) -> dict[str, Any]:"""Convert a DPOPair (or dict-shaped equivalent) into a data-juicer record.The record carries TWO shapes for chosen/rejected so that data-juicer opsthat expect string-typed text fields (e.g. ``text_length_filter``,``words_num_filter``, ``special_characters_filter``,``document_deduplicator``) work alongside chat-aware ops:- ``chosen`` / ``rejected``: flat strings (drives the standard text opsthat read string fields via ``text_keys``).- ``chosen_messages`` / ``rejected_messages``: chat-messages list(one assistant turn each), preserving the multi-turn-aware shape.The ``messages`` field carries the conversation context (matchesdata-juicer's ``messages`` convention for chat-aware filters)."""p = cast(dict[str, Any], pair)chosen_str = p.get("chosen", "") or ""rejected_str = p.get("rejected", "") or ""return {"state_id": p.get("state_id", ""),"messages": p.get("state_messages", []),# Flat-string shape for length/word/special-char/dedup filters# that expect text_keys to point at strings."chosen": chosen_str,"rejected": rejected_str,# Chat-messages shape for chat-aware ops and the NormalizedDPOPair# round-trip."chosen_messages": [{"role": "assistant", "content": chosen_str}],"rejected_messages": [{"role": "assistant", "content": rejected_str}],"n_teachers_agreeing": p.get("n_teachers_agreeing", 0),}def _dj_record_to_normalized(rec: dict[str, Any]) -> NormalizedDPOPair:"""Inverse — convert a data-juicer record back to NormalizedDPOPair.Tolerates records that only carry one of the two shapes:- If ``chosen_messages``/``rejected_messages`` are present, use themdirectly.- Otherwise wrap the flat-string ``chosen``/``rejected`` fields intoa single-assistant-turn messages list. This handles the case wherea data-juicer op rewrites the string field but doesn't touch themessages field."""def _to_messages(val: Any, fallback_str: Any) -> list[dict[str, Any]]:if isinstance(val, list) and val:return val # already chat-messages shapeif isinstance(fallback_str, str) and fallback_str:return [{"role": "assistant", "content": fallback_str}]if isinstance(fallback_str, list):# Edge case: someone put the messages list in the flat field.return fallback_strreturn []chosen_messages = _to_messages(rec.get("chosen_messages"), rec.get("chosen", ""))rejected_messages = _to_messages(rec.get("rejected_messages"), rec.get("rejected", ""))return NormalizedDPOPair(state_id=rec.get("state_id", ""),state_messages=rec.get("messages", []),chosen_messages=chosen_messages,rejected_messages=rejected_messages,n_teachers_agreeing=rec.get("n_teachers_agreeing", 0),metadata=rec.get("__dj_meta__", {}),)
41 unmodified lines42434445464748495051525382 unmodified lines13613713841 unmodified linesdef test_dpo_pair_to_dj_record_shape():p = _make_pair("s1")rec = _dpo_pair_to_dj_record(p)assert rec["state_id"] == "s1"assert rec["messages"] == [{"role": "user", "content": "What is 2+2?"}]assert rec["chosen"] == [{"role": "assistant", "content": "Four."}]assert rec["rejected"] == [{"role": "assistant", "content": "Five."}]assert rec["n_teachers_agreeing"] == 282 unmodified linesassert rec["state_id"] == "x"assert rec["messages"] == [] # missing state_messages → empty listassert rec["n_teachers_agreeing"] == 0 # missing → default 041 unmodified lines42434445464748495051525354555657585960616263646582 unmodified lines14814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632741 unmodified linesdef test_dpo_pair_to_dj_record_shape():"""Records carry BOTH flat-string and chat-messages shapes for chosen/rejected.See default.yaml header for why: data-juicer's text_length_filter et alconsume the flat strings; chat-aware consumers and the round-trip usethe *_messages fields."""p = _make_pair("s1")rec = _dpo_pair_to_dj_record(p)assert rec["state_id"] == "s1"assert rec["messages"] == [{"role": "user", "content": "What is 2+2?"}]# Flat-string shape (drives text_length_filter, words_num_filter, ...)assert rec["chosen"] == "Four."assert rec["rejected"] == "Five."assert isinstance(rec["chosen"], str)assert isinstance(rec["rejected"], str)# Chat-messages shape (preserved for chat-aware ops)assert rec["chosen_messages"] == [{"role": "assistant", "content": "Four."}]assert rec["rejected_messages"] == [{"role": "assistant", "content": "Five."}]assert rec["n_teachers_agreeing"] == 282 unmodified linesassert rec["state_id"] == "x"assert rec["messages"] == [] # missing state_messages → empty listassert rec["n_teachers_agreeing"] == 0 # missing → default 0# ---------------------------------------------------------------------# Dual-shape contract (Wave 13 review Suggestion 3)# ---------------------------------------------------------------------## data-juicer's text_length_filter / words_num_filter /# special_characters_filter / document_deduplicator all expect string-typed# fields under `text_keys`. Earlier the converter wrapped chosen/rejected# into list-of-dicts (chat-messages), which would have caused those ops to# crash or no-op silently. The fix carries BOTH shapes:# - chosen / rejected → flat strings (filter ops)# - chosen_messages / rejected_messages → list-of-dicts (chat-aware ops + round-trip)## The tests below pin that contract.def test_record_chosen_rejected_are_flat_strings_for_dj_text_ops():"""text_length_filter & friends expect text_keys to point at strings.If we ever regress to wrapping `chosen`/`rejected` into list-of-dicts,data-juicer's text-key ops break. Keep this red-line explicit."""p = _make_pair("s_strings",chosen="A long-enough chosen response.",rejected="A long-enough rejected response.",)rec = _dpo_pair_to_dj_record(p)assert isinstance(rec["chosen"], str)assert isinstance(rec["rejected"], str)assert rec["chosen"] == "A long-enough chosen response."assert rec["rejected"] == "A long-enough rejected response."# Sanity: text_length_filter style usage works without crashing.assert len(rec["chosen"]) >= 8assert len(rec["rejected"]) >= 8def test_record_chosen_rejected_messages_carry_chat_shape():"""The *_messages variants preserve the chat-template-aware shape."""p = _make_pair("s_msgs", chosen="hello world", rejected="goodbye world")rec = _dpo_pair_to_dj_record(p)assert isinstance(rec["chosen_messages"], list)assert isinstance(rec["rejected_messages"], list)assert rec["chosen_messages"] == [{"role": "assistant", "content": "hello world"}]assert rec["rejected_messages"] == [{"role": "assistant", "content": "goodbye world"}]# Both shapes must agree on content.assert rec["chosen_messages"][0]["content"] == rec["chosen"]assert rec["rejected_messages"][0]["content"] == rec["rejected"]def test_dj_record_to_normalized_uses_chat_messages_when_present():"""When *_messages fields are present, the round-trip uses them directly(does not re-wrap the flat string)."""rec = {"state_id": "s_present","messages": [{"role": "user", "content": "q"}],"chosen": "some flat str — should be ignored when _messages present","rejected": "another flat str","chosen_messages": [{"role": "assistant", "content": "real chosen"},],"rejected_messages": [{"role": "assistant", "content": "real rejected"},],"n_teachers_agreeing": 4,}norm = _dj_record_to_normalized(rec)assert norm.chosen_messages == [{"role": "assistant", "content": "real chosen"}]assert norm.rejected_messages == [{"role": "assistant", "content": "real rejected"}]assert norm.n_teachers_agreeing == 4def test_dj_record_to_normalized_falls_back_to_flat_strings():"""When *_messages fields are absent (e.g. an op only rewrote the flatstring), the round-trip wraps the flat string into a single assistantturn so downstream consumers always see the chat-messages shape."""rec = {"state_id": "s_fallback","messages": [{"role": "user", "content": "q"}],"chosen": "rewritten chosen","rejected": "rewritten rejected",# NOTE: no chosen_messages / rejected_messages"n_teachers_agreeing": 1,}norm = _dj_record_to_normalized(rec)assert norm.chosen_messages == [{"role": "assistant", "content": "rewritten chosen"}]assert norm.rejected_messages == [{"role": "assistant", "content": "rewritten rejected"}]def test_round_trip_preserves_strings_through_skip_dj():"""End-to-end shape sanity: pair → normalize(skip_dj=True) → assertchat-messages content matches original strings."""pairs = [_make_pair("rt1", chosen="alpha", rejected="beta", n_teachers_agreeing=2),_make_pair("rt2", chosen="gamma", rejected="delta", n_teachers_agreeing=3),]out = DJNormalizer(skip_dj=True).normalize(pairs)assert len(out) == 2assert out[0].chosen_messages[0]["content"] == "alpha"assert out[0].rejected_messages[0]["content"] == "beta"assert out[1].chosen_messages[0]["content"] == "gamma"assert out[1].rejected_messages[0]["content"] == "delta"# ---------------------------------------------------------------------# End-to-end test against the real data-juicer engine.# ---------------------------------------------------------------------## Install path tried during Wave 13 fix: `pip install py-data-juicer`# (the canonical PyPI distribution name; `data-juicer` redirects there).# If that succeeded in the runtime environment, the e2e test runs the# actual op-graph from default.yaml against a tiny fixture and verifies# the dual-shape contract holds at the JSONL boundary. If data-juicer# is NOT importable, the test is skipped.try:import data_juicer # type: ignore[import-not-found] # noqa: F401_HAS_DJ = Trueexcept ImportError:_HAS_DJ = False@pytest.mark.skipif(not _HAS_DJ, reason="data-juicer not installed")def test_dj_normalizer_e2e_default_recipe(tmp_path):"""E2E: real data-juicer engine + default.yaml on a 3-record fixture.Verifies:1. The engine runs without a type-mismatch crash on the flat-stringtext_keys (this is the bug Wave 13 Suggestion 3 flagged).2. Output records survive the round-trip with both shapes intact."""pairs = [_make_pair("e2e1",chosen="A reasonably long chosen response with several words.",rejected="A reasonably long rejected response with several words.",n_teachers_agreeing=2,),_make_pair("e2e2",chosen="Another solid chosen completion that has enough text.",rejected="Another solid rejected completion that has enough text.",n_teachers_agreeing=3,),_make_pair("e2e3",chosen="Third chosen example with sufficient length to pass.",rejected="Third rejected example with sufficient length to pass.",n_teachers_agreeing=2,),]normalizer = DJNormalizer(skip_dj=False)out = normalizer.normalize(pairs)# Length filter, etc., should NOT drop any of these — all are# comfortably within bounds. If we get back fewer than 1, the op-graph# is misconfigured.assert len(out) >= 1for n in out:assert isinstance(n, NormalizedDPOPair)# Round-trip should always give us chat-messages shape on the way out.assert isinstance(n.chosen_messages, list)assert isinstance(n.rejected_messages, list)assert n.chosen_messages and n.chosen_messages[0]["role"] == "assistant"assert n.rejected_messages and n.rejected_messages[0]["role"] == "assistant"
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416"""Integration tests for ADR-007 distillation kwargs in compose_loss.
These tests exercise the wiring between `compose_loss` and the threepluggable losses (SimPO, TAID, Entropy-Aware OPD). They use a tinyhand-rolled language model wrapper (no HF, no TRL) so the tests runin <1s on CPU and are isolated from external library churn.
Coverage requirements (from Wave 13 BLOCKER 2 fix): (a) defaults reproduce existing compose_loss output bit-exact (b) dpo_variant='simpo' produces a different total than dpo (c) sdpo_wrapper='taid' with schedule_step=0 reproduces existing SDPO when alpha_min=alpha_max=1.0 (d) sdpo_wrapper='taid' interpolates as expected when schedule_step=total_steps/2 (e) sdpo_wrapper='entropy_opd' returns a finite differentiable scalar (f) error case: sdpo_wrapper='taid' without taid_schedule_step raises ValueError"""from __future__ import annotations
import pytestimport torchimport torch.nn as nn
from composer_replication import LossComponents, compose_loss
# ----------------------------------------------------------------------# Tiny LM stand-in# ----------------------------------------------------------------------
class TinyLM(nn.Module): """Minimal `nn.Module` with the HF-style `model(input_ids=...).logits` API.
Vocab=32, hidden=16, two-layer MLP head. Tiny enough that all tests run in milliseconds on CPU. """
def __init__(self, vocab: int = 32, hidden: int = 16, seed: int = 0): super().__init__() torch.manual_seed(seed) self.embed = nn.Embedding(vocab, hidden) self.fc = nn.Linear(hidden, hidden) self.head = nn.Linear(hidden, vocab)
def forward(self, input_ids: torch.Tensor): h = torch.tanh(self.fc(self.embed(input_ids))) logits = self.head(h)
class _Out: pass out = _Out() out.logits = logits return out
# ----------------------------------------------------------------------# Batch fixtures# ----------------------------------------------------------------------
VOCAB = 32B = 2T = 8
def _base_batch(seed: int = 7, *, with_dpo: bool = True) -> dict[str, torch.Tensor]: """Build a deterministic input batch with all 3 channels populated.""" g = torch.Generator().manual_seed(seed) inputs: dict[str, torch.Tensor] = { "input_ids": torch.randint(0, VOCAB, (B, T), generator=g), "response_mask": torch.zeros(B, T, dtype=torch.long), "ctx_teacher_input_ids": torch.randint(0, VOCAB, (B, T), generator=g), "sdpo_loss_mask": torch.zeros(B, T, dtype=torch.long), } # Mark the second half as response tokens so the LM-CE channel is non-trivial. inputs["response_mask"][:, T // 2:] = 1 inputs["sdpo_loss_mask"][:, T // 2:] = 1
if with_dpo: inputs["dpo_chosen_input_ids"] = torch.randint(0, VOCAB, (B, T), generator=g) inputs["dpo_chosen_response_mask"] = torch.ones(B, T, dtype=torch.long) inputs["dpo_rejected_input_ids"] = torch.randint(0, VOCAB, (B, T), generator=g) inputs["dpo_rejected_response_mask"] = torch.ones(B, T, dtype=torch.long) # Standard DPO needs ref logprobs; SimPO ignores them. inputs["dpo_chosen_ref_logprobs"] = torch.randn(B, generator=g) inputs["dpo_rejected_ref_logprobs"] = torch.randn(B, generator=g) return inputs
def _model_seeded(seed: int = 0) -> TinyLM: m = TinyLM(vocab=VOCAB, hidden=16, seed=seed) m.eval() # Deterministic forward — no dropout. return m
# ----------------------------------------------------------------------# (a) Defaults reproduce existing output bit-exact# ----------------------------------------------------------------------
def test_defaults_bit_exact_with_legacy_kwargs(): """Calling compose_loss with new kwargs at their defaults must equal calling it with only the legacy kwargs. Bit-exact: every channel + total agree to 0 ULPs because the code path is identical. """ inputs = _base_batch()
model_a = _model_seeded(seed=0) out_legacy = compose_loss( model_a, inputs, alpha_sdpo=0.1, beta_replay=0.05, sdpo_jsd_beta=0.5, sdpo_temperature=1.0, replay_dpo_beta=0.1, )
model_b = _model_seeded(seed=0) out_new = compose_loss( model_b, inputs, alpha_sdpo=0.1, beta_replay=0.05, sdpo_jsd_beta=0.5, sdpo_temperature=1.0, replay_dpo_beta=0.1, dpo_variant="dpo", sdpo_wrapper="none", )
assert isinstance(out_new, LossComponents) assert torch.equal(out_legacy.lm_ce, out_new.lm_ce) assert torch.equal(out_legacy.sdpo_jsd, out_new.sdpo_jsd) assert torch.equal(out_legacy.trace_replay_dpo, out_new.trace_replay_dpo) assert torch.equal(out_legacy.total, out_new.total)
# ----------------------------------------------------------------------# (b) dpo_variant='simpo' produces a different total than dpo# ----------------------------------------------------------------------
def test_simpo_variant_changes_total(): """SimPO uses average-logprob and drops the reference subtraction, so it must produce a different (and finite) trace_replay_dpo + total.""" inputs = _base_batch()
model_a = _model_seeded(seed=0) out_dpo = compose_loss( model_a, inputs, alpha_sdpo=0.0, # isolate channel 3 beta_replay=0.05, dpo_variant="dpo", )
model_b = _model_seeded(seed=0) out_simpo = compose_loss( model_b, inputs, alpha_sdpo=0.0, beta_replay=0.05, dpo_variant="simpo", )
assert torch.isfinite(out_simpo.total) assert torch.isfinite(out_simpo.trace_replay_dpo) # Different formulae => different values. assert not torch.allclose( out_dpo.trace_replay_dpo, out_simpo.trace_replay_dpo ) assert not torch.allclose(out_dpo.total, out_simpo.total) # Gradient flow check. out_simpo.total.backward() assert any( p.grad is not None and torch.isfinite(p.grad).all() for p in model_b.parameters() )
def test_simpo_does_not_require_ref_logprobs(): """SimPO is reference-free; compose_loss should run when those keys are absent from `inputs` (only when dpo_variant='simpo').""" inputs = _base_batch() inputs.pop("dpo_chosen_ref_logprobs") inputs.pop("dpo_rejected_ref_logprobs")
model = _model_seeded(seed=0) out = compose_loss( model, inputs, alpha_sdpo=0.0, beta_replay=0.05, dpo_variant="simpo", ) assert torch.isfinite(out.total) assert torch.isfinite(out.trace_replay_dpo)
# ----------------------------------------------------------------------# (c) TAID with schedule_step=0, alpha_min=alpha_max=1.0 ==> pure SDPO# ----------------------------------------------------------------------
def test_taid_alpha_one_recovers_sdpo(): """With alpha_min=alpha_max=1.0, the TAID schedule is pinned at α=1 regardless of step. The blended target collapses to pure teacher, making channel 2 numerically equivalent to the standard SDPO path (modulo the softmax→log roundtrip in `taid_blended_logits`, which is bit-equivalent for finite logits). """ inputs = _base_batch(with_dpo=False)
model_a = _model_seeded(seed=1) out_sdpo = compose_loss( model_a, inputs, alpha_sdpo=0.1, beta_replay=0.0, # disable channel 3 so we isolate channel 2 sdpo_wrapper="none", )
model_b = _model_seeded(seed=1) # Provide a student_init_logits snapshot — for α=1 its value doesn't # affect the blended target (P_blended = teacher when α=1), so any # valid-shape tensor works. Use the teacher shape. with torch.no_grad(): init_logits = model_b(input_ids=inputs["ctx_teacher_input_ids"]).logits.clone() inputs_taid = dict(inputs) inputs_taid["student_init_logits"] = init_logits
out_taid = compose_loss( model_b, inputs_taid, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="taid", taid_schedule_step=0, taid_total_steps=100, taid_alpha_min=1.0, taid_alpha_max=1.0, )
# Same channel-2 value up to numerical roundtrip through softmax→log. assert torch.allclose(out_sdpo.sdpo_jsd, out_taid.sdpo_jsd, atol=1e-5, rtol=1e-5) assert torch.allclose(out_sdpo.total, out_taid.total, atol=1e-5, rtol=1e-5)
# ----------------------------------------------------------------------# (d) TAID interpolates at schedule_step = total_steps / 2# ----------------------------------------------------------------------
def test_taid_interpolates_at_midpoint(): """At step=total_steps/2 with schedule='linear' and alpha_min=0, alpha_max=1, the schedule yields α=0.5. The resulting loss must differ from both endpoints (α=0 → init-only target, α=1 → pure SDPO), and must be finite + differentiable. """ inputs = _base_batch(with_dpo=False)
# Build a single shared student_init_logits snapshot. We use a # *different-seed* model to produce it so the blended target actually # differs from the live student's teacher forward (otherwise α=0 and # α=1 would both target the same distribution and the test would # become vacuous). snapshot_model = _model_seeded(seed=99) with torch.no_grad(): init_logits = snapshot_model( input_ids=inputs["ctx_teacher_input_ids"] ).logits.clone() inputs = dict(inputs) inputs["student_init_logits"] = init_logits
# Endpoint α=1 (pure SDPO target — init_logits ignored) model_end = _model_seeded(seed=2) out_alpha_one = compose_loss( model_end, inputs, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="taid", taid_schedule_step=100, taid_total_steps=100, taid_alpha_min=0.0, taid_alpha_max=1.0, )
# Endpoint α=0 (pure init target — teacher_logits ignored) model_start = _model_seeded(seed=2) out_alpha_zero = compose_loss( model_start, inputs, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="taid", taid_schedule_step=0, taid_total_steps=100, taid_alpha_min=0.0, taid_alpha_max=1.0, )
# Midpoint α=0.5 model_mid = _model_seeded(seed=2) out_mid = compose_loss( model_mid, inputs, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="taid", taid_schedule_step=50, taid_total_steps=100, taid_alpha_min=0.0, taid_alpha_max=1.0, )
# All finite. for out in (out_alpha_zero, out_mid, out_alpha_one): assert torch.isfinite(out.total), f"non-finite total: {out.total}" assert torch.isfinite(out.sdpo_jsd), f"non-finite sdpo_jsd: {out.sdpo_jsd}"
# Midpoint must differ from both endpoints — different blended target. assert not torch.allclose( out_mid.sdpo_jsd, out_alpha_zero.sdpo_jsd, atol=1e-5 ), "midpoint TAID matches α=0 endpoint — schedule not interpolating" assert not torch.allclose( out_mid.sdpo_jsd, out_alpha_one.sdpo_jsd, atol=1e-5 ), "midpoint TAID matches α=1 endpoint — schedule not interpolating"
# Differentiable. out_mid.total.backward() assert any( p.grad is not None and torch.isfinite(p.grad).all() for p in model_mid.parameters() )
# ----------------------------------------------------------------------# (e) Entropy-Aware OPD returns a finite differentiable scalar# ----------------------------------------------------------------------
def test_entropy_opd_returns_finite_differentiable_scalar(): inputs = _base_batch(with_dpo=False)
model = _model_seeded(seed=3) out = compose_loss( model, inputs, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="entropy_opd", )
assert isinstance(out, LossComponents) assert out.total.shape == () assert torch.isfinite(out.total) assert torch.isfinite(out.sdpo_jsd) assert out.total.requires_grad
out.total.backward() grads = [p.grad for p in model.parameters() if p.grad is not None] assert len(grads) > 0 assert all(torch.isfinite(g).all() for g in grads)
# ----------------------------------------------------------------------# (f) Error: sdpo_wrapper='taid' without taid_schedule_step# ----------------------------------------------------------------------
def test_taid_requires_schedule_step(): inputs = _base_batch(with_dpo=False) model = _model_seeded(seed=4) with pytest.raises(ValueError, match="taid_schedule_step"): compose_loss( model, inputs, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="taid", taid_total_steps=100, # taid_schedule_step omitted on purpose )
def test_taid_requires_total_steps(): inputs = _base_batch(with_dpo=False) model = _model_seeded(seed=4) with pytest.raises(ValueError, match="taid_total_steps"): compose_loss( model, inputs, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="taid", taid_schedule_step=0, # taid_total_steps omitted on purpose )
def test_invalid_dpo_variant_raises(): inputs = _base_batch() model = _model_seeded(seed=5) with pytest.raises(ValueError, match="dpo_variant"): compose_loss( model, inputs, dpo_variant="bogus", # type: ignore[arg-type] )
def test_invalid_sdpo_wrapper_raises(): inputs = _base_batch() model = _model_seeded(seed=5) with pytest.raises(ValueError, match="sdpo_wrapper"): compose_loss( model, inputs, sdpo_wrapper="bogus", # type: ignore[arg-type] )
# ----------------------------------------------------------------------# Bonus: TAID accepts precomputed init logits# ----------------------------------------------------------------------
def test_taid_accepts_precomputed_student_init_logits(): """The preferred path: caller saves a step-0 logits snapshot and passes it as `inputs['student_init_logits']`.""" inputs = _base_batch(with_dpo=False) model = _model_seeded(seed=6)
# Pre-compute init logits the way a real trainer would. with torch.no_grad(): init_logits = model(input_ids=inputs["ctx_teacher_input_ids"]).logits.clone() inputs["student_init_logits"] = init_logits
out = compose_loss( model, inputs, alpha_sdpo=0.1, beta_replay=0.0, sdpo_wrapper="taid", taid_schedule_step=10, taid_total_steps=100, ) assert torch.isfinite(out.total)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484# API Reference — composer-replication-framework
Complete reference for every public symbol in `composer_replication`. Source-of-truth is the `.py` files in `composer_replication/`; docstrings have been pulled verbatim where they exist and supplemented where missing.
**Legend**
- ⚠️ **UNTESTED-CONTRACT** — symbol exists and is callable, but its behaviour is not pinned by an automated test in `composer_replication/**/tests/` or `spikes/**/tests/`.- 🟡 **SKELETON** — class/method body raises `NotImplementedError`; ships as design-of-record per ADR-005 / ADR-006.
**Module groups (in this document)**
1. `composer_replication` (top-level re-exports)2. `composer_replication.loss`3. `composer_replication.batch`4. `composer_replication.opsd`5. `composer_replication.distillation`6. `composer_replication.teacher_replay`7. `composer_replication.replaysim`8. `composer_replication.ingestion` (+ `.claude_code`)9. `composer_replication.hint_generator`10. `composer_replication.trainer` (+ `.composer_trainer`, `.data_collator`)11. `composer_replication.diloco`12. `composer_replication.diloco.serverless` (+ `.executor`, `.allreduce`, `.modal`, `.hf_jobs`, `.replica_entrypoint`)13. `composer_replication.recipes.prime_rl.composer_loss`14. `composer_replication.recipes.monarch.actors`
---
## 1. `composer_replication` — top-level package
The package re-exports the most common entry points from sub-modules. `__all__` is the canonical list of public top-level names.
### `composer_replication.__version__: str`
Package version string. Currently `"0.1.0"`.
```pythonimport composer_replicationprint(composer_replication.__version__) # "0.1.0"```
### `composer_replication._DILOCO_AVAILABLE: bool`
`True` iff `torchft` is importable in the running Python environment (gates `make_diloco_outer_loop`). Set to `False` and `make_diloco_outer_loop` is set to `None` when `torchft` is missing.
```pythonfrom composer_replication import _DILOCO_AVAILABLEif _DILOCO_AVAILABLE: from composer_replication import make_diloco_outer_loop```
### Re-exports
| Name | Source module ||---|---|| `compose_loss` | `composer_replication.loss` || `LossComponents` | `composer_replication.loss` || `build_batch` | `composer_replication.batch` || `generalized_jsd_loss` | `composer_replication.opsd` || `ClaudeCodeIngester` | `composer_replication.ingestion.claude_code` || `IngestionStats` | `composer_replication.ingestion.claude_code` || `SYSTEM_PROMPT` | `composer_replication.ingestion.claude_code` || `DEFAULT_TEACHERS` | `composer_replication.teacher_replay` || `DPOPair` | `composer_replication.teacher_replay` || `TeacherCallResult` | `composer_replication.teacher_replay` || `TeacherSpec` | `composer_replication.teacher_replay` || `TraceState` | `composer_replication.teacher_replay` || `extract_dpo_pairs` | `composer_replication.teacher_replay` || `replay_trace` | `composer_replication.teacher_replay` || `ComposerReplicationTrainer` | `composer_replication.trainer` || `make_diloco_outer_loop` | `composer_replication.diloco` (or `None` if `torchft` missing) |
See each source module below for full signatures.
---
## 2. `composer_replication.loss`
Verification-harness 3-channel loss. Free function, does not depend on `trl`.
### `class LossComponents`
```python@dataclassclass LossComponents: lm_ce: torch.Tensor sdpo_jsd: torch.Tensor trace_replay_dpo: torch.Tensor total: torch.Tensor
def detached(self) -> dict[str, float]: ...```
Per-channel breakdown of the total loss for logging and ablation. All four fields are scalar `torch.Tensor`s (`shape=()`); `total = lm_ce + alpha_sdpo * sdpo_jsd + beta_replay * trace_replay_dpo`.
**`detached() -> dict[str, float]`** — returns Python-float copies of all four fields with no grad. Useful for W&B logging.
```pythonfrom composer_replication import compose_loss, build_batchcomponents = compose_loss(model, build_batch(tokenizer))print(components.detached()) # {'lm_ce': 2.34, 'sdpo_jsd': 0.12, ...}components.total.backward()```
### `compose_loss(model, inputs, *, ...) -> LossComponents`
```pythondef compose_loss( model: torch.nn.Module, inputs: dict[str, torch.Tensor], *, alpha_sdpo: float = 0.1, beta_replay: float = 0.05, sdpo_jsd_beta: float = 0.5, sdpo_temperature: float = 1.0, sdpo_token_clip: float | None = None, replay_dpo_beta: float = 0.1, lm_ce_label_smoothing: float = 0.0, dpo_variant: Literal["dpo", "simpo"] = "dpo", sdpo_wrapper: Literal["none", "taid", "entropy_opd"] = "none", taid_schedule_step: int | None = None, taid_total_steps: int | None = None, simpo_beta: float = 2.0, simpo_gamma: float = 1.0, taid_schedule: str = "linear", taid_alpha_min: float = 0.0, taid_alpha_max: float = 1.0, entropy_opd_h_max: float | None = None,) -> LossComponents```
Compute `total = lm_ce + alpha_sdpo * sdpo_jsd + beta_replay * trace_replay_dpo`.
**Required keys in `inputs`**
- `input_ids`: `(B, T_s)` student rollout token ids.- `response_mask`: `(B, T_s)` 1 on assistant-response tokens, 0 elsewhere.
**Optional keys** (channel auto-disables if missing OR if its weight = 0):
- SDPO: `ctx_teacher_input_ids` `(B, T_t)`, `sdpo_loss_mask` `(B, T_t)`.- DPO (`dpo_variant="dpo"`): `dpo_chosen_input_ids`, `dpo_chosen_response_mask`, `dpo_rejected_input_ids`, `dpo_rejected_response_mask`, `dpo_chosen_ref_logprobs`, `dpo_rejected_ref_logprobs` (precomputed).- SimPO (`dpo_variant="simpo"`): same DPO ids/masks; reference logprobs are silently ignored.- TAID (`sdpo_wrapper="taid"`): `student_init_logits` `(B, T_t, V)` precomputed, OR `student_init_input_ids` `(B, T_t)` for a no-grad-fallback forward.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `model` | `torch.nn.Module` | — | HF causal-LM. Must accept `input_ids=` and return an object with `.logits`. || `inputs` | `dict[str, torch.Tensor]` | — | Batch dict (see required/optional keys above). || `alpha_sdpo` | `float` | `0.1` | Weight on SDPO/JSD channel. `0.0` disables. || `beta_replay` | `float` | `0.05` | Weight on trace-replay DPO channel. `0.0` disables. || `sdpo_jsd_beta` | `float` | `0.5` | β param for `generalized_jsd_loss` (0=fwd KL, 0.5=JSD, 1=rev KL). || `sdpo_temperature` | `float` | `1.0` | Softmax temperature in SDPO. || `sdpo_token_clip` | `float \| None` | `None` | Per-token JSD clamp. || `replay_dpo_beta` | `float` | `0.1` | β in standard DPO logit. || `lm_ce_label_smoothing` | `float` | `0.0` | `F.cross_entropy(label_smoothing=)`. || `dpo_variant` | `Literal["dpo","simpo"]` | `"dpo"` | Channel-3 algorithm. || `sdpo_wrapper` | `Literal["none","taid","entropy_opd"]` | `"none"` | Channel-2 wrapper. || `taid_schedule_step` | `int \| None` | `None` | Required when `sdpo_wrapper="taid"`. || `taid_total_steps` | `int \| None` | `None` | Required when `sdpo_wrapper="taid"`. || `simpo_beta` | `float` | `2.0` | SimPO β (paper default). || `simpo_gamma` | `float` | `1.0` | SimPO target margin γ (paper default). || `taid_schedule` | `str` | `"linear"` | One of `"linear"`, `"cosine"`, `"exp"`. || `taid_alpha_min` | `float` | `0.0` | Lower α bound. || `taid_alpha_max` | `float` | `1.0` | Upper α bound. || `entropy_opd_h_max` | `float \| None` | `None` | Max-entropy normalizer; `None` ⇒ `log(V)`. |
**Returns** `LossComponents` (see above).
**Raises** `ValueError` if `dpo_variant` or `sdpo_wrapper` is unknown, if `sdpo_wrapper="taid"` is requested without both `taid_schedule_step` and `taid_total_steps`, or if TAID's frozen-init logits cannot be resolved (neither `student_init_logits` nor `student_init_input_ids` provided / shape mismatch).
```pythonfrom composer_replication import compose_loss, build_batchbatch = build_batch(tokenizer)out = compose_loss(model, batch, alpha_sdpo=0.1, beta_replay=0.05)out.total.backward()print(out.detached())```
---
## 3. `composer_replication.batch`
Verification-harness batch builder.
### `build_batch(tokenizer, *, ...) -> dict[str, torch.Tensor]`
```pythondef build_batch( tokenizer: Any, *, device: torch.device | str = "cpu", seed: int = 42, variant: str = "factorial", align_sdpo_shapes: bool = False,) -> dict[str, torch.Tensor]```
Construct a full 3-channel batch from a real HF tokenizer. The DPO ref-logprobs are dummy tensors (the smoke verifies loss composition wires together, not the reference-policy precompute).
**Returned keys**: `input_ids`, `response_mask`, `ctx_teacher_input_ids`, `sdpo_loss_mask`, `dpo_chosen_input_ids`, `dpo_chosen_response_mask`, `dpo_rejected_input_ids`, `dpo_rejected_response_mask`, `dpo_chosen_ref_logprobs`, `dpo_rejected_ref_logprobs`.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `tokenizer` | HF `AutoTokenizer` (duck-typed) | — | Must support `apply_chat_template` and `__call__`. || `device` | `torch.device \| str` | `"cpu"` | Target device for all returned tensors. || `seed` | `int` | `42` | Fixes `torch.manual_seed`. || `variant` | `str` | `"factorial"` | One of `"factorial"`, `"binary_search"`. || `align_sdpo_shapes` | `bool` | `False` | If True, truncate/pad `ctx_teacher_input_ids` to `input_ids` length so the SDPO channel actually fires. |
**Raises** `ValueError` if `variant` is unknown.
```pythonfrom transformers import AutoTokenizerfrom composer_replication import build_batchtok = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")batch = build_batch(tok, variant="factorial", align_sdpo_shapes=True)print({k: v.shape for k, v in batch.items()})```
---
## 4. `composer_replication.opsd`
Self-distillation generalized-JSD loss, lifted verbatim from `siyan-zhao/OPSD` (MIT) per ADR-006.
### `generalized_jsd_loss(student_logits, teacher_logits, labels=None, beta=0.5, ...) -> torch.Tensor`
```pythondef generalized_jsd_loss( student_logits: torch.Tensor, teacher_logits: torch.Tensor, labels: torch.Tensor | None = None, beta: float = 0.5, temperature: float = 1.0, reduction: str = "batchmean", logits_are_probs: bool = False, top_k: int | None = None, token_clip: float | None = None,) -> torch.Tensor```
Generalized JSD between student and teacher distributions. Same model on different contexts in the SDPO recipe; student and teacher params come from the SAME model.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `student_logits` | `Tensor (B, T, V)` | — | Student logits with grad. || `teacher_logits` | `Tensor (B, T, V)` | — | Teacher logits (no grad in SDPO). || `labels` | `Tensor (B, T) \| None` | `None` | Per-token mask. `-100` positions are ignored (HF convention). || `beta` | `float` in [0, 1] | `0.5` | 0=fwd KL, 1=rev KL, 0.5=symmetric JSD. || `temperature` | `float` | `1.0` | Softmax temperature. || `reduction` | `str` | `"batchmean"` | `"batchmean"`, `"sum"`, `"mean"`, `"none"`. || `logits_are_probs` | `bool` | `False` | Skip softmax if inputs are already probabilities. || `top_k` | `int \| None` | `None` | Restrict KL to teacher's top-k tokens. || `token_clip` | `float \| None` | `None` | Clip per-token JSD for stability. |
**Returns** scalar tensor (or `(B, T)` if `reduction="none"`).
**Raises** `ValueError` for unknown `reduction`.
```pythonimport torchfrom composer_replication.opsd import generalized_jsd_losss = torch.randn(2, 8, 32, requires_grad=True)t = torch.randn(2, 8, 32)loss = generalized_jsd_loss(s, t, beta=0.5, reduction="batchmean")loss.backward()```
---
## 5. `composer_replication.distillation`
Pluggable self-distillation losses (ADR-007). All pure PyTorch.
### `simpo_loss(chosen_avg_logprobs, rejected_avg_logprobs, *, beta=2.0, gamma=1.0) -> torch.Tensor`
```pythondef simpo_loss( chosen_avg_logprobs: torch.Tensor, rejected_avg_logprobs: torch.Tensor, *, beta: float = 2.0, gamma: float = 1.0,) -> torch.Tensor```
Reference-free DPO with target margin γ (Meng et al., NeurIPS 2024). `L = -log σ(β · (avg_logπ(c) − avg_logπ(r)) − γ)`.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `chosen_avg_logprobs` | `Tensor (B,)` | — | Per-sequence avg logprob over chosen response tokens. || `rejected_avg_logprobs` | `Tensor (B,)` | — | Same for rejected. || `beta` | `float` | `2.0` | Scaling factor (paper default). || `gamma` | `float` | `1.0` | Target margin (paper default). |
**Returns** scalar; **Raises** `ValueError` if shapes mismatch.
```pythonimport torchfrom composer_replication.distillation import simpo_lossloss = simpo_loss(torch.tensor([-2.1, -1.8]), torch.tensor([-3.0, -2.5]), beta=2.0, gamma=1.0)```
### `avg_sequence_logprob(model_logprobs, response_mask) -> torch.Tensor`
⚠️ UNTESTED-CONTRACT (helper exported from `simpo.py` but not asserted by a test).
```pythondef avg_sequence_logprob( model_logprobs: torch.Tensor, response_mask: torch.Tensor,) -> torch.Tensor```
Convert `(B, T)` per-token logprobs + `(B, T)` response mask into `(B,)` per-sequence average over response tokens.
```pythonfrom composer_replication.distillation.simpo import avg_sequence_logprobimport torchlp = torch.randn(2, 8); m = torch.tensor([[0,0,1,1,1,0,0,0],[0,1,1,1,1,1,0,0]])out = avg_sequence_logprob(lp, m) # shape (2,)```
### `taid_loss(student_logits, teacher_logits, student_init_logits, *, schedule_step, total_steps, ...) -> torch.Tensor`
```pythondef taid_loss( student_logits: torch.Tensor, teacher_logits: torch.Tensor, student_init_logits: torch.Tensor, *, schedule_step: int, total_steps: int, schedule: str = "linear", alpha_min: float = 0.0, alpha_max: float = 1.0, jsd_beta: float = 0.5, temperature: float = 1.0, reduction: str = "batchmean",) -> torch.Tensor```
TAID-wrapped generalized-JSD: target distribution is `(1-α)·P_student_init + α·P_teacher` with α annealed by `schedule_step / total_steps`. At α=0 you regularize toward init; at α=1 it reduces to plain SDPO.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `student_logits` | `Tensor (B,T,V)` | — | Current student (with grad). || `teacher_logits` | `Tensor (B,T,V)` | — | Teacher logits (no grad). || `student_init_logits` | `Tensor (B,T,V)` | — | Frozen step-0 student logits. Caller must keep a snapshot. || `schedule_step` | `int` | — | Current training step. || `total_steps` | `int` | — | Total planned steps. || `schedule` | `str` | `"linear"` | One of `"linear"`, `"cosine"`, `"exp"`. || `alpha_min`, `alpha_max` | `float`, `float` | `0.0`, `1.0` | Schedule range. || `jsd_beta` | `float` | `0.5` | β param of `generalized_jsd_loss`. || `temperature` | `float` | `1.0` | Softmax temperature. || `reduction` | `str` | `"batchmean"` | Forwarded to `generalized_jsd_loss`. |
**Raises** `ValueError` for unknown `schedule`, non-positive `total_steps`, negative `step`, or shape mismatch.
```pythonfrom composer_replication.distillation import taid_lossloss = taid_loss(s_logits, t_logits, init_logits, schedule_step=500, total_steps=10_000, schedule="linear")```
### `taid_alpha_schedule(step, total_steps, *, schedule="linear", alpha_min=0.0, alpha_max=1.0, warmup_frac=0.0) -> float`
```pythondef taid_alpha_schedule( step: int, total_steps: int, *, schedule: str = "linear", alpha_min: float = 0.0, alpha_max: float = 1.0, warmup_frac: float = 0.0,) -> float```
Compute α(t) for the TAID schedule. Returns a Python float in `[alpha_min, alpha_max]`.
**Raises** `ValueError` on `total_steps <= 0`, `step < 0`, or unknown `schedule`.
```pythonfrom composer_replication.distillation.taid import taid_alpha_schedulea = taid_alpha_schedule(step=500, total_steps=10000, schedule="cosine") # 0.012...```
### `taid_blended_logits(student_init_logits, teacher_logits, alpha) -> torch.Tensor`
```pythondef taid_blended_logits( student_init_logits: torch.Tensor, teacher_logits: torch.Tensor, alpha: float,) -> torch.Tensor```
Return logits whose softmax is `(1-α)·P_student_init + α·P_teacher`. Mixes in probability space then `log()`.
**Raises** `ValueError` if `alpha` ∉ `[0,1]` or shapes differ.
```pythonfrom composer_replication.distillation.taid import taid_blended_logitsblended = taid_blended_logits(init_logits, teacher_logits, alpha=0.3)```
### `entropy_aware_opd_loss(student_logits, teacher_logits, *, labels=None, h_max=None, temperature=1.0, reduction="batchmean") -> torch.Tensor`
```pythondef entropy_aware_opd_loss( student_logits: torch.Tensor, teacher_logits: torch.Tensor, *, labels: torch.Tensor | None = None, h_max: float | None = None, temperature: float = 1.0, reduction: str = "batchmean",) -> torch.Tensor```
Per-token mixture of forward and reverse KL gated by teacher entropy: `w(t) = clamp(H_teacher(t)/h_max, 0, 1)`. High-entropy tokens use forward KL (mode-covering), low-entropy tokens use reverse KL (mode-seeking).
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `student_logits` | `Tensor (B,T,V)` | — | Student logits (grad). || `teacher_logits` | `Tensor (B,T,V)` | — | Teacher logits (no grad). || `labels` | `Tensor (B,T) \| None` | `None` | 0/1 mask, applied multiplicatively after the per-token mix. || `h_max` | `float \| None` | `None` ⇒ `log(V)` | Max-entropy normalizer. || `temperature` | `float` | `1.0` | Softmax temperature on both. || `reduction` | `str` | `"batchmean"` | `"batchmean"`, `"sum"`, `"mean"`, `"none"`. |
**Raises** `ValueError` on shape mismatch (student vs teacher; labels vs per-token loss) or unknown `reduction`.
```pythonfrom composer_replication.distillation import entropy_aware_opd_lossloss = entropy_aware_opd_loss(s_logits, t_logits, temperature=1.0)loss.backward()```
### `teacher_entropy(teacher_logits) -> torch.Tensor`
⚠️ UNTESTED-CONTRACT (helper exposed from `entropy_aware_opd.py`'s `__all__` but not directly asserted).
Per-token entropy in nats. Input `(B,T,V)`, output `(B,T)`.
```pythonfrom composer_replication.distillation.entropy_aware_opd import teacher_entropyH = teacher_entropy(teacher_logits) # (B, T)```
---
## 6. `composer_replication.teacher_replay`
N-teacher OpenRouter parallel client + DPO-pair extractor. `httpx` is lazy-imported inside `replay_trace`; the deterministic local logic is testable without it.
### `DEFAULT_TEACHERS: list[TeacherSpec]`
Three-teacher default set: `anthropic/claude-opus-4.7`, `openai/gpt-5`, `deepseek/deepseek-v4-pro` with paper-baseline OpenRouter pricing.
```pythonfrom composer_replication.teacher_replay import DEFAULT_TEACHERSprint([t["slug"] for t in DEFAULT_TEACHERS])```
### `class TeacherSpec(TypedDict)`
```pythonclass TeacherSpec(TypedDict): slug: str input_per_mtok: float output_per_mtok: float```
OpenRouter model slug + per-million-token pricing.
```pythonspec: TeacherSpec = {"slug": "openai/gpt-5", "input_per_mtok": 1.25, "output_per_mtok": 10.0}```
### `class TraceState(TypedDict)`
```pythonclass TraceState(TypedDict): state_id: str # unique within the trace messages: list[dict] # OpenAI-style chat history up to (and incl.) this user prompt student_action: str # what the student actually did at this step```
One step of a frozen agentic trace. `student_action` is the raw text emitted by the student; teachers are queried with `messages` and asked to predict the assistant's next action.
```pythonstate: TraceState = {"state_id": "ex001::0042", "messages": [{"role": "user", "content": "..."}], "student_action": "[TOOL_USE] name=Read input={...}"}```
### `class TeacherCallResult(TypedDict)`
```pythonclass TeacherCallResult(TypedDict): state_id: str teacher_slug: str response_text: str | None # None on error latency_s: float prompt_tokens: int completion_tokens: int cost_usd: float error: str | None # None on success```
One row of N×T results from `replay_trace`.
```pythonr: TeacherCallResult = {"state_id": "x", "teacher_slug": "openai/gpt-5", "response_text": "ok", "latency_s": 1.2, "prompt_tokens": 100, "completion_tokens": 5, "cost_usd": 0.001, "error": None}```
### `class DPOPair(TypedDict)`
```pythonclass DPOPair(TypedDict): state_id: str state_messages: list[dict] chosen: str # teacher-consensus action rejected: str # student action n_teachers_agreeing: int```
One preference pair extracted from teacher-vs-student disagreement.
```pythonp: DPOPair = {"state_id": "x", "state_messages": [...], "chosen": "...", "rejected": "...", "n_teachers_agreeing": 2}```
### `async replay_trace(states, teachers=DEFAULT_TEACHERS, max_total_usd=5.0, api_key=None) -> list[TeacherCallResult]`
```pythonasync def replay_trace( states: Sequence[TraceState], teachers: Sequence[TeacherSpec] = tuple(DEFAULT_TEACHERS), max_total_usd: float = 5.0, api_key: str | None = None,) -> list[TeacherCallResult]```
For each state, fan-out one parallel call per teacher via OpenRouter. Hard-caps cumulative spend at `max_total_usd` (stops after the offending state completes).
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `states` | `Sequence[TraceState]` | — | Frozen trace, one entry per assistant turn. || `teachers` | `Sequence[TeacherSpec]` | `DEFAULT_TEACHERS` | Models to query in parallel. || `max_total_usd` | `float` | `5.0` | Cumulative spend cap. || `api_key` | `str \| None` | `None` | OpenRouter key; defaults to `OPENROUTER_API_KEY` env or `~/.hermes/.env`. |
**Returns** flat list of `TeacherCallResult`s (length `len(states) * len(teachers)` modulo budget cutoff).
**Raises** `RuntimeError` if `OPENROUTER_API_KEY` is not findable; `ImportError` if `httpx` is missing at call time.
```pythonimport asynciofrom composer_replication import replay_traceresults = asyncio.run(replay_trace(states=my_trace, max_total_usd=1.0))```
### `extract_dpo_pairs(states, teacher_actions, agreement_threshold=2) -> list[DPOPair]`
```pythondef extract_dpo_pairs( states: Sequence[TraceState], teacher_actions: Sequence[TeacherCallResult], agreement_threshold: int = 2,) -> list[DPOPair]```
Group teacher_actions by `state_id`, normalize whitespace, and emit one `DPOPair` per state where ≥`agreement_threshold` teachers agreed on an action that differs from the student's. `chosen` is the original (un-normalized) teacher response text.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `states` | `Sequence[TraceState]` | — | Same as passed to `replay_trace`. || `teacher_actions` | `Sequence[TeacherCallResult]` | — | Output of `replay_trace`. || `agreement_threshold` | `int` | `2` | Min teachers that must agree for a pair to fire. |
**Returns** list of `DPOPair`. At most one pair per state (the most-agreed-upon action wins).
```pythonfrom composer_replication import extract_dpo_pairspairs = extract_dpo_pairs(my_states, results, agreement_threshold=2)```
### `save_pairs(pairs, path) -> None`
⚠️ UNTESTED-CONTRACT.
```pythondef save_pairs(pairs: Sequence[DPOPair], path: str | Path) -> None```
Write pairs to JSONL (one dict per line). Creates parent dirs.
```pythonfrom composer_replication.teacher_replay import save_pairssave_pairs(pairs, "/tmp/dpo_pairs.jsonl")```
---
## 7. `composer_replication.replaysim`
ADR-004 normalization layer over `teacher_replay`. Re-exports `DPOPair`, `TeacherCallResult`, `extract_dpo_pairs`, `replay_trace` from `teacher_replay`.
### `class NormalizedDPOPair`
```python@dataclassclass NormalizedDPOPair: state_id: str state_messages: list[dict[str, Any]] chosen_messages: list[dict[str, Any]] rejected_messages: list[dict[str, Any]] n_teachers_agreeing: int metadata: dict[str, Any]```
Post-normalization shape. `chosen_messages`/`rejected_messages` are chat-format (`[{"role": "assistant", "content": ...}]`). `metadata` carries op-graph provenance, including `{"skipped": True}` when the normalizer was bypassed (`skip_dj=True`).
```pythonfrom composer_replication.replaysim import NormalizedDPOPairn = NormalizedDPOPair(state_id="x", state_messages=[], chosen_messages=[{"role": "assistant", "content": "ok"}], rejected_messages=[{"role": "assistant", "content": "no"}], n_teachers_agreeing=2, metadata={})```
### `class DJNormalizer`
```pythonclass DJNormalizer: DEFAULT_RECIPE: ClassVar[Path] # composer_replication/recipes/replaysim/default.yaml
def __init__( self, recipe_path: str | os.PathLike[str] | None = None, *, skip_dj: bool = False, ) -> None: ...
def normalize( self, pairs: Iterable[DPOPair | dict[str, Any]], ) -> list[NormalizedDPOPair]: ...```
`data-juicer`-backed normalizer. Pipeline: each `DPOPair` → JSONL record → `data_juicer.core.DefaultExecutor.run()` against the recipe → JSONL → `NormalizedDPOPair`.
**Constructor parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `recipe_path` | `str \| PathLike \| None` | `None` ⇒ default recipe | data-juicer YAML recipe path. || `skip_dj` | `bool` (kw-only) | `False` | If True: passthrough; records get `metadata={"skipped": True}` and no ops run. |
**`normalize(pairs) -> list[NormalizedDPOPair]`** runs the op-graph. Output may be shorter than input if filter ops drop records.
**Raises** `RuntimeError` at construction time if `skip_dj=False` and `data_juicer` is not importable. `FileNotFoundError` if `recipe_path` (default or explicit) is missing and `skip_dj=False`.
```pythonfrom composer_replication.replaysim import DJNormalizernorm = DJNormalizer(skip_dj=True)out = norm.normalize(my_pairs)```
### `async replay_and_normalize_trace(*, states, teachers=None, agreement_threshold=2, max_total_usd=5.0, normalizer=None, **replay_kwargs) -> tuple[list[TeacherCallResult], list[NormalizedDPOPair]]`
```pythonasync def replay_and_normalize_trace( *, states: Any, teachers: Any = None, agreement_threshold: int = 2, max_total_usd: float = 5.0, normalizer: DJNormalizer | None = None, **replay_kwargs: Any,) -> tuple[list[TeacherCallResult], list[NormalizedDPOPair]]```
End-to-end async: replay → extract pairs → normalize.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `states` | `Sequence[TraceState]` | — | Frozen trace. || `teachers` | `Sequence[TeacherSpec] \| None` | `None` ⇒ defaults | Forwarded to `replay_trace`. || `agreement_threshold` | `int` | `2` | Forwarded to `extract_dpo_pairs`. || `max_total_usd` | `float` | `5.0` | Spend cap. || `normalizer` | `DJNormalizer \| None` | `None` ⇒ `DJNormalizer()` | Pass `DJNormalizer(skip_dj=True)` to bypass. || `**replay_kwargs` | `Any` | — | Forwarded to `replay_trace` (e.g. `api_key`). |
**Returns** `(raw_teacher_actions, normalized_pairs)`.
```pythonimport asynciofrom composer_replication.replaysim import replay_and_normalize_trace, DJNormalizerraw, norm = asyncio.run(replay_and_normalize_trace( states=my_states, normalizer=DJNormalizer(skip_dj=True)))```
### `replay_and_normalize_trace_sync(*args, **kwargs) -> tuple[list[TeacherCallResult], list[NormalizedDPOPair]]`
⚠️ UNTESTED-CONTRACT (sync wrapper around the async function; tests call the async form via `asyncio.run`).
```pythondef replay_and_normalize_trace_sync(*args, **kwargs) -> ...```
Sync convenience wrapping `asyncio.run(replay_and_normalize_trace(...))`.
```pythonfrom composer_replication.replaysim.normalize import replay_and_normalize_trace_syncraw, norm = replay_and_normalize_trace_sync(states=my_states)```
---
## 8. `composer_replication.ingestion` & `composer_replication.ingestion.claude_code`
Trace-source adapters (ADR-002). v0.1 supports Claude Code session JSONL.
### `SYSTEM_PROMPT: str`
Default synthetic system prompt injected at `messages[0]` for ingested traces (most Claude Code sessions don't write one). Truncated head: `"You are a senior software engineer working as a coding agent in a terminal environment..."`.
```pythonfrom composer_replication import SYSTEM_PROMPTprint(SYSTEM_PROMPT[:60])```
### `class IngestionStats`
```python@dataclassclass IngestionStats: n_records_total: int = 0 n_records_skipped: int = 0 n_states_emitted: int = 0 n_assistant_turns: int = 0 n_tool_use_blocks: int = 0 n_text_blocks: int = 0 skipped_subagent: int = 0 skipped_summary: int = 0 skipped_truncated_lines: int = 0 version_warnings: list[str] | None = None # initialized to [] in __post_init__```
Counters populated by `ClaudeCodeIngester.ingest()` and exposed as `ingester.last_stats`.
```pythonfrom composer_replication import IngestionStatss = IngestionStats(n_records_total=5)print(s.version_warnings) # []```
### `class ClaudeCodeIngester`
```pythonclass ClaudeCodeIngester: def __init__( self, *, system_prompt: str = SYSTEM_PROMPT, skip_sidechain: bool = True, strip_thinking: bool = True, max_history_tokens: int | None = None, ) -> None: ...
def ingest(self, path: Path) -> Iterator[TraceState]: ...```
Convert a Claude Code session JSONL to a stream of `TraceState`s — one per assistant TURN (not per `tool_use` block).
**Constructor parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `system_prompt` | `str` | `SYSTEM_PROMPT` | Synthetic system message injected at history[0]. || `skip_sidechain` | `bool` | `True` | Skip subagent files (`agent-*.jsonl`) and records with `isSidechain=True`. || `strip_thinking` | `bool` | `True` | Remove `[THINKING]` blocks from history handed to teachers (kept inside `student_action`). || `max_history_tokens` | `int \| None` | `None` | ⚠️ UNTESTED-CONTRACT — accepted but currently not used to truncate. |
**`ingest(path) -> Iterator[TraceState]`**: generator over `TraceState` objects. Each turn's `state_id` is `f"{path.stem}::{idx:04d}"`. Side effect: replaces `self.last_stats` with a fresh `IngestionStats` and updates it as records stream.
```pythonfrom pathlib import Pathfrom composer_replication import ClaudeCodeIngestering = ClaudeCodeIngester()for state in ing.ingest(Path("session.jsonl")): print(state["state_id"])print(ing.last_stats.n_states_emitted)```
---
## 9. `composer_replication.hint_generator`
⚠️ UNTESTED-CONTRACT (entire module — used by the data collator config but not pinned by a test).
Template-based hint registry for SDPO error-site injection.
### `class HintContext(TypedDict, total=False)`
```pythonclass HintContext(TypedDict, total=False): error_kind: str error_message: str available_tools: list[str] tool_name: str tool_schema: dict intent: str```
Per-error context dict consumed by hint templates.
### `HINT_TEMPLATES: dict[str, Callable[[HintContext], str]]`
Default registry keys: `"tool_not_found"`, `"json_decode"`, `"type_error"`, `"runtime_error"`, `"repeated_failure"`.
### `dispatch(error_kind, ctx=None) -> str | None`
```pythondef dispatch(error_kind: str, ctx: HintContext | None = None) -> str | None```
Look up `error_kind` in `HINT_TEMPLATES`. Returns the template's hint text, or `None` if the kind is unknown.
```pythonfrom composer_replication.hint_generator import dispatchhint = dispatch("json_decode") # "Reminder: tool arguments must be valid JSON. ..."```
### `register(error_kind, fn) -> None`
```pythondef register(error_kind: str, fn: Callable[[HintContext], str]) -> None```
Add or override a custom hint template.
```pythonfrom composer_replication.hint_generator import registerregister("my_error", lambda ctx: "Reminder: try X.")```
### Individual template functions
⚠️ UNTESTED-CONTRACT — exported only via `HINT_TEMPLATES`, useful as building blocks:
- `hint_tool_not_found(ctx) -> str`- `hint_json_decode(ctx) -> str`- `hint_type_error(ctx) -> str`- `hint_runtime_error(ctx) -> str`- `hint_repeated_failure(ctx) -> str`
Each accepts a `HintContext` and returns hint text. Signatures are uniform: `Callable[[HintContext], str]`.
```pythonfrom composer_replication.hint_generator import hint_tool_not_foundtext = hint_tool_not_found({"available_tools": ["Read", "Write"]})```
---
## 10. `composer_replication.trainer` & sub-modules
Production trainer (TRL `GRPOTrainer` subclass) plus data collator.
### `class ComposerReplicationTrainer`
```pythonclass ComposerReplicationTrainer(GRPOTrainer): def __init__( self, *args: Any, alpha_sdpo: float = 0.1, beta_replay: float = 0.05, sdpo_jsd_beta: float = 0.5, sdpo_temperature: float = 1.0, sdpo_token_clip: float | None = None, replay_dpo_beta: float = 0.1, **kwargs: Any, ) -> None: ...
def _compute_loss( self, model: torch.nn.Module, inputs: dict[str, torch.Tensor], ) -> torch.Tensor: ...```
`trl.GRPOTrainer` subclass that overrides `_compute_loss(model, inputs)` to compose `total = grpo + α·sdpo + β·trace_replay_dpo`. When `trl` is not installed, the parent class falls back to `object` so the module imports — but instantiation will fail because the parent's GRPO machinery is missing.
**Constructor (kw-only beyond GRPOTrainer's own `*args, **kwargs`)**
| Name | Type | Default | Meaning ||---|---|---|---|| `alpha_sdpo` | `float` | `0.1` | Channel-2 weight. || `beta_replay` | `float` | `0.05` | Channel-3 weight. || `sdpo_jsd_beta` | `float` | `0.5` | β for `generalized_jsd_loss`. || `sdpo_temperature` | `float` | `1.0` | SDPO softmax temperature. || `sdpo_token_clip` | `float \| None` | `None` | Per-token JSD clip. || `replay_dpo_beta` | `float` | `0.1` | DPO β. |
**`_compute_loss(model, inputs) -> torch.Tensor`** — overrides `GRPOTrainer._compute_loss`. Calls `super()._compute_loss` for channel 1, then `_compute_sdpo_loss` and `_compute_trace_replay_loss`, then composes. Logs per-channel components every `args.logging_steps` (default 50). **Raises** whatever `super()` raises (TRL-shaped errors).
**Internal methods (publicly accessible, exercised by spike tests)**
- ⚠️ UNTESTED-CONTRACT `_compute_sdpo_loss(model, inputs) -> torch.Tensor` — generalized-JSD between student forward and `ctx_teacher_input_ids` forward. Returns `0.0` (with grad) when `alpha_sdpo == 0`, the key is missing, or shapes mismatch. Logs a warning on shape mismatch.- ⚠️ UNTESTED-CONTRACT `_compute_trace_replay_loss(model, inputs) -> torch.Tensor` — standard DPO over `dpo_chosen_*` and `dpo_rejected_*`, using precomputed `dpo_chosen_ref_logprobs` / `dpo_rejected_ref_logprobs`.- ⚠️ UNTESTED-CONTRACT `@staticmethod _sequence_logprobs(model, input_ids, response_mask) -> torch.Tensor` — sum logprobs over response tokens; standard DPO accounting.
```pythonfrom composer_replication import ComposerReplicationTrainertrainer = ComposerReplicationTrainer( model=my_model, args=my_grpo_args, train_dataset=ds, data_collator=my_collator, alpha_sdpo=0.1, beta_replay=0.05,)# trainer.train() # uses overridden _compute_loss```
### `class TraceTurn(TypedDict, total=False)` — `trainer.data_collator`
```pythonclass TraceTurn(TypedDict, total=False): role: str # "user" | "assistant" | "tool" content: str tool_call: dict | None tool_error: str | None error_meta: dict```
One turn of an agentic trace as consumed by `ComposerDataCollator`.
### `class TraceExample(TypedDict, total=False)` — `trainer.data_collator`
```pythonclass TraceExample(TypedDict, total=False): trace_id: str turns: list[TraceTurn] final_reward: float dpo_pairs: list[dict] | None```
One training example: `(turns, optional dpo_pairs)`. `dpo_pairs` shape matches `DPOPair`.
### `class TokenizerLike` — `trainer.data_collator`
⚠️ UNTESTED-CONTRACT (duck-typed protocol; used as a type hint).
```pythonclass TokenizerLike: pad_token_id: int def __call__(self, text: str | list[str], **kwargs: Any) -> dict[str, list]: ... def apply_chat_template(self, messages: list[dict], **kwargs: Any) -> str | list[int]: ...```
Minimal protocol the collator needs. Compatible with HF `AutoTokenizer`.
### `class CollatorConfig` — `trainer.data_collator`
```python@dataclassclass CollatorConfig: max_seq_len: int = 4096 max_dpo_seq_len: int = 2048 pad_token_id: int = 0 ignore_index: int = -100 enable_sdpo: bool = True hint_generator: Callable[[str, dict], str | None] | None = None enable_replay_dpo: bool = True rlvr_reward_key: str = "final_reward"```
Tunables for `ComposerDataCollator`.
| Field | Default | Meaning ||---|---|---|| `max_seq_len` | `4096` | Truncation cap for student/teacher sequences. || `max_dpo_seq_len` | `2048` | Truncation cap for DPO chosen/rejected sequences. || `pad_token_id` | `0` | Padding token id. || `ignore_index` | `-100` | HF "ignore in loss" sentinel for SDPO mask. || `enable_sdpo` | `True` | Toggle channel-2 fields. || `hint_generator` | `Callable[[str, dict], str \| None] \| None` (`None`) | `(error_kind, error_meta) -> hint_text`. SDPO is no-op without this. || `enable_replay_dpo` | `True` | Toggle channel-3 fields. || `rlvr_reward_key` | `"final_reward"` | Key in `TraceExample` to read scalar reward. |
```pythonfrom composer_replication.trainer.data_collator import CollatorConfigcfg = CollatorConfig(max_seq_len=2048, hint_generator=my_dispatch)```
### `class ComposerDataCollator` — `trainer.data_collator`
```python@dataclassclass ComposerDataCollator: tokenizer: TokenizerLike config: CollatorConfig = field(default_factory=CollatorConfig)
def __call__( self, batch: Sequence[TraceExample] ) -> dict[str, torch.Tensor]: ...```
Build trainer-ready batches from raw traces + optional DPO pairs.
**Output dict keys** (tested in `spikes/005-integrated-trainer-skeleton/tests/test_data_collator.py`):
- Channel 1 (always): `input_ids`, `attention_mask`, `response_mask`, `rewards`.- Channel 2 (when `enable_sdpo=True` AND batch has at least one error site AND `hint_generator` is set): `ctx_teacher_input_ids`, `sdpo_loss_mask`.- Channel 3 (when `enable_replay_dpo=True` AND batch has at least one `dpo_pair`): `dpo_chosen_input_ids`, `dpo_chosen_response_mask`, `dpo_rejected_input_ids`, `dpo_rejected_response_mask`. (Reference logprobs are NOT computed here — the trainer does that pass.)
```pythonfrom composer_replication.trainer.data_collator import ( ComposerDataCollator, CollatorConfig)collator = ComposerDataCollator(tokenizer=tok, config=CollatorConfig())batch = collator([{"trace_id": "x", "turns": [...], "final_reward": 1.0}])```
---
## 11. `composer_replication.diloco`
DiLoCo outer-loop wrapper around `torchft.local_sgd.DiLoCo`. Optional dep — when `torchft` is missing the package re-export `composer_replication.make_diloco_outer_loop` is `None`.
### Module-level attributes
- `DiLoCo: Any` — `torchft.local_sgd.DiLoCo` if importable else `None`.- `Manager: Any` — `torchft.manager.Manager` if importable else `None`.- `_DummyWork: Any` — `torchft.work._DummyWork` if importable else `None`.- `_TORCHFT_AVAILABLE: bool` — whether the imports succeeded.
```pythonfrom composer_replication.diloco import _TORCHFT_AVAILABLE, DiLoCo```
### `make_diloco_outer_loop(manager, model_fragments, inner_optimizer, *, ...) -> torchft.local_sgd.DiLoCo`
```pythondef make_diloco_outer_loop( manager: Any, model_fragments: list[torch.nn.Module], inner_optimizer: torch.optim.Optimizer, *, outer_lr: float = 0.7, outer_momentum: float = 0.9, nesterov: bool = True, sync_every: int = 100, fragment_sync_delay: int = 0, fragment_update_alpha: float = 0.0,) -> Any```
Construct a `torchft.DiLoCo` configured with framework-default hyperparams (DiLoCo paper §3.2: `lr=0.7, momentum=0.9, Nesterov`).
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `manager` | `torchft.Manager` (or duck-typed `MockManager`) | — | Provides `allreduce`, `should_commit`, `current_step`, `start_quorum`, etc. || `model_fragments` | `list[torch.nn.Module]` | — | One module for vanilla DiLoCo; N modules for Streaming DiLoCo. || `inner_optimizer` | `torch.optim.Optimizer` | — | Inner-step optimizer (steps every batch). || `outer_lr` | `float` | `0.7` | Outer SGD lr. || `outer_momentum` | `float` | `0.9` | Outer SGD momentum. || `nesterov` | `bool` | `True` | Nesterov momentum on outer SGD. || `sync_every` | `int` | `100` | Inner steps per outer round. || `fragment_sync_delay` | `int` | `0` | 0 = vanilla; >0 = Streaming DiLoCo (requires CUDA streams). || `fragment_update_alpha` | `float` | `0.0` | 0 = full replacement on sync; >0 = exponential mix. |
**Returns** a `torchft.local_sgd.DiLoCo` instance — usable as a context manager.
**Raises** `RuntimeError` if `torchft` is not installed.
```pythonimport torchfrom composer_replication.diloco import make_diloco_outer_loopopt = torch.optim.AdamW(model.parameters(), lr=1e-5)outer = make_diloco_outer_loop(manager=mgr, model_fragments=[model], inner_optimizer=opt, sync_every=100)with outer: for _ in range(N): opt.zero_grad(); loss.backward(); opt.step()```
---
## 12. `composer_replication.diloco.serverless`
ADR-005 serverless DiLoCo executors + object-store all-reduce.
### `class ReplicaHandle` — `serverless.executor`
```python@dataclassclass ReplicaHandle: rank: int backend_name: str metadata: dict[str, Any] = field(default_factory=dict)```
Opaque handle returned by `ServerlessExecutor.launch_replicas`. `metadata` is backend-specific.
```pythonfrom composer_replication.diloco.serverless import ReplicaHandleh = ReplicaHandle(rank=0, backend_name="local_process", metadata={"pid": 12345})```
### `class ServerlessExecutor` (Protocol) — `serverless.executor`
```python@runtime_checkableclass ServerlessExecutor(Protocol): backend_name: str supports_inter_replica_network: bool
def launch_replicas( self, n_replicas: int, entrypoint: str | Callable[..., Any], entrypoint_args: Mapping[str, Any], *, gpu: str | None = None, timeout: int = 3600, ) -> list[ReplicaHandle]: ...
def poll(self, handle: ReplicaHandle) -> str: ... def stream_logs(self, handle: ReplicaHandle, *, n_lines: int = 200) -> str: ... def cancel(self, handle: ReplicaHandle) -> None: ... def collect( self, handles: list[ReplicaHandle], *, timeout: int | None = None, ) -> list[dict[str, Any]]: ...```
Structural protocol for serverless backends.
- `launch_replicas(...)` returns `list[ReplicaHandle]` of length `n_replicas` in rank order. `entrypoint` is either an importable module path (uses `main()`) or a `module.function` path or a `Callable` (Local executor only). `entrypoint_args` may include `rank_env` (default `"REPLICA_RANK"`).- `poll(handle) -> str`: one of `"pending"`, `"running"`, `"succeeded"`, `"failed"`, `"cancelled"`.- `stream_logs(handle, n_lines=200) -> str`: best-effort recent stdout/stderr.- `cancel(handle) -> None`: best-effort.- `collect(handles, timeout=None) -> list[dict]`: blocks; each result dict has `rank`, `status`, `exit_code`, `error` (and `result` from `LocalProcessExecutor`).
```pythonfrom composer_replication.diloco.serverless import ServerlessExecutordef supports(x: ServerlessExecutor) -> bool: return isinstance(x, ServerlessExecutor) # runtime_checkable```
### `class LocalProcessExecutor` — `serverless.executor`
```pythonclass LocalProcessExecutor: backend_name = "local_process" supports_inter_replica_network = True
def __init__(self) -> None: ... # implements ServerlessExecutor protocol```
Reference implementation using Python `multiprocessing` (`spawn` context). Used for tests, CI smokes, and local development with `file://` rendezvous.
`launch_replicas(...)`: emits a soft warning on `gpu != None` (local processes share whatever GPUs are visible). `metadata = {"pid": ..., "start_ts": ...}`.
```pythonfrom composer_replication.diloco.serverless import LocalProcessExecutorex = LocalProcessExecutor()handles = ex.launch_replicas( n_replicas=2, entrypoint="composer_replication.diloco.serverless.replica_entrypoint", entrypoint_args={"rendezvous_uri": "/tmp/run/", "world_size": 2, "trainer_module": "my.trainer"},)results = ex.collect(handles, timeout=60)```
### `class ObjectStoreAllReduce` — `serverless.allreduce`
```pythonclass ObjectStoreAllReduce: def __init__( self, uri: str, rank: int, world_size: int, *, round_id: int | None = None, timeout_s: float = 1800.0, poll_interval_s: float = 1.0, ) -> None: ...
@property def round_id(self) -> int: ...
def allreduce( self, tensor: torch.Tensor, *, name: str | None = None, ) -> torch.Tensor: ...```
fsspec-backed pseudo-gradient rendezvous. `uri` accepts `s3://`, `gs://`, `az://`, `hf://`, `file://`, or a plain local path.
**Constructor parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `uri` | `str` | — | fsspec URI or local path. Trailing `/` enforced. || `rank` | `int` | — | This replica's rank. || `world_size` | `int` | — | Total replicas. || `round_id` | `int \| None` (kw-only) | `None` ⇒ start at 0 | Initial round counter. || `timeout_s` | `float` (kw-only) | `1800.0` | Per-`allreduce` timeout. || `poll_interval_s` | `float` (kw-only) | `1.0` | Sleep between peer-file existence checks. |
**`allreduce(tensor, name=None) -> torch.Tensor`**: serializes `tensor.detach().cpu()` to `round_NNNNNN/rank_RRRR.pt`, blocks until all peers post, then averages. **Modifies `tensor` in place** AND returns it. Increments the internal `_round_counter`.
**Raises** `ValueError` on invalid `rank`, `RuntimeError` if non-local URI is requested without `fsspec` installed, `TimeoutError` if peers don't show up before `timeout_s`.
```pythonfrom composer_replication.diloco.serverless import ObjectStoreAllReduceimport torchstore = ObjectStoreAllReduce("/tmp/run/", rank=0, world_size=2)g = torch.zeros(10)store.allreduce(g) # blocks for rank 1```
### `class MockManager` — `serverless.allreduce`
```pythonclass MockManager: def __init__(self, store: ObjectStoreAllReduce) -> None: ...
# torchft.Manager-shaped surface: num_participants: int rank: int _use_async_quorum: bool # always False _step: int _state_dict_fns: dict[str, tuple[Any, Any]]
def allreduce(self, tensor: torch.Tensor, **_kwargs: Any) -> "_ImmediateWork": ... def should_commit(self) -> bool: ... def start_quorum(self) -> None: ... def wait_quorum(self) -> int: ... def current_step(self) -> int: ... def allow_state_dict_read(self) -> None: ... def disallow_state_dict_read(self) -> None: ... def register_state_dict_fn(self, key: str, load_fn: Any, save_fn: Any) -> None: ... def is_leader(self) -> bool: ...```
Drop-in replacement for `torchft.Manager` that routes `allreduce` through `ObjectStoreAllReduce`. All other methods are no-ops or simple counters appropriate for single-shot serverless DiLoCo.
- `allreduce(tensor)` returns an `_ImmediateWork` whose `.wait()` is a no-op (the tensor is already averaged).- `should_commit()` always `True` (no fault-tolerance failover).- `start_quorum()` bumps `_step`.- `is_leader()` returns `rank == 0`.
```pythonfrom composer_replication.diloco.serverless import MockManager, ObjectStoreAllReducestore = ObjectStoreAllReduce("/tmp/run/", rank=0, world_size=2)mgr = MockManager(store)# pass mgr into make_diloco_outer_loop(manager=mgr, ...)```
### `class _ImmediateWork` — `serverless.allreduce`
⚠️ UNTESTED-CONTRACT internal helper exported from `__all__`. `Work`-shaped wrapper with `.wait() -> True` and `.get_future() -> torch.futures.Future`. Consumed by torchft DiLoCo's `perform_sync`.
```pythonfrom composer_replication.diloco.serverless.allreduce import _ImmediateWork```
### `class ModalExecutor` — `serverless.modal`
🟡 SKELETON — raises `NotImplementedError`; see ADR-005. Class body documents the v0 implementation pattern (Modal `app.function` + `function.spawn(rank=...)`).
```pythonfrom composer_replication.diloco.serverless.modal import ModalExecutor# ModalExecutor() # would NotImplementedError when instantiated```
### `class HFJobsExecutor` — `serverless.hf_jobs`
🟡 SKELETON — raises `NotImplementedError`; see ADR-005. Class body documents the v0 pattern using `huggingface_hub.run_job` against `hf://datasets/.../` rendezvous.
```pythonfrom composer_replication.diloco.serverless.hf_jobs import HFJobsExecutor# instantiation will fail until v0 implementation lands```
### `replica_entrypoint.main(...)` — `serverless.replica_entrypoint`
```pythondef main( rendezvous_uri: str, world_size: int, trainer_module: str, trainer_fn: str = "train", trainer_kwargs: dict[str, Any] | None = None,) -> Any```
Script run by every replica. Reads `REPLICA_RANK` env var, builds `ObjectStoreAllReduce` + `MockManager`, imports `trainer_module`, and calls `getattr(mod, trainer_fn)(**trainer_kwargs, manager=..., rank=..., world_size=...)`. Returns whatever the train fn returns.
**Raises** `RuntimeError` if `REPLICA_RANK` env var is missing; `ValueError` if rank ∉ `[0, world_size)`.
The `if __name__ == "__main__"` block accepts CLI flags `--rendezvous`, `--world-size`, `--trainer-module`, `--trainer-fn`, `--trainer-kwargs-json`.
```python# In-process invocationimport osos.environ["REPLICA_RANK"] = "0"from composer_replication.diloco.serverless.replica_entrypoint import mainresult = main(rendezvous_uri="/tmp/run/", world_size=1, trainer_module="my.trainer", trainer_fn="train")```
---
## 13. `composer_replication.recipes.prime_rl.composer_loss`
PRIME-RL adapter (ADR-006). Maps PRIME-RL's `LossInputs` struct onto channel 1 (DPPO + KL on the importance ratio, mirroring PRIME-RL's upstream `default_loss_fn` at `prime_rl/trainer/rl/loss.py` lines 116-165). Channel 2 raises `NotImplementedError`; channel 3 is out of scope.
### `loss_fn(inputs, *, alpha_sdpo=0.0, beta_dpo=0.0, dppo_mask_high=0.2, dppo_mask_low=0.2, adv_tau=1.0, kl_tau=1e-3) -> torch.Tensor`
```pythondef loss_fn( inputs: Any, # PRIME-RL's LossInputs (duck-typed) *, alpha_sdpo: float = 0.0, beta_dpo: float = 0.0, dppo_mask_high: float = 0.2, dppo_mask_low: float = 0.2, adv_tau: float = 1.0, kl_tau: float = 1e-3,) -> Any # torch.Tensor scalar```
PRIME-RL passes per-sample **1-D `(seq,)` tensors** (not batched). The function mirrors PRIME-RL's upstream DPPO+KL formula:
- Mask gate is on **probability-space** `probs_diff = exp(trainer_lp) - exp(inference_lp)` (NOT on the log-ratio).- A token is dropped iff its advantage sign matches the offending bound: positive-advantage tokens are dropped when `probs_diff > dppo_mask_high`, negative-advantage tokens when `probs_diff < -dppo_mask_low`. (PRIME-RL stores both bounds with `Field(..., ge=0)` and applies the sign internally.)- The PG term is `keep * (adv_tau * advantages) * exp(trainer_lp - inference_lp)` (importance-ratio corrected, not REINFORCE).- A KL penalty `kl_tau * log_importance_ratio**2` is added on the full `loss_mask` (DPPO masking does not gate it).- Reduction is a plain `sum()`; PRIME-RL's outer `compute_loss` divides by `loss_scale`.
**Parameters**
| Name | Type | Default | Meaning ||---|---|---|---|| `inputs` | PRIME-RL `LossInputs` (duck-typed) | — | Must expose `trainer_logprobs`, `inference_logprobs`, `advantages`, `loss_mask` (all 1-D), and optionally `teacher_logprobs`. || `alpha_sdpo` | `float` (kw-only) | `0.0` | Channel-2 weight. Must be `0` in v0; >0 → `NotImplementedError`. || `beta_dpo` | `float` (kw-only) | `0.0` | Channel-3 weight. Non-zero emits a `UserWarning`. || `dppo_mask_high` | `float` (kw-only), `>= 0` | `0.2` | Upper probability-diff threshold. PRIME-RL `DefaultLossConfig` default. || `dppo_mask_low` | `float` (kw-only), `>= 0` | `0.2` | Magnitude of lower probability-diff threshold (sign flipped internally). PRIME-RL default. || `adv_tau` | `float` (kw-only), `>= 0` | `1.0` | Advantage temperature. PRIME-RL default. || `kl_tau` | `float` (kw-only), `>= 0` | `1e-3` | KL term temperature. PRIME-RL default. |
**Returns** scalar `torch.Tensor` (PRIME-RL's trainer calls `.backward()`).
**Raises** `ValueError` if any of `trainer_logprobs`, `inference_logprobs`, `advantages`, `loss_mask` is not 1-D, or any of the four `>=0`-constrained knobs is negative. `NotImplementedError` if `alpha_sdpo > 0` (channel 2 deferred).
```pythonfrom composer_replication.recipes.prime_rl.composer_loss import loss_fn# In PRIME-RL config:# loss:# custom:# import_path: composer_replication.recipes.prime_rl.composer_loss:loss_fn# kwargs:# dppo_mask_high: 0.2# dppo_mask_low: 0.2# adv_tau: 1.0# kl_tau: 1.0e-3```
---
## 14. `composer_replication.recipes.monarch.actors`
🟡 SKELETON module per ADR-006. Importable; classes raise `NotImplementedError` on instantiation. Documents the actor signatures so the recipe matrix is complete.
### `class TrainerActor` 🟡
```pythonclass TrainerActor: backend = "monarch" role = "trainer"
def __init__(self) -> None: raise NotImplementedError(...) async def train_outer_step(self, batch_id: int) -> dict[str, Any]: raise NotImplementedError```
Hosts the framework's 3-channel composer trainer. Real impl deferred to v0.2+.
### `class GeneratorActor` 🟡
```pythonclass GeneratorActor: backend = "monarch" role = "generator" def __init__(self) -> None: raise NotImplementedError(...) async def rollout(self, prompts: list[str]) -> list[str]: raise NotImplementedError```
vLLM-backed rollout actor.
### `class RewarderActor` 🟡
```pythonclass RewarderActor: backend = "monarch" role = "rewarder" def __init__(self) -> None: raise NotImplementedError(...) async def score(self, completions: list[str]) -> list[float]: raise NotImplementedError```
verifiers-protocol rewarder.
### `class TeacherPoolActor` 🟡
```pythonclass TeacherPoolActor: backend = "monarch" role = "teacher_pool" def __init__(self) -> None: raise NotImplementedError(...)```
Channel-3 teacher pool wrapping `composer_replication.teacher_replay`.
```python# All Monarch actors raise on instantiation in v0:from composer_replication.recipes.monarch.actors import TrainerActor# TrainerActor() # NotImplementedError```
---
## Notes on test coverage
Tested contracts (referenced spike/test paths):
- `compose_loss` + `LossComponents` + `build_batch`: `composer_replication/tests/test_compose_loss_integration.py`, `spikes/006-real-hf-model-smoke/tests/`.- `generalized_jsd_loss`: `spikes/005-integrated-trainer-skeleton/tests/test_opsd_loss.py`.- `simpo_loss`, `taid_loss`, `taid_alpha_schedule`, `taid_blended_logits`, `entropy_aware_opd_loss`: `composer_replication/distillation/tests/test_distillation_losses.py`.- `replay_trace`, `extract_dpo_pairs`, `DPOPair`, `TraceState`, `TeacherCallResult`, `TeacherSpec`, `DEFAULT_TEACHERS`: `spikes/005-integrated-trainer-skeleton/tests/test_teacher_replay.py`.- `DJNormalizer`, `NormalizedDPOPair`, `replay_and_normalize_trace`: `composer_replication/replaysim/tests/test_replaysim.py`.- `ClaudeCodeIngester`, `IngestionStats`, `SYSTEM_PROMPT`: `spikes/007-real-trace-ingestion/tests/`.- `ComposerDataCollator`, `CollatorConfig`, `TraceTurn`, `TraceExample`: `spikes/005-integrated-trainer-skeleton/tests/test_data_collator.py`.- `ComposerReplicationTrainer._compute_loss` (composition arithmetic): `spikes/005-integrated-trainer-skeleton/tests/test_loss_composition_smoke.py`.- `make_diloco_outer_loop` + sign convention: `spikes/008-streaming-diloco/tests/test_diloco_smoke.py`.- `ObjectStoreAllReduce`, `MockManager`, `LocalProcessExecutor`, `ReplicaHandle`, `ServerlessExecutor`, `replica_entrypoint.main`: `composer_replication/diloco/serverless/tests/test_serverless_local.py`, `test_serverless_diloco_integration.py`.- `recipes.prime_rl.composer_loss.loss_fn`: `composer_replication/recipes/prime_rl/tests/test_composer_loss.py`.
Untested-contract symbols (⚠️) and skeletons (🟡) are flagged inline above.
---
**Document path**: `/mnt/e/CS/HF/composer-replication-framework/docs/API_REFERENCE.md`123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998# INTEGRATION_RECIPES.md — Wiring the 3-channel composer loss into your RL stack
> **Status:** Wave 14 release reference. Supersedes the historical> [`docs/INTEGRATION_ARCHITECTURE.md`](INTEGRATION_ARCHITECTURE.md) (Recipes> A–D), which is retained as background reading for the original> mechanism-level diagrams.>> **Companion docs:**> - [`docs/USER_GUIDE.md`](USER_GUIDE.md) — narrative walk-through, sections 1–8> - [`docs/API_REFERENCE.md`](API_REFERENCE.md) — exact kwarg signatures> - [`docs/TROUBLESHOOTING.md`](TROUBLESHOOTING.md) — error → fix index> - [`docs/V3_SUBSTRATE_COVERAGE.md`](V3_SUBSTRATE_COVERAGE.md) — what each> substrate covers> - [`docs/adrs/ADR-006-rl-frameworks.md`](adrs/ADR-006-rl-frameworks.md) —> why these five recipes and not others
This document is the canonical answer to **"how do I plug the 3-channelcomposer loss into framework X?"** for the five frameworks the projectsupports as of Wave 14:
1. [TRL `GRPOTrainer` subclass](#recipe-1--trl-grpotrainer-subclass)2. [VeRL custom `adv_estimator` + DataProto extension](#recipe-2--verl-custom-adv_estimator--dataproto-extension)3. [PRIME-RL custom-loss config](#recipe-3--prime-rl-customlossconfig)4. [Serverless Decoupled DiLoCo (Modal / HF Jobs / SageMaker)](#recipe-4--serverless-decoupled-diloco)5. [Monarch actor mesh (TorchForge-style topology)](#recipe-5--monarch-actor-mesh)
Each recipe follows the same seven-part template:
1. **When to use it** — decision criteria.2. **Install command** — which optional extras of `composer-replication`.3. **Minimum-viable Python script** — copy-pasteable, ≤ 60 lines.4. **Decoupled DiLoCo wiring** — how `ServerlessExecutor` + `ObjectStoreAllReduce` + `MockManager` layer on top.5. **Distillation-loss wiring** — how to switch DPO → SimPO and add TAID via `compose_loss(..., dpo_variant=..., sdpo_wrapper=...)` or the recipe's own loss-config field.6. **Cost ballpark** — GPU $/hr + API spend, sourced from [`docs/research/DILOCO_SERVERLESS_RECONNAISSANCE.md`](research/DILOCO_SERVERLESS_RECONNAISSANCE.md).7. **Known limitations as of Wave 14**.
A cross-recipe [comparison matrix](#comparison-matrix) closes the doc.
## TL;DR — the unified loss
For any of the five recipes, the v0.1 trainer step computes:
```total_loss = grpo_loss + α * sdpo_kl_loss (channel 2 — Composer hint-distill; optional TAID or Entropy-OPD wrapper) + β * trace_replay_loss (channel 3 — N-teacher DPO; switchable to SimPO)```
This is implemented once, in[`composer_replication/loss.py::compose_loss`](../composer_replication/loss.py),and re-used by every recipe via the kwargs documented in[`API_REFERENCE.md`](API_REFERENCE.md). The verified surface is:
```pythondef compose_loss( model, inputs, *, alpha_sdpo: float = 0.1, beta_replay: float = 0.05, sdpo_jsd_beta: float = 0.5, sdpo_temperature: float = 1.0, sdpo_token_clip: float | None = None, replay_dpo_beta: float = 0.1, # ADR-007 extensions dpo_variant: Literal["dpo", "simpo"] = "dpo", sdpo_wrapper: Literal["none", "taid", "entropy_opd"] = "none", taid_schedule_step: int | None = None, taid_total_steps: int | None = None, simpo_beta: float = 2.0, simpo_gamma: float = 1.0, taid_schedule: str = "linear", taid_alpha_max: float = 1.0, entropy_opd_h_max: float | None = None,) -> torch.Tensor: ...```
All five recipes below either call `compose_loss` directly or call athin per-framework adapter that forwards these kwargs unchanged.
---
## Recipe 1 — TRL `GRPOTrainer` subclass
### 1. When to use it
This is the **default v0.0/v0.1 path** and the one we recommend for~99% of users today. Pick TRL when:
- Your model fits on ≤ 32 GPUs (typically ≤ 70B-param FSDP).- You already have a HuggingFace `model` + `tokenizer` + `datasets` flow.- You want minimum integration cost — `ComposerReplicationTrainer` is a single subclass override of `_compute_loss` over `trl.GRPOTrainer`, no Ray, no actor mesh.- You're doing single-host (one node, possibly multi-GPU FSDP) training.
Don't pick TRL when you need >100 B-param scale, when you must async-decoupletool calls from the GPU loop, or when a Ray cluster is already in your stack(in which case Recipe 2 is cheaper).
### 2. Install command
```bashpip install -e ".[train,replaysim]"```
The `train` extra pulls `trl>=0.12`, `peft`, `accelerate`, and `datasets`.The `replaysim` extra pulls `data-juicer` for CPU-side DPO normalization(channel 3 cleaning step). Add `[serverless]` if you also want DecoupledDiLoCo (see step 4).
### 3. Minimum-viable Python script
```python# train_trl.py — minimum viable Recipe 1from datasets import load_datasetfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom composer_replication import ComposerReplicationTrainer
MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct" # swap for 7B once it worksmodel = AutoModelForCausalLM.from_pretrained(MODEL_ID)tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)dataset = load_dataset("trl-lib/tldr", split="train[:512]")
def reward_length(completions, **_): return [-abs(len(c) - 64) for c in completions]
trainer = ComposerReplicationTrainer( model = model, processing_class = tokenizer, reward_funcs = [reward_length], train_dataset = dataset, # Composer extras (defaults shown): alpha_sdpo = 0.1, beta_replay = 0.05, sdpo_jsd_beta = 0.5, sdpo_temperature = 1.0, sdpo_token_clip = None, replay_dpo_beta = 0.1,)trainer.train()```
Channels 2 and 3 **auto-disable per step** when their inputs aren'tpresent in the batch (e.g. batches with no error sites get`sdpo_kl=0`). Set `alpha_sdpo=0` / `beta_replay=0` to disable globallyfor ablations.
### 4. Decoupled DiLoCo wiring
`ComposerReplicationTrainer` is a single-process trainer. To run Nreplicas of it under Decoupled DiLoCo, layer the serverless stack on theoutside: each replica runs the script above; `MockManager` stands in for`torchft.Manager` on the inner loop and `ObjectStoreAllReduce` runs theouter-loop pseudo-gradient exchange:
```python# diloco_replica.py — what each of the N replicas runsimport osfrom composer_replication.diloco import make_diloco_outer_loopfrom composer_replication.diloco.serverless import ( LocalProcessExecutor, ObjectStoreAllReduce, MockManager,)
rendezvous = ObjectStoreAllReduce( uri = "s3://my-bucket/diloco-runs/run42/", world_size = 4, rank = int(os.environ["REPLICA_RANK"]),)manager = MockManager(allreduce=rendezvous)# trainer.optimizer is the *inner* optimizer; the outer is built here:outer = make_diloco_outer_loop( inner_optimizer = trainer.optimizer, manager = manager, sync_every_h = 500,)trainer.add_callback(outer.callback()) # syncs every H inner stepstrainer.train()```
The driver process spins these up with any `ServerlessExecutor`:
```python# Wave 14: ModalExecutor / HFJobsExecutor are skeletons (raise NotImplementedError);# use LocalProcessExecutor for testing. Swap once the cloud backends land.executor = LocalProcessExecutor()handles = executor.launch_replicas( n_replicas = 4, entrypoint = "diloco_replica.py", entrypoint_args = {"rendezvous": rendezvous.uri, "rank_env": "REPLICA_RANK"},)result = executor.collect(handles, timeout=3600)```
### 5. Distillation-loss wiring
`ComposerReplicationTrainer` exposes the new ADR-007 channels via theshared `compose_loss` kwargs — pass them through `**kwargs` on thetrainer and they're forwarded to `compose_loss`:
```pythontrainer = ComposerReplicationTrainer( model = model, processing_class = tokenizer, reward_funcs = [reward_length], train_dataset = dataset, # SimPO instead of DPO for channel 3: dpo_variant = "simpo", simpo_beta = 2.0, simpo_gamma = 1.0, # TAID-wrapped SDPO for channel 2: sdpo_wrapper = "taid", taid_schedule = "linear", taid_schedule_step = 0, # bumped each call by your callback taid_total_steps = 10_000, taid_alpha_max = 1.0,)```
Or, equivalently, drop `entropy_opd` in for `taid` if you wantper-token entropy-gated forward/reverse KL instead of thelinear-blend interpolation. SimPO does **not** require referencelog-probs (channel 3 batches with `dpo_chosen_ref_logprobs` /`dpo_rejected_ref_logprobs` set are silently ignored).
### 6. Cost ballpark
- **GPU**: single host, `g5.12xlarge` ($5.67/hr) or RunPod 4×A100-80GB (~$5–9/hr) gets you Qwen2.5-7B at moderate throughput. For Qwen2.5-72B you'll want 2–4× H100 — `p5.48xlarge` (~$98/hr on AWS, ~$25–30/hr on Lambda Cloud / RunPod community).- **API**: channel 3 teacher replay via OpenRouter — verified ~$0.98/trace at 50 steps × 3 teachers (spike 001). For a 100-trace curriculum that's ~$100 in teacher tokens.- **Storage**: negligible until you turn on DiLoCo (then see Recipe 4).
### 7. Known limitations as of Wave 14
- **Tool calls block the GPU.** TRL's rollout is synchronous; long tool-call latency idles the trainer. Async-decouple via Recipe 2/3/5 if this matters.- **No native multi-node.** TRL is single-process; multi-host scaling is via Decoupled DiLoCo (Recipe 4) on top, not via TRL itself.- **vLLM weight sync is co-located** — no resharding between FSDP and TP. At 70B+ this becomes the bottleneck and you should move to Recipe 2.- **`reward_funcs` must be Python callables** that return `list[float]`; shell-out reward graders need a wrapper.
---
## Recipe 2 — VeRL custom `adv_estimator` + DataProto extension
### 1. When to use it
Pick VeRL when:
- You need >70B-param scale or >32-GPU multi-host, *and* a Ray cluster is acceptable in your stack.- You're already using or willing to adopt **3D-HybridEngine** for efficient FSDP↔TP weight resharding (verified ~5× weight-sync speed-up vs co-located vLLM at 70B+).- You need async multi-turn rollouts where tool-call latency must not block the GPU loop. VeRL's `AsyncServer` + `AgentLoop` is the best-in-class option here.- You want extension points the framework's authors *expect* third parties to use — the `@register_adv_est("...")` decorator and the `DataProto` extension contract are first-class APIs.
Don't pick VeRL if you're <7B-param or single-host (overkill —Recipe 1's Trainer subclass is one file, not a Ray cluster).
### 2. Install command
```bashpip install -e ".[replaysim]"pip install verl # not packaged as an extra; pinned at >=0.3# Optional, for the Composer adapter:pip install -e ".[serverless]" # for Decoupled DiLoCo on top```
The framework's verl adapter lives at`composer_replication.recipes.verl` (currently shape-only — see[Limitations](#7-known-limitations-as-of-wave-14-2) below).
### 3. Minimum-viable Python script
VeRL's actual entry point is a Hydra/YAML config + `verl.trainer.main_ppo`CLI; the pythonic surface looks like this:
```python# train_verl.py — minimum viable Recipe 2 sketchfrom verl.trainer.ppo import core_algosfrom verl.trainer.ppo.ray_trainer import RayPPOTrainerfrom composer_replication.loss import compose_loss
@core_algos.register_adv_est("grpo_composer")def composer_advantage(data, **kwargs): """Custom adv-estimator that adds SDPO + DPO channels to GRPO.
Reads three extra DataProto keys (populated by the data prep step): - data.batch["sdpo_teacher_logits"] (channel 2) - data.non_tensor_batch["teacher_actions"] (channel 3) and returns the standard (advantages, returns) tuple plus a stashed composer-loss term consumed by the critic worker. """ advantages, returns = core_algos.compute_grpo_outcome_advantage(data, **kwargs) composer_term = compose_loss( model = kwargs["actor_module"], inputs = data.batch, alpha_sdpo = 0.1, beta_replay = 0.05, dpo_variant = "dpo", sdpo_wrapper = "none", ) data.meta_info["composer_loss"] = composer_term return advantages, returns
# Then in your YAML:# algorithm:# adv_estimator: grpo_composer# and run: python -m verl.trainer.main_ppo --config-name composer_grpo```
The full driver wires `RayPPOTrainer` against your config; consult VeRL'sown quickstart for the Ray-cluster boilerplate. The composer-specificpiece is just the registered estimator above.
### 4. Decoupled DiLoCo wiring
VeRL's actor workers run in Ray; DiLoCo replicates the **whole VeRL job**.Each "replica" is one Ray cluster running Recipe 2 end-to-end; the outerloop is independent of Ray and just exchanges pseudo-gradients via theobject store between Ray-job invocations:
```pythonfrom composer_replication.diloco.serverless import ( LocalProcessExecutor, ObjectStoreAllReduce,)
rendezvous = ObjectStoreAllReduce( uri = "s3://verl-diloco/run/", world_size = 4,)executor = LocalProcessExecutor() # Wave 14: ModalExecutor is a skeleton (raises NotImplementedError) — keep LocalProcessExecutor for nowhandles = executor.launch_replicas( n_replicas = 4, entrypoint = "verl.trainer.main_ppo", entrypoint_args = { "+algorithm.adv_estimator": "grpo_composer", "+algorithm.diloco.rendezvous": rendezvous.uri, "+algorithm.diloco.sync_every_h": 500, },)executor.collect(handles, timeout=24 * 3600)```
The Ray cluster inside each replica handles intra-replica scaling(FSDP / TP / vLLM); the object-store exchange handles cross-replicasync. Bandwidth is identical to Recipe 1 (~2 GB / 30 min per replicafor a 7B-param model in bf16) and well within S3 free-tier.
### 5. Distillation-loss wiring
The custom `adv_estimator` from step 3 already calls `compose_loss`;flip the kwargs there to switch DPO → SimPO or add TAID:
```pythoncomposer_term = compose_loss( model = kwargs["actor_module"], inputs = data.batch, alpha_sdpo = 0.1, beta_replay = 0.05, dpo_variant = "simpo", # ← SimPO swap simpo_beta = 2.0, simpo_gamma = 1.0, sdpo_wrapper = "taid", # ← TAID wrap taid_schedule_step = data.meta_info.get("global_step", 0), taid_total_steps = 10_000,)```
VeRL's `data.meta_info` carries the global step automatically, which isexactly what TAID's interpolation schedule needs. Channel 2 batcheswithout `student_init_logits` / `student_init_input_ids` are auto-skipped(returns 0 for that step).
### 6. Cost ballpark
- **GPU**: 8× H100 (`p5.48xlarge` ~$98/hr on AWS, ~$25/hr on Lambda or RunPod community) is the entry point for 70B-class. Expect 32–256 H100 for full 671B (matches DeepSeek's reported VeRL config).- **API**: same ~$0.98/trace as Recipe 1 (channel 3 is a Python helper, not a VeRL primitive — costs are framework-independent).- **Ray cluster overhead**: head node + redis + dashboard adds ~1 CPU-instance ($0.10–0.50/hr) per cluster, negligible at GPU scale.
### 7. Known limitations as of Wave 14
- **`composer_replication.recipes.verl` is shape-only.** The decorator registration and DataProto extension are documented but not yet shipped as a runnable adapter — Wave 14 release exposes the *contract*, not the glue. Expect this to land in a v0.2 follow-up spike.- **Ray dependency.** Adds a heavyweight runtime; debugging cross-actor crashes can be painful. Use VeRL's `--debug` mode early.- **Custom-`adv_estimator` LOC**: writing your own takes ~50–150 LOC including DataProto plumbing. Not a one-liner.- **No first-class TAID hook in VeRL itself** — we route TAID through the meta_info channel; this works but means you can't use VeRL's built-in checkpoint-replay tooling without re-stamping `taid_schedule_step` on each replay.
---
## Recipe 3 — PRIME-RL `CustomLossConfig`
### 1. When to use it
Pick PRIME-RL when:
- You're operating in the **PRIME-Intellect / decentralized training** universe and want INTELLECT-style scaling on a long-horizon training run.- You need **DPPO importance-ratio masking** (the rationale most users arrive with) — PRIME-RL's headline contribution is the out-of-band-token *mask* (not clip) on `log_ratio = trainer_lp - inference_lp`, with defaults `low=-4.0, high=4.0`.- You want a **first-class custom-loss surface**: PRIME-RL ships `CustomLossConfig` that takes an importable Python function and a `LossInputs` struct exposing exactly the tensors we need (`trainer_logprobs`, `inference_logprobs`, `teacher_logprobs`, `advantages`, `loss_mask`). No fork, no Trainer subclass, no monkey-patch.- You have access to multi-node infrastructure that PRIME-RL's trainer/inference/orchestrator split is designed for.
Don't pick PRIME-RL if you need full vocab logits (channel 2 SDPOrequires logits not log-probs — see Limitations).
### 2. Install command
```bashpip install -e ".[prime-rl,replaysim]"# pulls prime-rl>=0.5```
### 3. Minimum-viable Python script
PRIME-RL drives via YAML config; the only Python you write is thecustom-loss function (already shipped at`composer_replication/recipes/prime_rl/composer_loss.py`). Wire it in:
```yaml# prime_rl_config.yaml — point at the framework's adapterloss: custom: import_path: composer_replication.recipes.prime_rl.composer_loss:loss_fn kwargs: alpha_sdpo: 0.0 # channel 2 deferred in v0 (see below) beta_dpo: 0.0 # channel 3 emits a warning if non-zero dppo_mask_high: 4.0 # PRIME-RL DPPO mask bounds dppo_mask_low: -4.0 epsilon: 1.0e-6
trainer: model: Qwen/Qwen2.5-7B-Instruct ... # standard PRIME-RL fields```
The shipped `loss_fn` signature is fixed by PRIME-RL's contract:
```pythondef loss_fn( inputs: LossInputs, *, alpha_sdpo: float = 0.0, beta_dpo: float = 0.0, dppo_mask_high: float = 4.0, dppo_mask_low: float = -4.0, epsilon: float = 1e-6,) -> torch.Tensor: log_ratio = inputs.trainer_logprobs - inputs.inference_logprobs dppo_invalid = (log_ratio > dppo_mask_high) | (log_ratio < dppo_mask_low) keep_mask = inputs.loss_mask & ~dppo_invalid grpo = -(inputs.advantages * inputs.trainer_logprobs * keep_mask).sum() \ / keep_mask.sum().clamp_min(epsilon) if alpha_sdpo != 0.0: raise NotImplementedError( "Channel 2 SDPO requires full-vocab logits; PRIME-RL v0.5 " "exposes only log-probs. Deferred to v0.2." ) if beta_dpo != 0.0: import warnings; warnings.warn( "Channel 3 trace-replay DPO is out-of-scope for PRIME-RL recipe v0", stacklevel=2, ) return grpo```
**Shape note** (caught in the Wave 13 cross-model review): PRIME-RLcalls the loss function **once per sample**; tensors are 1-D `(seq,)`,*not* batched `(B, T)`. The 10 unit tests in`composer_replication/recipes/prime_rl/tests/test_composer_loss.py`cover this plus DPPO mask edges.
### 4. Decoupled DiLoCo wiring
PRIME-RL was designed for decentralized training and ships its ownweight-sync primitives. Stack DiLoCo on top via the`ServerlessExecutor` Protocol — each replica runs an independentPRIME-RL job pointing at the same `composer_loss:loss_fn`:
```pythonfrom composer_replication.diloco.serverless import ( LocalProcessExecutor, ObjectStoreAllReduce,)
rendezvous = ObjectStoreAllReduce( uri = "s3://prime-rl-diloco/run/", world_size = 4,)# Wave 14: ModalExecutor is a skeleton (raises NotImplementedError until v0.x).# Use LocalProcessExecutor for the inner-replica wiring; swap to the cloud# executor once it lands. The DiLoCo + rendezvous code below is identical.executor = LocalProcessExecutor()handles = executor.launch_replicas( n_replicas = 4, entrypoint = "prime_rl.cli:main", entrypoint_args = { "config": "prime_rl_config.yaml", "+diloco.rendezvous": rendezvous.uri, "+diloco.sync_every_h": 500, },)executor.collect(handles, timeout=24 * 3600)```
Note PRIME-RL's own multi-node story (the trainer / inference /orchestrator split) is **orthogonal** to Decoupled DiLoCo: PRIME-RLmulti-node = single replica scaled across many GPUs; DiLoCo = Nindependent replicas synchronizing via object store. Combine both for"big PRIME-RL job × N replicas".
### 5. Distillation-loss wiring
Channel 2 (SDPO + TAID + Entropy-OPD) is **deferred** in v0 becausePRIME-RL's `LossInputs` exposes log-probs not full vocab logits. TheSimPO swap on channel 3 is also gated by the same shape constraint, butDPPO-clip itself doesn't change. To get TAID/SimPO into a PRIME-RL jobtoday you must:
1. Switch to Recipe 1 or 2 for the SFT/distill phase.2. Use PRIME-RL only for the on-policy GRPO+DPPO phase.
The v0.2 plan (per ADR-007) is to extend `LossInputs` with a`teacher_logits` field; the loss adapter is already shape-ready.
### 6. Cost ballpark
- **GPU**: similar profile to Recipe 2 — 8–32 H100 typical, scales to hundreds for INTELLECT-class runs. Lambda Cloud or RunPod community H100 community pricing (~$2–4/hr per H100) is most cost-effective.- **API**: channel 3 is gated, so the only OpenRouter spend is from the *offline data-prep* spike (using the verifier harness in Recipe 1 to pre-bake DPO pairs), not from the training loop itself. Order of magnitude: $50–500 for a curriculum-bake one-time, then $0/run.- **Network**: PRIME-RL's own decentralized weight sync uses substantial bandwidth between training replicas (one of its design constraints); this is *separate* from the Decoupled DiLoCo bandwidth and shows up as a ceiling on cross-region replica placement.
### 7. Known limitations as of Wave 14
- **Channel 2 deferred** — see step 5. `alpha_sdpo > 0` raises `NotImplementedError`.- **Channel 3 emits a warning** if `beta_dpo != 0`; trace-replay DPO pairs must be folded into the *training data* (offline) rather than the *loss* (online) until v0.2.- **PRIME-RL ≥ 0.5 required.** Earlier versions don't ship `CustomLossConfig`.- **Smoke test deferred.** Per `prime_rl_recipe.md`, the runtime smoke test requires a CUDA box + `prime-rl >= 0.5` install and is gated to a follow-up spike. The 10 unit tests run cleanly without GPU.- **DPPO defaults are PRIME-RL's, not ours.** We pin `low=-4.0, high=4.0` to match. If you change them, you're now diverging from PRIME-RL's example configs.
---
## Recipe 4 — Serverless Decoupled DiLoCo
### 1. When to use it
Pick Decoupled DiLoCo when:
- You have **N independent training replicas** that should sync occasionally but can't (or shouldn't) cross-talk on every step.- The cost or operational burden of an always-on multi-node cluster is unacceptable, but you're happy paying for 4× independent **serverless jobs**.- Your inner trainer is one of Recipes 1–3 — DiLoCo wraps any inner optimizer; it's *purely outer-loop*.- You need **failure isolation**: if one replica crashes, the others keep training; on restart it picks up from the last outer round.
DiLoCo's design rests on two abstractions (per ADR-005):
1. **`ServerlessExecutor` Protocol** — uniform interface for spinning up N replicas across cloud backends (Modal / HF Jobs / SageMaker / k8s).2. **`ObjectStoreAllReduce`** — fsspec-backed pseudo-gradient exchange that replaces the in-process `torchft.Manager.allreduce` call.
The communication pattern is `S3 PutObject + N GetObjects` once perinner-H steps, matching DiLoCo paper §3.2 (arXiv:2311.08105). For1B-param bf16 that's ~2 GB / 30 min per replica — well within S3free-tier.
### 2. Install command
```bashpip install -e ".[diloco,serverless]"# also one of the inner-trainer extras:pip install -e ".[train]" # if the inner trainer is Recipe 1# OR pip install verl # if the inner trainer is Recipe 2# OR pip install -e ".[prime-rl]" # if the inner trainer is Recipe 3```
### 3. Minimum-viable Python script
This pattern is independent of the inner trainer — pick any of Recipes1/2/3 and wrap it with a `ServerlessExecutor`. The replica entrypointruns the inner trainer; the driver launches N of them and waits.
```python# diloco_driver.py — driver that launches N replicasfrom composer_replication.diloco.serverless import ( LocalProcessExecutor, # for dev — runs replicas as local subprocesses ObjectStoreAllReduce,)
rendezvous = ObjectStoreAllReduce( uri = "s3://my-bucket/diloco-runs/run42/", # or file:// for local world_size = 4,)executor = LocalProcessExecutor() # Wave 14: ModalExecutor skeleton raises NotImplementedError; swap once cloud backend landshandles = executor.launch_replicas( n_replicas = 4, entrypoint = "diloco_replica.py", # (script below) entrypoint_args = { "rendezvous": rendezvous.uri, "rank_env": "REPLICA_RANK", },)result = executor.collect(handles, timeout=3600)print({h.replica_id: h.exit_code for h in result})```
```python# diloco_replica.py — runs inside each replicaimport osfrom composer_replication.diloco import make_diloco_outer_loopfrom composer_replication.diloco.serverless import ( ObjectStoreAllReduce, MockManager,)
# Build inner trainer (Recipe 1 example):from train_trl import trainer
rendezvous = ObjectStoreAllReduce( uri = os.environ["DILOCO_RENDEZVOUS"], world_size = 4, rank = int(os.environ["REPLICA_RANK"]),)manager = MockManager(allreduce=rendezvous)outer = make_diloco_outer_loop( inner_optimizer = trainer.optimizer, manager = manager, sync_every_h = 500,)trainer.add_callback(outer.callback())trainer.train()```
### 4. Decoupled DiLoCo wiring
This recipe **is** the DiLoCo wiring — see step 3. The availableexecutor adapters are:
| Executor | Status | Use case ||---------------------------|-------------------------------|--------------------------------------|| `LocalProcessExecutor` | Production-ready | Dev loop — N subprocesses on one box || `ModalExecutor` | Skeleton (modal-client gated) | Modal cloud, $/sec billing || `HFJobsExecutor` | Skeleton (hf-hub gated) | HuggingFace Jobs, transformer-shop || `SageMakerExecutor` | Roadmap (post-v0.2) | AWS, warm-pool ~10s cold start || `K8sExecutor` | Roadmap | KubeRay / Volcano gang scheduling |
Cross-cloud replica placement (e.g. 2× Modal + 2× HF Jobs) is supportedin principle — they all read/write the same S3 / GCS / HF rendezvous —but treat as experimental.
### 5. Distillation-loss wiring
DiLoCo is loss-agnostic — it operates purely on inner-optimizer state.Whichever inner trainer you're running (Recipe 1, 2, or 3) handlesdistillation kwargs as documented in that recipe's step 5. The onlyDiLoCo-specific knob worth knowing: TAID's `taid_schedule_step` is a*global* counter, but each replica increments it independently. If youcare about replicas all reading the same α at outer-sync time, set`taid_schedule_step = trainer.state.global_step + replica_offset` andlet the outer-loop sync average them out.
### 6. Cost ballpark
Pulled from[`docs/research/DILOCO_SERVERLESS_RECONNAISSANCE.md`](research/DILOCO_SERVERLESS_RECONNAISSANCE.md):
| Backend | A100-80GB $/hr | H100 $/hr | Cold-start | Notes ||---------------|----------------|-----------|------------|------------------------------------------|| Modal | $1.39/sec → 4× ≈ $20/hr per A100 | ~$8/hr per H100 | 1–60s warm, 60–120s first-run | $/sec billing; no minimum || AWS SageMaker | $4.10/A100·hr | $12.29/hr | 2–5 min cold, ~10s warm pool | Min 60min on warm pool || GCP Vertex | $3.67/A100·hr | $11/hr | 2–6 min cold | 30–50% premium over raw GPU || Azure ML | ~$3.67/A100·hr | ~$12.25/hr | 3–8 min cold | Use curated env to cut cold-start || RunPod | $1.19/hr (community), $2.17 (secure) | $1.99/hr (community), $4.18 (secure) | seconds | No federation; same-DC only || HF Jobs | comparable to Modal | ~$8–12/hr | 30–90s | Best DX for HF-shop |
**Object-store cost.** ~$0.02/GB-month for S3 standard, ~$0/free-tier.Pseudo-gradients are ~2 GB per replica per outer round; for a 24-hour4-replica run at H=500 that's ~50 outer rounds × 2 GB × 4 replicas = ~400GB written. Free-tier blows through fast — budget $10–20 in storage.
### 7. Known limitations as of Wave 14
- **`ModalExecutor` and `HFJobsExecutor` are skeletons.** They check `import modal` / `import huggingface_hub` at *adapter init* time and raise; the actual `launch_replicas` is shape-only until the relevant spike lands. Use `LocalProcessExecutor` for dev.- **`ObjectStoreAllReduce(world_size=1)`** must passthrough cleanly — the unit test `test_object_store_allreduce_world_size_1_passthrough` is the regression guard. Don't override unless you've read it.- **Rank validation is mandatory.** Tests assert `ObjectStoreAllReduce(rank=N, world_size=N)` raises (rank must be `< world_size`); silent corruption otherwise.- **`MockManager` is *not* feature-complete.** It implements the `Manager.allreduce` surface that DiLoCo's outer-loop needs, but not the full `torchft.Manager` API (no fault-tolerance, no membership protocol). Don't use it as a drop-in for live torchft.- **No native heterogeneous compute** — all replicas are assumed to have the same compute shape. Mixed A100+H100 placements work but the slow replica gates outer-loop progress.
---
## Recipe 5 — Monarch actor mesh
### 1. When to use it
Pick Monarch when:
- You're at **TorchForge-style topology scale**: trainer / generator / rewarder / N-teachers all want to be independent, asynchronously scheduled, fault-tolerant actors on a typed mesh.- You want **heterogeneous executor support** — different actors run in different clouds (e.g. `TrainerActor` on Modal A100s, `GeneratorActor` on dedicated H100s, `TeacherPoolActor` as 0-GPU CPU pods on k8s).- You need **hot-swap of actor implementations** — replace "OpenRouter teachers" with "local vLLM teachers" by changing one Monarch binding, no trainer code change.- You're prepared to track **upstream Monarch** (v0.4.1 stable, v0.5 dev daily); the API is moving and v0 of this recipe is intentionally deferred per ADR-006.
Don't pick Monarch in Wave 14 unless you're explicitly scoping av0.2+ pilot. The framework ships *skeleton* actors that fail-fast oninstantiation; this is a reference-pattern reading exercise, not aproduction target.
### 2. Install command
```bashpip install -e ".[prime-rl,monarch]"# pulls monarch>=0.4.1 plus the PRIME-RL trainer used inside actors```
### 3. Minimum-viable Python script
The framework ships skeleton actor definitions at`composer_replication/recipes/monarch/actors.py`; they raise`NotImplementedError` on instantiation in Wave 14. The shape of thefinal answer:
```python# monarch_train.py — what v0.2+ usage will look likefrom monarch import Actor, mesh, endpointfrom composer_replication.recipes.monarch.actors import ( TrainerActor, GeneratorActor, RewarderActor, TeacherPoolActor,)
# Topologytrainers = mesh.spawn(TrainerActor, n=4, gpu="A100")generator = mesh.spawn(GeneratorActor, n=1, gpu="A100")rewarder = mesh.spawn(RewarderActor, n=1, gpu=None)teachers = mesh.spawn(TeacherPoolActor, n=1, gpu=None)
# Wire endpointsasync def outer_step(batch_id: int): prompts = await trainers[0].sample_prompts.call(batch_id) rollouts = await generator.rollout.call(prompts) rewards = await rewarder.score.call(rollouts) teacher_acts = await teachers.replay.call([ {"state": r["state"]} for r in rollouts ]) await trainers.train_outer_step.call( batch_id, rollouts=rollouts, rewards=rewards, teacher_actions=teacher_acts, )
# Runimport asynciofor batch_id in range(1000): asyncio.run(outer_step(batch_id))```
The Composer 3-channel loss lives inside `TrainerActor.train_outer_step`,which calls `compose_loss(...)` exactly as Recipe 1 does. The*orchestration* changes; the *loss math* doesn't.
### 4. Decoupled DiLoCo wiring
Monarch + Decoupled DiLoCo compose naturally: each `TrainerActor` is aDiLoCo replica, and Monarch's supervision tree handles the failurerecovery that ADR-005 lists as a DiLoCo design constraint. The wire-upis identical to Recipe 4's `LocalProcessExecutor` pattern, just runninginside Monarch instead of `subprocess`:
```pythonfrom composer_replication.diloco.serverless import ( ObjectStoreAllReduce, MockManager,)
class TrainerActor(Actor): def __init__(self, rendezvous_uri: str, rank: int, world_size: int): self.rendezvous = ObjectStoreAllReduce( uri=rendezvous_uri, rank=rank, world_size=world_size, ) self.manager = MockManager(allreduce=self.rendezvous) # ... build inner ComposerReplicationTrainer ...
@endpoint async def train_outer_step(self, batch_id: int, **kw): # Inner H steps locally, then sync via self.rendezvous ...```
The "object store" is the cross-actor synchronization point that*doesn't* go through Monarch's RDMA data plane — by design, slowsyncs (S3) and fast syncs (RDMA for in-actor weight broadcast) live ondifferent planes.
### 5. Distillation-loss wiring
Monarch sees the loss as opaque: it lives inside `TrainerActor` andtakes the same `compose_loss` kwargs as Recipe 1. The mesh-levelbenefit is **swap-by-binding**: you can replace `TeacherPoolActor`("OpenRouter") with a `LocalVLLMTeacherActor` to switch the*supplier* of teacher log-probs without touching the loss config.
```python# Original binding — channel 3 via OpenRouterteachers = mesh.spawn(TeacherPoolActor, n=1, gpu=None)
# Swap binding — channel 3 via local vLLMteachers = mesh.spawn(LocalVLLMTeacherActor, n=1, gpu="A100", model_id="Qwen/Qwen2.5-72B-Instruct")
# Trainer config unchanged:trainer.compose_loss_kwargs = dict( dpo_variant = "simpo", # same as before sdpo_wrapper = "taid", taid_schedule_step = batch_id, taid_total_steps = 10_000,)```
### 6. Cost ballpark
In Wave 14: $0 (skeleton fails fast; no compute used). Projected for v0.2+:
- **Mesh overhead**: Monarch's coordination plane is light — typically <1% of total compute even at 4-actor scale. The dominant cost is whatever the actors run.- **Heterogeneous placement** is the cost lever: e.g. a 4-trainer mesh with `TeacherPoolActor` on 0-GPU CPU pods can cut total $/hr by ~10–20% vs forcing all actors onto GPU nodes.- **Cluster bring-up**: Monarch v0.5's Slurm backend is stable; k8s backend is dev-track; bare-metal SSH backend is documented.
### 7. Known limitations as of Wave 14
- **Skeleton only, fails fast.** Importing `actors.py` is fine; instantiating `TrainerActor(...)` raises `NotImplementedError("v0 skeleton; deferred to v0.2 per ADR-006")`. By design.- **Upstream Monarch API is moving.** v0.4.1 stable + v0.5 dev daily means breaking changes are expected. Pin to a Monarch hash if you prototype.- **TorchForge is paused.** Per its own repo banner — don't take TorchForge's recipes as production patterns. Monarch alone is active; Forge as a layered framework is reference reading.- **Open question (deferred):** does Monarch v0.5's Slurm backend hand-shake cleanly with HF Jobs lifecycle? See `monarch_actor_layout.md` for the open-questions list.- **Open question (deferred):** can `TrainerActor` host `ComposerReplicationTrainer` unmodified, or does it need a `step_init` / `step_compute` split for Monarch's async actor model?
---
## Comparison matrix
| Dimension | Recipe 1 — TRL | Recipe 2 — VeRL | Recipe 3 — PRIME-RL | Recipe 4 — Serverless DiLoCo | Recipe 5 — Monarch ||------------------------------------|-----------------------------|----------------------------------|-----------------------------------|------------------------------------|-------------------------------------|| **Maturity (Wave 14)** | Production-ready | Production-ready (adapter shape-only) | Recipe ready, runtime smoke deferred | `LocalProcessExecutor` ready; cloud adapters skeleton | Skeleton only; v0.2+ scope || **Supports DAPO / GRPO** | GRPO ✅; DAPO via TRL master | GRPO ✅; DAPO ✅ (built-in) | GRPO+DPPO ✅ (DAPO mask is the headline) | Inherits from inner trainer | Inherits from inner trainer || **Custom-loss extension cost (LOC)** | ~30 LOC (subclass override) | ~50–150 LOC (registered estimator) | ~20 LOC (single Python fn) | 0 (transparent wrapper) | ~30 LOC (loss inside actor) || **OpenEnv-compatible** | ✅ (HF datasets layer) | ✅ (DataProto extension) | ✅ (rollout JSONL contract) | ✅ (orthogonal) | ✅ (RewarderActor binding) || **Native multi-node** | ❌ (single-host FSDP only) | ✅ (Ray cluster + 3D-HybridEngine) | ✅ (trainer/inference/orchestrator split) | ✅ (the *whole point*) | ✅ (mesh of actors) || **Native Decoupled DiLoCo** | ❌ — wrap with Recipe 4 | ❌ — wrap with Recipe 4 | ❌ — wrap with Recipe 4 | ✅ (this *is* it) | ✅ (compose with Recipe 4 inside actor) || **License** | Apache 2.0 (TRL) | Apache 2.0 (VeRL) | Apache 2.0 (PRIME-RL) | Apache 2.0 (this repo) | BSD-3 (Monarch) || **Our recommendation (Wave 14)** | **Default for ≤ 70B / single-host** | Pick at >70B *if* Ray is acceptable | Pick if PRIME-Intellect / DPPO mask is required | Stack on top of 1/2/3 for N replicas | Reference pattern only — revisit v0.2 |
---
## Cross-recipe checklist
Regardless of which recipe you pick, these invariants are tested acrossthe 124-test suite and should be true of your wired-up system:
- **`alpha_sdpo=0`** must reproduce the channel-1-only baseline bit-exact (`test_compose_loss_integration.py`).- **`beta_replay=0`** must reproduce the no-channel-3 baseline bit-exact.- **`sdpo_wrapper="taid"` without `taid_schedule_step`** must `ValueError` at first step (`test_compose_loss_integration.py`).- **`sdpo_wrapper="taid"` at `taid_schedule_step / taid_total_steps = 0`** must ignore the teacher signal (`test_taid_loss_alpha_zero_ignores_teacher`).- **`sdpo_wrapper="taid"` at `taid_schedule_step / taid_total_steps = 1`** must equal plain SDPO (`test_taid_blended_logits_endpoints`).- **`dpo_variant="simpo"`** must be differentiable through the `loss-of-sigmoid` path (`test_simpo_loss_differentiable`).- **`sdpo_wrapper="entropy_opd"`** must zero out when student ≡ teacher (`test_entropy_aware_opd_zero_when_distributions_match`).- **`ObjectStoreAllReduce(world_size=1)`** must passthrough cleanly (`test_object_store_allreduce_world_size_1_passthrough`).
If any of these fail in your wired-up system, run the correspondingunit test to localize: most break because a kwarg got dropped at theadapter boundary, not because the loss math is wrong.
---
## Picking a recipe — decision flow
1. **Piloting Monarch (v0.2+)?** → Recipe 5.2. **Else, need >70B / multi-host?** → Recipe 2 (VeRL) if Ray is OK, Recipe 3 (PRIME-RL) if you're in the PRIME-Intellect / DPPO universe, otherwise wait for Recipe 5.3. **Else** → Recipe 1 (TRL) is the v0.0/v0.1 default.4. **At any of 1–3, need N independent replicas / failure isolation?** → Stack Recipe 4 (Decoupled DiLoCo) on top.
---
## Pointers to source
- Loss core: [`composer_replication/loss.py`](../composer_replication/loss.py)- TRL trainer: [`composer_replication/trainer/composer_trainer.py`](../composer_replication/trainer/composer_trainer.py)- PRIME-RL adapter: [`composer_replication/recipes/prime_rl/composer_loss.py`](../composer_replication/recipes/prime_rl/composer_loss.py), recipe doc: [`composer_replication/recipes/prime_rl/prime_rl_recipe.md`](../composer_replication/recipes/prime_rl/prime_rl_recipe.md)- Monarch skeleton: [`composer_replication/recipes/monarch/actors.py`](../composer_replication/recipes/monarch/actors.py), layout doc: [`composer_replication/recipes/monarch/monarch_actor_layout.md`](../composer_replication/recipes/monarch/monarch_actor_layout.md)- Serverless DiLoCo: [`composer_replication/diloco/serverless/`](../composer_replication/diloco/serverless/)- VeRL adapter (shape-only): `composer_replication/recipes/verl/`- ADRs: [`docs/adrs/ADR-005-serverless-diloco.md`](adrs/ADR-005-serverless-diloco.md), [`docs/adrs/ADR-006-rl-frameworks.md`](adrs/ADR-006-rl-frameworks.md), [`docs/adrs/ADR-007-distillation-losses.md`](adrs/ADR-007-distillation-losses.md)
---
**File path:** `/mnt/e/CS/HF/composer-replication-framework/docs/INTEGRATION_RECIPES.md`123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823# TROUBLESHOOTING — Wave 14
This document catalogs every Wave-14-known failure mode in the ComposerReplication Framework, along with how to diagnose, fix, and verify eachone. It is intentionally surgical: the surface area added in Waves 12–14(SimPO/TAID/Entropy-OPD distillation kwargs, the PRIME-RL composer-lossadapter, the serverless DiLoCo `MockManager` + `ObjectStoreAllReduce`path, and the data-juicer-backed replaysim normalizer) introduced newways for users to trip themselves up. Each failure mode here is somethinga maintainer has actually seen or anticipated during the cross-modelreview of Wave 14.
If you hit something not covered below, jump to the[How to file a bug report](#how-to-file-a-bug-report) section at the end —the template there gives a maintainer everything they need to reproduce.
---
## Common things to check first
Before reading any further, run through this checklist. ~80% of "frameworkbroken" reports turn out to be one of these:
1. **Python version.** The framework targets Python 3.10–3.12. The `pyproject.toml` `target-version` is `py310`. If you are on 3.13+, transitive deps (notably Ray, pulled in by data-juicer) may not yet ship wheels and will try to build from source. Run `python --version`.
2. **Fresh virtual environment.** Mixing the framework into an existing environment that already has `torch`, `transformers`, `trl`, or `torchft` pinned to incompatible versions is the #1 source of import- time errors. Create a new venv: `python -m venv .venv && source .venv/bin/activate && pip install -e .[dev]`.
3. **Editable install.** Most contributors run `pip install -e .` so that local edits to `composer_replication/` are picked up. If you `pip install composer-replication` from a registry instead, your edits to the source tree will be ignored. Confirm with `pip show composer-replication | grep Location`.
4. **Optional extras.** Several modules are optional-dep gated: - `[replay]` — adds `pyyaml`, the OpenAI/Anthropic/Together SDKs. - `[replaysim]` — adds `data-juicer` (and via it, Ray as a transitive). - `[serverless]` — adds `fsspec`. For non-local rendezvous URIs you also need a backend-specific fsspec adapter (see Failure Mode 5). - `[dev]` — adds `pytest`, `ruff`, etc. If you see `ModuleNotFoundError: No module named 'data_juicer'`, you forgot the extra. Install with `pip install -e .[replaysim]`.
5. **Run the test suite first.** Before debugging anything, run the subset of tests touching the area you care about: ``` pytest composer_replication/tests/ # core compose_loss pytest composer_replication/distillation/tests/ # SimPO / TAID / OPD pytest composer_replication/recipes/prime_rl/tests/ # PRIME-RL adapter pytest composer_replication/diloco/serverless/tests/ # MockManager + DiLoCo pytest composer_replication/replaysim/tests/ # data-juicer normalizer ``` If any green test fails for you locally, the problem is environmental — fix that before digging into your own code.
6. **Read the docstring of the symbol you're calling.** Wave 14 docstrings are written to be the first line of documentation. The `compose_loss` docstring (`composer_replication/loss.py`) lists every required and optional input key. The `MockManager` docstring enumerates the torchft surface methods it implements.
---
## Failure modes
### 1. `pip install -e .[replaysim]` hangs or fails on Python 3.12 with a Ray-related path error
**SYMPTOM.** Installing the `[replaysim]` extra (which pulls`data-juicer`) triggers a transitive install of Ray. On Python 3.12, thefirst `import ray` (often during `pip` build hooks or the first timedata-juicer is loaded) fails with messages mentioning`/tmp/ray/session_*` paths, missing `pyarrow` symbols, or `OSError:[Errno 2] No such file or directory: '/dev/shm/ray-...'` inside Docker.
**DIAGNOSIS.** `data-juicer` declares `ray` as a transitive dependency.On Python 3.12 the wheel matrix is incomplete for some Ray versions, andRay's first-import probes `/dev/shm` and `/tmp/ray` for its sessionstate. In a sandboxed container, restricted CI runner, or WSLenvironment with a non-default `/tmp`, those probes fail. Wave 14subagent T2 hit this in CI and worked around it by pinning Ray and bymaking sure `/tmp` exists and is writable.
**FIX.**- Prefer Python 3.11 if you're on 3.12+ and don't need 3.12 features.- If you must stay on 3.12, ensure `/tmp` is writable and pre-create the session directory: `mkdir -p /tmp/ray && chmod 1777 /tmp/ray`.- In Docker, mount a real tmpfs at `/dev/shm`: `docker run --shm-size=2g …`.- If you don't need replaysim normalization, you can skip the extra entirely. The `DJNormalizer(skip_dj=True)` passthrough (see `composer_replication/replaysim/normalize.py:165`) does not import `data_juicer` and therefore does not import Ray.
**VERIFICATION.** The skip-dj passthrough is exercised by`test_dj_normalizer_skip_dj_passthrough` and`test_dj_normalizer_skip_dj_preserves_count` in`composer_replication/replaysim/tests/test_replaysim.py`. Both runwithout `data_juicer` installed:
```pytest composer_replication/replaysim/tests/test_replaysim.py::test_dj_normalizer_skip_dj_passthrough -xvs```
If that passes in your environment, your `[replaysim]`-less install ishealthy — only the full data-juicer code path requires Ray.
---
### 2. `compose_loss` produces wrong-looking numbers when combining new kwargs
**SYMPTOM.** You pass several Wave-14 distillation kwargs to`compose_loss` (e.g. `dpo_variant="simpo"`, `sdpo_wrapper="taid"`,`taid_schedule_step=0`, `simpo_beta=2.0`, `entropy_opd_h_max=…`), andthe loss curve looks wrong: NaNs, identically-zero `sdpo_jsd` channel,or a `total` that is bit-different from your reference run with nodistillation kwargs at all.
**DIAGNOSIS.** `compose_loss` now has 13 keyword arguments and thecontract between them is non-trivial. Subagent T1's review identifiedthree combinations that look reasonable but are unsupported:- Passing `taid_schedule_step` without `taid_total_steps` (or vice versa). The function raises `ValueError` clearly, but the message can scroll past in noisy logs.- Passing `dpo_variant="simpo"` while still supplying `dpo_chosen_ref_logprobs`. Those keys are **silently ignored** — SimPO is reference-free.- Passing `sdpo_wrapper="taid"` without supplying either `student_init_logits` OR `student_init_input_ids` in `inputs`. The function will fall back to a forward pass through the (possibly drifted) live model, which is a footgun late in training (see Failure Mode 8).
**FIX.** Read the docstring at the top of`composer_replication/loss.py` (lines 25–39 list the three pluggablelosses and their preconditions). The general rule:
```pythonfrom composer_replication import compose_loss
# Defaults (no distillation knobs) reproduce legacy 3-channel composition bit-exact.out = compose_loss(model, inputs)
# To opt into SimPO, pass dpo_variant ONLY. Do not pass ref-logprob keys.out = compose_loss(model, inputs, dpo_variant="simpo", simpo_beta=2.0, simpo_gamma=1.0)
# To opt into TAID, pass BOTH schedule_step AND total_steps, AND make sure# inputs["student_init_logits"] is populated (see Failure Mode 8).out = compose_loss(model, inputs, sdpo_wrapper="taid", taid_schedule_step=step, taid_total_steps=total_steps)```
Setting all 13 kwargs to their defaults is **bit-exact equivalent** tothe pre-Wave-13 3-channel loss; if your defaults call gives differentnumbers than your old code, file a bug.
**VERIFICATION.** The bit-exact equivalence and every supportedcombination is locked in by the 11 integration tests in`composer_replication/tests/test_compose_loss_integration.py`. The mostimportant ones:- `test_defaults_bit_exact_with_legacy_kwargs` — passing the new kwargs at their defaults is identical to legacy.- `test_simpo_does_not_require_ref_logprobs` — SimPO works with the ref-logprob keys absent from `inputs`.- `test_taid_alpha_one_recovers_sdpo` — TAID with `alpha_min=alpha_max=1` reproduces standard SDPO.- `test_taid_requires_schedule_step` / `test_taid_requires_total_steps` — the partial-config error path.
```pytest composer_replication/tests/test_compose_loss_integration.py -xvs```
---
### 3. `MockManager` works today but silently breaks after a torchft upgrade
**SYMPTOM.** Your serverless DiLoCo run starts, the first outer roundcompletes, and then `torchft.DiLoCo` raises an `AttributeError` onsomething like `_use_async_quorum`, `should_commit`, or`current_step` — or worse, it silently uses the wrong sync semantics.
**DIAGNOSIS.** `MockManager` is a duck-typed shim that mirrors`torchft.Manager` rather than subclassing it. The surface it implementsis enumerated in the docstring at`composer_replication/diloco/serverless/allreduce.py:215`:
> Methods/attributes DiLoCo touches: `allreduce`, `should_commit`,> `start_quorum`, `current_step`, `disallow_state_dict_read`,> `allow_state_dict_read`, `register_state_dict_fn`, `_use_async_quorum`> (attribute), `num_participants`, `rank`.
The two **private** members in that list — `_use_async_quorum` and theinternal `current_step` counter — are private torchft API that may berenamed without notice in any torchft minor release. Wave 14 subagentT3 specifically called this out: "If torchft renames `_use_async_quorum`to anything else, MockManager silently breaks because there is nothingholding the contract beyond a string."
**FIX.**- **Pin torchft.** In `pyproject.toml` keep your torchft version pinned to a known-good range (e.g. `torchft>=0.2,<0.4`). When you need to upgrade, do so deliberately and re-run the integration tests below before merging.- **Watch the deprecation warning.** Wave 14 sets up a clear path to warn if `_use_async_quorum` is read on a fresh instance — see the comment at `allreduce.py:255`.- **Don't pass an arbitrary torchft branch.** If you've patched torchft locally, the `MockManager` may need updating in lockstep. The surface-compatibility tests below will catch this in CI.
**VERIFICATION.** The full DiLoCo × MockManager surface is exercised by:- `test_mock_manager_shape_compat` in `composer_replication/diloco/serverless/tests/test_serverless_local.py` — sanity check that all expected methods/attributes exist.- `test_mockmanager_has_full_diloco_call_surface` in `composer_replication/diloco/serverless/tests/test_serverless_diloco_integration.py` — runs an end-to-end outer round through real torchft `DiLoCo`, hitting every method on the surface list above.- `test_mockmanager_diloco_outer_round_completes` — full one-round smoke ending in a successful outer SGD step.
If any of these tests turn red after a torchft bump, **do not ship**:inspect the new torchft Manager surface and update `MockManager`to match.
```pytest composer_replication/diloco/serverless/tests/test_serverless_diloco_integration.py -xvs```
---
### 4. SimPO loss curve looks like noise
**SYMPTOM.** You wired in `dpo_variant="simpo"`, the run starts, andthe `trace_replay_dpo` channel either drifts to large negative values(→ `total` blows up) or oscillates with much higher variance thanstandard DPO. The loss curve "looks like noise."
**DIAGNOSIS.** SimPO uses **average per-token log-probability**(`Σ logπ(c_t) / |c|`), not sum log-prob. From the SimPO docstring(`composer_replication/distillation/simpo.py:11–18`):
> SimPO drops the reference-policy term, replaces it with a target> margin γ, and uses **average sequence log-probability instead of> sum**. […] L_SimPO = -log σ( β · [avg_logπ(c) - avg_logπ(r)] - γ )
If you compute `chosen_logprobs.sum()` (or any unmasked aggregation) andhand it to SimPO as `chosen_avg_logprobs`, the loss is undefined: β=2.0times a sum-log-prob is on a totally different scale than β=2.0 times anaverage. The result looks plausible per-batch but the optimum isnowhere near the dataset's true preference signal.
**FIX.** Use the helper`composer_replication.distillation.simpo.avg_sequence_logprob`:
```pythonfrom composer_replication.distillation.simpo import ( simpo_loss, avg_sequence_logprob,)
chosen_avg = avg_sequence_logprob(chosen_logprobs, chosen_response_mask)rejected_avg = avg_sequence_logprob(rejected_logprobs, rejected_response_mask)loss = simpo_loss(chosen_avg, rejected_avg, beta=2.0, gamma=1.0)```
The mask is **1 on response tokens, 0 on prompt+padding** — sameconvention as the rest of the framework. If you must roll your ownaggregation, divide by `response_mask.sum(dim=-1).clamp_min(1.0)`,not by `response_mask.shape[-1]`.
**VERIFICATION.** The avg-vs-sum semantics are pinned by`test_avg_sequence_logprob` in`composer_replication/distillation/tests/test_distillation_losses.py`,which constructs known per-token log-probs and asserts the helperreturns the correct per-sequence average. The end-to-end SimPOloss-shape check is `test_simpo_loss_returns_scalar` in the same file.
```pytest composer_replication/distillation/tests/test_distillation_losses.py::test_avg_sequence_logprob -xvspytest composer_replication/distillation/tests/test_distillation_losses.py::test_simpo_loss_lower_for_better_separation -xvs```
---
### 5. `ObjectStoreAllReduce` works locally but fails on `s3://` at first allreduce
**SYMPTOM.** You construct`ObjectStoreAllReduce(uri="s3://my-bucket/run42/", rank=0,world_size=4)`. The constructor succeeds. The first call to`allreduce(tensor, name="...")` raises `ImportError: Install s3fs toaccess S3` or `botocore.exceptions.NoCredentialsError: Unable to locatecredentials`.
**DIAGNOSIS.** `ObjectStoreAllReduce` uses fsspec to reach thebackend, but **fsspec only ships protocol stubs, not adapters**. Theconstructor doesn't know which protocol you'll use and doesn'teagerly validate, so it accepts any URI. The `s3://` adapter requires:1. The `s3fs` package (`pip install s3fs`), which is **not** in the default `[serverless]` extra.2. Working AWS credentials (env vars, `~/.aws/credentials`, IAM role, or whatever your environment normally provides to boto3).
The same is true for `gs://` (`gcsfs`), `az://` (`adlfs`), and`hf://` (`huggingface_hub`'s fsspec integration, which is included ifyou have `huggingface_hub` installed).
**FIX.**- Install the right adapter alongside the framework: ``` pip install s3fs # for s3:// pip install gcsfs # for gs:// pip install adlfs # for az:// ```- Verify credentials work outside the framework first: ``` python -c "import s3fs; print(s3fs.S3FileSystem().ls('my-bucket'))" ```- If you're running on Modal/HF Jobs, set the credentials as Modal secrets / HF Jobs env vars in the executor config — not in your local shell.
The constructor could in principle perform an eager probe (e.g. a`HEAD` on the rendezvous prefix) to fail fast at init time. Wave 14deliberately did not add this because it adds a network round-trip onevery replica startup. If you want pre-flight validation in yourtraining script, call `fsspec.filesystem(protocol).ls(uri)` yourselfbefore constructing the manager.
**VERIFICATION.** The `file://` and bare-path code paths — the onlyones that don't need an extra adapter — are exercised by:- `test_object_store_allreduce_local_paths_create_dir`- `test_object_store_allreduce_world_size_1_passthrough`- `test_object_store_allreduce_round_id_increments`
…all in`composer_replication/diloco/serverless/tests/test_serverless_local.py`.If those pass and your `s3://` URI fails, the framework is fine andyour fsspec adapter or credentials are the problem.
```pytest composer_replication/diloco/serverless/tests/test_serverless_local.py -xvs```
---
### 6. Custom replaysim recipe drops every record (or crashes data-juicer)
**SYMPTOM.** You wrote a custom replaysim YAML recipe modeled on`composer_replication/recipes/replaysim/default.yaml`. It loadswithout error, but every input DPO pair is dropped, OR data-juicerraises `KeyError: 'text_key'`, OR it raises a complaint about"expected str, got list" inside one of the filters.
**DIAGNOSIS.** Wave 14 fixed two related bugs in the *default* recipethat custom-recipe authors will hit again. Both are documented in theheader comment at`composer_replication/recipes/replaysim/default.yaml:21–35`:
1. **`text_keys` plural vs `text_key` singular.** The top-level dataset contract uses `text_keys: chosen` (plural). Each individual op uses `text_key: chosen` (singular). They are not interchangeable. data-juicer's dataset loader validates that the `text_keys` field exists on every record before any op runs; an op that uses `text_keys` instead of `text_key` is silently misconfigured.
2. **`chosen` / `rejected` as strings vs as list-of-dicts.** data-juicer ops like `text_length_filter`, `words_num_filter`, `special_characters_filter`, and `document_deduplicator` read a single string field. Pointing them at the chat-messages list (`chosen_messages`, `rejected_messages`) crashes or silently no-ops. The framework's `_dpo_pair_to_dj_record` keeps **both** shapes side-by-side: `chosen`/`rejected` (strings) for filter ops, and `chosen_messages`/`rejected_messages` (chat-messages list) for chat-aware ops + the `NormalizedDPOPair` round-trip.
**FIX.** Treat the default recipe as your starting template. Concretely:- Always declare `text_keys: chosen` at the top.- For every length/word/special-char op you add, duplicate it: once with `text_key: chosen`, once with `text_key: rejected`. (Each op takes only one `text_key` — see comment at lines 31–35 of `default.yaml`.)- Never point a filter op at `chosen_messages` or `rejected_messages`. Those are list-of-dicts; only chat-aware ops accept that shape.
**VERIFICATION.** The two-shape contract is locked in by:- `test_record_chosen_rejected_are_flat_strings_for_dj_text_ops` — asserts `chosen` and `rejected` are bare strings on every record produced by `_dpo_pair_to_dj_record`.- `test_record_chosen_rejected_messages_carry_chat_shape` — asserts `chosen_messages` / `rejected_messages` exist as list-of-dicts.- `test_dj_normalizer_e2e_default_recipe(tmp_path)` — runs the actual default recipe through real data-juicer end-to-end (skipped if `data_juicer` isn't importable).
…all in`composer_replication/replaysim/tests/test_replaysim.py`. If thosepass and your custom recipe still drops everything, diff your YAMLagainst `default.yaml` until the two shapes align.
```pytest composer_replication/replaysim/tests/test_replaysim.py -xvs```
---
### 7. `ValueError: expected (seq,) shape, got (B, T)` from PRIME-RL composer_loss
**SYMPTOM.** You wired the PRIME-RL recipe into a training loop youadapted from another framework (TRL, openrlhf, etc.), and on the veryfirst `loss_fn` call you get a `ValueError` mentioning shape`(seq,)` versus `(B, T)`.
**DIAGNOSIS.** PRIME-RL calls its loss function **one sample at atime**, with 1-D `(seq,)` tensors — not batched `(B, T)` tensors. Therecipe's docstring spells this out at`composer_replication/recipes/prime_rl/composer_loss.py:16–30`:
> Note the **per-sample (seq,) shape** — PRIME-RL's runner calls the> loss function one sample at a time, not on a batched (B, T) tensor.
Wave 14 fixed an earlier draft of the recipe that incorrectly assumed`(B, T)`. The new version raises a clear `ValueError` if you hand itthe wrong shape, instead of silently broadcasting and producingnonsense gradients. Users who are used to TRL or openrlhf — both ofwhich call the loss with batched tensors — see this on day one.
**FIX.**- If you are running inside PRIME-RL via its `CustomLossConfig`, you don't need to do anything: PRIME-RL's runner produces `(seq,)` tensors and the recipe accepts them.- If you are calling the recipe directly from your own runner, slice your batch into per-sample 1-D tensors before each call: ```python for b in range(B): inputs_b = LossInputs( trainer_logprobs=batched.trainer_logprobs[b], inference_logprobs=batched.inference_logprobs[b], advantages=batched.advantages[b], loss_mask=batched.loss_mask[b], teacher_logprobs=None if batched.teacher_logprobs is None else batched.teacher_logprobs[b], ) loss = loss_fn(inputs_b, ...) ```- If you genuinely need a batched API, write a thin wrapper around `loss_fn`. Don't patch the recipe — its shape contract is dictated by PRIME-RL, not by us.
**VERIFICATION.** The shape contract is pinned by two tests in`composer_replication/recipes/prime_rl/tests/test_composer_loss.py`:- `test_advantages_shape_validates_seq_accepted` — `(seq,)` succeeds.- `test_advantages_shape_validates_bt_rejected` — `(B, T)` raises `ValueError`.
```pytest composer_replication/recipes/prime_rl/tests/test_composer_loss.py -xvs```
---
### 8. TAID can't run mid-training because `student_init_logits` is missing
**SYMPTOM.** You decide partway through a training run to enable`sdpo_wrapper="taid"` (e.g. you read the TAID paper after step 2000and want to retrofit). The next training step blows up — either witha `KeyError` for `student_init_logits` / `student_init_input_ids`, orwith a strange-looking loss because the framework fell back tore-running a forward pass through the *current* (drifted) modelinstead of the init model.
**DIAGNOSIS.** TAID interpolates between the **student's distributionat step 0** and the teacher's distribution. From the TAID docstring at`composer_replication/distillation/taid.py:10–24`:
> TAID interpolates between an "identity" target (the student's own> distribution at step 0) and the teacher's distribution, with the> interpolation coefficient annealed from 0 → 1 over training.
That step-0 reference target has to come from somewhere. The frameworkaccepts it via either:1. `inputs["student_init_logits"]` — a precomputed `(B, T, V)` tensor captured at training start (preferred for production), OR2. `inputs["student_init_input_ids"]` — input ids for a frozen forward pass through `model`. **This assumes `model` has not yet drifted from init.** It is correct only at step 0 or in tests; in production it silently produces the wrong target.
If you forgot to capture the init logits at step 0, you cannotfaithfully use TAID mid-run.
**FIX.** Capture init logits at step 0 and persist them:
```python# At step 0, before any optimizer.step() call:with torch.no_grad(): init_logits = model(input_ids=batch["input_ids"]).logits # Save to disk if you'll need them across restarts: torch.save(init_logits, "checkpoints/init_logits_batch0.pt") inputs["student_init_logits"] = init_logits
# Or, if you have a fixed eval probe set, capture init logits once# for that fixed set and reuse them every step:inputs["student_init_logits"] = cached_init_logits```
If you genuinely have no step-0 snapshot, **TAID is not retrofittable**to your run. Your options are:- Restart from a checkpoint that *was* the step-0 model.- Use a different distillation wrapper (`sdpo_wrapper="entropy_opd"`) that doesn't need init logits.- Accept the bias from the live-model fallback path. Don't.
**VERIFICATION.** The precomputed-vs-live-fallback contract is exercised by:- `test_taid_accepts_precomputed_student_init_logits` in `composer_replication/tests/test_compose_loss_integration.py` — passes precomputed logits and asserts the TAID-wrapped channel uses them.- `test_taid_alpha_one_recovers_sdpo` — asserts that with `alpha_min=alpha_max=1.0` (i.e. pure teacher target, init logits ignored) TAID reproduces standard SDPO. If your training ignores init logits silently, *this* is the test that would have failed.
```pytest composer_replication/tests/test_compose_loss_integration.py::test_taid_accepts_precomputed_student_init_logits -xvs```
---
### 9. `ModalExecutor()` or `HFJobsExecutor()` raises `NotImplementedError` at construction
**SYMPTOM.** You write`executor = ModalExecutor(app_name="my-app")` (or the HF Jobsequivalent) in a production script and the constructor immediatelyraises:
```NotImplementedError: ModalExecutor is a v0 skeleton; full implementation pending.Use LocalProcessExecutor for testing.```
Same for `HFJobsExecutor`. This is at *init time*, not at the first`launch_replicas` call.
**DIAGNOSIS.** Per ADR-005 the v0 release ships only the`ServerlessExecutor` Protocol and the reference `LocalProcessExecutor`.The Modal and HF Jobs implementations are **import-safe skeletons** —the classes exist and you can `from … import ModalExecutor`, but`__init__` raises `NotImplementedError` to prevent silent partialbehavior. See `modal.py:64` and `hf_jobs.py:64`.
This is intentional. We didn't want to ship a half-working Modalexecutor that succeeds at `launch_replicas` and then silently failstwo-thirds of the way through `collect`.
**FIX.**- Use `LocalProcessExecutor` for development, CI, and any single-host multi-process testing.- For real cloud deployment in the v0 era, run your training script directly in Modal/HF Jobs by hand: write your own thin Modal function that constructs `MockManager(ObjectStoreAllReduce(uri, rank, world_size))` and runs the training loop. The skeleton docstrings at `modal.py:24–48` and `hf_jobs.py:26–49` show exactly the pattern.- Watch the `BACKLOG.md` for v0 polish — the real implementations are scheduled.
**VERIFICATION.** That `LocalProcessExecutor` is fully functional andcorrectly implements the Protocol is locked in by:- `test_local_executor_runs_allreduce_across_replicas` in `composer_replication/diloco/serverless/tests/test_serverless_local.py` — runs N replicas locally, performs an allreduce across them.- `test_local_executor_handles_multiple_rounds`- `test_local_executor_reports_failed_replicas`
If those tests pass, your serverless DiLoCo machinery works — only thespecific cloud adapters are missing. The skeletons themselves are notunder test (raising in `__init__` is the contract).
```pytest composer_replication/diloco/serverless/tests/test_serverless_local.py -xvs```
---
### 10. DPPO mask drops every token — "loss became 0" or "no gradients"
**SYMPTOM.** You ported a PPO config from another framework (KLpenalty + clip ε=0.2 + value loss), wired it into the PRIME-RL recipewith the default `dppo_mask_high=0.2` / `dppo_mask_low=0.2`, and thetraining loss is suspiciously close to zero. Inspecting the recipe'sinternal `keep_mask` shows nearly every token is being masked out.
**DIAGNOSIS.** PRIME-RL's "DPPO mask" is **not** the same as PPOclipping, and not even the same as a log-ratio threshold. From therecipe docstring at`composer_replication/recipes/prime_rl/composer_loss.py` (mirroringPRIME-RL upstream `prime_rl/trainer/rl/loss.py` lines 137-148):
> The mask gate is on **probability-space**> `probs_diff = exp(trainer_lp) - exp(inference_lp)`, NOT on the> log-ratio. A positive-advantage token is dropped iff> `probs_diff > dppo_mask_high`; a negative-advantage token iff> `probs_diff < -dppo_mask_low`. Masked tokens are **dropped from the> policy-gradient term** but still contribute to the KL penalty.
The defaults `dppo_mask_high=dppo_mask_low=0.2` match PRIME-RL's`DefaultLossConfig`. Because the gate is on probability-space, the"in-band" zone is`exp(trainer_lp) ∈ [exp(inference_lp) - 0.2, exp(inference_lp) + 0.2]`.For a token with inference probability ~0.5 this is a fairly tightband; for tokens at probability ~0.001 or ~0.999 the same thresholdbehaves very differently from a log-ratio bound. This is by design —PRIME-RL is bounding the absolute change in token probability, not themultiplicative change.
The two failure modes:
1. **All tokens masked.** Trainer and inference engines disagree sharply (fp16 vs bf16, stale rollout cache, mismatched chat templates) and `probs_diff` exceeds 0.2 almost everywhere.2. **No tokens masked.** Trainer ≈ inference (e.g. you forgot to step the optimizer between rollouts) so the bound is never binding and the policy never sees any DPPO regularization.
**FIX.** Inspect the empirical `probs_diff` distribution beforetuning:
```python# In your training loop:probs_diff = torch.exp(trainer_logprobs) - torch.exp(inference_logprobs)print(torch.quantile(probs_diff.abs(), torch.tensor([0.5, 0.9, 0.99])))```
For a healthy on-policy run with bf16 trainer + bf16 inference andfresh rollouts, the central 99% of `|probs_diff|` should sit wellbelow `0.2`. If yours doesn't, the upstream divergence is theproblem, not the bound. Bumping `dppo_mask_high/low` to 0.5 or 1.0 isa workaround but it disables the trust-region intent of DPPO.
**Do not** translate PPO ε=0.2 directly. PPO ε=0.2 is a multiplicativelog-ratio bound (`|log_ratio| < log(1.2) ≈ 0.18`); DPPO's 0.2 is an**additive probability-space** bound. The semantics are different andthe defaults are deliberately tight in probability space.
If you genuinely want to disable the mask (e.g. for bug-isolation),pass `dppo_mask_high=1e6, dppo_mask_low=1e6` (both are`Field(..., ge=0)` upstream — negative values are rejected byboth PRIME-RL and our adapter). There is a regression test forexactly this knob.
**VERIFICATION.**- `test_dppo_mask_high_drops_positive_advantage_outliers` and `test_dppo_mask_low_drops_negative_advantage_outliers` in `composer_replication/recipes/prime_rl/tests/test_composer_loss.py` — assert that out-of-bound tokens are dropped from the policy-gradient term (with the upstream sign-of-advantage gate).- `test_dppo_mask_sign_conditioned_on_advantage` — asserts that a positive-advantage token with a large *negative* probs_diff is NOT dropped (PRIME-RL only checks the upper bound for positive-advantage tokens).- `test_dppo_bounds_can_be_disabled` — asserts that very wide bounds (`1e6`) pass every token through.- `test_parity_with_prime_rl_default_loss_fn` — when `prime-rl` is installed, runs identical inputs through PRIME-RL upstream and our adapter and asserts the loss matches.
```pytest composer_replication/recipes/prime_rl/tests/test_composer_loss.py -xvs```
---
### 11. `compose_loss` runs but the GRPO channel doesn't behave like real GRPO
**SYMPTOM.** You read the README, saw the "3-channel composition: GRPO+ SDPO + trace-replay DPO" tagline, called `compose_loss(model,inputs)` directly in your training loop, and your reward curve nevermoves the way it would in a real GRPO trainer. Or: you comparedagainst a TRL `GRPOTrainer` baseline and `compose_loss` producestotally different numbers.
**DIAGNOSIS.** From the docstring at the top of`composer_replication/loss.py:1–16`:
> This is a verification-harness mirror of> `ComposerReplicationTrainer._compute_loss` that does NOT depend on> TRL's GRPOTrainer parent. The GRPO channel is replaced with standard> LM next-token-prediction cross-entropy, which is the limit GRPO> converges to under deterministic rewards.>> Use it for: CPU smokes on real HF models, unit tests of loss> composition without spinning up TRL, anywhere we want to verify> gradient flow through the 3-channel sum without paying TRL's full> machinery cost.>> **Do NOT use it as the production training loss.** Production => ComposerReplicationTrainer (a real GRPOTrainer subclass).
The `lm_ce` channel labelled "GRPO" in the LossComponents dataclass isa **stub**: it is plain language-modeling cross-entropy. It is thecorrect channel for verification (gradient flow, channel weighting,distillation wiring), but it is not GRPO's surrogate objective andwill never produce the same numbers as real GRPO under stochasticrewards.
Real GRPO requires:- A reward model or rule-based reward,- Per-prompt advantage estimation across G samples,- An importance-sampling-ratio clip / mask.
Those live in TRL's `GRPOTrainer`, in our PRIME-RL recipe at`composer_replication/recipes/prime_rl/composer_loss.py`, or (whenshipped) in a future VeRL recipe.
**FIX.**- For production GRPO training, do **not** call `compose_loss` directly. Instead use one of: - `composer_replication.trainer.composer_trainer.ComposerReplicationTrainer` — TRL `GRPOTrainer` subclass, full machinery. - `composer_replication.recipes.prime_rl.composer_loss.loss_fn` — PRIME-RL's `CustomLossConfig` adapter (channel 1 is real DPPO-clipped GRPO).- For ablations, smokes, and unit tests, `compose_loss` is the right tool — but log the `lm_ce` channel as `lm_ce`, not as `grpo`. The `LossComponents` dataclass already names the field correctly; if your wandb logger relabels it as "GRPO loss", fix the label.
**VERIFICATION.**- The 11-test integration suite at `composer_replication/tests/test_compose_loss_integration.py` only asserts gradient flow + bit-exact composition; it deliberately does not assert any GRPO-specific property of `compose_loss`. That's the contract.- The PRIME-RL recipe's real DPPO+KL behavior is asserted by `test_returns_finite_scalar`, `test_dppo_mask_high_drops_positive_advantage_outliers`, `test_dppo_mask_sign_conditioned_on_advantage`, and `test_parity_with_prime_rl_default_loss_fn` (skip-marked when `prime-rl` is not installed) in `composer_replication/recipes/prime_rl/tests/test_composer_loss.py`. Those tests verify a real importance-sampling-ratio gradient with PRIME-RL's advantage-conditioned mask, which `compose_loss` would not pass.
If you find yourself wanting `compose_loss` to behave like real GRPO,that is the signal to switch to one of the production paths above.
```pytest composer_replication/tests/test_compose_loss_integration.py::test_defaults_bit_exact_with_legacy_kwargs -xvspytest composer_replication/recipes/prime_rl/tests/test_composer_loss.py::test_returns_finite_scalar -xvs```
---
## How to file a bug report
If you've read the relevant section above and your problem persists,file a bug. Include **all** sections of the template below — the mostcommon reason a maintainer can't repro is a missing piece ofenvironmental context.
```markdown### What I expected vs what happened(One paragraph.)
### Repro steps1. ...2. ...3. ...
Minimal self-contained snippet (no `from my_local_thing import …`):
```python# repro.pyfrom composer_replication import compose_loss...```
### Environment- OS: (uname -a or `ver` on Windows)- Python: (python --version)- composer-replication: (pip show composer-replication | head -3)- torch: (python -c "import torch; print(torch.__version__)")- torchft: (python -c "import torchft; print(torchft.__version__)" || echo "n/a")- transformers / trl: (versions, or "not installed")- data-juicer / fsspec: (versions, or "not installed")- s3fs / gcsfs / adlfs: (versions if relevant)- GPU: (nvidia-smi -L or "CPU only")- Install method: pip install -e . / wheel / other- Extras installed: [replay] [replaysim] [serverless] [dev]
### What you've already tried- [ ] Read the relevant Failure Mode section of docs/TROUBLESHOOTING.md (which one: ___)- [ ] Ran `pytest <relevant test path>` and confirmed those tests pass- [ ] Ran the repro snippet in a fresh venv- [ ] Confirmed it reproduces on Python 3.11 (if you were on 3.12 / 3.13)
### Logs(Full traceback. If it's a wrong-loss-curve rather than an exception,paste loss values for the first 10 steps and link any wandb/tb run.)
### Hypothesis(Optional. If you have a guess at where the bug is, name the file +line number. We'll look there first.)```
A few rules:- **Do not** paste API keys, AWS credentials, or HuggingFace tokens.- **Do** include the failing test name if you've narrowed it to one.- **Do** distinguish "never worked" from "regressed between commit X and Y." A regression-bisect goes straight to the front of the queue.- **One bug per issue.** Multi-headed reports lose items in triage.
The Wave-14 surface area is large, but the test suite covers itdensely — every section above corresponds to a green test that provesthe fix worked.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683# Composer Replication Framework — User Guide
A zero-to-training walkthrough for the open replication of Cursor Composer 2.5.Pace: an ML engineer who knows GRPO/DPO at a textbook level but has neveropened this repo. Every step references real code, and every kwarg namelisted below has been imported and verified against`composer_replication/` source.
---
## 1. What is this framework?
A pure-PyTorch replication of the **3-channel composer loss** that powersagentic-coding model training. One model, one optimizer, three additiveloss terms — composed every step:
``` ┌────────────────────────────────────────────┐ │ compose_loss(model, batch) │ └────────────────────────────────────────────┘ │ ┌─────────────────────────────────┼─────────────────────────────────┐ ▼ ▼ ▼┌───────────────────┐ ┌──────────────────────┐ ┌──────────────────────┐│ Channel 1 (RL) │ │ Channel 2 (SDPO) │ │ Channel 3 (replay) ││ GRPO │ │ hint-distillation │ │ multi-teacher DPO ││ → lm_ce stub in │ │ generalized JSD │ │ on (chosen, ││ verification │ │ student vs teacher │ │ rejected) pairs ││ harness │ │ (hint-conditioned) │ │ from N teachers │└─────────┬─────────┘ └──────────┬───────────┘ └──────────┬───────────┘ │ weight = 1 (always on) │ alpha_sdpo beta_replay │ └────────────────┬──────────────┴─────────────────┬──────────────────┘ ▼ ▼ total = lm_ce + α·sdpo_jsd + β·trace_replay_dpo (channel auto-disables if its weight=0 OR its inputs are missing)```
Two API surfaces, on purpose:
- **Verification harness** — `compose_loss(model, batch, ...)` is a free function (channel 1 = LM cross-entropy, the GRPO limit under deterministic rewards). Use it for CPU smokes, unit tests, and gradient-flow debugging.- **Production trainer** — `ComposerReplicationTrainer` is a `trl.GRPOTrainer` subclass that overrides `_compute_loss` with the same 3 channels on top of TRL's real reward + advantage machinery.
The verification harness is what you'll use for sections 2–6; the productiontrainer (and its alternates VeRL/PRIME-RL/Monarch) is section 8.
Source of truth: `composer_replication/loss.py` for `compose_loss`,`composer_replication/trainer/composer_trainer.py` for the trainer subclass.
---
## 2. Install — which extras to pick
Always start with the core install:
```bashgit clone https://huggingface.co/Codeseys/composer-replication-frameworkcd composer-replication-frameworkpip install -e .```
That gets you `torch>=2.0` + `transformers>=4.46` and is enough for theverification harness on CPU (sections 3, 5, 6).
The seven optional extras are declared in `pyproject.toml` `[project.optional-dependencies]`:
``` Do you need … │ ┌──────────────────────────┼──────────────────────────┐ ▼ ▼ ▼ real teacher calls DiLoCo on production over OpenRouter? >1 GPU? GRPO training? │ │ │ │ yes │ yes │ yes ▼ ▼ ▼ pip install -e ".[replay]" pip install -e ".[diloco]" pip install -e ".[train]" (httpx) (torchft-nightly) (trl, peft, accelerate, datasets) │ │ │ │ + want CPU-side │ + scaling beyond a │ + want PRIME-RL │ DPO normalization? │ single host? │ (Recipe C)? ▼ ▼ ▼ pip install -e \".[replaysim]\" pip install -e \".[serverless]\" pip install -e \".[prime-rl]\" (data-juicer; depends (fsspec, huggingface_hub) (prime-rl>=0.5) on [replay]) │ + Monarch actor mesh? ▼ pip install -e \".[monarch]\" (monarch>=0.4.1)```
Quick decision table:
| Goal | Install ||-------------------------------------------------------|------------------------------------------|| CPU smoke / verification (sections 3, 5, 6) | `pip install -e .` || Section 4 (replaysim DJNormalizer) | `pip install -e ".[replaysim]"` || Section 7 dev loop (LocalProcessExecutor + file://) | `pip install -e ".[serverless]"` || Real DiLoCo outer-loop | `pip install -e ".[diloco,serverless]"` || Section 8 Recipe A (TRL GRPO) | `pip install -e ".[train]"` || Section 8 Recipe C (PRIME-RL) | `pip install -e ".[prime-rl]"` || Section 8 Recipe C+D (PRIME-RL + Monarch) | `pip install -e ".[prime-rl,monarch]"` || Everything for development | `pip install -e ".[dev]"` |
---
## 3. Quickstart: `examples/qwen_05b_quickstart` end-to-end on CPU
The fastest way to convince yourself the framework works on a real HF model.~3–5 min wall-clock on CPU, ~1 GB disk for Qwen2.5-0.5B weights.
```bashpip install -e .python examples/qwen_05b_quickstart/run.py```
What the script does (read the source at`examples/qwen_05b_quickstart/run.py`):
1. Pin RNG (`random.seed(42)`, `torch.manual_seed(42)`) so the per-step numbers below are reproducible.2. Load `Qwen/Qwen2.5-0.5B-Instruct` on CPU in fp32, set `model.train()`.3. `batch = build_batch(tokenizer, device="cpu")` — a real chat-template-formatted batch with all keys the 3-channel composer might consume.4. Five backward steps with `compose_loss(model, batch, alpha_sdpo=0.1, beta_replay=0.05)`; an `AdamW(lr=1e-5)` optimizer; finite-grad check after each step.
Expected output (transcribed from `examples/qwen_05b_quickstart/run.log`):
```[quickstart] loading Qwen/Qwen2.5-0.5B-Instruct (CPU, fp32) ...[quickstart] loaded — 0.494B params[quickstart] building real chat-template batch ...[quickstart] running 5 backward steps ... step 0: total=0.7390 lm_ce=0.7358 sdpo=0.0000 dpo=0.0639 finite=True step 1: total=0.0379 lm_ce=0.0351 sdpo=0.0000 dpo=0.0563 finite=True step 2: total=0.0122 lm_ce=0.0110 sdpo=0.0000 dpo=0.0240 finite=True step 3: total=0.0060 lm_ce=0.0055 sdpo=0.0000 dpo=0.0098 finite=True step 4: total=0.0031 lm_ce=0.0029 sdpo=0.0000 dpo=0.0044 finite=True======================================================== Initial loss: 0.7390 → Final loss: 0.0031 → Reduction: 99.6% Verdict: PASS========================================================```
How to read this:
- **`total` collapses by ~99%.** The model successfully memorizes the single batch — exactly what you expect from an SGD pass on a 0.5B model with one fixed input. This is a wiring check, not a generalization claim.- **`lm_ce` carries almost all the magnitude.** Channel 1 (the GRPO stub) is doing the work — the response tokens are short and have low entropy under the trained model.- **`sdpo=0.0000` on every step.** Channel 2 has auto-disabled because the default `build_batch` does not include `ctx_teacher_input_ids`. Compare the conditional in `compose_loss`: ```python if (alpha_sdpo > 0.0 and "ctx_teacher_input_ids" in inputs and inputs["ctx_teacher_input_ids"].numel() > 0): ``` — channel auto-off if either the weight or the inputs are missing.- **`dpo > 0` and trending down.** The batch *does* include `dpo_chosen_input_ids`, `dpo_chosen_response_mask`, `dpo_chosen_ref_logprobs` (and the rejected counterparts), so channel 3 is live.- **`finite=True`** — every step's `p.grad` was finite for every parameter. This is the wiring contract; if it ever flips to `False` the smoke fails.
If you see `Verdict: PASS`, the framework is correctly installed andgradients flow through all live channels. You are ready for section 4.
---
## 4. Adding the trace-replay channel
The quickstart batch *had* DPO inputs, but they were synthetic — the`build_batch` helper bakes them in. To get **real** DPO pairs frommulti-teacher disagreement, use the replaysim package.
### 4a. Spin up `replay_trace`
```pythonimport asynciofrom composer_replication import ( DEFAULT_TEACHERS, replay_trace, extract_dpo_pairs,)
# Trace must be a list[TraceState]; see composer_replication/teacher_replay.py# for the exact TypedDict shape. Each state holds a chat-messages prefix +# the student's actual action at that step.states = [...] # your frozen agentic trace; see spike 001 for a 50-step example
teacher_actions = asyncio.run( replay_trace( states=states, teachers=DEFAULT_TEACHERS, # claude-opus-4.7 + gpt-5 + deepseek-v4-pro max_total_usd=10.0, # hard ceiling (spike 001 measured $0.98/trace mean) ))```
The 3 teachers are queried in parallel via OpenRouter(`OPENROUTER_API_KEY` in env or `~/.hermes/.env`), latencies recorded,costs tracked.
### 4b. Get `DPOPair`s from disagreement
```pythonpairs = extract_dpo_pairs( states=states, teacher_actions=teacher_actions, agreement_threshold=2, # at least 2/3 teachers must agree on the chosen action)```
Each pair is a `DPOPair` TypedDict with the exact shape the`DJNormalizer` and downstream training expects:
```pythonclass DPOPair(TypedDict): state_id: str state_messages: list[dict] # conversation context chosen: str # teacher-consensus action rejected: str # student action n_teachers_agreeing: int```
(verified in `composer_replication/teacher_replay.py:99–105`).
### 4c. Run `DJNormalizer` with `default.yaml`
```pythonfrom composer_replication.replaysim import DJNormalizer
normalizer = DJNormalizer() # uses recipes/replaysim/default.yamlnormalized = normalizer.normalize(pairs)# → list[NormalizedDPOPair]```
`DJNormalizer` shells out to data-juicer's `DefaultExecutor` under the hood(file-in / file-out contract). The default recipe at`composer_replication/recipes/replaysim/default.yaml` runs four CPU-only opsin order:
1. `text_length_filter` (8 ≤ chars ≤ 32000) on `chosen` and `rejected`2. `words_num_filter` (2 ≤ words ≤ 4096) on both3. `special_characters_filter` (≤50% non-alpha) on both4. `document_deduplicator` (per-batch hashing, lowercase, ignore non-character) on `chosen`
Records carry **two parallel shapes** for `chosen`/`rejected`:- flat strings (`chosen`, `rejected`) → consumed by data-juicer's text_key-based filters- chat-messages lists (`chosen_messages`, `rejected_messages`) → preserved for chat-aware ops + round-trip
This dual-shape design (verified in the test`test_dpo_pair_to_dj_record_shape`,`composer_replication/replaysim/tests/test_replaysim.py:44`) is whatunblocked the data-juicer integration in Wave 14.
### 4d. The 3-record fixture from spike 001
The fixture lives at`spikes/001-teacher-replay-cost/states.jsonl` (50 states) and`spikes/001-teacher-replay-cost/results.jsonl` (the teacher responses, allpriced and timed). The first 3 states are:
```jsonl{"id": "state-000", "task": "Fix the failing test in tests/test_auth.py::test_login_with_email", ...}{"id": "state-001", "task": "Add rate-limiting middleware to the Flask app", ...}{"id": "state-002", "task": "Refactor the parse_config function — it's 200 lines and has 3 responsibilities", ...}```
For each, all 3 teachers answered (claude-opus-4.7, gpt-5, deepseek-v4-pro);agreement on the `(c)` choice for state-000 and state-001 (read morefiles / check schema first) drives a clean DPO pair where the student'saction becomes the rejected. For state-002, all 3 agreed on `(c)` (writecharacterization tests first) → another clean pair. These three recordspass through the `DJNormalizer` default recipe unchanged (length, words,special-char ratios all in bounds; no duplicates).
The full 50-state trace cost **$0.98 mean** end-to-end across all threeteachers (spike 001 verdict). The framework's cost ceiling(`max_total_usd`) and VOI gating drop this to ~$0.30/trace projected.
### 4e. End-to-end one-liner
```pythonfrom composer_replication.replaysim import replay_and_normalize_trace
teacher_actions, normalized_pairs = await replay_and_normalize_trace( states=states, teachers=DEFAULT_TEACHERS, agreement_threshold=2, max_total_usd=10.0,)```
(`async def`; for sync callers use the sibling `replay_and_normalize_trace_sync`in `composer_replication.replaysim.normalize`.)
---
## 5. Switching DPO → SimPO: one kwarg
```pythoncomponents = compose_loss( model, batch, alpha_sdpo=0.1, beta_replay=0.05, dpo_variant="simpo", # ← the only line that changes simpo_beta=2.0, # paper default simpo_gamma=1.0, # paper default)```
The kwarg is verified in `compose_loss`'s signature(`composer_replication/loss.py:81`):
```pythondpo_variant: Literal["dpo", "simpo"] = "dpo",```
### What changes in the loss curve
- **Channel 3 input requirements drop.** `compose_loss` no longer reads `dpo_chosen_ref_logprobs` / `dpo_rejected_ref_logprobs`. Reference-model VRAM cost goes to zero. (Source: `composer_replication/loss.py:111–113` and `composer_replication/distillation/simpo.py:23–27`.)- **Loss scale shifts.** Standard DPO is `-logsigmoid(β·[(logπ(c) - logπ_ref(c)) - (logπ(r) - logπ_ref(r))])`. SimPO is `-logsigmoid(β·[avg_logπ(c) - avg_logπ(r)] - γ)` — average per-token log-prob (length-normalized) and a constant target margin γ.- **Loss is ≤ DPO loss when chosen/rejected separation is large.** The unit test `test_simpo_loss_lower_for_better_separation` (`composer_replication/distillation/tests/test_distillation_losses.py:35`) verifies that a wider chosen-vs-rejected gap drives lower SimPO loss — meaning, in practice, SimPO curves are *steeper* than DPO when the preference signal is strong, and *flatter* when it's weak.- **No KL-against-reference regularization.** This is both the upside (no ref-model serving) and the risk (more tendency to drift). Watch for reward-hacking-style degeneracies if your preference data has noise.
### When to use SimPO
- **GPU-poor.** You can't afford to keep a frozen reference policy resident alongside the trainer.- **Cold-start preference data.** Length-normalization (avg_logπ vs sum) helps when chosen/rejected lengths are wildly imbalanced — common in agentic traces where the student's failed attempt is short and the teacher's correction is long.- **You don't have ref logprobs precomputed.** SimPO needs nothing from the reference policy.
When to **stay on DPO**: when you need the explicit KL anchor againsta known-good reference (e.g., when training over a long horizon and youwant to bound the drift), or when your preference data is very noisy andthe reference acts as a regularizer.
---
## 6. Adding TAID / Entropy-Aware OPD wrappers
Channel 2 (SDPO/OPSD) can be wrapped by **TAID** (Sakana AI,arXiv:2501.16937) for capacity-gap distillation, or replaced by**Entropy-Aware OPD** (ICLR 2026 Spotlight) for per-token forward/reverse-KLgating. Both are verified in the public `compose_loss` kwargs:
```pythonsdpo_wrapper: Literal["none", "taid", "entropy_opd"] = "none",taid_schedule_step: int | None = None,taid_total_steps: int | None = None,taid_schedule: str = "linear", # "linear" | "cosine" | "exp"taid_alpha_min: float = 0.0,taid_alpha_max: float = 1.0,entropy_opd_h_max: float | None = None,```
(verified at `composer_replication/loss.py:82–93`.)
### TAID schedule kwargs explained
TAID interpolates between the **student's own distribution at step 0**(`P_student_init`) and the teacher distribution:
```P_target(t) = (1 - α(t)) · P_student_init + α(t) · P_teacher```
where `α(t)` is a schedule controlled by:- **`taid_schedule_step`** — the current global step. Required when `sdpo_wrapper="taid"`; `compose_loss` raises `ValueError` if you forget it.- **`taid_total_steps`** — total planned training steps. Same.- **`taid_schedule`** — `"linear"`, `"cosine"`, or `"exp"` (paper default exp uses `1 - exp(-5·progress)`).- **`taid_alpha_min`** / **`taid_alpha_max`** — schedule range. Default `[0, 1]`. Pin both to `1.0` to recover plain SDPO; pin both to `0.0` to pin the loss against `P_student_init` (a regularizer that ignores the teacher entirely — see proof below).
To use TAID, also provide the frozen-init logits via either:- `inputs["student_init_logits"]` (precomputed snapshot — preferred), OR- `inputs["student_init_input_ids"]` (frozen forward fallback; only valid early in training before the model has drifted).
If neither is provided, `_resolve_student_init_logits` raises`ValueError` with a clear message(`composer_replication/loss.py:351–392`).
### Entropy-Aware OPD
Drop-in for channel 2 — gates between forward KL (mode-covering) andreverse KL (mode-seeking) per token, weighted by the teacher's entropy:
```L = Σ_t w(t) · KL_fwd_t + (1 - w(t)) · KL_rev_tw(t) = clamp(H_teacher(t) / h_max, 0, 1)```
`entropy_opd_h_max=None` (the default) auto-sets `h_max = log(V)` (themaximum-entropy bound for a vocab-V softmax).
### Boundary-condition unit test (proof of correctness)
The test `test_taid_loss_alpha_zero_ignores_teacher`(`composer_replication/distillation/tests/test_distillation_losses.py:153`)pins the most important TAID invariant — at `α=0` the teacher is*completely* hidden from the gradient:
```pythondef test_taid_loss_alpha_zero_ignores_teacher(): """At alpha=0, teacher gradient should not flow through to student.""" B, T, V = 1, 2, 4 student_init = torch.randn(B, T, V) s1 = torch.randn(B, T, V, requires_grad=True) teacher_a = torch.zeros(B, T, V); teacher_a[..., 0] = 10.0 teacher_b = torch.zeros(B, T, V); teacher_b[..., 3] = 10.0 # alpha pinned to 0 → blended target = student_init regardless of teacher loss_a = taid_loss(s1, teacher_a, student_init, schedule_step=0, total_steps=100, alpha_min=0.0, alpha_max=0.0) loss_b = taid_loss(s1, teacher_b, student_init, schedule_step=0, total_steps=100, alpha_min=0.0, alpha_max=0.0) # Two completely different teachers must give the same loss. assert abs(float(loss_a) - float(loss_b)) < 1e-4```
This is the load-bearing test for TAID: if the schedule's α=0 endpointever leaks teacher signal into the gradient, this test fires and thecontract is broken. Companion tests(`test_taid_alpha_schedule_endpoints` line 86,`test_taid_blended_logits_endpoints` line 115) pin the schedule'sendpoints (α=0 → student_init, α=1 → teacher) and the half-way mixingbehavior.
For Entropy-OPD, the boundary test is`test_entropy_aware_opd_zero_when_distributions_match` (line 217): whenstudent logits ≡ teacher logits, both KLs are 0 and the loss must be 0to numerical precision.
---
## 7. Going multi-replica with serverless DiLoCo
DiLoCo is the outer-loop optimizer that lets you run N replicas inparallel, sync them every H inner steps, and tolerate slow links — see`docs/adrs/ADR-005-serverless-diloco.md` for the design. The frameworkgives you three increasingly-distant deployments:
### Step 1 — `LocalProcessExecutor` for development
```pythonfrom composer_replication.diloco.serverless import ( LocalProcessExecutor, ObjectStoreAllReduce,)import tempfile
with tempfile.TemporaryDirectory() as td: rendezvous = ObjectStoreAllReduce(td, rank=0, world_size=4) executor = LocalProcessExecutor() handles = executor.launch_replicas( n_replicas=4, entrypoint="composer_replication.diloco.serverless.replica_entrypoint", entrypoint_args={"rendezvous_uri": td, "rank_env": "REPLICA_RANK"}, ) results = executor.collect(handles, timeout=600)```
`LocalProcessExecutor` (`composer_replication/diloco/serverless/executor.py:160`)spawns N child processes via `multiprocessing.get_context("spawn")` andsets `REPLICA_RANK={0..N-1}` in each child's env. It satisfies the`ServerlessExecutor` Protocol (line 35) — the same Protocol the cloudadapters implement. So the dev-loop code is byte-identical to the clouddeploy: only the executor instance changes.
### Step 2 — `ObjectStoreAllReduce` as the rendezvous
```python# Local file:// for testsrendezvous = ObjectStoreAllReduce("/tmp/diloco-runs/run42/", rank=0, world_size=4)
# Real S3 (after `pip install -e .[serverless]`)rendezvous = ObjectStoreAllReduce( "s3://my-bucket/diloco-runs/run42/", rank=0, world_size=4, timeout_s=1800.0,)```
The communication pattern is `S3 PutObject + N GetObjects` once perinner H steps (matches DiLoCo's actual sync cadence,arXiv:2311.08105 §3.2). For 1B-param bf16, that's ~2 GB / 30 minutesper replica — well within S3 free-tier. On the inner side the frameworkexposes a `MockManager` that drops into the `torchft.Manager` slot, soyou can validate the rendezvous logic before plugging in real torchft(verified by `test_serverless_diloco_integration.py`).
### Step 3 — point at `ModalExecutor` / `HFJobsExecutor`
```python# Modal (skeleton at composer_replication/diloco/serverless/modal.py)from composer_replication.diloco.serverless.modal import ModalExecutorexecutor = ModalExecutor(image="modal:python3.11", gpu="A100")
# HuggingFace Jobs (skeleton at composer_replication/diloco/serverless/hf_jobs.py)from composer_replication.diloco.serverless.hf_jobs import HFJobsExecutorexecutor = HFJobsExecutor(hardware="a10g-large")
# Same Protocol — same launch_replicas / poll / collect calls as Localhandles = executor.launch_replicas(n_replicas=4, ...)```
Both adapters check their cloud SDK at `__init__` time (not at moduleimport) so they don't break the package if you don't have `modal` or`huggingface_hub` installed. Production maturity: dev-ready for cloudtrial; per ADR-005, full HA-cluster fan-out lives in v0.2+.
---
## 8. Picking an RL backend
Four canonical recipes, each tied to an upstream framework. Source:`docs/INTEGRATION_ARCHITECTURE.md` Recipes A–D plus`docs/adrs/ADR-006-rl-frameworks.md`.
### Recipe A — TRL `GRPOTrainer` subclass
`ComposerReplicationTrainer` is a `trl.GRPOTrainer` subclass thatoverrides `_compute_loss(model, inputs)` to compose the same 3 channelson top of TRL's real reward + advantage machinery. Install:`pip install -e ".[train]"`. Then:
```pythonfrom composer_replication import ComposerReplicationTrainertrainer = ComposerReplicationTrainer(model=..., reward_funcs=[...], ...)trainer.train()```
**When to use it:** This is the v0.0/v0.1 recommended path. You wantreal GRPO with rewards, you have HF model + dataset + (mostly) standardGRPO infrastructure, and you don't need >100B-param scale. TRL ismature, the trainer is a small subclass, and the same `compose_loss`math runs in both the verification harness and in production with nore-coding.
→ See `docs/INTEGRATION_ARCHITECTURE.md` § "Recipe A: TRL `GRPOTrainer`subclass" (line 205).
### Recipe B — VeRL custom `adv_estimator` + DataProto extension
VeRL replaces TRL's reward+advantage machinery with a Ray-driven actorgraph that's specifically optimized for distributed RL training oflarge LMs. Composition with the framework: extend `DataProto` with thehint-conditioned columns + DPO pair fields, register a custom`adv_estimator` that calls the same `compose_loss` body.
**When to use it:** You're past 7B-param, you have multi-host setup(Ray cluster), and TRL's single-process trainer is the bottleneck. VeRLis the recommended scale path for v0.2+. Trade-off: the extension surfaceis larger and the docs are sparser than TRL's.
→ See `docs/INTEGRATION_ARCHITECTURE.md` § "Recipe B: VeRL custom`adv_estimator`" (line 289).
### Recipe C — PRIME-RL with DPPO-clip details
`composer_replication/recipes/prime_rl/composer_loss.py` ships a`loss_fn(inputs, *, alpha_sdpo=0.0, beta_dpo=0.0, dppo_mask_high=0.2,dppo_mask_low=0.2, adv_tau=1.0, kl_tau=1e-3)` adapter that mapsPRIME-RL's `LossInputs` struct (1-D per-sample tensors:`trainer_logprobs`, `inference_logprobs`, `teacher_logprobs`,`advantages`, `loss_mask`) into our 3-channel composition.
The DPPO+KL bit is what makes PRIME-RL distinctive — and we mirrorPRIME-RL's upstream `default_loss_fn` exactly (verified against`prime_rl/trainer/rl/loss.py` lines 116-165):
```pythonlog_ir = trainer_logprobs - inference_logprobsir = exp(log_ir) # importance ratioprobs_diff = exp(trainer_logprobs) - exp(inference_logprobs)invalid_high = probs_diff > dppo_mask_high # for positive-advantage tokensinvalid_low = probs_diff < -dppo_mask_low # for negative-advantage tokensinvalid = where(advantages > 0, invalid_high, invalid_low)keep = loss_mask & ~invalidpg_loss = keep * (adv_tau * advantages) * irkl_loss = loss_mask * log_ir**2loss = (-pg_loss + kl_tau * kl_loss).sum()```
Three things to remember: (1) the mask gate is on **probability-space**`exp(trainer_lp) - exp(inference_lp)`, not on the log-ratio; (2) thepolicy-gradient term is multiplied by the importance ratio`exp(trainer_lp - inference_lp)`, not by `trainer_lp` directly (properIS-corrected gradient, not REINFORCE); (3) the mask is **conditioned onthe sign of the advantage** — positive-advantage tokens are dropped onthe upper bound, negative-advantage tokens on the lower. Defaults`dppo_mask_high=dppo_mask_low=0.2` and `adv_tau=1.0, kl_tau=1e-3`match PRIME-RL's `DefaultLossConfig` (all fields `Field(..., ge=0)`).SDPO (channel 2) is gated `NotImplementedError` in v0 because PRIME-RLexposes log-probs, not full vocab logits; channel 3 (trace-replay DPO)emits a warning if `beta_dpo != 0`.
**When to use it:** You're already in the PRIME-Intellect / decentralizedtraining universe, you want INTELLECT-style scaling on a long-horizontraining run, and DPPO masking is part of your existing reward+advantagerecipe. Install: `pip install -e ".[prime-rl]"`.
→ See `composer_replication/recipes/prime_rl/prime_rl_recipe.md` and`docs/INTEGRATION_ARCHITECTURE.md` § "Recipe C: TorchForge + Monarch"(line 356).
### Recipe C+D — Monarch as actor mesh
Monarch (the actor framework underpinning TorchForge) hosts thetrainer/generator/manager actors in a topology-aware mesh. The frameworkships *skeleton* actor definitions at`composer_replication/recipes/monarch/actors.py` (TrainerActor,GeneratorActor) and a layout doc at `monarch_actor_layout.md`. v0intentionally *fails fast* if you try to instantiate them(`raise NotImplementedError("v0 skeleton; deferred to v0.2 per ADR-006")`)because the upstream Monarch API is still moving.
**When to use it:** Reference-pattern reading only in v0. Decision pointis v0.2 once the upstream actor API stabilizes. Treat the skeleton asshape-of-the-final-answer documentation, not as a production target.Install: `pip install -e ".[prime-rl,monarch]"` for the full surface.
→ See `composer_replication/recipes/monarch/monarch_actor_layout.md`and `docs/adrs/ADR-006-rl-frameworks.md`.
---
## Common pitfalls + what tests catch them
The framework's 124-test suite is structured so each pitfall has aspecific test-file home. If you hit one of these in production, thecorresponding test is your fastest reproducer.
| Pitfall | Symptom | Test file (catches it) ||-----------------------------------------------------------------------------------------------|------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|| Forgetting `taid_schedule_step` when `sdpo_wrapper="taid"` | `ValueError` at first step | `composer_replication/tests/test_compose_loss_integration.py` (kwarg validation) || TAID α=0 endpoint leaks teacher signal | Teacher swap changes the loss when α should be 0 | `test_taid_loss_alpha_zero_ignores_teacher` in `composer_replication/distillation/tests/test_distillation_losses.py:153` || TAID α=1 endpoint differs from plain SDPO | Bit-difference vs reference SDPO at the schedule end | `test_taid_blended_logits_endpoints` in `composer_replication/distillation/tests/test_distillation_losses.py:115` || SimPO loss not differentiable through the loss-of-sigmoid path | `chosen.grad is None` after backward | `test_simpo_loss_differentiable` in `composer_replication/distillation/tests/test_distillation_losses.py:50` || SimPO shape-mismatch slips through silently | Broadcasting bug, NaN downstream | `test_simpo_loss_shape_mismatch_raises` in `composer_replication/distillation/tests/test_distillation_losses.py:61` || Entropy-OPD failing to zero out when distributions match | Loss > 0 when student≡teacher | `test_entropy_aware_opd_zero_when_distributions_match` in `composer_replication/distillation/tests/test_distillation_losses.py:217` || Entropy of one-hot ≠ 0 / entropy of uniform ≠ log(V) | Wrong gating weights w(t) | `test_teacher_entropy_one_hot_is_zero` and `test_teacher_entropy_uniform_is_log_v` in `composer_replication/distillation/tests/test_distillation_losses.py:175,183` || `DJNormalizer` records missing the chat-messages shape | Filters silently no-op or crash | `test_dpo_pair_to_dj_record_shape` in `composer_replication/replaysim/tests/test_replaysim.py:44` || `DJNormalizer` round-trip drops `state_messages` / metadata | Lost provenance | `test_dj_record_to_normalized_roundtrip` and `test_dj_record_to_normalized_preserves_state_messages` in `composer_replication/replaysim/tests/test_replaysim.py` || `ObjectStoreAllReduce` accepts an out-of-bounds rank | Silent corruption of the all-reduce average | `test_object_store_allreduce_init_validates_rank` in `composer_replication/diloco/serverless/tests/test_serverless_local.py:31` || `ObjectStoreAllReduce(world_size=1)` doesn't passthrough cleanly | False all-reduce on single replica | `test_object_store_allreduce_world_size_1_passthrough` in `composer_replication/diloco/serverless/tests/test_serverless_local.py:46` || `LocalProcessExecutor` doesn't propagate child failures to `collect()` | Silent test pass when a replica crashed | `test_serverless_diloco_integration.py` in `composer_replication/diloco/serverless/tests/` || PRIME-RL adapter accidentally uses `(B, T)` shape instead of per-sample `(seq,)` | Shape mismatch / wrong reduction | `composer_replication/recipes/prime_rl/tests/test_composer_loss.py` (10 tests covering shape and DPPO mask edges) || Channel 2/3 fails to auto-disable when its inputs are absent | Crash on missing key, not graceful skip | `composer_replication/tests/test_compose_loss_integration.py` (`(a) defaults reproduce existing compose_loss output bit-exact`) |
Run the full suite with `pytest` from the repo root.
---
**File path:** `/mnt/e/CS/HF/composer-replication-framework/docs/USER_GUIDE.md`100 unmodified lines101102103104105106107108109110111112113114115116117118119120121122123124125126127128100 unmodified linesdifferentiable, returns scalar, matches paper formulas at boundaryconditions)### Wave 14+ work — `compose_loss` integration is NOT in this waveAn earlier draft of this ADR claimed `composer_replication.compose_loss`would receive new kwargs (`dpo_variant`, `sdpo_wrapper`, `taid_schedule_step`,`taid_total_steps`). **The Wave 13 cross-model review(docs/research/WAVE_13_FINAL_REVIEW.md Finding 2) flagged that thosekwargs were never actually added to `compose_loss`** — the standalonelosses landed but the integration into the framework's loss compositionis not done. To stay honest:- **What works in Wave 13**: `from composer_replication.distillationimport simpo_loss, taid_loss, entropy_aware_opd_loss` — all three areimportable, type-checked, unit-tested, and ready to be called directly.- **What does NOT work in Wave 13**: passing`compose_loss(model, batch, dpo_variant="simpo", sdpo_wrapper="taid", ...)`.That call signature does not exist; it would raise `TypeError`.- **Wave 14 plan**: add the four kwargs to `compose_loss` with a smallintegration test exercising at least one combination (SDPO+TAID + plainDPO would suffice). Estimated ~30 LOC + 2-3 tests.Users wanting the new losses *now* should use them as standalonefunctions in their own loss-composition code:```pythonfrom composer_replication.distillation import simpo_loss, taid_loss100 unmodified lines101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152100 unmodified linesdifferentiable, returns scalar, matches paper formulas at boundaryconditions)### Closed in Wave 14 — `compose_loss` integration landed**Status (Wave 14, T1):** the four kwargs (`dpo_variant`,`sdpo_wrapper`, `taid_schedule_step`, `taid_total_steps`) have beenadded to `composer_replication.compose_loss` and are exercised by`composer_replication/tests/test_compose_loss_integration.py`. Thegap flagged by the Wave 13 cross-model review(`docs/research/WAVE_13_FINAL_REVIEW.md` Finding 2) is closed:- `compose_loss(model, batch, dpo_variant="simpo", sdpo_wrapper="taid",taid_schedule_step=step, taid_total_steps=max_steps, ...)` is now avalid call signature.- All three standalone losses (`simpo_loss`, `taid_loss`,`entropy_aware_opd_loss`) remain importable and unit-tested asbefore — the Wave 14 work was purely the kwarg surface + compositionglue, not a loss-formula change.The historical sections below are preserved verbatim for context but**describe the pre-Wave-14 state** and are superseded by the closedstatus above.---#### Superseded — pre-Wave-14 wording (kept for history)> An earlier draft of this ADR claimed `composer_replication.compose_loss`> would receive new kwargs (`dpo_variant`, `sdpo_wrapper`, `taid_schedule_step`,> `taid_total_steps`). **The Wave 13 cross-model review> (docs/research/WAVE_13_FINAL_REVIEW.md Finding 2) flagged that those> kwargs were never actually added to `compose_loss`** — the standalone> losses landed but the integration into the framework's loss composition> is not done. To stay honest:>> - **What works in Wave 13**: `from composer_replication.distillation> import simpo_loss, taid_loss, entropy_aware_opd_loss` — all three are> importable, type-checked, unit-tested, and ready to be called directly.> - **What does NOT work in Wave 13**: passing> `compose_loss(model, batch, dpo_variant="simpo", sdpo_wrapper="taid", ...)`.> That call signature does not exist; it would raise `TypeError`.> - **Wave 14 plan**: add the four kwargs to `compose_loss` with a small> integration test exercising at least one combination (SDPO+TAID + plain> DPO would suffice). Estimated ~30 LOC + 2-3 tests.Users wanting the new losses as standalone callables can still use themdirectly in their own loss-composition code (this path is unchanged bythe Wave 14 integration):```pythonfrom composer_replication.distillation import simpo_loss, taid_loss
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264# Wave 14 Adversarial Cross-Model Review
**Reviewer:** Claude Opus 4.7 (sub-agent via delegate_task)**Date:** 2026-05-26**Method:** Read every Wave 13 finding, every Wave 14 closure, all 4 doc files, **cloned PRIME-RL upstream to verify T4 claims**, ran 61 wave-relevant tests.
## Top-line verdict
**CONDITIONAL PASS with 1 BLOCKER + 4 SUGGESTIONs + 2 NITs.** Wave 14closes Wave 13 BLOCKER 2 (T1 — compose_loss kwargs) and Suggestion 3(T2 — replaysim) cleanly. T3 (MockManager surface audit) is solid butonly tests `world_size=1`. **T4 (PRIME-RL "real GRPO + DPPO") does notmatch PRIME-RL's actual `default_loss_fn`** despite claiming to mirrorit; that error has been pasted into USER_GUIDE.md, INTEGRATION_RECIPES.md,and API_REFERENCE.md.
Same signal-to-noise as Wave 11 + Wave 13 reviewers: 1 genuine BLOCKER.
---
## Finding 1 — BLOCKER: T4 PRIME-RL "DPPO importance-sampling-ratio clip" is neither importance sampling nor matches PRIME-RL.
**Severity:** BLOCKER**Evidence:** `composer_replication/recipes/prime_rl/composer_loss.py:118-131`.The implementation computes```pythongrpo_loss = -(advantages * trainer_lp * keep_mask).sum() / keep_mask.sum()```That's **pure REINFORCE-with-advantage** — the masking gate is the onlynod toward DPPO; there is no importance-sampling ratio multiplicationanywhere.
**Real PRIME-RL** (`/tmp/prime-rl-clone/src/prime_rl/trainer/rl/loss.py:128-153`,the `default_loss_fn` on `main` as of 2026-05-26):```pythonlog_importance_ratio = trainer_logprobs - inference_logprobsimportance_ratio = torch.exp(log_importance_ratio)probs_diff = torch.exp(trainer_logprobs) - torch.exp(inference_logprobs)positive_advantages = advantages > 0dppo_invalid_mask_high = probs_diff > loss_config.dppo_mask_highdppo_invalid_mask_low = probs_diff < -loss_config.dppo_mask_lowdppo_invalid_mask = torch.where(positive_advantages, dppo_invalid_mask_high, dppo_invalid_mask_low)keep_mask = loss_mask & ~dppo_invalid_maskpg_loss = -(keep_mask * advantages * importance_ratio).sum() # NO divisionkl_loss = adv_tau * (log_importance_ratio**2 * keep_mask).sum() # KL term```
**Three concrete divergences from Wave 14's implementation:**
1. **Mask gate is on `probs_diff`** (a probability-space quantity), NOT `log_ratio` (a log-space quantity). These have different magnitudes: `probs_diff=0.2` corresponds to `log_ratio≈log(1.2)≈0.18` for a trainer prob of 1.0 vs inference prob of 0.8. With our `log_ratio>4.0` gate, the mask never fires for normal training distributions; PRIME-RL's `probs_diff>0.2` gate fires routinely.
2. **PRIME-RL multiplies by `importance_ratio = exp(log_ratio)`**; Wave 14 multiplies by `trainer_lp` directly. This is the difference between actual policy-gradient correction (PRIME-RL) and naive REINFORCE.
3. **PRIME-RL's mask is sign-conditioned on advantage** (positive advantages clipped against `dppo_mask_high`, negative against `-dppo_mask_low`); Wave 14 ORs them together unconditionally.
**Plus:** the KL term is missing entirely.
**Plus:** the defaults claimed as "PRIME-RL's defaults" — `dppo_mask_high=4.0,dppo_mask_low=-4.0` — are wrong. PRIME-RL's `DefaultLossConfig`(`configs/trainer.py:412-424`) sets `dppo_mask_high=0.2, dppo_mask_low=0.2`with `Field(..., ge=0)` validation that would *reject* a negative value.PRIME-RL's code negates at use site: `probs_diff < -loss_config.dppo_mask_low`.
**Plus:** the docstring (`composer_loss.py:32-49`), USER_GUIDE.md:599-608,INTEGRATION_RECIPES.md:426-429 + 482-487, and API_REFERENCE.md:1364 allrepeat the wrong formula and the wrong "matches PRIME-RL" claim.
**Fix direction:** Either (a) actually mirror `default_loss_fn` (mask on`probs_diff`, multiply by `importance_ratio`, add KL term, advantage-conditioned mask, `.sum()` reduction with token-count returned forcaller-side scaling), or (b) drop the "matches PRIME-RL" framing andrename to "REINFORCE-with-advantage stub + log-ratio mask" everywhere.
Wave 13 Finding 6 is **not actually closed** by Wave 14.
---
## Finding 2 — SUGGESTION: ADR-007 still says Wave 14 hasn't done the integration.
**Severity:** SUGGESTION**Evidence:** `docs/adrs/ADR-007-self-distillation-losses.md:104-122` reads:> **Wave 14+ work — `compose_loss` integration is NOT in this wave**> ... Wave 14 plan: add the four kwargs ...
But Wave 14 *did* add them (verified — `loss.py:80-93`). The ADR waswritten defensively after Wave 13 review and never updated when T1 landed.
**Net effect:** a user reading ADR-007 is told the SimPO/TAID kwargsdon't work; a user reading USER_GUIDE/API_REFERENCE is told they do.
**Fix direction:** flip ADR-007 status section to "Closed in Wave 14 —see test_compose_loss_integration.py".
---
## Finding 3 — SUGGESTION: ModalExecutor instantiation example in INTEGRATION_RECIPES is dead code.
**Severity:** SUGGESTION**Evidence:** `docs/INTEGRATION_RECIPES.md:519-533` shows```pythonexecutor = ModalExecutor(app="composer-prime-rl")executor.launch_replicas(...)```But `composer_replication/diloco/serverless/modal.py:64-66` raises`NotImplementedError` from `__init__`. Same pattern in `HFJobsExecutor`.The recipe doc warns about skeleton-status much further down (line 731),but the inline code example at line 519 will break the moment a readercopy-pastes it.
Wave 13 Finding 7 noted this softness; Wave 14 made it worse by writingexample code that calls a constructor that always raises.
**Fix direction:** in every code block that calls `ModalExecutor(...)`,prepend a comment `# Wave 14: skeleton — raises NotImplementedError`or flip examples to `LocalProcessExecutor`.
---
## Finding 4 — SUGGESTION: MockManager + DiLoCo integration test only exercises `world_size=1`.
**Severity:** SUGGESTION**Evidence:** `composer_replication/diloco/serverless/tests/test_serverless_diloco_integration.py:44-51`,`:108-109`, `:161`. Both `test_mockmanager_diloco_outer_round_completes`and `test_mockmanager_diloco_two_outer_rounds_step_counter` use`world_size=1`.
With one replica, `ObjectStoreAllReduce.allreduce` returns the tensorunchanged (its own mean), so an averaging bug in the multi-replica pathcould not be caught by this test. The pseudo-gradient sign conventionis pinned by the unrelated spike-008 test, but **no test combinesMockManager + DiLoCo + multi-process** — i.e. the actual deploymentscenario is unverified end-to-end.
Wave 13 Finding 4 is closed in spirit (call surface is now exhaustive)but not in the deepest sense.
**Fix direction:** add one multi-process test that spawns `n_replicas`subprocesses, each constructing `MockManager(store) → make_diloco_outer_loop`,and asserts that after one outer round all replicas converge to the sameparameter values (i.e. averaging actually happened).
---
## Finding 5 — SUGGESTION: T4 unit tests pin the wrong implementation as ground truth.
**Severity:** SUGGESTION**Evidence:** `composer_replication/recipes/prime_rl/tests/test_composer_loss.py:90-128`(`test_dppo_mask_clips_extreme_ratios`). The expected value `1.5/3` iscomputed against the buggy formula (Finding 1).
The 10 PRIME-RL tests all pass — but they're testing self-consistency,not parity with PRIME-RL. A reader looking at "10 unit tests, all green"infers correctness; correctness is not what they verify. This is thekind of test honesty failure that Wave 11 + Wave 13 reviewers found indifferent forms.
**Fix direction:** add at least one test whose expected value ishand-computed from `default_loss_fn` in PRIME-RL (or import + invoke`default_loss_fn` if the dependency is available, mark the test`@pytest.mark.skipif(not _HAS_PRIME_RL)`).
---
## Finding 6 — NIT: README/test-count drift.
Wave 14 task description claims "124 tests passing as of Wave 14"; actual`pytest --collect-only` reports **134 collected**. Of those, the 61-testwave-relevant subset all pass. Not a defect, but the headline number isnow off in the same way Wave 13's "9 multi-process tests" was off.
---
## Finding 7 — NIT: `loss_fn` docstring claims "DPPO importance-sampling-ratio clipping — implemented" (`composer_loss.py:9`).
Implementation contains no importance-ratio multiplication anywhere.Even if Finding 1 is rejected and the team decides "PRIME-RL match isn'ta goal", the docstring is internally false: it announces ISR clippingin a function that does not multiply by `exp(log_ratio)`.
---
## Cross-cutting
The four doc subagents wrote internally consistent text but inheritedT4's mathematical error. **Three of the four doc files repeat the samewrong formula verbatim.** This is exactly the failure mode Wave 11/13reviewers flagged: parallel subagents cross-citing each other ratherthan the upstream source of truth.
The 61 tests in the Wave-14-touched dirs pass cleanly. T1, T2, and T3are real closures with real coverage. The framework is in a **better**state than end-of-Wave-13 — but it has not actually closed Wave 13Finding 6, and it has propagated a subtler version of the samemathematical-mismatch bug into the user-facing documentation.
---
## Summary scorecard
| Wave 13 Finding | Wave 14 status | Verdict ||---|---|---|| BLOCKER 1 (PRIME-RL SDPO degenerate) | Fixed parent-side; channel 2 raises NotImplementedError | ✅ closed || BLOCKER 2 (compose_loss kwargs not added) | T1 added them + 11 integration tests | ✅ closed || Suggestion 3 (replaysim YAML field types) | T2 dual-shape reshape + real DJ e2e + caught related bug | ✅ closed || Suggestion 4 (MockManager → DiLoCo gap) | T3 surface audit + integration test | 🟡 closed for `world_size=1`; multi-process unverified || Suggestion 5 ("9 multi-process tests" inflated count) | Not addressed | 🟡 carried over || Suggestion 6 (PRIME-RL channel 1 REINFORCE not GRPO) | T4 thought it closed this | ❌ **NOT closed — mathematically wrong** || Suggestion 7 (Modal/HFJobs skeleton clarity) | Made worse by INTEGRATION_RECIPES dead code | 🟡 regression || NIT 8 (SimPO test positive log-probs) | Not addressed | 🟡 carried over |
## Wave 14b follow-up (2026-05-26)
After Wave 14b closed Finding 1 by re-reading PRIME-RL upstream andmatching `default_loss_fn` byte-for-byte, the Wave 14b subagent flaggeda **new** structural issue not in the Wave 14 review:
**PRIME-RL's `setup_loss_fns` (upstream `loss.py:320-327`) expects thecustom loss function to return `LossOutputs(loss, metrics={...})`, nota bare scalar tensor.** Our recipe still returns a bare scalar. Thispredates Wave 14 (it's been wrong since the recipe was first written inWave 13) but was never caught because no test runs against actualPRIME-RL.
**Status:** documented; deferred to Wave 15. Not blocking for Wave 14b'sclosure of Finding 1, because the formula now matches upstream — thereturn-shape mismatch is a separate adapter-level issue. Tests stillpass because they invoke our `loss_fn` directly without going throughPRIME-RL's `compute_loss` pipeline.
**Fix direction (Wave 15):** wrap the return value in a duck-typed`LossOutputs` (provided by PRIME-RL when installed; substituted with aNamedTuple shim when not). Add an integration smoke test against PRIME-RLto catch this and similar adapter-shape regressions.
## Final Wave 14 + 14b status
| Wave 13 / 14 finding | Wave 14b status ||---|---|| W13 BLOCKER 1: PRIME-RL SDPO degenerate | ✅ closed (parent, channel 2 deferred) || W13 BLOCKER 2: compose_loss kwargs not added | ✅ closed (Wave 14 T1) || W13 Suggestion 3: replaysim YAML field types | ✅ closed (Wave 14 T2) || W13 Suggestion 4: MockManager → DiLoCo gap | ✅ closed (Wave 14 T3 + Wave 14b multi-process test) || W13 Suggestion 6: PRIME-RL channel 1 REINFORCE not GRPO | ✅ **closed in Wave 14b** (matches upstream `default_loss_fn`) || W14 Finding 1: PRIME-RL impl wrong | ✅ closed in Wave 14b || W14 Finding 2: ADR-007 stale | ✅ closed in Wave 14b || W14 Finding 3: ModalExecutor dead code | ✅ closed in Wave 14b || W14 Finding 4: world_size=1 only | ✅ closed in Wave 14b (multi-process convergence test) || W14 Finding 5: tests pin wrong impl as ground truth | ✅ closed in Wave 14b (parity test added) || W14 NIT 6: test count drift | 🟡 carried || W14 NIT 7: docstring claims ISR clipping | ✅ closed in Wave 14b (real ISR now implemented) || **NEW (Wave 14b)**: PRIME-RL `LossOutputs` return shape | 🟡 deferred to Wave 15 |
**Test count post-Wave-14b: 130 passing + 1 skip-marked (PRIME-RLparity test, runs when prime-rl is installed).**