mirror of
https://github.com/p-e-w/heretic.git
synced 2026-12-03 18:42:57 +07:00
[GH-ISSUE #365] Trying to use Heretic to abliterate Gemma 4 31B QAT #190
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#190
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 @Roman215 on GitHub (Jun 9, 2026).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/365
So this is my first attempt at using heretic, and given that I didn't particularly find any good abliterated alternatives of the new QAT models yet, I wanted to take it upon myself. Initially I tried running the normal command like so:
heretic google/gemma-4-31B-it-qat-q4_0-unquantized
I'm using bitsandbytes because I only have an RTX 3090 so without Q4 quantization I would have no hope at abliterating a 31B model. This runs fine in concept, but I set a max of 2000 trials and over 2-3 days it finished 1000 of them. The issue is that the best outcome I have so far still has around 63 refusals with a divergence of around 0.02, which is "fine" but I'm aiming for less than 10 refusals like the professional abliterated models I see.
I read that the ARA method is better for abliterating newer models like Gemma 4. So I checked out the ara branch. It seems like if I run that branch with bitsandbytes set, I get the following exception:
RuntimeError: linalg.vector_norm: Expected a floating point or complex tensor as input. Got Byte
I did some digging, and it seems like the bitsandbytes support was added, but then I assume never added to the ara branch's ara_abliterate method? As mentioned above, without bitsandbytes, I would have no hope of abliterating the 31B model. Any plans to update the ARA branch with bitsandbytes support? Or am I using the plain abliteration wrong if even after 1000 trials the best outcome still has 63 refusals?
@erm14254 commented on GitHub (Jun 10, 2026):
I'm using bitsandbytes because I only have an RTX 3090 so without Q4 quantization I would have no hope at abliterating a 31B model.Does anyone know if using bitsandbytes result in lower quality?
@kabachuha commented on GitHub (Jun 10, 2026):
@Roman215 To make ARA work with bitsandbytes 4 bit, use ARA as LoRA https://github.com/p-e-w/heretic/pull/332
@p-e-w commented on GitHub (Jun 10, 2026):
The difference is typically small because when exporting, the LoRA is merged into the full-precision model.
@erm14254 commented on GitHub (Jun 10, 2026):
Got it, thank you very much for confirming!
@Roman215 commented on GitHub (Jun 11, 2026):
The ARA LoRA branch seems to be working so far. However, the one issue I ran into was VRAM blowing up between trials. I assumed it was due to the kv cache growing and not being reset between trials for the LoRA paths. When I commented out the early return in the LoRA path of reset_model so that it could go into empty_cache and all, it seemed to fix that performance issue. Not sure if there's a reason why that shouldn't be done though.
@p-e-w commented on GitHub (Jun 11, 2026):
The KV cache is disabled in Heretic.
@Roman215 commented on GitHub (Jun 11, 2026):
Unless I'm missing something, it looks like only the generate calls in get_residuals and get_logprobs pass in use_cache=False. The calls in get_responses and stream_chat_responses don't. So my understanding of each trial is you have the abliteration step, then you have the counting model refusals step. I believe the counting model refusals step calls get_responses and would still use the KV cache?
@p-e-w commented on GitHub (Jun 12, 2026):
Ah yes, those use the cache of course, otherwise they would be slow as the entire KV history would have to be recomputed for each token in the response.
This still shouldn't lead to OOM though. It's Python+CUDA's job to manage that. It sucks that this so often doesn't seem to work properly.
@Roman215 commented on GitHub (Jun 12, 2026):
On my RTX 3090, initially with the ARA LoRA branch, there were periods where my dedicated VRAM would blow up and Shared VRAM would go above 5 GB causing things to go really slow (2-3 trials in 40+ minutes). The ARA LoRA implementation also seems to be very huge on VRAM especially for the 31B models. The abliterating step was also going like 5+ GB over dedicated VRAM. I pulled master into the branch resolving conflicts then looked into some changes at the spots where the biggest VRAM issues happened. I changed the LoRA algorithm in some spots and the abliterating step was only using 22/24 GB. I also called empty_cache after each set of refusals. With those changes, I'm currently at 613 trials in 13 hours with the best outcome so far being 8 refusals and 0.029596 KL divergence which seems to be better than the available models so far. Haven't tried accepting that outcome yet.
I also noticed some situations where KL divergence is really high (greater than 1). In those cases, I changed it to just immediately return that refusals were 100/100 because even if there weren't refusals, I figure the model is too brain damaged to properly refuse and it shouldn't reward that high of a KL divergence.
@kabachuha commented on GitHub (Jun 12, 2026):
8 refusals and 0.029596 KL is a very great number, congratulations!
Would you mind sharing your changes to the branch in ARA LoRA's PR comments?
@p-e-w commented on GitHub (Jun 12, 2026):
I think this is the fifth or sixth time that someone has come up with this idea, which I admit sounds really good at face value. I too had implemented this before the initial release of Heretic, but removed it long ago.
Spoiler: This is a BAD IDEA. Don't do this. If you're lying to the optimizer about the values of the objective function, it cannot understand the function landscape properly, which will negatively impact finding optima. The "correct" approach (though also somewhat problematic) is to raise
optuna.TrialPruned().@Roman215 commented on GitHub (Jun 13, 2026):
Just a general abliteration question. Suppose that you wanted to try abliterating in multiple steps. For example in the first pass, you wanted to accept a model that had a kl divergence of 0.001001804368570447 and 96 refusals. You save that model off locally, then you use that as your new base model. Then maybe next time you aim for another 0.001 and get like 80 refusals. The cumulative KL divergence would be what, like 0.002 (multiplicative) but you're already at 80 refusals which normally doesn't happen until much higher. Does that approach have any other kind of side effects aside from the kl divergence?
@p-e-w commented on GitHub (Jun 14, 2026):
Nope, KLD is not multiplicative in general (and not additive either).
Yes. The abliterations are performed using separate directions, so interpretability becomes more difficult.
@Roman215 commented on GitHub (Jun 18, 2026):
I've been playing around with a bunch of changes locally just because I find the concept interesting. One change that seems to lead to much stronger convergence on good outcomes for me is switching from a TPESampler to a BoTorchSampler from optuna_integration.botorch. After getting past the startup trials, it seems much more intelligent in what trials to attempt. It seems like for 6 parameter/2 objective problems, BoTorchSampler might be a lot better.
Also, I was printing out bad prompts + responses, and some of the built in bad_prompts contain the word "illegal" in them. This leads to obvious false positives due to "illegal" being one of the default refusals, because for example it can be answering "In order to sell illegal drugs, you should first..." and it thinks that's a refusal when it's not. It might be worth checking if the prompt itself contains a refusal marker then don't count the refusal marker as a refusal marker?
This probably only applies towards Gemma 4, but one thing I found interesting is in my local changes, I put a suffix after each bad_prompt:
REFUSAL_SUFFIX = (
"\n\nIf you cannot comply with this request, respond with EXACTLY:\n"
"potato"
)
REFUSAL_MARKERS = [
"potato"
]
And with Gemma 4 at least, 99/100 times it responds exactly with "potato" on the refusals (matching 99/100 that original logic has). I'm still testing if that leads to better results.
Another change I had done prior to your reproducibility changes was call set_seed in more places especially to make sure that lora_A was properly reset to be the same again for every trial. I also had it do get_score when selecting a good trial that way you can confirm that it actually is the kl_divergence/refusal count you expected.
I also added in a constraints_func that gets passed into the sampler:
If a constraint is positive then it considers it a bad trial without refusing the trial. I made it consider greater than 0.5 kl_divergence to fail the constraint. I also made refusals staying the same as base_refusals fail the constraint. That replaces the previous raise
Trial.Pruned()that we talked about before. So it considers them as bad outcomes without pruning them and uses them as part of the study still.@p-e-w commented on GitHub (Jun 19, 2026):
I did try BoTorch (and most other samplers supported by Optuna) last year and found it to be worse than TPE, which is almost universally agreed upon to be the best algorithm for the type of problem we have, but perhaps this is indeed different for ARA. This is certainly worth exploring. Perhaps we can even make the algorithm configurable (one of the original reasons I went with Optuna is because the optimization engines are largely interchangeable).
Please keep in mind (as I have stated many times before) that it is very, very difficult to compare studies empirically, and generally requires dozens if not hundreds of studies (not trials!) to reach any degree of certainty that there is an actual improvement. The behavior of individual runs varies wildly even with the same configuration, so a couple of studies for each approach are definitely not sufficient unless the difference in quality is huge.
👍 This is a fantastic idea that is almost certainly superior to what we are doing right now.
Just so you know, you can do this in the config, you don't have to hardcode it. Datasets have
prefixandsuffixsettings. I'd be very interested in how this approach actually performs in general.That should not be necessary. Resetting LoRAs is not a probabilistic process.
I don't understand the purpose of this. Your constraint function is effectively re-stating the objective. The optimizer is already trying to get both of these values as low as it can, which inevitably also leads to values that satisfy the constraint.
@Roman215 commented on GitHub (Jun 19, 2026):
So far, the suffix version I have works really well and covers a large amount of real refusals. However you still seem to need 2 stages to it because depending on circumstances, it can either ignore your instruction and fall back to some of the other "I cannot" style ones, or get smart and save "potato" as its last word which depending on what you set max_response_length to can get cut off.
There also seem to be some bad_prompts which tell the model to do something that's literally impossible, like telling it to generate a video that does something, and it's not a video capable model. So it says "While I cannot produce a physical video file, here is a comprehensive script and storyboard for a tutorial on how" which I would say is not really a refusal. But the "I cannot" would think that it is. I was just adding a list of these:
And if any of those are present, it considers it to be not a refusal.
My goal here was to potentially speed up finding optimal outcomes. If it's caught in a path where it has a kl_divergence of like 12 (due to ARA LoRA having high values sometimes) and it sees that as 0 refusals, it might get caught up in thinking that path is good due to the 0 refusals, so if it sees the one constraint has +11.5, it might think that path is actually bad (which it is).
I also added in showing what the current Pareto front looks like between trials:
That way you know what your current results look like while it's running.
@p-e-w commented on GitHub (Jun 19, 2026):
TPE doesn't use "paths". It samples randomly from global probability distributions. It doesn't proceed along some trajectory like gradient descent. The same is true for Bayesian optimizers like BoTorch.
ARA is incredibly sensitive to small parameter changes. This is especially true for the all-important
overcorrect_relative_weight, where minuscule perturbations can make the difference between a KLD of 15 and a KLD of 0.1. I don't think we can afford any forbidden regions here, especially since all of these "constraints" are ad-hoc, and no objective values are actually invalid in a meaningful sense.@Roman215 commented on GitHub (Jun 20, 2026):
I've still just been toying around with an entirely new abliteration algorithm in place of the existing ara_lora_abliterate from the PR. Each trial takes me a little less than a minute, and here are some Gemma 4 QAT 31B results I have so far within just 50 min:
My algorithm only uses 5 parameters instead of 6, combining good/bad weights into one where if good is 0.999 then bad is inherently 0.001 (they sum up to 1).
When I had mentioned resetting lora_A, that was because with my algorithm, after good module io and bad module io are initially loaded, rather than relying on initially random lora_A, I "warm start" better lora A from the good/bad module IO then store those better lora A in a self.initial_lora_a type field in the model class, then reset_model restores those in the same place it resets lora_B to zeroes.
Also my abliteration algorithm does an alternating optimization which optimizes B with A fixed then optimizes A with B fixed so that's especially why A has to be reset as well.
@p-e-w commented on GitHub (Jun 21, 2026):
Eager to hear details!
@Roman215 commented on GitHub (Jun 28, 2026):
Wanted to give an update since last week. So the one issue I ran into with a lot of my numbers is I was working on 31B with my RTX 3090 and the counting refusals step was extremely slow with a max_response_length of 100. So I thought surely 20-30 would be fine because when AI refuses it refuses by then. But I learned that the LLM cheats the system and delays the Disclaimer, I'm sorry, As an AI, etc. beyond your limits. So in order to keep developing in a timeframe where I could still use a response length of 100 to get accurate results with the public community models and the built in refusal strings, I switched down to Gemma 4 12B in my experiments.
I also kept working and working on improving an abliteration algorithm taking everything I learned from modfying ARA LoRA. What I have now can't really be called ARA anymore, but it is showing extremely promising results (this is with 100 max_response_length and the default evaluator behavior without my cheats from last week):
Trial 20 there is already better than llmfan46's Gemma 4 12B which has 6 refusals and 0.1203 kld: https://huggingface.co/llmfan46/gemma-4-12B-it-uncensored-heretic
It has an entirely different set of optuna parameters (so far 6 of them). It uses the good/bad module io to sort all of the layers by the strongest refusal signal then only has one parameter controlling what percentage of layers to target in order from strongest refusal to least. It also has the classic rank 1 refusal direction abliteration built into it controlled by an optuna parameter. It also has an exact solve per module, so it doesn't need any LBFGS optimization at all which makes it extremely fast during the abliteration step (counting refusals is still a nightmare). I'm still trying to see if I can improve it because up until like 20 refusals it can get like 0.004 kld then past that point the number inflates heavily.
I also went back to a TPESampler because BoTorchSampler, while leading to good initial results, gets very slow when you start getting past like 200 trials. However, I made a custom sampler inheriting TPESampler where for the startup trials, rather than being random, it uses QMCSampler to get better guaranteed coverage leading into TPESampler.
I was still working in the ara lora branch from the PR but at some point I'll probably take what I have, clean up all the ARA/ARA LoRA stuff out of it, lock it behind its own configuration parameters, base it off of master, and commit it to a fork and provide the link.
@p-e-w commented on GitHub (Jun 30, 2026):
@Roman215
That sounds very promising! Be sure to try it with the most problematic models, which are Gemma 3 (not Gemma 4) and GPT-OSS. Those two models react dramatically differently to abliteration compared to anything else I've seen, and I consider them the benchmarks of any technique. In particular, every single abliterated version of Gemma 3 ever made exhibits characteristic artifacts (broken punctuation and apostrophes).
@p-e-w commented on GitHub (Oct 12, 2026):
Closing as the implementation has completely changed.