From 5ca35aef3f897c4da0c657cd503633be36d09ea5 Mon Sep 17 00:00:00 2001 From: dipakchaudhari12717 Date: Mon, 20 Jul 2026 20:16:50 +0530 Subject: [PATCH 1/2] fix(vectors): clear ValueError for an out-of-range layer in normalize_alpha A layer missing from the steering vector raised a bare KeyError with no hint of which layers exist. Guard in normalize_alpha (dose() reaches it through the same path) and name both the bad layer and the available ones. Fixes #20 --- src/steerbench/vectors.py | 5 +++++ tests/test_vectors.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/steerbench/vectors.py b/src/steerbench/vectors.py index a57e5c0..6116cd5 100644 --- a/src/steerbench/vectors.py +++ b/src/steerbench/vectors.py @@ -123,6 +123,11 @@ def normalize_alpha( Returns all three units. See :func:`dose` for the scalar full dose. """ + if layer not in vector.directions: + raise ValueError( + f"layer {layer} not in steering vector; " + f"available layers: {sorted(vector.directions)}" + ) norm = float(torch.linalg.vector_norm(vector.directions[layer])) by_vector = alpha * norm by_residual = by_vector / residual_norm if residual_norm is not None else None diff --git a/tests/test_vectors.py b/tests/test_vectors.py index d094bb7..f490e5f 100644 --- a/tests/test_vectors.py +++ b/tests/test_vectors.py @@ -212,3 +212,25 @@ def test_gguf_superset_loads_in_repeng(tmp_path: Path) -> None: assert hint == "gpt2" names = {t.name for t in reader.tensors} assert names == {"direction.5", "direction.6"} + + +def test_normalize_alpha_out_of_range_layer_names_available() -> None: + vec = SteeringVector( + directions={3: torch.tensor([1.0, 0.0]), 7: torch.tensor([0.0, 2.0])} + ) + with pytest.raises(ValueError, match=r"layer 999 not in steering vector.*\[3, 7\]"): + normalize_alpha(vec, layer=999, alpha=1.0) + + +def test_dose_out_of_range_layer_names_available() -> None: + vec = SteeringVector( + directions={3: torch.tensor([1.0, 0.0]), 7: torch.tensor([0.0, 2.0])} + ) + with pytest.raises(ValueError, match=r"layer 999 not in steering vector.*\[3, 7\]"): + dose(vec, layer=999, alpha=1.0, residual_norm=10.0) + + +def test_normalize_alpha_valid_layer_unchanged_by_guard() -> None: + vec = SteeringVector(directions={3: torch.tensor([3.0, 4.0])}) # norm 5 + got = normalize_alpha(vec, layer=3, alpha=2.0) + assert got.by_vector_norm == pytest.approx(10.0) From 2b5aaaa55d8e28fe8f51867b965064c9b081e728 Mon Sep 17 00:00:00 2001 From: Bamdad Dashtban Date: Mon, 20 Jul 2026 16:27:32 +0100 Subject: [PATCH 2/2] Format vectors.py and test_vectors.py with ruff --- src/steerbench/vectors.py | 3 +-- tests/test_vectors.py | 8 ++------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/steerbench/vectors.py b/src/steerbench/vectors.py index 6116cd5..b29c1ed 100644 --- a/src/steerbench/vectors.py +++ b/src/steerbench/vectors.py @@ -125,8 +125,7 @@ def normalize_alpha( """ if layer not in vector.directions: raise ValueError( - f"layer {layer} not in steering vector; " - f"available layers: {sorted(vector.directions)}" + f"layer {layer} not in steering vector; available layers: {sorted(vector.directions)}" ) norm = float(torch.linalg.vector_norm(vector.directions[layer])) by_vector = alpha * norm diff --git a/tests/test_vectors.py b/tests/test_vectors.py index f490e5f..816f240 100644 --- a/tests/test_vectors.py +++ b/tests/test_vectors.py @@ -215,17 +215,13 @@ def test_gguf_superset_loads_in_repeng(tmp_path: Path) -> None: def test_normalize_alpha_out_of_range_layer_names_available() -> None: - vec = SteeringVector( - directions={3: torch.tensor([1.0, 0.0]), 7: torch.tensor([0.0, 2.0])} - ) + vec = SteeringVector(directions={3: torch.tensor([1.0, 0.0]), 7: torch.tensor([0.0, 2.0])}) with pytest.raises(ValueError, match=r"layer 999 not in steering vector.*\[3, 7\]"): normalize_alpha(vec, layer=999, alpha=1.0) def test_dose_out_of_range_layer_names_available() -> None: - vec = SteeringVector( - directions={3: torch.tensor([1.0, 0.0]), 7: torch.tensor([0.0, 2.0])} - ) + vec = SteeringVector(directions={3: torch.tensor([1.0, 0.0]), 7: torch.tensor([0.0, 2.0])}) with pytest.raises(ValueError, match=r"layer 999 not in steering vector.*\[3, 7\]"): dose(vec, layer=999, alpha=1.0, residual_norm=10.0)