Spaces:
Running on Zero
Running on Zero
Upload 133 files
Browse files- obliteratus/abliterate.py +29 -17
- obliteratus/bayesian_optimizer.py +14 -1
obliteratus/abliterate.py
CHANGED
|
@@ -2984,27 +2984,39 @@ class AbliterationPipeline:
|
|
| 2984 |
# before the standard projection loop. The found values override the
|
| 2985 |
# static layer_adaptive_strength weights.
|
| 2986 |
bayesian_regs: dict[int, float] = {}
|
| 2987 |
-
|
| 2988 |
-
|
| 2989 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2990 |
if bayesian_trials > 0 and self._strong_layers and self.handle:
|
| 2991 |
self.log(f"Running Bayesian optimization ({bayesian_trials} trials)...")
|
| 2992 |
-
|
| 2993 |
-
|
| 2994 |
-
|
| 2995 |
-
|
| 2996 |
-
|
| 2997 |
-
|
| 2998 |
-
|
| 2999 |
-
if bayesian_regs:
|
| 3000 |
-
self.log(
|
| 3001 |
-
f" Bayesian optimization complete: "
|
| 3002 |
-
f"optimized {len(bayesian_regs)} layer regularizations"
|
| 3003 |
)
|
| 3004 |
-
|
| 3005 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3006 |
)
|
| 3007 |
-
|
|
|
|
| 3008 |
|
| 3009 |
# ββ LoRA-based reversible ablation ββββββββββββββββββββββββββββββ
|
| 3010 |
# When enabled, compute LoRA adapters and merge them instead of
|
|
|
|
| 2984 |
# before the standard projection loop. The found values override the
|
| 2985 |
# static layer_adaptive_strength weights.
|
| 2986 |
bayesian_regs: dict[int, float] = {}
|
| 2987 |
+
# Use the explicit user override if set (even 0 = disabled);
|
| 2988 |
+
# only fall back to the method default when unset (None).
|
| 2989 |
+
_user_bayesian = getattr(self, "_bayesian_trials", None)
|
| 2990 |
+
if _user_bayesian is not None:
|
| 2991 |
+
bayesian_trials = int(_user_bayesian)
|
| 2992 |
+
else:
|
| 2993 |
+
bayesian_trials = METHODS.get(self.method, {}).get("bayesian_trials", 0)
|
| 2994 |
if bayesian_trials > 0 and self._strong_layers and self.handle:
|
| 2995 |
self.log(f"Running Bayesian optimization ({bayesian_trials} trials)...")
|
| 2996 |
+
try:
|
| 2997 |
+
from obliteratus.bayesian_optimizer import run_bayesian_optimization
|
| 2998 |
+
bayesian_regs = run_bayesian_optimization(
|
| 2999 |
+
self,
|
| 3000 |
+
n_trials=bayesian_trials,
|
| 3001 |
+
n_refusal_prompts=8,
|
| 3002 |
+
n_kl_prompts=5,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3003 |
)
|
| 3004 |
+
if bayesian_regs:
|
| 3005 |
+
self.log(
|
| 3006 |
+
f" Bayesian optimization complete: "
|
| 3007 |
+
f"optimized {len(bayesian_regs)} layer regularizations"
|
| 3008 |
+
)
|
| 3009 |
+
regs_str = ", ".join(
|
| 3010 |
+
f"{idx}:{reg:.3f}" for idx, reg in sorted(bayesian_regs.items())
|
| 3011 |
+
)
|
| 3012 |
+
self.log(f" Optimal regs: {regs_str}")
|
| 3013 |
+
except Exception as e:
|
| 3014 |
+
self.log(
|
| 3015 |
+
f" Bayesian optimization failed (non-fatal): {e}\n"
|
| 3016 |
+
f" Continuing with standard projection..."
|
| 3017 |
)
|
| 3018 |
+
bayesian_regs = {}
|
| 3019 |
+
self._free_gpu_memory()
|
| 3020 |
|
| 3021 |
# ββ LoRA-based reversible ablation ββββββββββββββββββββββββββββββ
|
| 3022 |
# When enabled, compute LoRA adapters and merge them instead of
|
obliteratus/bayesian_optimizer.py
CHANGED
|
@@ -571,9 +571,22 @@ def run_bayesian_optimization(
|
|
| 571 |
best_dir_idx = p.get("dir_idx", 0.0)
|
| 572 |
if best_dir_idx > 0.1:
|
| 573 |
pipeline.log(f" Applying interpolated direction (idx={best_dir_idx:.2f})...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
for idx in pipeline._strong_layers:
|
| 575 |
new_dir = _interpolate_direction(pipeline, idx, best_dir_idx)
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
# Store component scales for use in EXCISE (backward compat)
|
| 579 |
pipeline._bayesian_attn_scale = p.get("attn_max_weight", 1.0)
|
|
|
|
| 571 |
best_dir_idx = p.get("dir_idx", 0.0)
|
| 572 |
if best_dir_idx > 0.1:
|
| 573 |
pipeline.log(f" Applying interpolated direction (idx={best_dir_idx:.2f})...")
|
| 574 |
+
# Snapshot all original directions BEFORE the loop to avoid
|
| 575 |
+
# order-dependent contamination (each layer's interpolation
|
| 576 |
+
# should use the original directions, not already-interpolated ones).
|
| 577 |
+
orig_directions = {
|
| 578 |
+
idx: pipeline.refusal_directions[idx].clone()
|
| 579 |
+
for idx in pipeline._strong_layers
|
| 580 |
+
if idx in pipeline.refusal_directions
|
| 581 |
+
}
|
| 582 |
+
saved_refusal_dirs = pipeline.refusal_directions
|
| 583 |
+
pipeline.refusal_directions = orig_directions
|
| 584 |
for idx in pipeline._strong_layers:
|
| 585 |
new_dir = _interpolate_direction(pipeline, idx, best_dir_idx)
|
| 586 |
+
saved_refusal_dirs[idx] = new_dir
|
| 587 |
+
# Sync refusal_subspaces so EXCISE uses the optimized direction
|
| 588 |
+
pipeline.refusal_subspaces[idx] = new_dir.unsqueeze(0)
|
| 589 |
+
pipeline.refusal_directions = saved_refusal_dirs
|
| 590 |
|
| 591 |
# Store component scales for use in EXCISE (backward compat)
|
| 592 |
pipeline._bayesian_attn_scale = p.get("attn_max_weight", 1.0)
|