pliny-the-prompter commited on
Commit
0fdea90
Β·
verified Β·
1 Parent(s): bb3695e

Upload 133 files

Browse files
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
- bayesian_trials = getattr(self, "_bayesian_trials", 0) or (
2988
- METHODS.get(self.method, {}).get("bayesian_trials", 0)
2989
- )
 
 
 
 
2990
  if bayesian_trials > 0 and self._strong_layers and self.handle:
2991
  self.log(f"Running Bayesian optimization ({bayesian_trials} trials)...")
2992
- from obliteratus.bayesian_optimizer import run_bayesian_optimization
2993
- bayesian_regs = run_bayesian_optimization(
2994
- self,
2995
- n_trials=bayesian_trials,
2996
- n_refusal_prompts=8,
2997
- n_kl_prompts=5,
2998
- )
2999
- if bayesian_regs:
3000
- self.log(
3001
- f" Bayesian optimization complete: "
3002
- f"optimized {len(bayesian_regs)} layer regularizations"
3003
  )
3004
- regs_str = ", ".join(
3005
- f"{idx}:{reg:.3f}" for idx, reg in sorted(bayesian_regs.items())
 
 
 
 
 
 
 
 
 
 
 
3006
  )
3007
- self.log(f" Optimal regs: {regs_str}")
 
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
- pipeline.refusal_directions[idx] = new_dir
 
 
 
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)