File size: 9,590 Bytes
7acd624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
generation_utils.py — High-level generation helpers for SeqCond models.

These functions wrap SeqCondForCausalLM.generate() / generate_batch() with a
more user-friendly interface that handles tokenization, formatting, and
streaming.

Example usage:
    from transformers import AutoModelForCausalLM, AutoTokenizer
    model = AutoModelForCausalLM.from_pretrained("path/to/model", trust_remote_code=True)
    tokenizer = AutoTokenizer.from_pretrained("path/to/model", trust_remote_code=True)
    model.eval().cuda()

    text = generate(model, tokenizer, "What is 2 + 2?")
    print(text)

    # Batched
    texts = generate_batch(model, tokenizer, ["What is 2+2?", "Name a planet."])
"""

from typing import Iterator, List, Optional

import torch
import torch.nn.functional as F


_SEQ_LENS = [8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]  # power-of-2 for CUDA graphs


def _quantized_seq_len(pos: int) -> int:
    needed = pos + 1
    for s in _SEQ_LENS:
        if s >= needed:
            return s
    return _SEQ_LENS[-1]


@torch.no_grad()
def generate(
    model,
    tokenizer,
    prompt: str,
    max_new_tokens: int = 512,
    temperature: float = 0.7,
    top_p: float = 0.9,
    top_k: int = 50,
    repetition_penalty: float = 1.0,
    use_chat_template: bool = True,
    use_triton: bool = False,
    strip_thinking: bool = False,
    max_thinking_tokens: Optional[int] = None,
) -> str:
    """
    Generate a single completion for *prompt*.

    Args:
        model: SeqCondForCausalLM instance.
        tokenizer: SeqCondTokenizer instance.
        prompt: Plain-text user prompt.
        max_new_tokens: Maximum tokens to generate.
        temperature: Sampling temperature (0 = greedy).
        top_p: Nucleus sampling probability.
        top_k: Top-k filtering (0 = disabled).
        repetition_penalty: Penalty for repeating tokens.
        use_chat_template: If True, wrap prompt in <|im_start|>user…<|think_start|>.
        use_triton: If True, use Triton kernels for SeqCond steps.
        strip_thinking: If True, return only the text after <|think_end|>.
        max_thinking_tokens: If set, inject <|think_end|> after this many
            thinking tokens to cap reasoning length.

    Returns:
        Generated text (completion only, EOS stripped).
    """
    device = next(model.parameters()).device
    eos_id = tokenizer.im_end_id
    think_end_id = tokenizer.think_end_id

    if use_chat_template:
        ids = tokenizer.encode_chat(prompt, add_think_start=True)
    else:
        ids = tokenizer.encode(prompt)

    input_ids = torch.tensor([ids], dtype=torch.long, device=device)
    logits, states = model.model.prefill(input_ids)
    logits = logits.squeeze(1)

    generated: List[int] = []
    token_buf = torch.zeros((1, 1), dtype=torch.long, device=device)
    seq_len = len(ids)

    in_thinking = use_chat_template
    thinking_tokens = 0
    think_end_injected = False
    counts: dict = {}

    for _ in range(max_new_tokens):
        ls = logits[0] / max(temperature, 1e-8) if temperature > 0 else logits[0].clone()

        if repetition_penalty != 1.0:
            for t in set(generated):
                if 0 <= t < model.config.vocab_size:
                    ls[t] /= repetition_penalty

        if temperature == 0:
            next_token = int(torch.argmax(ls))
        else:
            if top_k > 0:
                kth = torch.topk(ls, top_k).values[-1]
                ls = ls.masked_fill(ls < kth, float("-inf"))
            if top_p < 1.0:
                sorted_ls, sorted_idx = torch.sort(ls, descending=True)
                cum = torch.cumsum(F.softmax(sorted_ls, dim=-1), dim=-1)
                remove = cum > top_p
                remove[1:] = remove[:-1].clone(); remove[0] = False
                ls[sorted_idx[remove]] = float("-inf")
            probs = F.softmax(ls, dim=-1)
            next_token = int(torch.multinomial(probs, 1))

        # Thinking budget
        if next_token == think_end_id:
            in_thinking = False
        if in_thinking:
            thinking_tokens += 1
        if (
            max_thinking_tokens is not None
            and in_thinking
            and thinking_tokens >= max_thinking_tokens
            and not think_end_injected
        ):
            next_token = think_end_id
            in_thinking = False
            think_end_injected = True

        generated.append(next_token)
        if next_token == eos_id:
            break

        token_buf[0, 0] = next_token
        seq_len += 1
        logits, states = model.model.step(token_buf, states, seq_len=seq_len, use_triton=use_triton)

    # Decode
    if generated and generated[-1] == eos_id:
        generated = generated[:-1]

    text = tokenizer.decode(generated)
    if strip_thinking and "<|think_end|>" in text:
        text = text.split("<|think_end|>", 1)[1].strip()
    return text


@torch.no_grad()
def generate_batch(
    model,
    tokenizer,
    prompts: List[str],
    max_new_tokens: int = 512,
    temperature: float = 0.7,
    use_chat_template: bool = True,
    use_triton: bool = False,
    strip_thinking: bool = False,
) -> List[str]:
    """
    Batched generation for a list of prompts.

    Each prompt is prefilled individually (no padding noise), then all
    sequences are decoded in lockstep with per-sample early stopping.

    Returns a list of completion strings (EOS stripped).
    """
    device = next(model.parameters()).device
    eos_id = tokenizer.im_end_id
    B = len(prompts)

    if use_chat_template:
        all_ids = [tokenizer.encode_chat(p, add_think_start=True) for p in prompts]
    else:
        all_ids = [tokenizer.encode(p) for p in prompts]

    # Individual prefills
    all_logits, all_states = [], []
    for ids in all_ids:
        inp = torch.tensor([ids], dtype=torch.long, device=device)
        lg, st = model.model.prefill(inp)
        all_logits.append(lg.squeeze(1))
        all_states.append(st)

    logits = torch.cat(all_logits, dim=0)
    num_blocks = len(all_states[0])
    states = [
        tuple(torch.cat([s[i][j] for s in all_states], dim=0) for j in range(len(all_states[0][i])))
        for i in range(num_blocks)
    ]

    generated = [[] for _ in range(B)]
    finished = [False] * B
    active_map = list(range(B))
    token_buf = torch.zeros((B, 1), dtype=torch.long, device=device)
    seq_len = max(len(ids) for ids in all_ids)

    for _ in range(max_new_tokens):
        B_cur = len(active_map)
        if B_cur == 0:
            break

        if temperature == 0:
            next_tokens = torch.argmax(logits, dim=-1)
        else:
            probs = F.softmax(logits / max(temperature, 1e-8), dim=-1)
            next_tokens = torch.multinomial(probs, 1).squeeze(-1)

        newly_done: set = set()
        for bi in range(B_cur):
            oi = active_map[bi]
            tok = int(next_tokens[bi])
            generated[oi].append(tok)
            if tok == eos_id:
                finished[oi] = True
                newly_done.add(bi)
            else:
                token_buf[bi, 0] = tok

        if all(finished):
            break

        if newly_done:
            keep = [bi for bi in range(B_cur) if bi not in newly_done]
            if not keep:
                break
            keep_idx = torch.tensor(keep, device=device)
            token_buf = token_buf[keep_idx].contiguous()
            states = [tuple(s[keep_idx].contiguous() for s in st) for st in states]
            logits = logits[keep_idx]
            active_map = [active_map[bi] for bi in keep]

        seq_len += 1
        logits, states = model.model.step(token_buf, states, seq_len=seq_len, use_triton=use_triton)

    results = []
    for toks in generated:
        if toks and toks[-1] == eos_id:
            toks = toks[:-1]
        text = tokenizer.decode(toks)
        if strip_thinking and "<|think_end|>" in text:
            text = text.split("<|think_end|>", 1)[1].strip()
        results.append(text)
    return results


@torch.no_grad()
def stream(
    model,
    tokenizer,
    prompt: str,
    max_new_tokens: int = 512,
    temperature: float = 0.7,
    use_chat_template: bool = True,
    use_triton: bool = False,
) -> Iterator[str]:
    """
    Streaming token-by-token generation.

    Yields decoded text fragments as they are produced. Useful for interactive
    applications (e.g., a chat interface).

    Example:
        for fragment in stream(model, tokenizer, "Explain gravity."):
            print(fragment, end="", flush=True)
    """
    device = next(model.parameters()).device
    eos_id = tokenizer.im_end_id

    if use_chat_template:
        ids = tokenizer.encode_chat(prompt, add_think_start=True)
    else:
        ids = tokenizer.encode(prompt)

    input_ids = torch.tensor([ids], dtype=torch.long, device=device)
    logits, states = model.model.prefill(input_ids)
    logits = logits.squeeze(1)

    token_buf = torch.zeros((1, 1), dtype=torch.long, device=device)
    seq_len = len(ids)

    for _ in range(max_new_tokens):
        if temperature == 0:
            next_token = int(torch.argmax(logits[0]))
        else:
            probs = F.softmax(logits[0] / max(temperature, 1e-8), dim=-1)
            next_token = int(torch.multinomial(probs, 1))

        if next_token == eos_id:
            break

        try:
            yield tokenizer.decode([next_token])
        except Exception:
            yield ""

        token_buf[0, 0] = next_token
        seq_len += 1
        logits, states = model.model.step(token_buf, states, seq_len=seq_len, use_triton=use_triton)