Project a unit refusal direction out of the residual-stream writes (attention, linear-attention and MLP outputs, and token embeddings) at runtime, enabled by VLLM_ABLITERATION pointing to a JSON or safetensors file with a refusal_direction vector. A bare name resolves to VLLM_ABLITERATION_DIR, default /opt/vllm/abliteration. For one direction this equals the weight edit W' = W - r(r^T W), so a base checkpoint reproduces its abliterated release with no extra weights. Unset, the path loads nothing and adds no work. Co-Authored-By: Hinata <hinata@idihore.id> Signed-off-by: MOVZX <movzx@yahoo.com>
123 lines
4.3 KiB
Python
123 lines
4.3 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
"""Runtime abliteration equals the weight edit W' = W - r (r^T W) on CPU."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
import vllm.envs as envs
|
|
from vllm.model_executor.models import abliteration
|
|
|
|
HIDDEN = 64
|
|
|
|
|
|
@pytest.fixture
|
|
def direction():
|
|
torch.manual_seed(0)
|
|
return torch.nn.functional.normalize(
|
|
torch.randn(HIDDEN, dtype=torch.float64), dim=0
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def direction_file(tmp_path, direction):
|
|
path = tmp_path / "direction.json"
|
|
path.write_text(
|
|
json.dumps({"hidden_size": HIDDEN, "refusal_direction": direction.tolist()})
|
|
)
|
|
return str(path)
|
|
|
|
|
|
def write_direction(tmp_path, values, name="d.json"):
|
|
path = tmp_path / name
|
|
path.write_text(json.dumps({"refusal_direction": values}))
|
|
return str(path)
|
|
|
|
|
|
def test_load_direction_normalizes_and_validates(tmp_path, direction, monkeypatch):
|
|
path = write_direction(tmp_path, (direction * 3).tolist())
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", path)
|
|
r = abliteration.load_direction(HIDDEN)
|
|
assert r.dtype == torch.float32
|
|
assert r.norm().item() == pytest.approx(1.0, abs=1e-6)
|
|
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", "")
|
|
assert abliteration.load_direction(HIDDEN) is None
|
|
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", path)
|
|
with pytest.raises(ValueError, match="hidden size"):
|
|
abliteration.load_direction(HIDDEN + 1)
|
|
|
|
zero = write_direction(tmp_path, [0.0] * HIDDEN, "zero.json")
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", zero)
|
|
with pytest.raises(ValueError, match="zero direction"):
|
|
abliteration.load_direction(HIDDEN)
|
|
|
|
|
|
def test_bare_name_resolves_to_the_builtin_dir(tmp_path, monkeypatch):
|
|
(tmp_path / "orcarouter.json").write_text(
|
|
json.dumps({"refusal_direction": [1.0] + [0.0] * (HIDDEN - 1)})
|
|
)
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", "orcarouter")
|
|
monkeypatch.setenv("VLLM_ABLITERATION_DIR", str(tmp_path))
|
|
r = abliteration.load_direction(HIDDEN)
|
|
assert r[0].item() == pytest.approx(1.0)
|
|
|
|
monkeypatch.delenv("VLLM_ABLITERATION_DIR")
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", "missing")
|
|
with pytest.raises(FileNotFoundError):
|
|
abliteration.load_direction(HIDDEN)
|
|
|
|
|
|
def test_project_matches_weight_edit(direction):
|
|
torch.manual_seed(1)
|
|
w = torch.randn(HIDDEN, 96, dtype=torch.float64)
|
|
x = torch.randn(7, 96, dtype=torch.float64)
|
|
edited = w - torch.outer(direction, direction @ w)
|
|
y = x @ w.T
|
|
abliteration.project_(y, direction)
|
|
torch.testing.assert_close(y, x @ edited.T, rtol=1e-9, atol=1e-9)
|
|
|
|
|
|
def test_project_handles_strided_input(direction):
|
|
torch.manual_seed(2)
|
|
y = torch.randn(7, 128, dtype=torch.float64)[:, :HIDDEN]
|
|
reference = y - torch.outer(y @ direction, direction)
|
|
abliteration.project_(y, direction)
|
|
torch.testing.assert_close(y, reference, rtol=1e-9, atol=1e-9)
|
|
|
|
|
|
def test_projection_is_idempotent(direction):
|
|
torch.manual_seed(3)
|
|
x = torch.randn(4, 3, HIDDEN, dtype=torch.float64)
|
|
abliteration.project_(x, direction)
|
|
assert (x.reshape(-1, HIDDEN) @ direction).abs().max().item() < 1e-12
|
|
once = x.clone()
|
|
abliteration.project_(x, direction)
|
|
torch.testing.assert_close(x, once, rtol=1e-12, atol=1e-12)
|
|
|
|
|
|
def test_projector_is_in_place_and_switchable(direction_file, monkeypatch):
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", direction_file)
|
|
x = torch.ones(2, HIDDEN, dtype=torch.float64)
|
|
out = abliteration.projector(HIDDEN)(x)
|
|
assert out.data_ptr() == x.data_ptr()
|
|
with open(direction_file) as f:
|
|
r = torch.tensor(json.load(f)["refusal_direction"], dtype=torch.float64)
|
|
assert (out @ r).abs().max().item() < 1e-6
|
|
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", "0")
|
|
y = torch.ones(2, HIDDEN, dtype=torch.float64)
|
|
assert abliteration.projector(HIDDEN)(y) is y
|
|
|
|
|
|
def test_projector_reads_env_once_per_call_site(direction_file, monkeypatch):
|
|
project = abliteration.projector(HIDDEN)
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", direction_file)
|
|
x = torch.ones(2, HIDDEN, dtype=torch.float64)
|
|
assert project(x).data_ptr() == x.data_ptr()
|
|
monkeypatch.setattr(envs, "VLLM_ABLITERATION", "")
|
|
assert project(x).data_ptr() == x.data_ptr()
|