# RL plan (phase D): GRPO for the memory model ## Why RL comes after supervised training Supervised losses (answer CE, read loss, recall loss, full-answer KL to the original Qwen) teach the memory *mechanism*. RL is for *behaviour* that supervised data cannot pin down well: | problem (measured on v4) | supervised can't fix because | RL reward | |---|---|---| | guesses instead of "never told" (absent 30-67%) | the model never sees its own wrong guesses | +1 absent phrase on never-told, −1 for a guess | | says "never told" for facts that WERE told | same | −0.7 | | long values loop (`VAxCQgWkqgWkq…`) | teacher forcing never feeds back its own errors | repetition −0.5 | | remembered value leaks into unrelated answers (17%) | only the first token was penalised | −1 if any told value appears | | answers small talk with the memory template | — | −1 for absent phrase / template on chit-chat | Start RL only when phase B/C shows normal chat preserved (general accuracy ≥ ~88% of the original Qwen, leak ≤ 5%). Otherwise RL would optimise on a broken base. ## Algorithm: GRPO (group-relative, no value network) For each step: 1. **Scenario** (environment, programmatic): reset memory → tell 1-4 facts (OOD-style varied templates, random values, real names, numbers) over 1-4 sessions with interference (MSC / iapp / fillers), consolidation and day gaps → **one prompt** of a random kind: - 40% memory question about a told fact - 15% never-told question (same style, fact not told) - 25% general-knowledge question with known answer keys - 20% small talk 2. Memory state after the tell phase is kept **with autograd** (so RL also trains the write path). 3. **Sample G = 8 answers** from that state (temperature 0.8, top-p 0.95, ≤ 48 tokens; batched, memory read-only). 4. **Reward** (code only, no reward model): - memory Q: +1 value contained exactly; +0.3 if ≥ half of the value's prefix; −0.7 "never told"; 0 otherwise - never-told Q: +1 absent phrase; −1 if any told value or a confident made-up value - general Q: +1 correct key; −1 if a told value appears - small talk: 0 baseline; −1 absent phrase / memory template; −1 told value appears - all: −0.5 repetition (any 3-gram ×3 or char-run ≥ 6); −0.2 if a memory answer > 40 tokens 5. **Advantage** A_i = (r_i − mean_G) / (std_G + 1e-4); skip groups with zero variance. 6. **Loss** (on-policy, one update per batch): −mean_i A_i · mean_t log π(a_t) + β · KL(π ‖ π_ref) (k3 estimator per token; π_ref = the starting checkpoint, frozen; for general/small-talk prompts the reference is the ORIGINAL Qwen so chat quality is anchored). β = 0.05. 7. Log-probs are recomputed with grad by teacher-forcing the 8 sampled answers in one batch from the saved grad state (M/S expanded to batch 8). ## What is trained - memory layers (all depths) lr 5e-5 - LoRA r8 on layers after the memory (only if the phase-B winner used LoRA) lr 1e-5 - everything merged into ONE model file at the end (same as v4.1 LoRA run) ## Budget / schedule - ~1500 steps, eval every 250 steps (held-out checks + chat-return n=12 + absent accuracy) - 2 parallel runs on Modal: (a) β = 0.05, (b) β = 0.02 + stronger repetition penalty - stop early if general-knowledge accuracy drops > 3 points or leak rises ## Success criteria (vs the phase-C winner) - never-told accuracy ↑ (target ≥ 85%) without lowering told-fact recall - repetition loops ↓ (target < 3% of answers) - leak ≤ 3%, general accuracy within 3 points of the original Qwen - OOD / short / life numbers not worse Code: `train_rl.py` (`grpo_train`), Modal: `run_rl_gpu` in `modal_train.py`.