ebrahim-ricursive commited on
Commit
48f6e3c
·
verified ·
1 Parent(s): 9f61852

Keep the generation regression harness outside the model repository

Browse files
Files changed (1) hide show
  1. tests/test_generation_positions.py +0 -93
tests/test_generation_positions.py DELETED
@@ -1,93 +0,0 @@
1
- """Run with pytest under Transformers 4.57.6 and 5.22.1; no model weights needed."""
2
-
3
- import importlib.util
4
- import sys
5
- from pathlib import Path
6
-
7
- import pytest
8
- import torch
9
-
10
-
11
- ROOT = Path(__file__).resolve().parents[1]
12
- SPEC = importlib.util.spec_from_file_location(
13
- "ripple_generation_regression", ROOT / "modeling_ricursive.py",
14
- submodule_search_locations=[str(ROOT)],
15
- )
16
- MODEL = importlib.util.module_from_spec(SPEC)
17
- sys.modules[SPEC.name] = MODEL
18
- SPEC.loader.exec_module(MODEL)
19
-
20
-
21
- @pytest.fixture
22
- def model():
23
- torch.manual_seed(19)
24
- config = MODEL.RicursiveConfig(
25
- vocab_size=97, hidden_size=32, intermediate_size=64,
26
- num_hidden_layers=5, num_attention_heads=4, num_key_value_heads=2,
27
- head_dim=8, sliding_window=16, max_position_embeddings=128,
28
- pad_token_id=0, eos_token_id=None, use_valres=True,
29
- )
30
- return MODEL.RicursiveForCausalLM(config).eval()
31
-
32
-
33
- @pytest.mark.parametrize("use_cache", [True, False])
34
- @pytest.mark.parametrize("custom_positions", [False, True])
35
- @torch.inference_mode()
36
- def test_explicit_positions_extend_during_generation(model, use_cache, custom_positions):
37
- prompt = torch.tensor([[3, 4, 5, 6, 7, 8], [0, 0, 3, 4, 5, 6]])
38
- mask = prompt.ne(0).long()
39
- positions = (mask.cumsum(-1) - 1).masked_fill(mask == 0, 0)
40
- if custom_positions:
41
- positions = torch.tensor([[2, 4, 5, 8, 9, 12], [0, 0, 17, 19, 20, 24]])
42
- original_positions = positions.clone()
43
- expected = prompt.clone()
44
- expected_mask, expected_positions = mask.clone(), positions.clone()
45
- # Independent full-forward decoding checks both offsets and newly added positions.
46
- for _ in range(4):
47
- logits = model(expected, attention_mask=expected_mask,
48
- position_ids=expected_positions, use_cache=False).logits
49
- expected = torch.cat([expected, logits[:, -1].argmax(-1, keepdim=True)], dim=-1)
50
- expected_mask = torch.cat([expected_mask, torch.ones((2, 1), dtype=mask.dtype)], dim=-1)
51
- expected_positions = torch.cat([expected_positions, expected_positions[:, -1:] + 1], dim=-1)
52
-
53
- observed = []
54
- def capture(_module, _args, kwargs):
55
- observed.append(kwargs["position_ids"].clone())
56
- handle = model.register_forward_pre_hook(capture, with_kwargs=True)
57
- try:
58
- actual = model.generate(prompt, attention_mask=mask, position_ids=positions,
59
- max_new_tokens=4, do_sample=False, use_cache=use_cache)
60
- finally:
61
- handle.remove()
62
- torch.testing.assert_close(actual, expected)
63
- torch.testing.assert_close(positions, original_positions)
64
- torch.testing.assert_close(observed[0], original_positions)
65
- for step, seen in enumerate(observed[1:], start=1):
66
- future = original_positions[:, -1:] + torch.arange(1, step + 1)
67
- wanted = future[:, -1:] if use_cache else torch.cat([original_positions, future], dim=-1)
68
- torch.testing.assert_close(seen, wanted)
69
-
70
-
71
- @torch.inference_mode()
72
- def test_explicit_positions_with_beam_search(model):
73
- prompt = torch.tensor([[3, 4, 5, 6, 7, 8]])
74
- expected = model.generate(prompt, num_beams=3, max_new_tokens=4, do_sample=False)
75
- actual = model.generate(prompt, position_ids=torch.arange(6).unsqueeze(0),
76
- num_beams=3, max_new_tokens=4, do_sample=False)
77
- torch.testing.assert_close(actual, expected)
78
-
79
-
80
- @pytest.mark.parametrize("num_new_tokens", [1, 3])
81
- @torch.inference_mode()
82
- def test_position_update_appends_exactly_the_accepted_token_count(model, num_new_tokens):
83
- prompt = torch.tensor([[3, 4, 5, 6]])
84
- positions = torch.tensor([[7, 8, 10, 13]])
85
- output = model(prompt, position_ids=positions, use_cache=True)
86
- updated = model._update_model_kwargs_for_generation(
87
- output,
88
- {"position_ids": positions, "attention_mask": torch.ones_like(prompt),
89
- "cache_position": torch.arange(4), "use_cache": True},
90
- num_new_tokens=num_new_tokens,
91
- )
92
- expected = torch.cat([positions, positions[:, -1:] + torch.arange(1, num_new_tokens + 1)], dim=-1)
93
- torch.testing.assert_close(updated["position_ids"], expected)