← Personal website
OR-Tools SLM Distillation
Empirical Study • Combinatorial Optimization & LoRA Distillation

Can a 3B Local Model Outperform its 14B Teacher on Formal Constraint Programming?

An empirical investigation into distilling Google OR-Tools CP-SAT modeling capabilities into Qwen2.5-Coder-3B-Instruct to generate and self-repair optimization scripts on consumer hardware (AMD Radeon RX 7900 XT, 20 GB VRAM). Comparing a 100% local teacher pipeline (Qwen2.5-Coder-14B) against a frontier cloud teacher (OpenAI) across In-Distribution (Gold 50) and Out-of-Distribution (OOD 50) benchmarks.

Valid Code (Gold 50)
86.0%
Student-Local-3B (up from 0% Base)
Gain vs. 14B Teacher
+32 pts
In-Distribution Exact: 72% vs. 40%
Frontier Retention
82%
Of OpenAI's exact accuracy on Gold 50
LoRA Training Cost
€0.0189
75.1 Wh on AMD RX 7900 XT (18 min)

1. Core Architecture & Data Engineering Pipeline

Evaluated out-of-the-box in Zero-Shot, Qwen2.5-Coder-3B scores 0% across our 50 reference problems: it hallucinates non-existent OR-Tools methods (such as cp_model.StatusName) or violates strict integer typing in CP-SAT. Building a reliable, specialized 3B solver model required combining three methodological pillars.

Pillar #1 • Solver Oracle

Execution-Verified Rejection Sampling

Every synthetic script generated by the teachers (OpenAI and Qwen-14B) is executed in an isolated Python sandbox against the ortools.sat.python.cp_model solver. Only scripts returning a verified OPTIMAL integer objective within 10 seconds are retained for distillation.

Pillar #2 • Semantic Alignment

Code-to-Problem Back-Translation

When an LLM writes a problem statement first and code second, it frequently hardcodes undocumented penalties or bounds in the script ("phantom constraints"). We invert the pipeline: the teacher inspects the verified CP-SAT code and writes an exhaustive specification capturing every constant, index bound, and objective term.

Pillar #3 • Reflexion Loop

Multi-Turn Self-Correction (5,120 tok)

Each 330-sample training split combines 275 direct trajectories with 55 multi-turn debugging trajectories (faulty CP-SAT code + Python traceback + repaired script) trained across a full 5,120-token context window, enabling autonomous recovery at inference time (--retries 1).

2. Evaluation Matrix: In-Distribution (Gold 50) vs. Out-of-Distribution (OOD 50)

We evaluate all models across two strictly balanced 50-problem test suites (25 OpenAI-authored + 25 Qwen-14B-authored instances per suite):

Metric 1: Valid Code (%)

Syntactic & Solver Feasibility: The generated Python script executes without runtime exceptions within a 10s timeout, invokes cp_model properly, reaches a proven OPTIMAL status (not INFEASIBLE or MODEL_INVALID), and prints a valid JSON payload.

Metric 2: Exact Objective (%)

Mathematical & Semantic Equivalence: Not only is the script valid and OPTIMAL, but its numerical objective value matches the ground-truth optimal integer (objective == ground_truth), proving every constraint and cost coefficient was modeled accurately.

Metric 3: Repaired (Count)

Automated Self-Correction (--retries 1): Number of problems out of 50 that failed on Pass 1 (syntax error, CP-SAT API error, or non-optimal status) and were autonomously fixed by the model on Pass 2 after receiving the exact Python traceback.

Teacher Local 14B (Zero-Shot)
Student-Cloud-3B (LoRA OpenAI)
Student-Local-3B (LoRA Qwen-14B)
Teacher OpenAI (Cloud Ceiling)
Model / Variant Parameters Gold 50 • Valid Gold 50 • Exact Gold 50 • Repaired OOD 50 • Valid OOD 50 • Exact OOD 50 • Repaired
Base-3B (Zero-Shot) 3.1B 0.0% (0/50) 0.0% (0/50) 0 0.0% (0/50) 0.0% (0/50) 0
Teacher Local Qwen-14B (Zero-Shot) 14.7B 46.0% (23/50) 40.0% (20/50) 10 48.0% (24/50) 42.0% (21/50) 12
Student-Cloud-3B (LoRA OpenAI • 5120) 3.1B 64.0% (32/50) 56.0% (28/50) 6 14.0% (7/50) 14.0% (7/50) 1
Student-Local-3B (LoRA Qwen-14B • 5120) 3.1B 86.0% (43/50) 72.0% (36/50) 7 6.0% (3/50) 4.0% (2/50) 0
Teacher OpenAI (Frontier • Ceiling) Frontier 100.0% (50/50) 88.0% (44/50) 0 98.0% (49/50) 86.0% (43/50) 0

1. Why the 3B Student Beats its 14B Teacher In-Distribution (+32 pts)

In Zero-Shot on Gold 50, Qwen2.5-Coder-14B suffers from frequent API hallucinations (46% valid code). Because our pipeline uses CP-SAT execution as an oracle filter, only verified optimal trajectories were distilled into the 3B model. The student absorbs the 14B's mathematical formulation while eliminating its syntactic instability.

2. The Specialization vs. Generalization Trade-Off

While Student-Local-3B dominates on trained logistics families (86% valid / 72% exact), it drops to 6% valid / 4% exact on unseen OOD archetypes. Intra-family LoRA SFT acts as a rigid domain compiler, whereas Student-Cloud-3B—trained on OpenAI's broader structural diversity—retains 3.5x higher exact accuracy on OOD (14% vs. 4%) with 100% semantic precision (7/7).

3. Parameter Scale Governs Zero-Shot OOD Synthesis

On unseen OOD domains, the Frontier Teacher achieves virtually identical exact accuracy to Gold (86% vs. 88%), and the 14B Teacher reaches 42% exact accuracy (leveraging 12 self-repairs). Synthesizing novel mathematical constraint topologies from scratch remains fundamentally bound by pre-trained parameter scale (14B+) rather than 3B adapter fine-tuning.

3. Qualitative Trajectory Inspector

Inspect how multi-turn self-correction repairs CP-SAT API violations at inference time, and how Code-to-Problem Back-Translation eliminates unstated phantom constraints.

Pass 1 • Faulty Generation & Solver Traceback Return Code != 0
# Pass 1 bug: Passing a linear sum directly inside OnlyEnforceIf
for w in range(num_warehouses):
    model.Add(
        sum(flow[w, c] for c in range(num_clients)) <= cap[w]
    ).OnlyEnforceIf(open_var[w])

# --- CP-SAT Runtime Traceback Fed Back to Student-3B ---
AttributeError: 'LinearExpr' object has no attribute 'Index'
  in model.Add(...).OnlyEnforceIf(open_var[w])
  (Note: OnlyEnforceIf requires a clean literal or properly
   constructed linear constraint, or linearize via big-M:
   sum(flow[w, c]) <= cap[w] * open_var[w])
Pass 2 • Autonomous Repair by Student-Local-3B STATUS: OPTIMAL
# Pass 2 fix: Student-3B replaces the faulty indicator with a
# direct big-M capacity coupling constraint and valid StatusName call
for w in range(num_warehouses):
    model.Add(
        sum(flow[w, c] for c in range(num_clients))
        <= cap[w] * open_var[w]
    )

solver = cp_model.CpSolver()
solver.parameters.max_time_in_seconds = 10.0
status = solver.Solve(model)
print(json.dumps({
    "status": solver.StatusName(status),
    "objective": round(solver.ObjectiveValue()),
    "variables": {"open": [solver.Value(v) for v in open_var]}
}))

4. AMD ROCm Hardware Telemetry & FinOps Breakdown

All fine-tuning runs were executed locally on Arch Linux using an AMD Radeon RX 7900 XT (20 GB VRAM) in native float16 precision (r=16, lora_alpha=32, targeting all linear projection layers: 29.93M trainable parameters or 0.96% of total weights, 3 epochs, 5,120-token max sequence length).

Student-Local-3B Training Run Final Loss: 0.0687
Wall-Clock Duration (3 epochs)
1,073.9 s (17m 54s)
Peak Allocated VRAM
11.94 GB / 20 GB
Measured GPU Energy
75.10 Wh (251.6 W avg)
Electricity Cost (€0.2516/kWh)
€0.0189
Student-Cloud-3B Training Run Final Loss: 0.0789
Wall-Clock Duration (3 epochs)
1,432.1 s (23m 52s)
Peak Allocated VRAM
15.14 GB / 20 GB
Measured GPU Energy
101.46 Wh (254.9 W avg)
Electricity Cost (€0.2516/kWh)
€0.0255
Telemetry Insight (Why Cloud Distillation Uses +27% VRAM & +33% Compute Time): Even with identical dataset sizes (330 trajectories) and hyperparameters, Student-Cloud-3B requires 15.14 GB VRAM and 23m 52s (final loss 0.0789) compared to 11.94 GB VRAM and 17m 54s (final loss 0.0687) for Student-Local-3B. OpenAI generates longer, more structurally diverse scripts, whereas Qwen-14B produces compact code naturally aligned with the Qwen-3B student's token distribution.
Synthesis & Engineering Takeaways

5. Conclusion: Can a Local Model Outperform its Teacher on Formal Constraint Programming?

To answer our core research question: Yes, but strictly within the boundaries of trained in-distribution problem families. While our study focused on lightweight 3B models due to consumer hardware constraints, these findings establish a broader architectural blueprint: large enterprises equipped with dedicated AI compute clusters can fine-tune custom, larger local models to achieve elite domain-specific performance while preserving absolute data sovereignty.

1. Outperforming Teachers In-Distribution

By filtering solver outputs through execution verification and back-translation, specialized local students reach 86% valid code and 72% exact objectives on target logistics domains—outperforming their 14B teacher by +32 points while eliminating API hallucinations.

2. Enterprise Sovereignty & Scale

While our 3B PoC ran locally on a single consumer GPU (AMD RX 7900 XT), enterprises with dedicated on-premise clusters can scale this methodology to much larger proprietary models (e.g., 32B or 70B+). This unlocks near-frontier reasoning capabilities while ensuring that sensitive operational data and proprietary cost matrices never leave corporate infrastructure.

3. The Generalization Boundary

Local fine-tuning acts as a powerful, secure domain compiler for recurring workflows. However, novel zero-shot OOD synthesis remains constrained by parameter scale. Production systems should leverage custom local models for core business operations and reserve cloud escalation for unprecedented edge cases.