perf: empty cache after using residuals and between trials#15
Merged
p-e-w merged 1 commit intop-e-w:masterfrom Nov 17, 2025
Merged
perf: empty cache after using residuals and between trials#15p-e-w merged 1 commit intop-e-w:masterfrom
p-e-w merged 1 commit intop-e-w:masterfrom
Conversation
p-e-w
reviewed
Nov 17, 2025
src/heretic/main.py
Outdated
| score, kl_divergence, refusals = evaluator.get_score() | ||
|
|
||
| # free memory between trials | ||
| empty_cache() |
Owner
There was a problem hiding this comment.
This is done by reload_model() already, which happens at the start of each trial. I'd be very surprised if that line makes a difference because we're literally milliseconds away from calling that function anyway.
Contributor
Author
There was a problem hiding this comment.
oops you're right, i'll remove that line
Owner
There was a problem hiding this comment.
Can you retest and check if the performance gain is still there with just the residuals garbage collected?
Contributor
Author
There was a problem hiding this comment.
Memory usage without residual gc:
█░█░█▀▀░█▀▄░█▀▀░▀█▀░█░█▀▀ v1.0.1
█▀█░█▀▀░█▀▄░█▀▀░░█░░█░█░░
▀░▀░▀▀▀░▀░▀░▀▀▀░░▀░░▀░▀▀▀ https://github.com/p-e-w/heretic
GPU type: NVIDIA GeForce RTX 4060 Laptop GPU
Loading model Qwen/Qwen3-0.6B...
* Trying dtype auto... Ok
* Transformer model with 28 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: 52/100
Calculating per-layer refusal directions...
GPU memory before residuals: 1261976064 (peak so far: 3135785472)
* Obtaining residuals for good prompts...
* Obtaining residuals for bad prompts...
GPU memory after residuals: 1358445056 (peak so far: 3135785472)
GPU memory after clearing residuals: 1358563840 (peak so far: 3135785472)
With GC:
█░█░█▀▀░█▀▄░█▀▀░▀█▀░█░█▀▀ v1.0.1
█▀█░█▀▀░█▀▄░█▀▀░░█░░█░█░░
▀░▀░▀▀▀░▀░▀░▀▀▀░░▀░░▀░▀▀▀ https://github.com/p-e-w/heretic
GPU type: NVIDIA GeForce RTX 4060 Laptop GPU
Loading model Qwen/Qwen3-0.6B...
* Trying dtype auto... Ok
* Transformer model with 28 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: 52/100
Calculating per-layer refusal directions...
GPU memory before residuals: 1261976064 (peak so far: 3135785472)
* Obtaining residuals for good prompts...
* Obtaining residuals for bad prompts...
GPU memory after residuals: 1358445056 (peak so far: 3135785472)
GPU memory after clearing residuals: 1262094848 (peak so far: 3135785472)
So we save ~100MB by clearing the residuals but it doesn't reduce the peak usage, it would scale with number of prompts I think?
p-e-w
reviewed
Nov 17, 2025
e29e667 to
cec47df
Compare
Owner
|
Thanks, this is a reasonable change! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Great work btw, this is an awesome project and looking forward to seeing it grow, I'm still learning and this has been a fun repo to hack around in :)
I'm pretty sure we can free the residuals as soon as the refusal directions have been computed, and we can also clear the cache between trials.
Tested on my laptop 4060 while abliterating Qwen3-0.6B and peak GPU usage saw a slight decline