-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_confidence.py
More file actions
78 lines (63 loc) · 2.18 KB
/
Copy pathtest_confidence.py
File metadata and controls
78 lines (63 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import annotations
from typing import ClassVar
import torch
from thinker_r.confidence import find_subsequence, score_generated_choice
class TinyTokenizer:
tokens: ClassVar[dict[str, list[int]]] = {
"<answer>": [1],
"<answer": [1],
"A": [2],
"B": [3],
"C": [4],
"D": [5],
" A": [6],
" B": [7],
" C": [8],
" D": [9],
">A": [12],
">B": [13],
">C": [14],
">D": [15],
}
decoded: ClassVar[dict[int, str]] = {
1: "<answer>",
2: "A",
3: "B",
4: "C",
5: "D",
10: "\n",
14: ">C",
}
def encode(self, text: str, add_special_tokens: bool = False) -> list[int]:
del add_special_tokens
return self.tokens.get(text, [99, 100])
def decode(self, ids: list[int], skip_special_tokens: bool = False) -> str:
del skip_special_tokens
return "".join(self.decoded.get(token, "?") for token in ids)
def test_find_subsequence() -> None:
assert find_subsequence([4, 1, 2, 3], [1, 2]) == 1
assert find_subsequence([1, 2], [3]) is None
def test_score_generated_choice_skips_whitespace_and_scores_labels() -> None:
tokenizer = TinyTokenizer()
generated = [20, 1, 10, 4]
scores = [torch.zeros(1, 20) for _ in generated]
scores[3][0, 4] = 8.0
result = score_generated_choice(tokenizer, generated, scores)
assert result is not None
assert result["position"] == 3
assert result["prediction"] == "C"
assert result["confidence"] > 0.99
assert result["margin"] > 0.98
def test_score_generated_choice_requires_answer_tag() -> None:
tokenizer = TinyTokenizer()
assert score_generated_choice(tokenizer, [4], [torch.zeros(1, 20)]) is None
def test_score_generated_choice_handles_tag_letter_token_merge() -> None:
tokenizer = TinyTokenizer()
generated = [20, 1, 14]
scores = [torch.zeros(1, 20) for _ in generated]
scores[2][0, 14] = 8.0
result = score_generated_choice(tokenizer, generated, scores)
assert result is not None
assert result["position"] == 2
assert result["prediction"] == "C"
assert result["confidence"] > 0.99