Does Training on Diffs Protect What a Code Model Already Knows?

Code models are increasingly specialized for program repair by teaching them to output only the change, a "diff" or edit script, instead of rewriting the whole file. Separately, specializing a model on a narrow domain is known to erase general skills such as multi-step reasoning, tool use, and writing programs from scratch. This note proposes to connect the two. Our hypothesis is that edit-based supervision is a quiet regularizer. Because a diff target leaves unchanged code implicit, most of the training signal lands on a few tokens. The weight updates should then stay smaller and bend the model's internal representations less, so a small model can gain repair skill while forgetting less than it would under whole-file fine-tuning. If this holds, practitioners choosing how to specialize small repair models get a real design rule. If it fails, the field learns that the benefits of diff training reported so far may carry a hidden cost that nobody has measured.

The claim can be tested and could come out either way. Diff training might preserve general ability because it perturbs the model less. Or it might damage general ability faster, because concentrated gradients on a handful of edit tokens push large, low-rank changes along the model's most important directions. The study is designed to tell these apart. It also separates the effect of the output format from the simpler effect of how many tokens carry loss.

Why this is open

The evidence that diff training helps comes only from code benchmarks. Training Language Models on Synthetic Edit Sequences Improves Code Synthesis builds edit sequences with a linter-guided deletion procedure (LintSeq) and lifts a 150M model's HumanEval pass@1 from 9.1% to 12.8%. D3: A Dataset for Training Code LMs to Act Diff-by-Diff mid-trains Llama 3.2 1B and 3B on 8 billion tokens of such diffs and reports 3 to 6 point pass@1 gains on HumanEvalSynth and HumanEvalFix over direct fine-tuning. Code Comparison Tuning for Code Large Language Models puts a contrastive loss at the first token where a buggy and a fixed program diverge. None of these three measures anything outside code. They supply our training data and methods, and their blind spot motivates the work.

Three papers make us expect a cost and shape the measurements. Specializing Smaller Language Models towards Multi-Step Reasoning distills math reasoning into Flan-T5 and finds that its chain-of-thought ability on BigBench Hard nearly vanishes. Awakening the Sleeping Agent shows a Lean-specialized prover dropping from 89.4% to about 4.6% on the Berkeley Function Calling Leaderboard, and shows that 100 traces restore it to 83.8%. That result means "forgetting" can be suppression rather than erasure, so we must test recoverability too. Understanding Catastrophic Forgetting In LoRA via Mean-Field Attention Dynamics supplies the geometric lens. LoRA is low-rank adaptation, which trains a small add-on matrix instead of the full weights. That paper shows LoRA updates align with the base model's dominant eigenvectors, and that forgetting appears as a sharp transition once the update's norm crosses a threshold, starting in deep layers and moving upward. The paper's toy and general-text setups never touch code edits. The question it leaves open is whether edit-format training changes where a model sits relative to that threshold.

Core Claim

At matched gains in repair accuracy, small models trained to emit edits lose less general capability than models trained to emit whole corrected files. We further claim the gap is explained mostly by the size and direction of the weight update, not by anything special about diff syntax. The second part is deliberately falsifiable. If a whole-file model trained with loss only on the changed lines matches the diff model, the lesson becomes "localize the loss," and that is a simpler and more useful recipe.

Research Program

We train Llama 3.2 1B and 3B, the models D3 used, and Qwen3 0.6B, which the mean-field paper analyzed, on the same repair pairs in four target formats. The first format is the full corrected file. The second is the full file with loss masked to tokens inside changed spans, which keeps the whole-file output format while matching the diff condition's loss concentration. The third is a unified diff or search-and-replace block. The fourth is D3-style diff-by-diff sequences. Repair pairs come from bug-injected Evol-Instruct-Code-80k Python programs, following CCT's construction, plus the D3 corpus. Every condition runs as full fine-tuning and as LoRA at several ranks, since the mean-field paper finds lower rank can interfere more.

We checkpoint densely along each run and treat each run as a trajectory, not an endpoint. At every checkpoint we record three things. The first is repair gain. The second is retained general ability. The third is geometry: the norm of the weight update, its projection onto the base model's top singular directions for each layer, and the layer at which representational drift first exceeds a fixed bound. This lets us plot, for each format, general ability against update norm and against repair gain. The central object of the study is the resulting frontier of repair gain versus retention. The geometric curves test whether formats differ only by where they sit on a shared norm-to-forgetting curve.

Finally, following the Goedel result, we test whether any lost ability is suppressed or erased. We fine-tune each endpoint on 100 general traces covering greenfield Python synthesis, tool calls, and reasoning, then measure both how much general ability returns and how much repair accuracy survives. This also tests the report in D3 that diff-trained models struggle with end-to-end greenfield generation.

Baselines

The do-nothing reference is the untouched base model, evaluated on every benchmark. It fixes the zero point for both repair gain and retention.

The main competitor is standard whole-file supervised fine-tuning with a small replay mixture of general instruction and reasoning data. It uses the same repair pairs, the same token budget, and a replay fraction tuned on a held-out split with the same number of tuning trials given to our conditions. Replay is the remedy practitioners reach for first, so any claim that format matters must survive it.

The strongest existing repair method in the file is CCT. It reports 47.7% pass@1 on HumanEvalFix for CodeLlama-13B, against 43.5% for standard instruction tuning and 47.0% for GPT-4 (Code Comparison Tuning for Code Large Language Models). Our models are smaller, and no reported result was found on Lacuna for absolute HumanEvalFix pass@1 of Llama 3.2 1B or 3B under D3. D3 reports only a 3 to 6 point gain over direct fine-tuning. We therefore re-run CCT's loss on our base models with our data and budget. Beating it means matching its HumanEvalFix gain within noise while retaining more BigBench Hard and function-calling accuracy. Simply scoring higher on repair does not count.

We also run a weight-level regularizer. Model Editing Harms General Abilities of Large Language Models prunes the update to the largest relative weight changes (RECT) and keeps over 94% of edit efficacy for factual edits. We apply it to the whole-file update after training. This asks whether post-hoc pruning of a whole-file update recovers what the diff format gives for free. We run SLERP merging with the base model as well, while expecting it to trade specialization for recovery as it did in the Goedel study.

The ablation that isolates our key ingredient is the loss-masked whole-file condition, run together with a norm-matched whole-file condition that stops training when the update norm equals the diff model's.

Why not just…?

Why not just mix in replay data? Replay is the real threat. It may close most of the retention gap regardless of format. The experiment settles this directly: we report the frontier for every format both with and without replay. If replay makes format irrelevant, we will say so. What would remain is diff training's claimed efficiency, repair gains at less replay cost, which we would also quantify.

Why not just use LoRA, since the base weights stay frozen? The mean-field analysis shows that trained LoRA updates distort the base model's dominant directions and can collapse abruptly past a norm threshold, with lower ranks sometimes interfering more (Understanding Catastrophic Forgetting In LoRA via Mean-Field Attention Dynamics). We treat LoRA as a condition, not a cure, and cross it with format.

Why not just prompt a frontier model to output diffs? For deployment on devices or in real-time loops, small models matter. Efficient Skill Grounding via Code Refactoring with Small Language Models gets within about 6.5 points of GPT-level systems with a 7B model. Frontier prompting also sidesteps rather than answers the scientific question of how specialization reshapes a model.

Why not just merge the specialist back with the base model? In the Goedel study, SLERP recovered tool use only by collapsing proving ability (Awakening the Sleeping Agent). We include merging so readers can see whether code repair behaves the same way.

Proposed Experiments

Repair is measured on HumanEvalFix pass@1, the Tufano small Java repair set (where NSEdit reports 24.04% top-1 in Fix Bugs with Transformer through a Neural-Symbolic Edit Grammar), and SWE-bench in the oracle-retrieval setting (SWE-bench). Retention is measured on four kinds of task. BigBench Hard and GSM8K cover reasoning. BFCL v4 covers tool use, including its irrelevance-detection split, because the Goedel study found recovered models over-call tools. HumanEval and BigCodeBench cover greenfield synthesis.

The decisive check is a single plot per model: retained general score against update norm, with one curve per format. If the diff and loss-masked curves coincide with the whole-file curve once norm is matched, format is irrelevant beyond norm, and the practical rule becomes "localize the loss and budget the norm." If diff curves sit strictly above the others at equal norm, and their updates project less onto top singular directions, then edit format itself protects the model. That would be a new reason to prefer it. The recovery experiment then decides whether any remaining loss is erasure or suppression.