Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/steerbench/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ def load_mmlu_slice(
cache miss. Slice size and seed are part of the cache key so different
configs coexist.
"""
if n < 1:
raise ValueError(f"n must be >= 1, got {n}")
path = _cache_path(cache_dir, f"mmlu_{n}_{seed}.json")
if path.exists():
rows = json.loads(path.read_text())
Expand Down Expand Up @@ -611,6 +613,8 @@ def load_gsm8k_slice(
Gold answers are normalised from the dataset's ``#### <n>`` format at load
time so scoring never re-parses them.
"""
if n < 1:
raise ValueError(f"n must be >= 1, got {n}")
path = _cache_path(cache_dir, f"gsm8k_{n}_{seed}.json")
if path.exists():
rows = json.loads(path.read_text())
Expand Down
19 changes: 19 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
extract_mmlu_answer,
formality_score,
format_mmlu_prompt,
load_gsm8k_slice,
load_mmlu_slice,
perplexity_from_token_nlls,
repetition_rate,
score_gsm8k,
Expand Down Expand Up @@ -220,6 +222,23 @@ def fake_generate(prompts: list[str]) -> list[str]:
assert evaluate_gsm8k(examples, fake_generate) == 1.0


# --------------------------------------------------------------------------- #
# SLICE LOADERS — n validation (guard fires before any cache/datasets access)
# --------------------------------------------------------------------------- #


@pytest.mark.parametrize("bad_n", [0, -3])
def test_load_mmlu_slice_rejects_n_below_1(bad_n: int) -> None:
with pytest.raises(ValueError):
load_mmlu_slice(n=bad_n)


@pytest.mark.parametrize("bad_n", [0, -3])
def test_load_gsm8k_slice_rejects_n_below_1(bad_n: int) -> None:
with pytest.raises(ValueError):
load_gsm8k_slice(n=bad_n)


def test_no_optional_deps_imported() -> None:
# Importing steerbench.metrics must not pull in the heavy optional deps.
# Checked in a fresh interpreter: a global sys.modules check is unreliable
Expand Down