File size: 7,230 Bytes
2054ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""Needle-in-a-haystack test for the Blacksmith checkpoints.

Places a passphrase at a given depth inside a long filler context, asks for it,
and checks whether the answer contains it. Sweeping length vs depth gives a
capability curve rather than a single anecdote, which is the only way to say
whether the 64k training and the YaRN extension to 128k actually work.

    python3 eval_retrieval.py --ckpt blacksmith-1_sft.pt \
        --lengths 4096 16384 32768 65536 --depths 0.0 0.5 1.0

For lengths beyond the trained 65536, pass the YaRN parameters that match the
converter's metadata:

    ... --lengths 131072 --rope-scale 2 --original-max-pos 65536
"""
import argparse
import json

import torch
import torch.nn.functional as F

from model import NanoGPT
from tokenmaker import Tokenizer, TokenizerVocabSize
from hyperparameters import d_model, n_heads, n_layers

PASSPHRASE = 'BLUE-HERRING-42'
NEEDLE = (f'\n\nThe secret passphrase is {PASSPHRASE}.\n\n')
QUESTION = 'What is the secret passphrase mentioned above? Answer with the passphrase only.'
# A single neutral paragraph repeated to the requested length. Using one fixed
# text keeps the local context identical at every length, so a failure can be
# attributed to distance/position rather than to whatever document happened to
# end at that token offset.
FILLER_TEXT = (
    'The museum opened to the public in the spring of that year, and its first '
    'exhibition was a survey of nineteenth century landscape painting. Visitors '
    'walked through rooms arranged by region rather than by date, which was '
    'unusual at the time and drew comment from several newspapers. The building '
    'itself had been a warehouse, and the architects kept the iron columns and '
    'the wide timber floors, adding only skylights and a new staircase. In the '
    'decades that followed the collection grew slowly, mostly through gifts, and '
    'the museum became known for the quietness of its upper galleries. '
)


def build_filler(n_tokens):
    """One paragraph repeated to the requested length."""
    unit = Tokenizer.encode(FILLER_TEXT)
    reps = n_tokens // len(unit) + 1
    return (unit * reps)[:n_tokens]


def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument('--ckpt', required=True)
    p.add_argument('--lengths', type=int, nargs='+', default=[4096, 16384, 32768, 65536])
    p.add_argument('--depths', type=float, nargs='+', default=[0.0, 0.5, 1.0],
                   help='0 = needle at the very start, 1 = immediately before the question')
    p.add_argument('--rope-scale', type=float, default=1.0,
                   help='YaRN stretch factor for lengths beyond the trained context')
    p.add_argument('--original-max-pos', type=int, default=65536,
                   help='context the base was trained at; YaRN ramp reference')
    p.add_argument('--max-new', type=int, default=24)
    p.add_argument('--samples', type=int, default=1,
                   help='generations per (length, depth); >1 exposes variance')
    p.add_argument('--temperature', type=float, default=0.0,
                   help='0 = greedy, the usual setting for retrieval tests')
    p.add_argument('--mode', choices=['chat', 'completion'], default='chat',
                   help='chat asks a question in the SFT format; completion makes '
                        'the model repeat the sentence, which tests copying '
                        'without requiring instruction following')
    return p.parse_args()


def main():
    args = parse_args()
    dev = 'cuda' if torch.cuda.is_available() else 'cpu'
    max_len = max(args.lengths)

    model = NanoGPT(TokenizerVocabSize, d_model, n_heads, n_layers, max_len,
                    rope_scale=args.rope_scale,
                    original_max_pos=args.original_max_pos).to(dev)
    model = model.to(torch.bfloat16)
    chk = torch.load(args.ckpt, map_location=dev, weights_only=False)
    state = {k.replace('module.', '', 1): v for k, v in chk.get('model', chk).items()}
    model.load_state_dict(state)
    model.eval()
    print(f'loaded {args.ckpt} (step {chk.get("step", "?")}), '
          f'rope_scale={args.rope_scale}, orig_ctx={args.original_max_pos}', flush=True)

    EOT = Tokenizer.eos_token_id
    IM_START = Tokenizer.encode('<|im_start|>')[0]
    IM_END = Tokenizer.encode('<|im_end|>')[0]
    NL = Tokenizer.encode('\n')[0]
    ROLE_USER = Tokenizer.encode('user')
    ROLE_ASSIST = Tokenizer.encode('assistant')

    filler = build_filler(max_len + 4096)
    needle_ids = Tokenizer.encode(NEEDLE)
    question_ids = Tokenizer.encode('\n\n' + QUESTION)

    @torch.no_grad()
    def run(length, depth):
        # Reserve room for the needle, the question and the reply.
        body_len = length - len(needle_ids) - len(question_ids)
        if body_len < 64:
            return None, ''
        body = filler[:body_len]
        cut = int(round(depth * body_len))
        body = body[:cut] + needle_ids + body[cut:]

        if args.mode == 'completion':
            # No chat wrapper: continue the sentence the needle started. This is
            # the fairest probe for a base model, which never saw a long
            # document followed by an instruction inside one turn.
            tail = Tokenizer.encode('\n\nReminder: the secret passphrase is')
            ids = list(body) + tail
        else:
            ids = [EOT, IM_START] + ROLE_USER + [NL] + body + question_ids
            ids += [IM_END, NL, IM_START] + ROLE_ASSIST + [NL]
        if len(ids) > length:
            ids = ids[:length]

        x = torch.tensor([ids], device=dev)
        logits, k, v = model(x, infer=True)
        pos = x.shape[1]
        out = []
        for _ in range(args.max_new):
            lg = logits[0, -1, :]
            tok = (lg.argmax() if args.temperature == 0
                   else torch.multinomial(F.softmax(lg / args.temperature, -1), 1)[0])
            if tok.item() in (EOT, IM_END):
                break
            out.append(tok.item())
            logits, k, v = model(tok[None, None], k, v, infer=True, pos_offset=pos)
            pos += 1
        text = Tokenizer.decode(out)
        return PASSPHRASE in text, text

    print(f'\n{"length":>8} {"depth":>6}  hit  reply')
    table = {}
    for length in args.lengths:
        for depth in args.depths:
            results = []
            for _ in range(args.samples):
                try:
                    hit, text = run(length, depth)
                except RuntimeError as e:
                    print(f'{length:>8} {depth:>6.2f}  ERR  {str(e)[:60]}')
                    break
                if hit is None:
                    break
                results.append(hit)
                print(f'{length:>8} {depth:>6.2f}  {"Y" if hit else "n":>3}  {text[:70]!r}', flush=True)
            if results:
                table[(length, depth)] = sum(results) / len(results)

    if table:
        print('\nsummary')
        for length in args.lengths:
            cells = [table.get((length, d)) for d in args.depths if (length, d) in table]
            if cells:
                print(f'  {length:>7}: {sum(cells) / len(cells):.2f} avg hit rate')


if __name__ == '__main__':
    main()