feat: Phase 46 β RLAIF (Reinforcement Learning from AI Feedback) - #53
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements Phase 46 β RLAIF (Reinforcement Learning from AI Feedback) β a third alignment signal, alongside GRPO (v1 Β§11, verifier-based) and DPO (v2 Β§28, human-preference-based). A frozen judge model scores pairs of self-sampled completions, automatically generating preference data that feeds the existing DPO training pipeline unchanged β useful for open-ended quality dimensions where neither a hard verifier nor a static human preference dataset is available.
RLAIF is deliberately architected as a data-generation front end, not a new training objective:
dpo_loss(v2 Β§28) is byte-for-byte unchanged. The output is(chosen, rejected)pairs in the exact{prompt, chosen, rejected}JSONL schemaDpoDataset::from_jsonlalready consumes.This faithfully implements the Phase 46 spec already authored in
ROADMAP_V4.md(lines 659β719),ARCHITECTURE_V4.mdΒ§60, andSELF_LEARNING_V4.mdΒ§46.What's new
crates/aarambh-studio-finetune/src/rlaif.rs(new module)RlaifConfig(serde,Default,validate):n_candidates(4), candidate sampling temperature/top-k/top-p/max-tokens, judge max-tokens,bias_discard(false),agreement_margin(0.1),max_pairs_per_prompt, baseseed, judge prompt template.JudgeGeneratortrait β deliberately free ofaarambh-studio-inferencetypes so the finetune crate (Layer 4) does not depend on the inference crate (Layer 5), mirroring Phase 45'sCompletionVerifierlayering.generate_verdict(judge_prompt, max_tokens)takes an already-built judge prompt so the finetune crate owns the template logic.CandidateSamplertrait β abstracts v1 Β§12's N-completion sampling pattern (sample N candidates with seedsbase + i).JudgeVerdict/JudgeChoice(A/B/Tie) /parse_judge_verdictβ robust JSON parser; malformed JSON, unknownpreferredvalues, or non-finite margins all fall back to a neutralTiewith margin0.0, discarded downstream rather than trusted at face value.BiasCorrectedPair/AgreementLevel/judge_pair_both_orderings/resolve_preferenceβ position-swap bias correction: every pair is judged twice, in both A/B and B/A orderings. Judges have a documented first-position bias; when the two orderings agree, the pair is emitted at weight1.0(or down-weighted by margin when belowagreement_margin); when they disagree, the pair is down-weighted toDISAGREEMENT_WEIGHT(0.25) using the more-confident ordering's verdict, or discarded entirely (--discard-disagreements) or when the disagreement is ambiguous (equal margins). Ties are discarded.generate_rlaif_datasetβ the main entrypoint: sample N candidates per prompt, form allC(N, 2)pairs, judge both orderings, resolve preferences, returnVec<DpoExample>+RlaifSummary.write_preference_jsonlβ writes the exact{prompt, chosen, rejected}schemaDpoDataset::from_jsonlconsumes.RlaifPaircarries aprovenance: "rlaif_judge"marker (Β§46's vocabulary) for downstream replay analysis.CLI:
finetune rlaifsubcommand (new)The
InferenceEngineimplementations ofJudgeGenerator/CandidateSamplerlive in the CLI binary (aarambh-studio/src/cmd/finetune.rs:InferenceJudge,InferenceSampler), alongside Phase 45'sMathVerifierAdapterβ preserving the Layer 4/5 architectural boundary. The subcommand wires policy + judge engines, supports self-judging (--judgedefaults to--base), and feeds the generated JSONL into the unmodifiedfinetune dpopipeline.Supporting files (new)
configs/rlaif_smoke.tomlβ CPU smoke training config (tiny Shakespeare, 8 steps) that produces a checkpoint the smoke script runs RLAIF against (policy == judge, self-judging).scripts/phase46_smoke.shβ runs the 16rlaifunit tests, trains a tiny checkpoint, generates a preference-pair JSONL viafinetune rlaif --n-candidates 2, verifies the JSONL is valid DPO schema, feeds it into the unmodifiedfinetune dpopipeline (1 step), verifies the new flags appear infinetune rlaif --help, and writes a scorecard toartifacts/phase46_rlaif_smoke.json.docs/phase46_rlaif.mdβ dedicated Phase 46 runbook (mirrorsdocs/phase45_test_time.mdstructure).Doc updates
ROADMAP_V4.mdβ Phase 46 checkboxes flipped[ ]β[x]+ status blockquote.ARCHITECTURE_V4.mdΒ§60 β added an "Implementation (Phase 46, v4.0.0-alpha.6)" subsection.SELF_LEARNING_V4.mdΒ§46 β added a "Status: Verified for v4.0.0-alpha.6 (Phase 46)" blockquote.CHANGELOG.mdβ added[4.0.0-alpha.6]section.README.mdβ bumped version reference to4.0.0-alpha.6, added Phase 46 / RLAIF to the v4 arc description and the Fine-tuning capabilities row.Cargo.tomlβ workspace version bumped to4.0.0-alpha.6..github/workflows/ci.ymlβ CLI smoke step now exercisesfinetune rlaif --help.Minimal change to existing code
DpoTrainer.train_loaderfield widened from private topub(crate)(aarambh-studio-finetune:dpo.rs) so the RLAIF integration test inrlaif.rscan pull one batch and prove the pairs feed through the unmodifiedtrain_step. Not part of the public API;dpo_loss,DpoDataset,DpoTrainer::new, andrun_dpo_from_configare byte-for-byte unchanged.Key design invariants (per the roadmap)
dpo_loss(v2 Β§28) is byte-for-byte unchanged.{prompt, chosen, rejected}JSONL, consumed by the unmodifiedDpoDataset::from_jsonl/run_dpo_from_config.InferenceEngineimpls live in the CLI binary (mirrors Phase 45'sCompletionVerifier/MathVerifierAdapter).unsafe,#![deny(missing_docs)]on the finetune crate.aarambh-studio-finetunecrate; release audit stays at 20 packages.Acceptance tests (4 roadmap-named, all pass)
position_swap_disagreement_is_downweighted_not_silently_trustedbias_discarddiscardsrlaif_generated_pairs_match_existing_dpo_pair_schema_exactly{prompt, chosen, rejected}and round-trips throughDpoExample+ JSONLrlaif_preference_pairs_fed_into_unmodified_dpo_pipeline_train_successfullyDpoDataset::from_examplesβ realDpoTrainer::train_step(finite loss)rlaif_dpo_run_reports_non_negative_win_rate_delta_on_preference_eval_taskCI gates (all green)
cargo fmt --all --checkcargo check --workspace --all-targets --locked(all 20 crates at4.0.0-alpha.6)cargo clippy --workspace --all-targets --locked -- -D warnings -D clippy::undocumented_unsafe_blockscargo test -p aarambh-studio-finetune --lib(68 tests: 16 RLAIF + 52 existing)scripts/phase28_release_audit.sh(20 packages, version4.0.0-alpha.6)bash -non allscripts/*.sh)aarambh-studio finetune rlaif --helpsurfaces all flags)scripts/phase46_smoke.shend-to-end (16 tests + tiny train + RLAIF generate + DPO pipeline + CLI help + scorecard)Milestone