1
0
Fork 0
mirror of https://github.com/p-e-w/heretic.git synced 2026-12-03 18:42:57 +07:00

[PR #69] [CLOSED] feat: Featuring Continuous Optimization Option #264

Closed
opened 2026-11-01 04:19:20 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/p-e-w/heretic/pull/69
Author: @Vinay-Umrethe
Created: 12/4/2025
Status: ❌ Closed

Base: master ← Head: feature/continuous-optimization


📝 Commits (6)

  • b9b6756 feat: add continuous optimization option
  • 1d60e3c style: apply ruff formatting
  • a01658f Merge branch 'p-e-w:master' into feature/continuous-optimization
  • 6cba3f9 Merge branch 'p-e-w:master' into feature/continuous-optimization
  • 3ad5890 Merge branch 'p-e-w:master' into feature/continuous-optimization
  • 66a0f65 Merge branch 'master' into feature/continuous-optimization

📊 Changes

1 file changed (+212 additions, -120 deletions)

View changed files

📝 src/heretic/main.py (+212 -120)

📄 Description

this PR introduces a "Continuous Optimization" feature that allows users to extend the ablation process by running additional trials if the initial results are unsatisfactory. The optimization and trial selection logic in src/heretic/main.py has been wrapped in a loop...

After initial batch of trials completes, the user is presented with a normal menu with [NEW] option:

"Continue optimization (run more trials)". 

If selected, the user can specify the number of additional trials to run. The existing Optuna study object is reused, ensuring that the TPE sampler continues to learn from the history of previous trials and explores parameter space more effectively, rather than starting from scratchh...

enabling a more efficient workflow for finding optimal ablation parameters when the default number of trials is insufficient.

Test Conducted: (On Colab as my preference)

Loading model Qwen/Qwen2.5-0.5B-Instruct...
Ok
* Transformer model with 24 layers
* Abliterable components:
  * attn.o_proj: 1 matrices per layer
  * mlp.down_proj: 1 matrices per layer

Loading good prompts from mlabonne/harmless_alpaca...
* 400 prompts loaded

Loading bad prompts from mlabonne/harmful_behaviors...
* 400 prompts loaded

Loading good evaluation prompts from mlabonne/harmless_alpaca...
* 100 prompts loaded
* Obtaining first-token probability distributions...

Loading bad evaluation prompts from mlabonne/harmful_behaviors...
* 100 prompts loaded
* Counting model refusals...
* Initial refusals: 83/100

Calculating per-layer refusal directions...
* Obtaining residuals for good prompts...
* Obtaining residuals for bad prompts...

Running trial 1 of 3...
* Parameters:
  * direction_index = per layer
  * attn.o_proj.max_weight = 1.42
  * attn.o_proj.max_weight_position = 14.07
  * attn.o_proj.min_weight = 0.88
  * attn.o_proj.min_weight_distance = 9.21
  * mlp.down_proj.max_weight = 0.94
  * mlp.down_proj.max_weight_position = 17.43
  * mlp.down_proj.min_weight = 0.87
  * mlp.down_proj.min_weight_distance = 7.03
* Reloading model...
* Abliterating...
* Evaluating...
  * Obtaining first-token probability distributions...
  * KL divergence: 0.11
  * Counting model refusals...
  * Refusals: 50/100

Elapsed time: 14s
Estimated remaining time: 29s

Running trial 2 of 3...
* Parameters:
  * direction_index = per layer
  * attn.o_proj.max_weight = 0.80
  * attn.o_proj.max_weight_position = 18.52
  * attn.o_proj.min_weight = 0.48
  * attn.o_proj.min_weight_distance = 7.39
  * mlp.down_proj.max_weight = 1.34
  * mlp.down_proj.max_weight_position = 21.15
  * mlp.down_proj.min_weight = 1.12
  * mlp.down_proj.min_weight_distance = 6.81
* Reloading model...
* Abliterating...
* Evaluating...
  * Obtaining first-token probability distributions...
  * KL divergence: 0.10
  * Counting model refusals...
  * Refusals: 65/100

Elapsed time: 28s
Estimated remaining time: 14s

Running trial 3 of 3...
* Parameters:
  * direction_index = per layer
  * attn.o_proj.max_weight = 1.41
  * attn.o_proj.max_weight_position = 17.79
  * attn.o_proj.min_weight = 0.68
  * attn.o_proj.min_weight_distance = 11.15
  * mlp.down_proj.max_weight = 0.92
  * mlp.down_proj.max_weight_position = 17.44
  * mlp.down_proj.min_weight = 0.32
  * mlp.down_proj.min_weight_distance = 10.13
* Reloading model...
* Abliterating...
* Evaluating...
  * Obtaining first-token probability distributions...
  * KL divergence: 0.09
  * Counting model refusals...
  * Refusals: 53/100

Elapsed time: 43s

Optimization finished!

The following trials resulted in Pareto optimal combinations of refusals and KL 
divergence. After selecting a trial, you will be able to save the model, upload 
it to Hugging Face, or chat with it to test how well it works. You can return to
this menu later to select a different trial. Note that KL divergence values 
above 1 usually indicate significant damage to the original model's 
capabilities.


Which trial do you want to use?
[1] [Trial   1] Refusals: 50/100, KL divergence: 0.11
[2] [Trial   3] Refusals: 53/100, KL divergence: 0.09
[3] Continue optimization (run more trials)
[4] None (exit program)
Enter number: 3

How many more trials do you want to run?: 4

Running trial 4 of 7...
* Parameters:
  * direction_index = per layer
  * attn.o_proj.max_weight = 1.29
  * attn.o_proj.max_weight_position = 15.66
  * attn.o_proj.min_weight = 0.51
  * attn.o_proj.min_weight_distance = 9.52
  * mlp.down_proj.max_weight = 0.83
  * mlp.down_proj.max_weight_position = 20.43
  * mlp.down_proj.min_weight = 0.08
  * mlp.down_proj.min_weight_distance = 11.11
* Reloading model...
* Abliterating...
* Evaluating...
  * Obtaining first-token probability distributions...
  * KL divergence: 0.07
  * Counting model refusals...
  * Refusals: 56/100

Elapsed time: 1m 11s
Estimated remaining time: 53s

Running trial 5 of 7...
* Parameters:
  * direction_index = per layer
  * attn.o_proj.max_weight = 0.81
  * attn.o_proj.max_weight_position = 17.58
  * attn.o_proj.min_weight = 0.74
  * attn.o_proj.min_weight_distance = 2.57
  * mlp.down_proj.max_weight = 0.89
  * mlp.down_proj.max_weight_position = 19.48
  * mlp.down_proj.min_weight = 0.67
  * mlp.down_proj.min_weight_distance = 6.48
* Reloading model...
* Abliterating...
* Evaluating...
  * Obtaining first-token probability distributions...
  * KL divergence: 0.08
  * Counting model refusals...
  * Refusals: 70/100

Elapsed time: 1m 26s
Estimated remaining time: 34s

Running trial 6 of 7...
* Parameters:
  * direction_index = per layer
  * attn.o_proj.max_weight = 1.20
  * attn.o_proj.max_weight_position = 22.33
  * attn.o_proj.min_weight = 1.09
  * attn.o_proj.min_weight_distance = 5.04
  * mlp.down_proj.max_weight = 1.01
  * mlp.down_proj.max_weight_position = 19.15
  * mlp.down_proj.min_weight = 0.55
  * mlp.down_proj.min_weight_distance = 2.48
* Reloading model...
* Abliterating...
* Evaluating...
  * Obtaining first-token probability distributions...
  * KL divergence: 0.06
  * Counting model refusals...
  * Refusals: 57/100

Elapsed time: 1m 40s
Estimated remaining time: 17s

Running trial 7 of 7...
* Parameters:
  * direction_index = 14.51
  * attn.o_proj.max_weight = 0.98
  * attn.o_proj.max_weight_position = 19.42
  * attn.o_proj.min_weight = 0.71
  * attn.o_proj.min_weight_distance = 4.55
  * mlp.down_proj.max_weight = 1.16
  * mlp.down_proj.max_weight_position = 17.09
  * mlp.down_proj.min_weight = 1.01
  * mlp.down_proj.min_weight_distance = 9.05
* Reloading model...
* Abliterating...
* Evaluating...
  * Obtaining first-token probability distributions...
  * KL divergence: 0.14
  * Counting model refusals...
  * Refusals: 59/100

Elapsed time: 1m 55s

Optimization finished!

The following trials resulted in Pareto optimal combinations of refusals and KL 
divergence. After selecting a trial, you will be able to save the model, upload 
it to Hugging Face, or chat with it to test how well it works. You can return to
this menu later to select a different trial. Note that KL divergence values 
above 1 usually indicate significant damage to the original model's 
capabilities.


Which trial do you want to use?
[1] [Trial   1] Refusals: 50/100, KL divergence: 0.11
[2] [Trial   3] Refusals: 53/100, KL divergence: 0.09
[3] [Trial   4] Refusals: 56/100, KL divergence: 0.07
[4] [Trial   6] Refusals: 57/100, KL divergence: 0.06
[5] Continue optimization (run more trials)
[6] None (exit program)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/p-e-w/heretic/pull/69 **Author:** [@Vinay-Umrethe](https://github.com/Vinay-Umrethe) **Created:** 12/4/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `feature/continuous-optimization` --- ### 📝 Commits (6) - [`b9b6756`](https://github.com/p-e-w/heretic/commit/b9b675682935bd1a720758b5bfea3fb5a924b0d2) feat: add continuous optimization option - [`1d60e3c`](https://github.com/p-e-w/heretic/commit/1d60e3c2be2f699ac9922e47b38622464b674172) style: apply ruff formatting - [`a01658f`](https://github.com/p-e-w/heretic/commit/a01658f9466893d8d397e8b30d2f24e9069079ec) Merge branch 'p-e-w:master' into feature/continuous-optimization - [`6cba3f9`](https://github.com/p-e-w/heretic/commit/6cba3f931fdb28f710ce456f91dcfd57524df231) Merge branch 'p-e-w:master' into feature/continuous-optimization - [`3ad5890`](https://github.com/p-e-w/heretic/commit/3ad58902693a8e05843c2eaae5273a7dcd4fa499) Merge branch 'p-e-w:master' into feature/continuous-optimization - [`66a0f65`](https://github.com/p-e-w/heretic/commit/66a0f65bfdf3630cda19c0e6b31eb3f6715367b7) Merge branch 'master' into feature/continuous-optimization ### 📊 Changes **1 file changed** (+212 additions, -120 deletions) <details> <summary>View changed files</summary> 📝 `src/heretic/main.py` (+212 -120) </details> ### 📄 Description this PR introduces a **"Continuous Optimization"** feature that allows users to extend the ablation process by running additional trials if the **initial** results are **unsatisfactory**. The optimization and trial selection logic in `src/heretic/main.py` has been wrapped in a loop... After initial batch of trials completes, the user is presented with a normal menu with **[NEW] option**: ```bash "Continue optimization (run more trials)". ``` If selected, the user can specify the number of additional trials to run. The existing Optuna `study `object is reused, ensuring that the TPE sampler continues to learn from the history of previous trials and explores parameter space more effectively, rather than starting from scratchh... enabling a more efficient workflow for finding optimal ablation parameters when the default number of trials is insufficient. ### Test Conducted: (On Colab as my preference) ```bash Loading model Qwen/Qwen2.5-0.5B-Instruct... Ok * Transformer model with 24 layers * Abliterable components: * attn.o_proj: 1 matrices per layer * mlp.down_proj: 1 matrices per layer Loading good prompts from mlabonne/harmless_alpaca... * 400 prompts loaded Loading bad prompts from mlabonne/harmful_behaviors... * 400 prompts loaded Loading good evaluation prompts from mlabonne/harmless_alpaca... * 100 prompts loaded * Obtaining first-token probability distributions... Loading bad evaluation prompts from mlabonne/harmful_behaviors... * 100 prompts loaded * Counting model refusals... * Initial refusals: 83/100 Calculating per-layer refusal directions... * Obtaining residuals for good prompts... * Obtaining residuals for bad prompts... Running trial 1 of 3... * Parameters: * direction_index = per layer * attn.o_proj.max_weight = 1.42 * attn.o_proj.max_weight_position = 14.07 * attn.o_proj.min_weight = 0.88 * attn.o_proj.min_weight_distance = 9.21 * mlp.down_proj.max_weight = 0.94 * mlp.down_proj.max_weight_position = 17.43 * mlp.down_proj.min_weight = 0.87 * mlp.down_proj.min_weight_distance = 7.03 * Reloading model... * Abliterating... * Evaluating... * Obtaining first-token probability distributions... * KL divergence: 0.11 * Counting model refusals... * Refusals: 50/100 Elapsed time: 14s Estimated remaining time: 29s Running trial 2 of 3... * Parameters: * direction_index = per layer * attn.o_proj.max_weight = 0.80 * attn.o_proj.max_weight_position = 18.52 * attn.o_proj.min_weight = 0.48 * attn.o_proj.min_weight_distance = 7.39 * mlp.down_proj.max_weight = 1.34 * mlp.down_proj.max_weight_position = 21.15 * mlp.down_proj.min_weight = 1.12 * mlp.down_proj.min_weight_distance = 6.81 * Reloading model... * Abliterating... * Evaluating... * Obtaining first-token probability distributions... * KL divergence: 0.10 * Counting model refusals... * Refusals: 65/100 Elapsed time: 28s Estimated remaining time: 14s Running trial 3 of 3... * Parameters: * direction_index = per layer * attn.o_proj.max_weight = 1.41 * attn.o_proj.max_weight_position = 17.79 * attn.o_proj.min_weight = 0.68 * attn.o_proj.min_weight_distance = 11.15 * mlp.down_proj.max_weight = 0.92 * mlp.down_proj.max_weight_position = 17.44 * mlp.down_proj.min_weight = 0.32 * mlp.down_proj.min_weight_distance = 10.13 * Reloading model... * Abliterating... * Evaluating... * Obtaining first-token probability distributions... * KL divergence: 0.09 * Counting model refusals... * Refusals: 53/100 Elapsed time: 43s Optimization finished! The following trials resulted in Pareto optimal combinations of refusals and KL divergence. After selecting a trial, you will be able to save the model, upload it to Hugging Face, or chat with it to test how well it works. You can return to this menu later to select a different trial. Note that KL divergence values above 1 usually indicate significant damage to the original model's capabilities. Which trial do you want to use? [1] [Trial 1] Refusals: 50/100, KL divergence: 0.11 [2] [Trial 3] Refusals: 53/100, KL divergence: 0.09 [3] Continue optimization (run more trials) [4] None (exit program) Enter number: 3 How many more trials do you want to run?: 4 Running trial 4 of 7... * Parameters: * direction_index = per layer * attn.o_proj.max_weight = 1.29 * attn.o_proj.max_weight_position = 15.66 * attn.o_proj.min_weight = 0.51 * attn.o_proj.min_weight_distance = 9.52 * mlp.down_proj.max_weight = 0.83 * mlp.down_proj.max_weight_position = 20.43 * mlp.down_proj.min_weight = 0.08 * mlp.down_proj.min_weight_distance = 11.11 * Reloading model... * Abliterating... * Evaluating... * Obtaining first-token probability distributions... * KL divergence: 0.07 * Counting model refusals... * Refusals: 56/100 Elapsed time: 1m 11s Estimated remaining time: 53s Running trial 5 of 7... * Parameters: * direction_index = per layer * attn.o_proj.max_weight = 0.81 * attn.o_proj.max_weight_position = 17.58 * attn.o_proj.min_weight = 0.74 * attn.o_proj.min_weight_distance = 2.57 * mlp.down_proj.max_weight = 0.89 * mlp.down_proj.max_weight_position = 19.48 * mlp.down_proj.min_weight = 0.67 * mlp.down_proj.min_weight_distance = 6.48 * Reloading model... * Abliterating... * Evaluating... * Obtaining first-token probability distributions... * KL divergence: 0.08 * Counting model refusals... * Refusals: 70/100 Elapsed time: 1m 26s Estimated remaining time: 34s Running trial 6 of 7... * Parameters: * direction_index = per layer * attn.o_proj.max_weight = 1.20 * attn.o_proj.max_weight_position = 22.33 * attn.o_proj.min_weight = 1.09 * attn.o_proj.min_weight_distance = 5.04 * mlp.down_proj.max_weight = 1.01 * mlp.down_proj.max_weight_position = 19.15 * mlp.down_proj.min_weight = 0.55 * mlp.down_proj.min_weight_distance = 2.48 * Reloading model... * Abliterating... * Evaluating... * Obtaining first-token probability distributions... * KL divergence: 0.06 * Counting model refusals... * Refusals: 57/100 Elapsed time: 1m 40s Estimated remaining time: 17s Running trial 7 of 7... * Parameters: * direction_index = 14.51 * attn.o_proj.max_weight = 0.98 * attn.o_proj.max_weight_position = 19.42 * attn.o_proj.min_weight = 0.71 * attn.o_proj.min_weight_distance = 4.55 * mlp.down_proj.max_weight = 1.16 * mlp.down_proj.max_weight_position = 17.09 * mlp.down_proj.min_weight = 1.01 * mlp.down_proj.min_weight_distance = 9.05 * Reloading model... * Abliterating... * Evaluating... * Obtaining first-token probability distributions... * KL divergence: 0.14 * Counting model refusals... * Refusals: 59/100 Elapsed time: 1m 55s Optimization finished! The following trials resulted in Pareto optimal combinations of refusals and KL divergence. After selecting a trial, you will be able to save the model, upload it to Hugging Face, or chat with it to test how well it works. You can return to this menu later to select a different trial. Note that KL divergence values above 1 usually indicate significant damage to the original model's capabilities. Which trial do you want to use? [1] [Trial 1] Refusals: 50/100, KL divergence: 0.11 [2] [Trial 3] Refusals: 53/100, KL divergence: 0.09 [3] [Trial 4] Refusals: 56/100, KL divergence: 0.07 [4] [Trial 6] Refusals: 57/100, KL divergence: 0.06 [5] Continue optimization (run more trials) [6] None (exit program) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-01 04:19:20 +07:00
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#264
No description provided.