# Adaptive Operator v4.1 — Professional Post-Training Pipeline > **Reference architecture** for distilling a Qwen3.5-9B adaptive operator model > with custom control tokens (`[FAST]`, `[THINK]`, `[VERIFY]`, `[RECOVER]`, > `[ESCALATE]`) and structured tool-calling. Aligns with production post-training > pipelines from Qwen, Llama 3, and Mistral teams. ## Table of Contents 1. [Pipeline Overview](#1-pipeline-overview) 2. [Stage 1: SFT (Supervised Fine-Tuning)](#2-stage-1-sft-supervised-fine-tuning) 3. [Stage 2: Preference Optimization (DPO)](#3-stage-2-preference-optimization-dpo) 4. [Stage 3: Evaluation and Testing](#4-stage-3-evaluation-and-testing) 5. [Stage 4: Domain Adaptation](#5-stage-4-domain-adaptation) 6. [Stage 5: Deployment](#6-stage-5-deployment) 7. [File Inventory](#7-file-inventory) 8. [Professional Standards Compliance Matrix](#8-professional-standards-compliance-matrix) --- ## 1. Pipeline Overview ``` ┌─────────────────────────────────────────────────────────────────┐ │ DATA GENERATION │ │ │ │ prompt_generator.py ──► 10K diverse task prompts │ │ │ │ │ │ ▼ ▼ │ │ qwen_inference.py 32 parallel workers │ │ (Qwen v3.1 teacher (H100 endpoint, │ │ on Together AI) ~9.4 prompts/sec) │ │ │ │ │ ▼ │ │ qwen_responses.jsonl ──► raw teacher responses │ │ │ (narrates tools, wrong format) │ │ ▼ │ │ review_orchestrator.py ──► 5-reviewer improvement │ │ ├── Code Quality & Correctness │ │ ├── Tool Selection & Usage │ │ ├── Control Token Routing │ │ ├── Error Handling & Edge Cases │ │ └── Response Format & Clarity │ │ │ │ │ ▼ │ │ improved_responses.jsonl ──► fixed, structured responses │ │ │ (proper [TOKEN] + XML tool calls) │ │ │ │ │ ├──► sft_export.py ──► sft_train.jsonl (SFT dataset) │ │ │ │ │ └──► dpo_generator.py ──► dpo_export.py │ │ ──► dpo_train.jsonl (DPO preference pairs) │ │ │ └─────────────────────────────────────────────────────────────────┘ │ │ ▼ ▼ ┌─────────────────┐ ┌─────────────────────┐ │ STAGE 1: SFT │ │ STAGE 2: DPO │ │ │ │ │ │ train_sft.py │─────────►│ train_dpo.py │ │ Qwen3.5-9B │ │ β=0.1, 1 epoch │ │ LoRA r=8 │ │ LoRA r=8 │ │ 3 epochs │ │ from SFT checkpoint│ │ LR=1e-5 │ │ LR=5e-6 │ │ │ │ │ │ Together AI │ │ Together AI │ └─────────────────┘ └─────────────────────┘ │ │ └──────────┬───────────────────┘ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ STAGE 3: EVALUATION │ │ │ │ eval_suite.py │ │ ├── Control token accuracy (right token per task type) │ │ ├── Tool selection accuracy (right tool for the job) │ │ ├── Tool call format validity (valid JSON, correct params) │ │ ├── Task completion (end-to-end success) │ │ └── Regression test (v4.1 vs v3.1 comparison) │ │ │ │ test_cases.py ──► 50+ cases across 7 categories │ │ regression_test.py ──► v4.1 vs v3.1, CI gate threshold 5% │ │ │ └─────────────────────────────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ STAGE 4: DEPLOYMENT │ │ │ │ convert_to_mlx.py ──► MLX 4-bit for Apple Silicon │ │ verify_mlx_model.py ──► smoke test (load, generate, parse) │ │ HuggingFace upload ──► public release │ │ │ └─────────────────────────────────────────────────────────────────┘ ``` ### Key Design Decisions | Decision | Rationale | Professional Reference | |----------|-----------|----------------------| | Teacher = v3.1 LoRA on H100 | Cheaper than GPT-4, domain-specific | Qwen self-distillation | | 5-reviewer pure-Python improvement | No LLM cost for review, deterministic | LIMA: quality > quantity | | SFT before DPO | Standard two-stage approach | Qwen3, Llama 3, Mistral | | LoRA r=8 (not full FT) | 9B model, cost-effective | Together AI best practices | | 32 parallel API workers | H100 batches concurrent requests | Endpoint autoscaling | | Resume support on all stages | Long runs can be interrupted | Production reliability | --- ## 2. Stage 1: SFT (Supervised Fine-Tuning) ### 2.1 Instruction Pairs **What it does:** Trains the model using curated prompt-and-response examples where the response includes the correct control token and structured tool call. **Data source:** 10,000 diverse task prompts generated across 10 categories: | Category | Count | Expected Mode | Description | |----------|-------|---------------|-------------| | tool_use_file_ops | 1,481 | FAST | File read/write/list operations | | coding_write | 1,454 | THINK | Write new code from spec | | tool_use_shell | 1,210 | FAST | Shell command execution | | coding_debug | 1,209 | RECOVER | Debug and fix broken code | | tool_use_git | 1,028 | FAST | Git operations | | tool_use_search | 859 | FAST | Grep/find file operations | | coding_test | 801 | VERIFY | Write/run tests | | coding_refactor | 749 | THINK | Refactor existing code | | planning | 722 | THINK | Multi-step task planning | | recovery | 487 | RECOVER | Error recovery scenarios | **Professional standard (Qwen/Llama 3):** - Dataset size: 50K–10M examples (we use 10K — LIMA showed 1K high-quality > 50K noisy) - Token budget: 200M–500M for SFT (our 10K × ~1K tokens = ~10M, appropriate for LoRA) - Data quality > quantity (LIMA finding, Zhou et al. 2023) ### 2.2 Behavior Cloning **What it does:** Teaches the model to: 1. Start every response with a control token (`[FAST]`, `[THINK]`, etc.) 2. Emit structured tool calls in XML+JSON format (not narrate them) 3. Follow the assistant persona (not just complete text) **The 5-reviewer orchestrator** fixes v3.1's key weakness: it narrates tool use ("select tool: git → git log --oneline -20") instead of emitting structured calls. The reviewers synthesize a corrected response with proper format: ``` [FAST] Direct action. {"name": "git", "arguments": {"command": "log --oneline -20"}} ``` **Professional standard:** - Use `tokenizer.apply_chat_template()` before training ✓ - Loss computed only on assistant tokens ✓ (Together AI handles this) - Messages >4096 tokens truncated per-message ✓ - Packing=True for short examples ✓ (Together AI default) ### 2.3 SFT Training Configuration ```python # train_sft.py parameters model = "Qwen/Qwen3.5-9B" n_epochs = 3 learning_rate = 1e-5 lora_r = 8 lora_alpha = 16 # 2x lora_r (standard practice) training_method = "sft" packing = True max_seq_length = 4096 ``` **Professional standard (Qwen3-1.7B reference):** - LR: 2e-5 to 5e-5 (ours: 1e-5, conservative for LoRA) ✓ - Epochs: 1-3 ✓ - Batch size: 2-8 with gradient accumulation ✓ - Warmup ratio: 0.1 ✓ (Together AI default) ### 2.4 Data Quality Filtering | Filter | Status | Professional Standard | |--------|--------|----------------------| | Deduplication by prompt | ✓ (sft_export.py) | Required | | Empty prompt/response removal | ✓ | Required | | Control token injection | ✓ (auto-add `[THINK]` if missing) | Domain-specific | | N-gram decontamination | ⚠️ Not implemented | Qwen uses this | | Length filtering | ⚠️ Not implemented | Mistral: 5-4000 chars | | Toxicity filtering | ⚠️ Not implemented | Llama 3 uses safety classifiers | --- ## 3. Stage 2: Preference Optimization (DPO) ### 3.1 Reward Tuning via DPO **What it does:** Uses Direct Preference Optimization to teach the model to prefer the improved (reviewed) response over the raw teacher response. **DPO loss function:** ``` L_DPO = -log σ(β * [log π_θ(y_w|x) - log π_ref(y_w|x) - log π_θ(y_l|x) + log π_ref(y_l|x)]) ``` **Preference pair construction:** - **Chosen (preferred):** The 5-reviewer improved response (proper format, correct tool calls) - **Rejected (non-preferred):** The raw Qwen v3.1 response (narrated tools, wrong format) This teaches the model that structured tool calls > narrated descriptions. ### 3.2 DPO Training Configuration ```python # train_dpo.py parameters model = "Qwen/Qwen3.5-9B" training_method = "dpo" dpo_beta = 0.1 # Standard from TRL n_epochs = 1 # DPO typically 1 epoch learning_rate = 5e-6 # Lower than SFT (standard) lora_r = 8 from_checkpoint = "" # Continue from SFT ``` **Professional standard:** - β: 0.05–0.1 (ours: 0.1, stable default) ✓ - LR: 5e-6 to 1e-5 ✓ - Epochs: 1 ✓ - Continue from SFT checkpoint ✓ ### 3.3 Value Alignment **What it does:** Teaches the AI to choose safe, polite, and preferred answers over harmful or wrong ones. | Alignment Dimension | Status | Implementation | |---------------------|--------|----------------| | Format preference | ✓ | Structured tool calls > narration | | Control token accuracy | ✓ | Correct token per task type | | Error recovery preference | ✓ | RECOVER token for error scenarios | | Safety (harmful tool avoidance) | ⚠️ | Not explicitly trained | | Politeness/clarity | ✓ | Reviewer 5 (Response Format & Clarity) | **Professional standard (Llama 3):** - Safety classifiers filter training data ⚠️ - Red teaming against harmful prompts ⚠️ - RLHF with reward model (alternative to DPO) — DPO is simpler, equally effective ### 3.4 DPO Data Format Together AI requires a specific format (not the flat `prompt/chosen/rejected`): ```json { "input": { "messages": [ {"role": "system", "content": "You are an adaptive engineering operator..."}, {"role": "user", "content": "Show the last 20 commits"} ] }, "preferred_output": [ {"role": "assistant", "content": "[FAST]\n{\"name\":\"git\",...}"} ], "non_preferred_output": [ {"role": "assistant", "content": "[FAST] Simple task. select tool: git..."} ] } ``` --- ## 4. Stage 3: Evaluation and Testing ### 4.1 Benchmark Testing **What it does:** Runs the model through safety and skill tests to check for regressions or errors. **Evaluation categories (eval_suite.py):** | Category | Test Count | What It Tests | |----------|-----------|---------------| | Control token accuracy | 50+ | Right token for task type | | Tool selection accuracy | 50+ | Right tool for the job | | Tool call format validity | 50+ | Valid JSON, correct params | | Task completion (end-to-end) | 50+ | Can it actually do the task? | | Regression (v4.1 vs v3.1) | All | No regressions from v3.1 | **Test case categories (test_cases.py):** | Category | Expected Mode | Description | |----------|---------------|-------------| | Simple file operations | FAST | read/write/list files | | Complex coding tasks | THINK | write algorithms, refactor | | Error recovery scenarios | RECOVER | fix broken code, handle errors | | Verification tasks | VERIFY | run tests, check output | | Multi-step planning | THINK | break down complex tasks | | Tool selection | varies | which tool to use? | | Edge cases | varies | empty input, unicode, large files | ### 4.2 Regression Testing **What it does:** Compares model v4.1 against v3.1 on the same test cases. **Metrics tracked:** - Improvement: v4.1 better than v3.1 - Regression: v4.1 worse than v3.1 - No change: identical performance **CI gate:** `--threshold 5%` — up to 5% regression allowed (Wilson 95% CI). **Professional standard:** - Wilson confidence intervals ✓ (regression_test.py) - 200-400 benchmark problems ✓ (50+ test cases) - Pass@1 metric ✓ (task completion rate) - LLM Regression Detector pattern ✓ (regression_test.py) ### 4.3 Statistical Significance | Metric | Status | Professional Standard | |--------|--------|----------------------| | Wilson 95% CI | ✓ | Required for CI gates | | Sample size > 30 | ✓ (50+ cases) | Minimum for significance | | Multiple seeds | ⚠️ | Should run each test × 3 seeds | | Effect size reporting | ⚠️ | Cohen's d or similar | --- ## 5. Stage 4: Domain Adaptation ### 5.1 Coding Domain **What it does:** Refines performance for coding-specific tasks. | Sub-domain | Coverage | Prompt Categories | |------------|----------|-------------------| | Writing new code | ✓ | coding_write (1,454 prompts) | | Debugging | ✓ | coding_debug (1,209 prompts) | | Refactoring | ✓ | coding_refactor (749 prompts) | | Testing | ✓ | coding_test (801 prompts) | | Code review | ⚠️ | Not covered | | Documentation | ⚠️ | Not covered | ### 5.2 Tool-Use Domain **What it does:** Trains the model to use 12 MAOS tools correctly. | Tool | Coverage | Expected Mode | |------|----------|---------------| | shell | ✓ | FAST | | file_read | ✓ | FAST | | file_write | ✓ | FAST | | file_edit | ✓ | FAST | | file_list | ✓ | FAST | | grep | ✓ | FAST | | find_file | ✓ | FAST | | git | ✓ | FAST | | web_search | ✓ | FAST | | web_fetch | ✓ | FAST | | todo_write | ✓ | FAST | | ask_user | ✓ | ESCALATE | ### 5.3 Recovery Domain **What it does:** Trains the model to handle errors gracefully. | Scenario | Coverage | Expected Mode | |----------|----------|---------------| | Test failure | ✓ | RECOVER | | Tool error | ✓ | RECOVER | | Syntax error | ✓ | RECOVER | | Missing file | ✓ | RECOVER | | Permission denied | ⚠️ | Not explicitly covered | | Network failure | ⚠️ | Not explicitly covered | --- ## 6. Stage 5: Deployment ### 6.1 MLX Conversion ```bash # Download adapter from Together AI, merge into base, convert to MLX 4-bit cd /Users/david/mac-ai-os uv run python -m training.v4_pipeline.convert_to_mlx \ --job-id \ --quantize 4bit \ --output-path /Users/david/Projects/local-operator/models/v4/mlx/ ``` ### 6.2 Verification ```bash # Smoke test: load, generate, check control tokens + tool call parsing uv run python -m training.v4_pipeline.verify_mlx_model \ --model-path /Users/david/Projects/local-operator/models/v4/mlx/Qwen3.5-9B-Adaptive-Operator-v4-MLX-4bit ``` ### 6.3 HuggingFace Upload Upload the merged FP16 model and the MLX 4-bit quantized version to HuggingFace for public release. --- ## 7. File Inventory | File | Lines | Stage | Purpose | |------|-------|-------|---------| | `prompt_generator.py` | 282 | Data Gen | Generate 10K diverse task prompts | | `qwen_inference.py` | 593 | Data Gen | Qwen v3.1 inference client (Together AI) | | `sft_generator.py` | 351 | Data Gen | Orchestrate SFT data generation (32 parallel workers) | | `review_orchestrator.py` | 1166 | Data Gen | 5-reviewer improvement (pure Python) | | `sft_export.py` | 270 | SFT | Export improved responses to Together AI SFT format | | `dpo_generator.py` | 421 | DPO | Generate preference pairs from responses | | `dpo_export.py` | 341 | DPO | Export to Together AI DPO format | | `train_sft.py` | 402 | SFT | Submit SFT job to Together AI | | `train_dpo.py` | 462 | DPO | Submit DPO job to Together AI | | `convert_to_mlx.py` | 619 | Deploy | Download, merge, convert to MLX | | `verify_mlx_model.py` | 393 | Deploy | Smoke test converted model | | `eval/eval_suite.py` | 1566 | Eval | Comprehensive evaluation harness | | `eval/test_cases.py` | 2068 | Eval | 50+ test cases across 7 categories | | `eval/regression_test.py` | 700 | Eval | v4.1 vs v3.1 regression comparison | ### Data Files | File | Current Count | Target | Description | |------|--------------|--------|-------------| | `prompts/prompts_10000.jsonl` | 10,000 | 10,000 | Task prompts | | `sft/qwen_responses.jsonl` | ~6,400+ | 10,000 | Raw teacher responses | | `sft/improved_responses.jsonl` | 485 | 10,000 | 5-reviewer improved responses | | `sft/sft_train.jsonl` | 287 | ~10,000 | SFT training dataset | | `dpo/preference_pairs.jsonl` | 287 | ~10,000 | DPO preference pairs | | `dpo/dpo_train.jsonl` | 287 | ~10,000 | DPO training dataset | --- ## 8. Professional Standards Compliance Matrix | Standard | SFT | DPO | Eval | Status | |----------|-----|-----|------|--------| | **Instruction pairs** (curated prompt-response) | ✓ | — | — | 10K prompts, 5-reviewer improvement | | **Behavior cloning** (format, commands, assistant persona) | ✓ | — | — | Control tokens + tool call format | | **Reward tuning** (DPO/RLHF) | — | ✓ | — | DPO β=0.1, chosen=improved, rejected=raw | | **Value alignment** (safe, preferred over harmful) | ⚠️ | ✓ | — | Format alignment ✓, safety ⚠️ | | **Benchmark testing** (safety + skill) | — | — | ✓ | 50+ cases, 7 categories | | **Regression detection** (no regressions) | — | — | ✓ | v4.1 vs v3.1, 5% CI gate | | **Domain adaptation** (coding, tools, recovery) | ✓ | — | ✓ | 10 categories covering coding + tools | | **Data deduplication** | ✓ | ✓ | — | By prompt ID | | **N-gram decontamination** | ⚠️ | ⚠️ | — | Not implemented | | **Length filtering** | ⚠️ | ✓ | — | DPO has 5-4000 char bounds | | **Toxicity/safety filtering** | ⚠️ | ⚠️ | — | Not implemented | | **Chat template application** | ✓ | ✓ | — | Together AI handles | | **Loss only on assistant tokens** | ✓ | ✓ | — | Together AI handles | | **Packing** | ✓ | — | — | Together AI default | | **Wilson 95% CI** | — | — | ✓ | regression_test.py | | **Multiple seeds** | — | — | ⚠️ | Should run × 3 | | **Statistical significance** | — | — | ⚠️ | Wilson CI, no effect size | ### Gaps to Address for Production Grade 1. **N-gram decontamination** — filter prompts that overlap with eval test cases 2. **Safety/toxicity filtering** — add a safety classifier to filter harmful prompts 3. **Multiple eval seeds** — run each test case × 3 seeds for variance estimation 4. **Effect size reporting** — add Cohen's d to regression test 5. **Code review + documentation prompts** — add to prompt generator 6. **Permission/network error recovery** — add to recovery scenarios --- ## References - **Qwen3 post-training**: Two-stage SFT (foundational + chat) → DPO. 248.7M SFT tokens. - **Llama 3**: 10M+ human-annotated examples, 15T pretraining tokens. - **LIMA** (Zhou et al. 2023): 1,000 high-quality examples > 50,000 noisy ones. - **Mistral**: Dolly 15K base, strict JSONL, function calling format. - **TRL (Hugging Face)**: Standard DPO implementation, β=0.1 default. - **Together AI**: LoRA fine-tuning, SFT + DPO support, OpenAI-compatible API.