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 #333] Suggestion: start-end layer probes before start #181

Closed
opened 2026-11-01 04:18:23 +07:00 by bsakaguchi · 6 comments
Owner

Originally created by @kabachuha on GitHub (May 27, 2026).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/333

Currently, in the processes such as ARA Heretic optimizes for six parameters

Parameters:

  • start_layer_index = 11
  • end_layer_index = 34
  • preserve_good_behavior_weight = 0.5510
  • steer_bad_behavior_weight = 0.0210
  • overcorrect_relative_weight = 0.9777
  • neighbor_count = 13

While the four latest are mathematical and ARA related, the first two seem to be offtopic and they are completely independent from ARA, potentially hindering its optimization landscape.

I wonder if Heretic can firstly perform a brief optimal start end layers fit check and only then proceed to refine the math coefficients of ARA.

Originally created by @kabachuha on GitHub (May 27, 2026). Original GitHub issue: https://github.com/p-e-w/heretic/issues/333 Currently, in the processes such as ARA Heretic optimizes for six parameters Parameters: * start_layer_index = 11 * end_layer_index = 34 * preserve_good_behavior_weight = 0.5510 * steer_bad_behavior_weight = 0.0210 * overcorrect_relative_weight = 0.9777 * neighbor_count = 13 While the four latest are mathematical and ARA related, the first two seem to be offtopic and they are *completely* independent from ARA, potentially hindering its optimization landscape. I wonder if Heretic can firstly perform a brief optimal start end layers fit check and only then proceed to refine the math coefficients of ARA.
Author
Owner

@p-e-w commented on GitHub (May 27, 2026):

How would that work? The layer range is NOT independent from the other parameters because we optimize for the overall model behavior. All parameters are interrelated.

We could try heuristics like discarding layers that have low silhouette scores on the residual vectors, indicating that the layer doesn't clearly distinguish refusals from non-refusals, but that is at best a rough first guess.

<!-- gh-comment-id:4554171192 --> @p-e-w commented on GitHub (May 27, 2026): How would that work? The layer range is NOT independent from the other parameters because we optimize for the overall model behavior. All parameters are interrelated. We could try heuristics like discarding layers that have low silhouette scores on the residual vectors, indicating that the layer doesn't clearly distinguish refusals from non-refusals, but that is at best a rough first guess.
Author
Owner

@umran666 commented on GitHub (Jun 18, 2026):

Building on what @p-e-w said: a two-phase optimization guarantees we get trapped in a local minimum. The layer ranges are completely mathematically entangled with the recovery coefficients.

Here's why: the depth of the ablation fundamentally changes the geometry of the required correction. Early layers heavily encode syntax and shallow semantics; if you ablate there, you need a massive preserve_good_behavior_weight to prevent the model from degrading into gibberish. Late layers encode crystallized safety concepts; ablating there requires a completely different balance, usually a much higher steer_bad_behavior_weight.

If we run a "brief optimal start/end layer fit check" using default static coefficients, the optimizer will simply gravitate toward a safe, middle-ground layer range that happens to survive the default weights. By the time the second phase starts refining the math coefficients, the optimization landscape has already been artificially bottlenecked into a suboptimal layer range. They have to be solved jointly.

<!-- gh-comment-id:4737797054 --> @umran666 commented on GitHub (Jun 18, 2026): Building on what @p-e-w said: a two-phase optimization guarantees we get trapped in a local minimum. The layer ranges are completely mathematically entangled with the recovery coefficients. Here's why: the depth of the ablation fundamentally changes the geometry of the required correction. Early layers heavily encode syntax and shallow semantics; if you ablate there, you need a massive `preserve_good_behavior_weight` to prevent the model from degrading into gibberish. Late layers encode crystallized safety concepts; ablating there requires a completely different balance, usually a much higher `steer_bad_behavior_weight`. If we run a "brief optimal start/end layer fit check" using default static coefficients, the optimizer will simply gravitate toward a safe, middle-ground layer range that happens to survive the default weights. By the time the second phase starts refining the math coefficients, the optimization landscape has already been artificially bottlenecked into a suboptimal layer range. They have to be solved jointly.
Author
Owner

@rocker-zhang commented on GitHub (Jun 22, 2026):

We could try heuristics like discarding layers that have low silhouette scores on the residual vectors, indicating that the layer doesn't clearly distinguish refusals from non-refusals, but that is at best a rough first guess.

I tested this. Master samples the global direction_index from [0.4, 0.9] * last_layer. I compared that fixed range against restricting it to the "silhouette band" — the contiguous layers whose silhouette score is at least 90% of the per-layer peak. Same seed, budget, and TPE settings for both arms, 150 trials, 3 seeds each, run locally, thinking disabled where the model supports it so first-token KL stays meaningful.

model base refusals peak silhouette better range
Qwen3-4B-Instruct-2507 100 0.428 silhouette band (floor 3 vs 6 refusals; best-KL-per-refusal 59:30)
SmolLM3-3B 89 0.315 fixed (floor 2 vs 3; 44:30)
granite-4.0-micro 95 0.273 fixed (both floor at 4, but fixed dominates the low-refusal region; 52:36)

One thing to flag up front: this is the direction_index selection in the default method, not the ARA start/end_layer_index window the original post was about. The same silhouette-as-prior idea may carry over to that window, but I haven't tested it there.

The band isn't uniformly a win: clear on Qwen3, a small loss on the other two. What separates the three is the absolute height of the silhouette peak, not its shape. A high peak means the layers genuinely separate refusals, so narrowing to the band is trustworthy; a low peak means the band is a noisy guide and the broad fixed range is safer.

That points to an adaptive rule rather than always-on: use the band only when its peak clears a threshold, otherwise keep [0.4, 0.9] * last. With three models this is just a separating cut — anything between SmolLM3's 0.315 and Qwen3's 0.428 works — not a calibrated constant, and "never worse than the better static range" is true by construction on these three, since the cut is drawn from them. It would take more models to turn it into an actual threshold.

One useful property: the rule only picks which range TPE searches, and range + seed + sampler fully determine the trials, so the adaptive arm reproduces the exact results of whichever static arm it selects. A candidate threshold can therefore be evaluated offline against existing fixed/silhouette runs, with no extra compute.

On @umran666's point that the layer range and the recovery coefficients are entangled: the band here comes from the residual geometry (the silhouette of the refusal/non-refusal clusters), computed before any optimization, not from a layer probe run under default static coefficients — which is the case where the optimizer drifts to a safe mid-range. So it's a different mechanism from the two-phase fit that gets trapped. It isn't free of the concern, though: narrowing the range before the joint search could in principle clip a range that's only good under tuned coefficients. The "never worse than the better static range" result is the only evidence I have that this doesn't bite in practice, and three models isn't much.

Implementation cost: the per-layer silhouette needs the per-prompt residuals rather than the running mean the default path keeps, so it's a memory/compute cost worth weighing before wiring in. PR #384 is reworking that residual-collection path, so this would want to sit on top of whatever lands there.

I can open a PR if this seems worth pursuing. The three models are public and the comparison reproduces.

<!-- gh-comment-id:4769599439 --> @rocker-zhang commented on GitHub (Jun 22, 2026): > We could try heuristics like discarding layers that have low silhouette scores on the residual vectors, indicating that the layer doesn't clearly distinguish refusals from non-refusals, but that is at best a rough first guess. I tested this. Master samples the global `direction_index` from `[0.4, 0.9] * last_layer`. I compared that fixed range against restricting it to the "silhouette band" — the contiguous layers whose silhouette score is at least 90% of the per-layer peak. Same seed, budget, and TPE settings for both arms, 150 trials, 3 seeds each, run locally, thinking disabled where the model supports it so first-token KL stays meaningful. | model | base refusals | peak silhouette | better range | |---|---|---|---| | Qwen3-4B-Instruct-2507 | 100 | 0.428 | silhouette band (floor 3 vs 6 refusals; best-KL-per-refusal 59:30) | | SmolLM3-3B | 89 | 0.315 | fixed (floor 2 vs 3; 44:30) | | granite-4.0-micro | 95 | 0.273 | fixed (both floor at 4, but fixed dominates the low-refusal region; 52:36) | One thing to flag up front: this is the `direction_index` selection in the default method, not the ARA `start/end_layer_index` window the original post was about. The same silhouette-as-prior idea may carry over to that window, but I haven't tested it there. The band isn't uniformly a win: clear on Qwen3, a small loss on the other two. What separates the three is the absolute height of the silhouette peak, not its shape. A high peak means the layers genuinely separate refusals, so narrowing to the band is trustworthy; a low peak means the band is a noisy guide and the broad fixed range is safer. That points to an adaptive rule rather than always-on: use the band only when its peak clears a threshold, otherwise keep `[0.4, 0.9] * last`. With three models this is just a separating cut — anything between SmolLM3's 0.315 and Qwen3's 0.428 works — not a calibrated constant, and "never worse than the better static range" is true by construction on these three, since the cut is drawn from them. It would take more models to turn it into an actual threshold. One useful property: the rule only picks which range TPE searches, and range + seed + sampler fully determine the trials, so the adaptive arm reproduces the exact results of whichever static arm it selects. A candidate threshold can therefore be evaluated offline against existing fixed/silhouette runs, with no extra compute. On @umran666's point that the layer range and the recovery coefficients are entangled: the band here comes from the residual geometry (the silhouette of the refusal/non-refusal clusters), computed before any optimization, not from a layer probe run under default static coefficients — which is the case where the optimizer drifts to a safe mid-range. So it's a different mechanism from the two-phase fit that gets trapped. It isn't free of the concern, though: narrowing the range before the joint search could in principle clip a range that's only good under tuned coefficients. The "never worse than the better static range" result is the only evidence I have that this doesn't bite in practice, and three models isn't much. Implementation cost: the per-layer silhouette needs the per-prompt residuals rather than the running mean the default path keeps, so it's a memory/compute cost worth weighing before wiring in. PR #384 is reworking that residual-collection path, so this would want to sit on top of whatever lands there. I can open a PR if this seems worth pursuing. The three models are public and the comparison reproduces.
Author
Owner

@umran666 commented on GitHub (Jun 22, 2026):

The logic holds. Since the silhouette is computed pre-optimization, it acts as a prior and sidesteps the two-phase trap. The adaptive rule is mandatory though — if the peak is weak, the band is noise. Using ~0.40 as the cutoff based on your runs makes sense.

Hold off on opening the PR until #384 lands so you don't have to rewrite the residual collection logic, but definitely pursue this once that merges.

<!-- gh-comment-id:4770164769 --> @umran666 commented on GitHub (Jun 22, 2026): The logic holds. Since the silhouette is computed pre-optimization, it acts as a prior and sidesteps the two-phase trap. The adaptive rule is mandatory though — if the peak is weak, the band is noise. Using ~0.40 as the cutoff based on your runs makes sense. Hold off on opening the PR until #384 lands so you don't have to rewrite the residual collection logic, but definitely pursue this once that merges.
Author
Owner

@p-e-w commented on GitHub (Jun 22, 2026):

the "silhouette band" — the contiguous layers whose silhouette score is at least 90% of the per-layer peak

What if strong silhouette separation isn't contiguous? I'm pretty sure I have observed this in some models.

That points to an adaptive rule rather than always-on: use the band only when its peak clears a threshold

The problem is that this threshold is yet another ad-hoc constant without theoretical underpinnings, and there is an ever-growing zoo of models, some of which might require very different thresholds.

This is the reason why we need to avoid complexity unless there is very strong evidence it brings a real improvement. The plugin system will be merged soon, and I think the best way forward will be to add plugin APIs that enable doing such experimental stuff from plugins. It's incredibly difficult to test any approach comprehensively, and I have to be conservative about what I merge into mainline to avoid creating a monstrosity that nobody can control or understand anymore.

<!-- gh-comment-id:4771007164 --> @p-e-w commented on GitHub (Jun 22, 2026): > the "silhouette band" — the contiguous layers whose silhouette score is at least 90% of the per-layer peak What if strong silhouette separation isn't contiguous? I'm pretty sure I have observed this in some models. > That points to an adaptive rule rather than always-on: use the band only when its peak clears a threshold The problem is that this threshold is yet another ad-hoc constant without theoretical underpinnings, and there is an ever-growing zoo of models, some of which might require very different thresholds. This is the reason why we need to avoid complexity unless there is very strong evidence it brings a real improvement. The plugin system will be merged soon, and I think the best way forward will be to add plugin APIs that enable doing such experimental stuff from plugins. It's incredibly difficult to test any approach comprehensively, and I have to be conservative about what I merge into mainline to avoid creating a monstrosity that nobody can control or understand anymore.
Author
Owner

@umran666 commented on GitHub (Jun 23, 2026):

Fair point. Magic numbers without theory are brittle across a large model zoo, and non-contiguous bands would break the logic.

Pushing this to the plugin system is the right call architecturally , it keeps mainline predictable while letting us test heuristics like this in isolation. Let's revisit the implementation once the plugin API lands.

<!-- gh-comment-id:4775187919 --> @umran666 commented on GitHub (Jun 23, 2026): Fair point. Magic numbers without theory are brittle across a large model zoo, and non-contiguous bands would break the logic. Pushing this to the plugin system is the right call architecturally , it keeps mainline predictable while letting us test heuristics like this in isolation. Let's revisit the implementation once the plugin API lands.
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#181
No description provided.