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

[PR #60] [MERGED] feat: Add 4-bit loading + LoRA support for low VRAM optimization #260

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

📋 Pull Request Information

Original PR: https://github.com/p-e-w/heretic/pull/60
Author: @accemlcc
Created: 12/1/2025
Status: ✅ Merged
Merged: 12/14/2025
Merged by: @p-e-w

Base: master ← Head: master


📝 Commits (10+)

  • c7a952d Add files via upload
  • 5e65ab1 perf: optimize abliteration matrix op (#46)
  • 3cacbae Fix line endings to LF
  • 8f1fafd Resolve merge conflict by keeping LoRA implementation
  • cc06d27 Add hybrid approach for GPT-OSS compatibility
  • c6bc76d Merge upstream/master - keep hybrid approach
  • 5657491 Fix projector bug, update print statement, revert README
  • 86b8852 Revert README changes to match upstream
  • 529a91a Fix import sorting for ruff
  • bdd3f57 Fix reload_model for evaluate_model, add type hints and validation

📊 Changes

7 files changed (+2387 additions, -1511 deletions)

View changed files

➕ .gitattributes (+1 -0)
📝 config.default.toml (+4 -0)
📝 pyproject.toml (+2 -0)
📝 src/heretic/config.py (+11 -0)
📝 src/heretic/main.py (+109 -13)
📝 src/heretic/model.py (+243 -54)
📝 uv.lock (+2017 -1444)

📄 Description

Hi! As discussed on Reddit, this PR implements a workflow to enable abliteration on consumer hardware with limited VRAM.

Changes:

  • Implements 4-bit loading via bitsandbytes.
  • Uses LoRA adapters instead of modifying full weights in-place.
  • Optimization is calculated on the frozen 4-bit model.
  • Allows saving the result as a small LoRA adapter.

Impact:

  • Drastically reduces VRAM usage.
  • As discussed, the refusal vector is dominant enough that finding it in 4-bit precision works perfectly fine when applied to the full model later.
  • Tested on Llama 3 70B with negligible Perplexity impact (4.19 -> 4.20).

Let me know if you need any changes!


PLEASE NOTE PEW's Technical Implementation Note:

Just for posterity, the "LoRA" approach in this implementation is a bit of a different beast from how LoRAs are normally used.

The idea behind a low-rank adaptation is to factor the adapter matrix (which is simply added to the module matrix) into a product of two low-rank matrices. This drastically decreases the number of trainable parameters, allowing for more efficient training.

But we don't do training here. In fact, we already pre-decide what the matrix product BA should be, from the abliteration parameters alone.

In principle, that makes PEFT overkill for this task, as we could simply use a basic module wrapper that applies the adapter in the forward pass. I've thought about this because I dislike unnecessary dependencies, but the reason PEFT still makes sense is that it is designed to work with Transformers, and saves us a lot of manual messing around with weights. Therefore, I believe that this PR is correct in using PEFT, despite the approach being somewhat unconventional for LoRA adapters.


🔄 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/60 **Author:** [@accemlcc](https://github.com/accemlcc) **Created:** 12/1/2025 **Status:** ✅ Merged **Merged:** 12/14/2025 **Merged by:** [@p-e-w](https://github.com/p-e-w) **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (10+) - [`c7a952d`](https://github.com/p-e-w/heretic/commit/c7a952de7bfee892e16c214388ab3e1093690a2b) Add files via upload - [`5e65ab1`](https://github.com/p-e-w/heretic/commit/5e65ab117999a4d11ed06898d3e9ab0bf128fbd9) perf: optimize abliteration matrix op (#46) - [`3cacbae`](https://github.com/p-e-w/heretic/commit/3cacbae28e8f15aaadb2ef1185bde8423d910044) Fix line endings to LF - [`8f1fafd`](https://github.com/p-e-w/heretic/commit/8f1fafd62f6db2125f3c7ea98e89979c115f2f0e) Resolve merge conflict by keeping LoRA implementation - [`cc06d27`](https://github.com/p-e-w/heretic/commit/cc06d27cdfb0f46c8806c78aa322fee97bc09d88) Add hybrid approach for GPT-OSS compatibility - [`c6bc76d`](https://github.com/p-e-w/heretic/commit/c6bc76dbb6427053a116d42cc747eefb549a21f9) Merge upstream/master - keep hybrid approach - [`5657491`](https://github.com/p-e-w/heretic/commit/56574911672758fb2416c415463a3f8a0638c403) Fix projector bug, update print statement, revert README - [`86b8852`](https://github.com/p-e-w/heretic/commit/86b88529c088ea04497b470c672cb2b76d2a72ce) Revert README changes to match upstream - [`529a91a`](https://github.com/p-e-w/heretic/commit/529a91ab9508abba7bbc47d05d24e3c4347a3828) Fix import sorting for ruff - [`bdd3f57`](https://github.com/p-e-w/heretic/commit/bdd3f57689146fe854e3a544f62b6883aebbb871) Fix reload_model for evaluate_model, add type hints and validation ### 📊 Changes **7 files changed** (+2387 additions, -1511 deletions) <details> <summary>View changed files</summary> ➕ `.gitattributes` (+1 -0) 📝 `config.default.toml` (+4 -0) 📝 `pyproject.toml` (+2 -0) 📝 `src/heretic/config.py` (+11 -0) 📝 `src/heretic/main.py` (+109 -13) 📝 `src/heretic/model.py` (+243 -54) 📝 `uv.lock` (+2017 -1444) </details> ### 📄 Description Hi! As discussed on Reddit, this PR implements a workflow to enable abliteration on consumer hardware with limited VRAM. **Changes:** - Implements 4-bit loading via `bitsandbytes`. - Uses LoRA adapters instead of modifying full weights in-place. - Optimization is calculated on the frozen 4-bit model. - Allows saving the result as a small LoRA adapter. **Impact:** - Drastically reduces VRAM usage. - As discussed, the refusal vector is dominant enough that finding it in 4-bit precision works perfectly fine when applied to the full model later. - Tested on Llama 3 70B with negligible Perplexity impact (4.19 -> 4.20). Let me know if you need any changes! --- **PLEASE NOTE PEW's Technical Implementation Note:** Just for posterity, the "LoRA" approach in this implementation is a bit of a different beast from how LoRAs are normally used. The idea behind a low-rank adaptation is to factor the adapter matrix (which is simply added to the module matrix) into a product of two low-rank matrices. This drastically decreases the number of trainable parameters, allowing for more efficient training. But we don't do training here. In fact, we already pre-decide what the matrix product BA should be, from the abliteration parameters alone. In principle, that makes PEFT overkill for this task, as we could simply use a basic module wrapper that applies the adapter in the forward pass. I've thought about this because I dislike unnecessary dependencies, but the reason PEFT still makes sense is that it is designed to work with Transformers, and saves us a lot of manual messing around with weights. Therefore, I believe that this PR is correct in using PEFT, despite the approach being somewhat unconventional for LoRA adapters. --- <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:18 +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#260
No description provided.