mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Section B of #675: skip left-padding position_ids for models that number positions from padding_idx+1 (RoBERTa/XLM-R/CamemBERT-style); restore modes via train(mode) for modules that override it; log the position_ids fallback warning once per model; STUDY_PRESETS is a read-only mapping returning copies; README explains metrics null vs omitted with a preset. Refs #675
685 lines
24 KiB
Python
685 lines
24 KiB
Python
"""Focused regression tests for causal-LM evaluation and report output."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import math
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock, Mock
|
|
|
|
import pytest
|
|
import torch
|
|
from torch import nn
|
|
|
|
from obliteratus.evaluation.evaluator import Evaluator
|
|
|
|
|
|
class _MapDataset:
|
|
def __init__(self, texts: list[object]):
|
|
self.texts = texts
|
|
self.read_indices: list[int] = []
|
|
self.selected_indices: list[int] | None = None
|
|
|
|
def __len__(self):
|
|
return len(self.texts)
|
|
|
|
def __getitem__(self, key):
|
|
if isinstance(key, str):
|
|
raise TypeError("the evaluator must not materialize a full dataset column")
|
|
if isinstance(key, slice):
|
|
return {"text": self.texts[key]}
|
|
self.read_indices.append(key)
|
|
return {"text": self.texts[key]}
|
|
|
|
def select(self, indices):
|
|
self.selected_indices = list(indices)
|
|
return _MapDataset([self.texts[index] for index in self.selected_indices])
|
|
|
|
|
|
class _ChangingDataset(_MapDataset):
|
|
"""Simulate a transformed dataset changing after index selection."""
|
|
|
|
def select(self, indices):
|
|
self.selected_indices = list(indices)
|
|
return _MapDataset([""])
|
|
|
|
|
|
class _Encoding(dict):
|
|
def to(self, _device):
|
|
return self
|
|
|
|
|
|
class _ScriptedTokenizer:
|
|
def __init__(self, encodings: list[_Encoding]):
|
|
self.encodings = list(encodings)
|
|
self.calls: list[list[str]] = []
|
|
|
|
def __call__(self, texts, **_kwargs):
|
|
self.calls.append(list(texts))
|
|
return self.encodings.pop(0)
|
|
|
|
|
|
class _LossModel(nn.Module):
|
|
def __init__(self, losses: list[float | None]):
|
|
super().__init__()
|
|
self.anchor = nn.Parameter(torch.zeros(()))
|
|
self.losses = list(losses)
|
|
self.labels: list[torch.Tensor] = []
|
|
|
|
def forward(self, *, input_ids, attention_mask, labels):
|
|
del input_ids, attention_mask
|
|
self.labels.append(labels.detach().clone())
|
|
loss = self.losses.pop(0)
|
|
return SimpleNamespace(loss=None if loss is None else torch.tensor(loss))
|
|
|
|
|
|
def _encoding(input_ids, attention_mask) -> _Encoding:
|
|
return _Encoding(
|
|
input_ids=torch.as_tensor(input_ids, dtype=torch.long),
|
|
attention_mask=torch.as_tensor(attention_mask, dtype=torch.long),
|
|
)
|
|
|
|
|
|
def _evaluator(
|
|
texts: list[object],
|
|
encodings: list[_Encoding],
|
|
losses: list[float | None],
|
|
*,
|
|
batch_size: int = 8,
|
|
max_samples: int | None = None,
|
|
):
|
|
dataset = _MapDataset(texts)
|
|
tokenizer = _ScriptedTokenizer(encodings)
|
|
model = _LossModel(losses)
|
|
handle = SimpleNamespace(model=model, tokenizer=tokenizer, task="causal_lm")
|
|
evaluator = Evaluator(
|
|
handle=handle,
|
|
dataset=dataset,
|
|
batch_size=batch_size,
|
|
max_samples=max_samples,
|
|
)
|
|
return evaluator, dataset, tokenizer, model
|
|
|
|
|
|
def test_causal_lm_skips_blank_rows_bounds_scan_and_masks_padding():
|
|
evaluator, dataset, tokenizer, model = _evaluator(
|
|
["", " \t", "alpha", "beta", "must not be read"],
|
|
[_encoding([[1, 2, 0], [3, 4, 5]], [[1, 1, 0], [1, 1, 1]])],
|
|
[math.log(4.0)],
|
|
batch_size=2,
|
|
max_samples=2,
|
|
)
|
|
|
|
result = evaluator.evaluate()
|
|
|
|
assert result["perplexity"] == pytest.approx(4.0)
|
|
assert dataset.read_indices == [0, 1, 2, 3]
|
|
assert dataset.selected_indices == [2, 3]
|
|
assert tokenizer.calls == [["alpha", "beta"]]
|
|
assert model.labels[0].tolist() == [[1, 2, -100], [3, 4, 5]]
|
|
|
|
|
|
def test_causal_lm_rejects_all_empty_dataset():
|
|
evaluator, dataset, tokenizer, model = _evaluator(["", " \n", None], [], [])
|
|
|
|
with pytest.raises(ValueError, match="no non-empty text"):
|
|
evaluator.evaluate()
|
|
|
|
assert dataset.read_indices == [0, 1, 2]
|
|
assert tokenizer.calls == []
|
|
assert model.labels == []
|
|
|
|
|
|
@pytest.mark.parametrize("max_samples", [0, -1])
|
|
def test_causal_lm_requires_positive_max_samples(max_samples):
|
|
evaluator, dataset, _tokenizer, _model = _evaluator(
|
|
["alpha"],
|
|
[],
|
|
[],
|
|
max_samples=max_samples,
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="max_samples must be a positive integer"):
|
|
evaluator.evaluate()
|
|
|
|
assert dataset.read_indices == []
|
|
|
|
|
|
def test_causal_lm_rejects_tokenless_and_one_token_sequences():
|
|
evaluator, _dataset, _tokenizer, model = _evaluator(
|
|
["tokenless", "single"],
|
|
[
|
|
_encoding(torch.empty((1, 0)), torch.empty((1, 0))),
|
|
_encoding([[1]], [[1]]),
|
|
],
|
|
[],
|
|
batch_size=1,
|
|
)
|
|
|
|
with pytest.raises(RuntimeError, match="zero valid prediction tokens"):
|
|
evaluator.evaluate()
|
|
|
|
assert model.labels == []
|
|
|
|
|
|
def test_causal_lm_skips_batch_emptied_by_dataset_transform():
|
|
dataset = _ChangingDataset(["alpha"])
|
|
tokenizer = _ScriptedTokenizer([])
|
|
model = _LossModel([])
|
|
handle = SimpleNamespace(model=model, tokenizer=tokenizer, task="causal_lm")
|
|
|
|
with pytest.raises(RuntimeError, match="zero valid prediction tokens"):
|
|
Evaluator(handle=handle, dataset=dataset).evaluate()
|
|
|
|
assert tokenizer.calls == []
|
|
assert model.labels == []
|
|
|
|
|
|
def test_causal_lm_skips_zero_target_batch_then_reports_valid_perplexity():
|
|
evaluator, _dataset, _tokenizer, model = _evaluator(
|
|
["padded target", "valid target"],
|
|
[
|
|
_encoding([[1, 0]], [[1, 0]]),
|
|
_encoding([[2, 3]], [[1, 1]]),
|
|
],
|
|
[math.log(3.0)],
|
|
batch_size=1,
|
|
)
|
|
|
|
result = evaluator.evaluate()
|
|
|
|
assert result["perplexity"] == pytest.approx(3.0)
|
|
assert len(model.labels) == 1
|
|
|
|
|
|
def test_causal_lm_rejects_missing_loss():
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["alpha"],
|
|
[_encoding([[1, 2]], [[1, 1]])],
|
|
[None],
|
|
)
|
|
|
|
with pytest.raises(RuntimeError, match="returned no loss"):
|
|
evaluator.evaluate()
|
|
|
|
|
|
@pytest.mark.parametrize("loss", [float("nan"), float("inf"), float("-inf")])
|
|
def test_causal_lm_rejects_non_finite_loss(loss):
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["alpha"],
|
|
[_encoding([[1, 2]], [[1, 1]])],
|
|
[loss],
|
|
)
|
|
|
|
with pytest.raises(RuntimeError, match="Non-finite evaluation loss"):
|
|
evaluator.evaluate()
|
|
|
|
|
|
def test_causal_lm_perplexity_is_weighted_by_prediction_tokens():
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["three tokens", "two tokens"],
|
|
[
|
|
_encoding([[1, 2, 3]], [[1, 1, 1]]),
|
|
_encoding([[4, 5]], [[1, 1]]),
|
|
],
|
|
[math.log(2.0), math.log(8.0)],
|
|
batch_size=1,
|
|
)
|
|
|
|
result = evaluator.evaluate()
|
|
|
|
expected = math.exp((2 * math.log(2.0) + math.log(8.0)) / 3)
|
|
assert result["perplexity"] == pytest.approx(expected)
|
|
|
|
|
|
def test_causal_lm_left_padding_does_not_score_first_real_token():
|
|
"""#609: a target whose preceding position is padding is not a prediction."""
|
|
evaluator, _dataset, _tokenizer, model = _evaluator(
|
|
["short", "long", "tail"],
|
|
[
|
|
_encoding([[0, 0, 1, 2], [3, 4, 5, 6]], [[0, 0, 1, 1], [1, 1, 1, 1]]),
|
|
_encoding([[7, 8]], [[1, 1]]),
|
|
],
|
|
[math.log(2.0), math.log(8.0)],
|
|
batch_size=2,
|
|
)
|
|
|
|
result = evaluator.evaluate()
|
|
|
|
assert model.labels[0][:, 1:].tolist() == [[-100, -100, 2], [4, 5, 6]]
|
|
# One target from the short row and three from the long row, then one.
|
|
expected = math.exp((4 * math.log(2.0) + math.log(8.0)) / 5)
|
|
assert result["perplexity"] == pytest.approx(expected)
|
|
|
|
|
|
def _tiny_causal_lm_handle(padding_side: str):
|
|
from transformers import GPT2Config, GPT2LMHeadModel
|
|
|
|
from tests.fixtures.tiny_offline_model import (
|
|
FIXTURE_SEED,
|
|
FIXTURE_VOCAB,
|
|
_fixture_tokenizer,
|
|
)
|
|
|
|
torch.manual_seed(FIXTURE_SEED)
|
|
config = GPT2Config(
|
|
vocab_size=len(FIXTURE_VOCAB), n_positions=32, n_embd=16, n_layer=1,
|
|
n_head=2, n_inner=32, bos_token_id=1, eos_token_id=1, pad_token_id=0,
|
|
)
|
|
model = GPT2LMHeadModel(config).eval()
|
|
tokenizer = _fixture_tokenizer()
|
|
tokenizer.padding_side = padding_side
|
|
return SimpleNamespace(model=model, tokenizer=tokenizer, task="causal_lm")
|
|
|
|
|
|
def _tiny_perplexity(padding_side: str, batch_size: int) -> float:
|
|
from datasets import Dataset
|
|
|
|
dataset = Dataset.from_dict({"text": ["hello world", "hello world safe test answer"]})
|
|
evaluator = Evaluator(
|
|
handle=_tiny_causal_lm_handle(padding_side),
|
|
dataset=dataset,
|
|
metrics=["perplexity"],
|
|
batch_size=batch_size,
|
|
max_length=12,
|
|
)
|
|
return evaluator.evaluate()["perplexity"]
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_causal_lm_perplexity_is_independent_of_padding_and_batch_size():
|
|
"""#609: batched left/right padding must match unbatched evaluation."""
|
|
reference = _tiny_perplexity("left", batch_size=1)
|
|
|
|
assert _tiny_perplexity("right", batch_size=1) == pytest.approx(reference, rel=1e-6)
|
|
assert _tiny_perplexity("right", batch_size=2) == pytest.approx(reference, rel=1e-5)
|
|
assert _tiny_perplexity("left", batch_size=2) == pytest.approx(reference, rel=1e-5)
|
|
|
|
|
|
class _KwargsOnlyWrapper(nn.Module):
|
|
"""A wrapper whose ``forward`` names no parameters, only ``**kwargs``."""
|
|
|
|
def __init__(self, inner: nn.Module):
|
|
super().__init__()
|
|
self.inner = inner
|
|
self.calls: list[tuple[dict, torch.Tensor]] = []
|
|
|
|
def forward(self, **kwargs):
|
|
outputs = self.inner(**kwargs)
|
|
self.calls.append((kwargs, outputs.logits.detach().clone()))
|
|
return outputs
|
|
|
|
|
|
def _real_token_logits(padding_side: str, inner: nn.Module | None = None):
|
|
from datasets import Dataset
|
|
|
|
handle = _tiny_causal_lm_handle(padding_side)
|
|
model = _KwargsOnlyWrapper(inner or handle.model)
|
|
handle.model = model
|
|
dataset = Dataset.from_dict({"text": ["hello world", "hello world safe test answer"]})
|
|
result = Evaluator(
|
|
handle=handle, dataset=dataset, metrics=["perplexity"], batch_size=2, max_length=12,
|
|
).evaluate()
|
|
(kwargs, logits), = model.calls
|
|
mask = kwargs["attention_mask"].bool()
|
|
return result["perplexity"], [logits[row][mask[row]] for row in range(len(mask))], kwargs
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_left_padding_positions_reach_kwargs_only_wrapper():
|
|
"""#634: a ``**kwargs`` wrapper around an absolute-position model gets positions."""
|
|
right_ppl, right_logits, _ = _real_token_logits("right")
|
|
left_ppl, left_logits, left_kwargs = _real_token_logits("left")
|
|
|
|
assert "position_ids" in left_kwargs
|
|
for left, right in zip(left_logits, right_logits):
|
|
assert torch.allclose(left, right, atol=1e-5)
|
|
assert left_ppl == pytest.approx(right_ppl, rel=1e-5)
|
|
|
|
|
|
class _NoPositionsModel(nn.Module):
|
|
"""An inner model that rejects ``position_ids`` like a strict signature would."""
|
|
|
|
def __init__(self, inner: nn.Module):
|
|
super().__init__()
|
|
self.inner = inner
|
|
|
|
def forward(self, *, input_ids, attention_mask, labels):
|
|
return self.inner(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_kwargs_wrapper_falls_back_when_inner_model_rejects_position_ids():
|
|
"""#634: an inner model without ``position_ids`` still evaluates."""
|
|
inner = _NoPositionsModel(_tiny_causal_lm_handle("left").model)
|
|
ppl, _logits, kwargs = _real_token_logits("left", inner=inner)
|
|
|
|
assert "position_ids" not in kwargs
|
|
assert math.isfinite(ppl)
|
|
|
|
|
|
def test_kwargs_wrapper_does_not_swallow_unrelated_type_errors():
|
|
class _Broken(nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.anchor = nn.Parameter(torch.zeros(()))
|
|
|
|
def forward(self, **kwargs):
|
|
raise TypeError("unsupported operand type(s)")
|
|
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["short", "long"],
|
|
[_encoding([[0, 0, 1, 2], [3, 4, 5, 6]], [[0, 0, 1, 1], [1, 1, 1, 1]])],
|
|
[math.log(2.0)],
|
|
batch_size=2,
|
|
)
|
|
evaluator.handle.model = _Broken()
|
|
|
|
with pytest.raises(TypeError, match="unsupported operand"):
|
|
evaluator.evaluate()
|
|
|
|
|
|
def test_explicit_empty_metrics_list_is_rejected():
|
|
"""#633: ``metrics=[]`` is an error, not a request for the task defaults."""
|
|
for task in ("causal_lm", "classification"):
|
|
handle = SimpleNamespace(model=None, tokenizer=None, task=task)
|
|
with pytest.raises(ValueError, match="metrics"):
|
|
Evaluator(handle, _MapDataset(["text"]), metrics=[])
|
|
|
|
|
|
def test_omitted_metrics_use_task_defaults():
|
|
"""#633: ``metrics=None`` keeps meaning the task defaults."""
|
|
causal = SimpleNamespace(model=None, tokenizer=None, task="causal_lm")
|
|
classifier = SimpleNamespace(model=None, tokenizer=None, task="classification")
|
|
|
|
assert Evaluator(causal, _MapDataset([])).metrics == ["perplexity"]
|
|
assert Evaluator(causal, _MapDataset([]), metrics=None).metrics == ["perplexity"]
|
|
assert Evaluator(classifier, _MapDataset([]), metrics=None).metrics == ["accuracy", "f1"]
|
|
|
|
|
|
def test_report_creates_nested_output_directory(monkeypatch, tmp_path):
|
|
import obliteratus.reporting.report
|
|
from obliteratus import cli
|
|
|
|
results_path = tmp_path / "results.json"
|
|
results_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"model_name": "fixture",
|
|
"baseline_metrics": {"perplexity": 4.0},
|
|
"results": [],
|
|
}
|
|
)
|
|
)
|
|
report = MagicMock()
|
|
monkeypatch.setattr(
|
|
obliteratus.reporting.report,
|
|
"AblationReport",
|
|
Mock(return_value=report),
|
|
)
|
|
output_dir = tmp_path / "nested" / "plots"
|
|
|
|
cli._cmd_report(
|
|
SimpleNamespace(results_json=str(results_path), output_dir=str(output_dir))
|
|
)
|
|
|
|
assert output_dir.is_dir()
|
|
report.save_plots.assert_called_once_with(output_dir)
|
|
|
|
|
|
class _AttemptCountingWrapper(nn.Module):
|
|
"""A ``**kwargs`` model whose inner call rejects ``position_ids`` with ``message``."""
|
|
|
|
def __init__(self, message: str):
|
|
super().__init__()
|
|
self.anchor = nn.Parameter(torch.zeros(()))
|
|
self.message = message
|
|
self.attempts: list[bool] = []
|
|
|
|
def forward(self, **kwargs):
|
|
self.attempts.append("position_ids" in kwargs)
|
|
if "position_ids" in kwargs:
|
|
raise TypeError(self.message)
|
|
return SimpleNamespace(loss=torch.tensor(math.log(2.0)))
|
|
|
|
|
|
def _two_left_padded_batches(model: nn.Module):
|
|
left_padded = _encoding([[0, 0, 1, 2], [3, 4, 5, 6]], [[0, 0, 1, 1], [1, 1, 1, 1]])
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["a", "bb", "c", "dd"],
|
|
[left_padded, _encoding(left_padded["input_ids"], left_padded["attention_mask"])],
|
|
[],
|
|
batch_size=2,
|
|
)
|
|
evaluator.handle.model = model
|
|
return evaluator
|
|
|
|
|
|
_CPYTHON_REJECTION = "forward() got an unexpected keyword argument 'position_ids'"
|
|
|
|
|
|
def test_position_ids_fallback_retries_once_across_batches():
|
|
"""#666: after one rejection, later batches are called once, without positions."""
|
|
model = _AttemptCountingWrapper(_CPYTHON_REJECTION)
|
|
|
|
result = _two_left_padded_batches(model).evaluate()
|
|
|
|
assert result["perplexity"] == pytest.approx(2.0)
|
|
# Batch 1: rejected attempt with positions, then the retry. Batch 2: one call.
|
|
assert model.attempts == [True, False, False]
|
|
|
|
|
|
def test_position_ids_fallback_logs_a_warning(caplog):
|
|
"""#666: falling back to no positions is logged, never silent."""
|
|
model = _AttemptCountingWrapper(_CPYTHON_REJECTION)
|
|
|
|
with caplog.at_level("WARNING", logger="obliteratus.evaluation.evaluator"):
|
|
_two_left_padded_batches(model).evaluate()
|
|
|
|
warnings = [r for r in caplog.records if r.levelname == "WARNING"]
|
|
assert len(warnings) == 1
|
|
assert "position_ids" in warnings[0].getMessage()
|
|
assert _CPYTHON_REJECTION in warnings[0].getMessage()
|
|
|
|
|
|
@pytest.mark.parametrize("message", [
|
|
"Wrapper.forward() got an unexpected keyword argument 'position_ids'",
|
|
"'position_ids' is an invalid keyword argument for forward()",
|
|
])
|
|
def test_position_ids_fallback_matches_other_rejection_wordings(message):
|
|
"""#666: rejection wordings other than CPython's plain-function one fall back too."""
|
|
model = _AttemptCountingWrapper(message)
|
|
|
|
assert math.isfinite(_two_left_padded_batches(model).evaluate()["perplexity"])
|
|
assert model.attempts == [True, False, False]
|
|
|
|
|
|
def test_position_ids_type_errors_that_are_not_rejections_still_raise():
|
|
model = _AttemptCountingWrapper("position_ids must be a LongTensor, got float")
|
|
|
|
with pytest.raises(TypeError, match="must be a LongTensor"):
|
|
_two_left_padded_batches(model).evaluate()
|
|
assert model.attempts == [True]
|
|
|
|
|
|
class _ModeRecordingModel(_LossModel):
|
|
def __init__(self, losses):
|
|
super().__init__(losses)
|
|
self.child = nn.Dropout(0.5)
|
|
self.modes: list[bool] = []
|
|
|
|
def forward(self, *, input_ids, attention_mask, labels):
|
|
self.modes.append(self.training or self.child.training)
|
|
return super().forward(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
|
|
|
|
|
|
def test_evaluation_runs_in_eval_mode_and_restores_module_modes():
|
|
"""#666: dropout is off while evaluating; every module's mode is restored after."""
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["alpha"], [_encoding([[1, 2]], [[1, 1]])], [],
|
|
)
|
|
model = _ModeRecordingModel([math.log(2.0)])
|
|
model.train()
|
|
model.child.eval() # a submodule deliberately in eval mode stays that way
|
|
evaluator.handle.model = model
|
|
|
|
evaluator.evaluate()
|
|
|
|
assert model.modes == [False]
|
|
assert model.training is True
|
|
assert model.child.training is False
|
|
|
|
|
|
def _tiny_classifier_logits(padding_side: str):
|
|
from datasets import Dataset
|
|
from transformers import GPT2Config, GPT2ForSequenceClassification
|
|
|
|
from tests.fixtures.tiny_offline_model import FIXTURE_SEED, FIXTURE_VOCAB, _fixture_tokenizer
|
|
|
|
torch.manual_seed(FIXTURE_SEED)
|
|
config = GPT2Config(
|
|
vocab_size=len(FIXTURE_VOCAB), n_positions=32, n_embd=16, n_layer=1, n_head=2,
|
|
n_inner=32, bos_token_id=1, eos_token_id=1, pad_token_id=0, num_labels=3,
|
|
)
|
|
model = GPT2ForSequenceClassification(config).eval()
|
|
captured: list[torch.Tensor] = []
|
|
model.register_forward_hook(lambda _m, _a, out: captured.append(out.logits.detach().clone()))
|
|
tokenizer = _fixture_tokenizer()
|
|
tokenizer.padding_side = padding_side
|
|
handle = SimpleNamespace(model=model, tokenizer=tokenizer, task="classification")
|
|
dataset = Dataset.from_dict(
|
|
{"text": ["hello world", "hello world safe test answer"], "label": [0, 1]},
|
|
)
|
|
result = Evaluator(handle=handle, dataset=dataset, batch_size=2, max_length=12).evaluate()
|
|
(logits,) = captured
|
|
return result, logits
|
|
|
|
|
|
@pytest.mark.cpu
|
|
def test_classification_left_padding_matches_right_padding():
|
|
"""#666: left-padded classification batches get positions from the first real token."""
|
|
right_result, right_logits = _tiny_classifier_logits("right")
|
|
left_result, left_logits = _tiny_classifier_logits("left")
|
|
|
|
assert torch.allclose(left_logits, right_logits, atol=1e-5)
|
|
assert left_result == right_result
|
|
|
|
|
|
class _TrainRecordingDropout(nn.Dropout):
|
|
"""A submodule that overrides ``train()`` and records each call's mode."""
|
|
|
|
def __init__(self):
|
|
super().__init__(0.5)
|
|
self.train_calls: list[bool] = []
|
|
|
|
def train(self, mode: bool = True):
|
|
self.train_calls.append(mode)
|
|
return super().train(mode)
|
|
|
|
|
|
class _OverrideModeModel(_ModeRecordingModel):
|
|
def __init__(self, losses):
|
|
super().__init__(losses)
|
|
self.child = _TrainRecordingDropout()
|
|
self.plain = nn.Dropout(0.5)
|
|
|
|
|
|
def test_mode_restore_calls_train_overrides():
|
|
"""#675 B2: restoring a mode goes through ``train(mode)``, honouring overrides."""
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["alpha"], [_encoding([[1, 2]], [[1, 1]])], [],
|
|
)
|
|
model = _OverrideModeModel([math.log(2.0)])
|
|
model.train()
|
|
model.plain.eval() # a plain submodule without an override keeps its own mode
|
|
model.child.train_calls.clear()
|
|
evaluator.handle.model = model
|
|
|
|
evaluator.evaluate()
|
|
|
|
assert model.modes == [False]
|
|
assert model.child.train_calls[0] is False # eval() on entry
|
|
assert model.child.train_calls[-1] is True # restored through the override
|
|
assert (model.training, model.child.training, model.plain.training) == (True, True, False)
|
|
|
|
|
|
def test_modes_are_restored_when_evaluation_raises():
|
|
"""#675 B4: a failing evaluation still restores every module's mode."""
|
|
evaluator, _dataset, _tokenizer, _model = _evaluator(
|
|
["alpha"], [_encoding([[1, 2]], [[1, 1]])], [],
|
|
)
|
|
model = _OverrideModeModel([float("nan")])
|
|
model.train()
|
|
model.plain.eval()
|
|
model.child.train_calls.clear()
|
|
evaluator.handle.model = model
|
|
|
|
with pytest.raises(RuntimeError, match="Non-finite evaluation loss"):
|
|
evaluator.evaluate()
|
|
|
|
assert model.modes == [False]
|
|
assert model.child.train_calls[-1] is True
|
|
assert (model.training, model.child.training, model.plain.training) == (True, True, False)
|
|
|
|
|
|
def test_position_ids_fallback_warns_once_per_model(caplog):
|
|
"""#675 B3: repeated evaluations of one model (e.g. ablation specs) warn once."""
|
|
model = _AttemptCountingWrapper(_CPYTHON_REJECTION)
|
|
other = _AttemptCountingWrapper(_CPYTHON_REJECTION)
|
|
|
|
with caplog.at_level("WARNING", logger="obliteratus.evaluation.evaluator"):
|
|
_two_left_padded_batches(model).evaluate()
|
|
_two_left_padded_batches(model).evaluate()
|
|
assert len([r for r in caplog.records if r.levelname == "WARNING"]) == 1
|
|
_two_left_padded_batches(other).evaluate()
|
|
|
|
warnings = [r for r in caplog.records if r.levelname == "WARNING"]
|
|
assert len(warnings) == 2
|
|
# Each evaluation still falls back, it is only the warning that is not repeated.
|
|
assert model.attempts == [True, False, False, True, False, False]
|
|
|
|
|
|
def _tiny_roberta_classifier_positions(padding_side: str):
|
|
"""Run a tiny RoBERTa-style classifier; return the positions its embeddings saw."""
|
|
from datasets import Dataset
|
|
from transformers import RobertaConfig, RobertaForSequenceClassification
|
|
|
|
from tests.fixtures.tiny_offline_model import FIXTURE_SEED, FIXTURE_VOCAB, _fixture_tokenizer
|
|
|
|
torch.manual_seed(FIXTURE_SEED)
|
|
config = RobertaConfig(
|
|
vocab_size=len(FIXTURE_VOCAB), hidden_size=16, num_hidden_layers=1,
|
|
num_attention_heads=2, intermediate_size=32, max_position_embeddings=40,
|
|
pad_token_id=0, bos_token_id=1, eos_token_id=1, num_labels=3,
|
|
)
|
|
model = RobertaForSequenceClassification(config).eval()
|
|
seen: list[torch.Tensor] = []
|
|
model.roberta.embeddings.position_embeddings.register_forward_pre_hook(
|
|
lambda _m, args: seen.append(args[0].detach().clone()),
|
|
)
|
|
tokenizer = _fixture_tokenizer()
|
|
tokenizer.padding_side = padding_side
|
|
handle = SimpleNamespace(model=model, tokenizer=tokenizer, task="classification")
|
|
dataset = Dataset.from_dict(
|
|
{"text": ["hello world", "hello world safe test answer"], "label": [0, 1]},
|
|
)
|
|
Evaluator(handle=handle, dataset=dataset, batch_size=2, max_length=12).evaluate()
|
|
(positions,) = seen
|
|
mask = tokenizer(
|
|
["hello world", "hello world safe test answer"], return_tensors="pt", padding=True,
|
|
)["attention_mask"].bool()
|
|
return positions, mask, config.pad_token_id
|
|
|
|
|
|
@pytest.mark.cpu
|
|
@pytest.mark.parametrize("padding_side", ["left", "right"])
|
|
def test_padding_offset_position_models_keep_their_own_positions(padding_side):
|
|
"""#675 B1: RoBERTa-style positions start at ``padding_idx + 1``, never 0."""
|
|
positions, mask, padding_idx = _tiny_roberta_classifier_positions(padding_side)
|
|
|
|
for row in range(len(mask)):
|
|
real = positions[row][mask[row]]
|
|
expected = torch.arange(len(real)) + padding_idx + 1
|
|
assert real.tolist() == expected.tolist()
|
|
assert (positions[row][~mask[row]] == padding_idx).all()
|