Skip to content

Commit 43f8e86

Browse files
committed
fix: minor cleanups and improvements
1 parent da92f74 commit 43f8e86

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/heretic/main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ def _is_help_invocation() -> bool:
1717
if _is_help_invocation():
1818
Settings() # ty:ignore[missing-argument]
1919

20+
# FIXME: Rich progress bars are currently disabled because of rendering issues
21+
# when used from multiple threads in parallel (e.g. by huggingface_hub).
22+
"""
2023
from .progress import patch_tqdm
2124
2225
# This patches tqdm class definitions, which must happen
2326
# before any other module imports tqdm.
2427
patch_tqdm()
28+
"""
2529

2630
import logging
2731
import math
@@ -420,9 +424,6 @@ def run():
420424

421425
needs_full_residuals = settings.print_residual_geometry or settings.plot_residuals
422426

423-
good_residuals = None
424-
bad_residuals = None
425-
426427
if needs_full_residuals:
427428
print("* Obtaining residuals for good prompts...")
428429
good_residuals = model.get_residuals_batched(good_prompts)
@@ -460,8 +461,12 @@ def run():
460461
refusal_directions - projection_vector.unsqueeze(1) * good_directions
461462
)
462463
refusal_directions = F.normalize(refusal_directions, p=2, dim=1)
464+
del good_directions, projection_vector
465+
466+
del good_means, bad_means
463467

464468
# Clear cache before starting the optimization study.
469+
# This should free up memory from the objects released with the del statements above.
465470
empty_cache()
466471

467472
trial_index = 0

src/heretic/model.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,15 @@ def __init__(self, settings: Settings):
154154
# so we don't need to do anything manually.
155155

156156
print(f"* Transformer model with [bold]{len(self.get_layers())}[/] layers")
157-
print("* Abliterable components:")
157+
158158
all_components = {}
159159
for layer_index in range(len(self.get_layers())):
160160
for component, modules in self.get_layer_modules(layer_index).items():
161161
if component not in all_components:
162162
all_components[component] = 0
163163
all_components[component] += len(modules)
164+
165+
print("* Abliterable components:")
164166
for component, count in all_components.items():
165167
print(f" * [bold]{component}[/]: [bold]{count}[/] modules total")
166168

@@ -368,8 +370,8 @@ def try_add(component: str, module: Any):
368370
with suppress(Exception):
369371
try_add("attn.o_proj", layer.self_attn.o_proj) # ty:ignore[possibly-missing-attribute]
370372

371-
# Qwen3.5 MoE hybrid layers use GatedDeltaNet (linear attention) instead
372-
# of standard self-attention, so self_attn.o_proj doesn't exist on those layers.
373+
# Qwen3.5 MoE hybrid layers use GatedDeltaNet (linear attention) instead of
374+
# standard self-attention, so self_attn.o_proj doesn't exist on those layers.
373375
with suppress(Exception):
374376
try_add("attn.o_proj", layer.linear_attn.out_proj) # ty:ignore[possibly-missing-attribute]
375377

@@ -403,11 +405,13 @@ def try_add(component: str, module: Any):
403405
return modules
404406

405407
def get_abliterable_components(self) -> list[str]:
408+
components: set[str] = set()
409+
406410
# Scan all layers because hybrid models (e.g. Qwen3.5 MoE) have different
407411
# components on different layers (some have self_attn, others linear_attn).
408-
components: set[str] = set()
409412
for layer_index in range(len(self.get_layers())):
410413
components.update(self.get_layer_modules(layer_index).keys())
414+
411415
return sorted(components)
412416

413417
def abliterate(
@@ -744,9 +748,8 @@ def get_logprobs(self, prompts: list[Prompt]) -> Tensor:
744748
# The returned tensor has shape (prompt, token).
745749
logprobs = F.log_softmax(logits, dim=-1)
746750

747-
del outputs
748-
749751
if self.settings.offload_outputs_to_cpu:
752+
del outputs, logits
750753
logprobs = logprobs.cpu()
751754
empty_cache()
752755

0 commit comments

Comments
 (0)