mirror of
https://github.com/p-e-w/heretic.git
synced 2026-12-03 18:42:57 +07:00
[GH-ISSUE #490] [Help wanted] Trial scores differ from scores for the exported model #218
Labels
No labels
bug
enhancement
enhancement
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
p-e-w/heretic#218
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @p-e-w on GitHub (Oct 9, 2026).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/490
This issue is something I (and several users) have noticed for a while.
For example, you might pick a trial with KLD 0.15 and 3/100 refusals for export, then, when you re-load and score the exported model with
--evaluate-model, you get KLD 0.17 and 5/100 refusals.I believe I finally understand the root cause of this, and fixing it while preserving Heretic's runtime and memory performance looks to be very difficult.
The basic issue is that PEFT's "live LoRA" system applies the base matrix and the adapters separately to the input vector, while rounding intermediate results to the appropriate dtype (usually BF16) in a way that cannot be reproduced by the exported linear operator.
Here,
Ris the "round to dtype" operator,xis the input vector,yis the output vector,Wis the base matrix, andAandBare the LoRA matrices (withBAbeing the complete adapter, by construction, so that the effective weight isW + BAin theory)."Live LoRA" (trial inference):
Exported model:
In general, the two are not the same.
In other words, we optimize for a score that we're not getting in the exported model. This leads to systematically worse results than we should be getting, which sucks, but I'm at a loss for what to do here. The "obvious" fix is to make the LoRA behave like the exported model, but that would seem to require massive amounts of additional memory.
@elementmerc commented on GitHub (Oct 9, 2026):
I hit this and went the other way. Bake the delta into the weights per trial, keeping a host-RAM clone of only the touched tensors to restore between them, so the score comes off the operator you actually export and there's nothing to reconcile.
It costs that snapshot and a weight write per trial instead of an adapter apply, which is a genuine slowdown, so it's a trade rather than a fix.
Code if it helps: https://github.com/elementmerc/senbonzakura
@p-e-w commented on GitHub (Oct 12, 2026):
@elementmerc
Yes, I'm aware of that possibility, but that is way too high a performance price to pay.
@akushonkamen commented on GitHub (Oct 14, 2026):
Confirmed the root cause at HEAD: trial scores come from the live-LoRA operator while the exported model runs the merged operator. In
src/heretic/main.py, the Optunaobjectivescoresevaluator.get_scores()on the live-LoRA model (~line 636), whereas the--evaluate-modelpath (~lines 576-584) scores reloaded exported weights. The two operators apply different rounding orders — live isy = R(R(xWᵀ) + R(R(xAᵀ)Bᵀ))viaapply_lora(src/heretic/model.py:192-230), export isy = R(x·R(W+BA)ᵀ)viaget_merged_model()(src/heretic/model.py:266-313) — so the optimizer selects on numbers the shipped operator does not reproduce.Since per-trial baking is off the table for runtime reasons (agreed), I'm working on a bounded fix: after the study, re-score only the top-K trials (small K, e.g. ≤3) on their merged models using the existing evaluate path, re-rank by merged scores, and emit an explicit live↔merged delta report so the divergence is visible instead of silent. Memory stays bounded (at most one CPU full-precision copy at a time, reusing
get_merged_model()'s existing path).I'll verify with a CPU-side numerical micro-repro of the bf16 rounding divergence plus unit tests; I don't have CUDA locally, so end-to-end KLD numbers on a real model will be stated as such rather than claimed.
(This contribution was prepared with AI coding assistance, reviewed and to be tested by me.)
@akushonkamen commented on GitHub (Oct 14, 2026):
Opened #495 with a bounded fix: after the study, the top
n_merged_score_trialstrials (default 3,0disables) are re-scored on their merged models, a live → merged delta report is printed and persisted, and the menu is re-ranked by merged scores when the whole front was covered. Trial-time scoring is untouched, memory stays bounded (one merged model at a time), and a failed re-score only skips that trial.