Prajhan Claude Sonnet 4.6 commited on
Commit
be120bf
Β·
1 Parent(s): 04e8ad5

Add safe LoRA adapter save and model saving writeup section

Browse files

- Training script now saves adapters via save_pretrained (not naive merge)
- Warning comment against upcast + merge_and_unload on 4-bit models
- Writeup explains why the dangerous path corrupts weights and what we do instead

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (2) hide show
  1. docs/ROUND2_WRITEUP.md +15 -0
  2. scripts/round2_train.py +10 -0
docs/ROUND2_WRITEUP.md CHANGED
@@ -137,6 +137,21 @@ This is the generation inspection the checklist specifically calls out. Scalars
137
 
138
  All sampled completions are saved to `artifacts/round2_generation_samples.json` at the end of each run.
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  ## Training direction
141
 
142
  For the training side, I used:
 
137
 
138
  All sampled completions are saved to `artifacts/round2_generation_samples.json` at the end of each run.
139
 
140
+ ## Model saving: LoRA adapters, not naive merges
141
+
142
+ We use QLoRA via Unsloth β€” 4-bit base model with LoRA adapters trained on top. The correct save path matters here.
143
+
144
+ The dangerous pattern is: upcast the 4-bit base to float16, then call `merge_and_unload()`. That path can silently corrupt the model weights because the quantization boundaries are lost during the upcast. The result looks fine until you run inference and get degraded outputs.
145
+
146
+ What we do instead:
147
+
148
+ - Save LoRA adapters only via `model.save_pretrained("outputs/round2_lora_adapter")`
149
+ - Post-training inference runs immediately in the same script β€” the model is already in memory, so there is no cold-start gap where a bad save could hide
150
+ - GRPOConfig uses `save_steps=50` to checkpoint adapter weights during training, not merged weights
151
+ - If a full merge is ever needed, the correct path is Unsloth's `save_pretrained_merged` which handles the quantization boundaries safely
152
+
153
+ The practical rule: never leave model export until the end of a training run. Test inference before the session ends, using the same in-memory model that just finished training.
154
+
155
  ## Training direction
156
 
157
  For the training side, I used:
scripts/round2_train.py CHANGED
@@ -832,3 +832,13 @@ print(f" LLM before training: {comparison['llm_before_training']}")
832
  print(f" LLM after training: {comparison['llm_after_training']}")
833
  delta = comparison["llm_after_training"]["avg_reward"] - comparison["llm_before_training"]["avg_reward"]
834
  print(f" Delta (LLM): {delta:+.3f}")
 
 
 
 
 
 
 
 
 
 
 
832
  print(f" LLM after training: {comparison['llm_after_training']}")
833
  delta = comparison["llm_after_training"]["avg_reward"] - comparison["llm_before_training"]["avg_reward"]
834
  print(f" Delta (LLM): {delta:+.3f}")
835
+
836
+ # ── Safe model save ────────────────────────────────────────────────────────────
837
+ # Save LoRA adapters only. Do NOT use merge_and_unload() on a 4-bit model β€”
838
+ # that path can corrupt weights. Use Unsloth's save path or keep adapters separate.
839
+ print("\nSaving LoRA adapters …")
840
+ model.save_pretrained("outputs/round2_lora_adapter")
841
+ tokenizer.save_pretrained("outputs/round2_lora_adapter")
842
+ print("Saved LoRA adapters to outputs/round2_lora_adapter/")
843
+ print("To merge safely later: use FastLanguageModel.get_peft_model + save_pretrained_merged")
844
+ print("Do not upcast to float16 and merge naively β€” that path damages 4-bit model weights.")