1
0
Fork 0
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

Open
opened 2026-11-01 04:18:45 +07:00 by bsakaguchi · 4 comments
Owner

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, R is the "round to dtype" operator, x is the input vector, y is the output vector, W is the base matrix, and A and B are the LoRA matrices (with BA being the complete adapter, by construction, so that the effective weight is W + BA in theory).

"Live LoRA" (trial inference):

y = R(R(x W^T) + R(R(x A^T) @ B^T))

Exported model:

y = R(x @ R(W + BA)^T)

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.

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, `R` is the "round to dtype" operator, `x` is the input vector, `y` is the output vector, `W` is the base matrix, and `A` and `B` are the LoRA matrices (with `BA` being the complete adapter, by construction, so that the effective weight is `W + BA` in theory). ### "Live LoRA" (trial inference): ``` y = R(R(x W^T) + R(R(x A^T) @ B^T)) ``` ### Exported model: ``` y = R(x @ R(W + BA)^T) ``` 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.
Author
Owner

@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

<!-- gh-comment-id:6084905521 --> @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
Author
Owner

@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.

<!-- gh-comment-id:6124232426 --> @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.
Author
Owner

@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 Optuna objective scores evaluator.get_scores() on the live-LoRA model (~line 636), whereas the --evaluate-model path (~lines 576-584) scores reloaded exported weights. The two operators apply different rounding orders — live is y = R(R(xWᵀ) + R(R(xAᵀ)Bᵀ)) via apply_lora (src/heretic/model.py:192-230), export is y = R(x·R(W+BA)ᵀ) via get_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.)

<!-- gh-comment-id:6158792319 --> @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 Optuna `objective` scores `evaluator.get_scores()` on the live-LoRA model (~line 636), whereas the `--evaluate-model` path (~lines 576-584) scores reloaded exported weights. The two operators apply different rounding orders — live is `y = R(R(xWᵀ) + R(R(xAᵀ)Bᵀ))` via `apply_lora` (`src/heretic/model.py:192-230`), export is `y = R(x·R(W+BA)ᵀ)` via `get_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.)
Author
Owner

@akushonkamen commented on GitHub (Oct 14, 2026):

Opened #495 with a bounded fix: after the study, the top n_merged_score_trials trials (default 3, 0 disables) 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.

<!-- gh-comment-id:6159311147 --> @akushonkamen commented on GitHub (Oct 14, 2026): Opened #495 with a bounded fix: after the study, the top `n_merged_score_trials` trials (default 3, `0` disables) 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.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
p-e-w/heretic#218
No description provided.