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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# inference.py: Decoding helpers shared with the generator: prefix construction, constrained decoding, token extraction.

from __future__ import annotations

import logging
from typing import List, Optional

import torch

logger = logging.getLogger(__name__)


def build_inference_prefix(
    tokenizer,
    registry,
    prompt: str,
    stage: int,
) -> torch.Tensor:
    if stage not in (1, 2):
        raise ValueError(f"Unsupported stage {stage}; must be 1 or 2")

    prompt_ids = tokenizer.encode(prompt, add_special_tokens=False)

    if stage == 1:
        prefix = (
            [registry.bos_id, registry.user_prompt_id]
            + prompt_ids
            + [registry.audio_start_id]
        )
    else:
        prefix = (
            [registry.bos_id, registry.user_prompt_id]
            + prompt_ids
            + [registry.think_start_id]
        )

    return torch.tensor([prefix], dtype=torch.long)


def extract_speech_token_ids(
    generated_ids: List[int],
    registry,
    prefix_len: int,
) -> List[int]:
    if prefix_len > len(generated_ids):
        return []

    tail = generated_ids[prefix_len:]
    speech_ids: List[int] = []
    started = False

    for tid in tail:
        if registry.is_speech_token(tid):
            speech_ids.append(tid)
            started = True
        elif started:
            break
        elif tid == registry.audio_end_id or tid == registry.eos_id:
            break

    return speech_ids


def _flatten_generation_output(output) -> List[int]:
    if hasattr(output, "sequences"):
        tensor = output.sequences
    else:
        tensor = output

    if isinstance(tensor, torch.Tensor):
        if tensor.dim() == 2:
            return tensor[0].tolist()
        return tensor.tolist()

    return list(tensor)


def _sample_from_logits(
    logits: torch.Tensor,
    *,
    temperature: float,
    top_p: float,
    top_k: int,
) -> int:
    eff_temperature = max(temperature, 1e-4)

    if top_k and top_k > 0:
        top_vals, top_ix = torch.topk(logits, k=min(top_k, logits.numel()))
        masked = torch.full_like(logits, float("-inf"))
        masked[top_ix] = top_vals
        logits = masked

    probs = torch.softmax(logits / eff_temperature, dim=-1)

    if top_p and top_p < 1.0:
        sorted_probs, sorted_ix = torch.sort(probs, descending=True)
        cumulative = torch.cumsum(sorted_probs, dim=-1)
        cutoff = cumulative > top_p
        cutoff[..., 0] = False
        sorted_probs[cutoff] = 0.0
        sorted_probs = sorted_probs / sorted_probs.sum().clamp(min=1e-12)
        next_ix = torch.multinomial(sorted_probs, 1)
        return int(sorted_ix.gather(-1, next_ix).item())
    return int(torch.multinomial(probs, 1).item())


def _build_speech_mask(model, device) -> torch.Tensor:
    reg = model.token_registry
    vocab_size = model.backbone.config.vocab_size
    allowed = torch.zeros(vocab_size, dtype=torch.bool, device=device)
    allowed[reg.speech_token_id_min : reg.speech_token_id_max + 1] = True
    return allowed


def _speech_constrained_decode(
    model,
    prefix: torch.Tensor,
    *,
    max_new_tokens: int,
    temperature: float,
    top_p: float,
    top_k: int,
    level_cycle: int = 0,
    level_temps: Optional[List[float]] = None,
    stop_ids: Optional[List[int]] = None,
) -> List[int]:
    device = prefix.device
    allowed = _build_speech_mask(model, device)
    neg_inf = torch.tensor(float("-inf"), device=device)

    level_masks = None
    if level_cycle > 0:
        reg = model.token_registry
        id_min = reg.speech_token_id_min
        n_speech = reg.speech_token_id_max - id_min + 1
        csize = n_speech // level_cycle
        level_masks = []
        for lvl in range(level_cycle):
            m = torch.zeros_like(allowed)
            m[id_min + lvl * csize: id_min + (lvl + 1) * csize] = True
            level_masks.append(m & allowed)

    stop_mask = None
    if stop_ids:
        stop_mask = torch.zeros_like(allowed)
        for sid in stop_ids:
            stop_mask[sid] = True

    seq = prefix.clone()
    speech_ids: List[int] = []

    with torch.no_grad():
        for i in range(max_new_tokens):
            out = model(input_ids=seq)
            logits = out["logits"][0, -1].float()
            step_mask = (
                level_masks[i % level_cycle] if level_masks is not None else allowed
            )
            if stop_mask is not None and (level_cycle == 0 or i % level_cycle == 0):
                step_mask = step_mask | stop_mask
            logits = torch.where(step_mask, logits, neg_inf)

            step_temp = temperature
            if level_masks is not None and level_temps:
                step_temp = level_temps[(i % level_cycle) % len(level_temps)]
            next_id = _sample_from_logits(
                logits, temperature=step_temp, top_p=top_p, top_k=top_k,
            )
            if stop_ids and next_id in stop_ids:
                break
            speech_ids.append(next_id)
            seq = torch.cat(
                [seq, torch.tensor([[next_id]], device=device, dtype=seq.dtype)],
                dim=-1,
            )

    return speech_ids


def speculative_generate(
    model,
    prefix: torch.Tensor,
    *,
    max_new_tokens: int,
    temperature: float = 0.0,
    speech_only: bool = False,
) -> tuple[List[int], dict]:
    if temperature > 0:
        raise NotImplementedError(
            "speculative_generate currently supports greedy decoding only "
            "(temperature=0). Sampling-mode speculative requires rejection "
            "sampling (Leviathan et al.) and is deferred to a later phase."
        )

    reg = model.token_registry
    device = prefix.device

    if speech_only:
        speech_mask = _build_speech_mask(model, device)
        neg_inf = torch.tensor(float("-inf"), device=device)

        def constrain(logits: torch.Tensor) -> torch.Tensor:
            return torch.where(speech_mask, logits, neg_inf)
    else:
        def constrain(logits: torch.Tensor) -> torch.Tensor:
            return logits

    seq = prefix.clone()
    generated: List[int] = []
    iterations = 0
    drafts_accepted = 0

    with torch.no_grad():
        while len(generated) < max_new_tokens:
            iterations += 1

            out = model(input_ids=seq)
            main_logits = constrain(out["logits"][0, -1].float())
            t = int(main_logits.argmax().item())
            generated.append(t)
            if len(generated) >= max_new_tokens:
                seq = torch.cat(
                    [seq, torch.tensor([[t]], device=device, dtype=seq.dtype)],
                    dim=-1,
                )
                break

            hidden_T = out["hidden_states"][0, -1]
            draft = int(model.mtp_module.predict_offset_2(hidden_T).item())
            if speech_only:
                h_normed = model.mtp_module.spec_shared_norm(hidden_T.unsqueeze(0))
                spec_logits = constrain(
                    model.mtp_module.spec_shared_head(h_normed)[0].float()
                )
                draft = int(spec_logits.argmax().item())

            seq = torch.cat(
                [seq, torch.tensor([[t, draft]], device=device, dtype=seq.dtype)],
                dim=-1,
            )
            out_v = model(input_ids=seq)
            verify_logits = constrain(out_v["logits"][0, -2].float())
            verify_t1 = int(verify_logits.argmax().item())

            if verify_t1 == draft:
                generated.append(draft)
                drafts_accepted += 1
            else:
                seq = seq[:, :-1]
                seq[0, -1] = t
                generated.append(verify_t1)
                seq = torch.cat(
                    [seq, torch.tensor([[verify_t1]], device=device, dtype=seq.dtype)],
                    dim=-1,
                )

    if len(generated) > max_new_tokens:
        generated = generated[:max_new_tokens]

    meta = {
        "iterations": iterations,
        "drafts_proposed": iterations,
        "drafts_accepted": drafts_accepted,
        "acceptance_rate": drafts_accepted / max(iterations, 1),
        "tokens_emitted": len(generated),
        "forward_passes": 2 * iterations,
    }
    return generated, meta


def _cot_constrained_decode(
    model,
    prefix: torch.Tensor,
    *,
    max_new_tokens: int,
    max_think_tokens: int,
    temperature: float,
    top_p: float,
    top_k: int,
) -> tuple[List[int], List[int], dict]:
    reg = model.token_registry
    device = prefix.device
    speech_mask = _build_speech_mask(model, device)
    neg_inf = torch.tensor(float("-inf"), device=device)

    seq = prefix.clone()
    think_ids: List[int] = []
    speech_ids: List[int] = []
    meta = {"think_truncated": False, "transition": None}

    THINKING, AUDIO = 0, 1
    state = THINKING
    think_count = 0
    tokens_used = 0

    def emit(token_id: int):
        nonlocal seq, tokens_used
        seq = torch.cat(
            [seq, torch.tensor([[token_id]], device=device, dtype=seq.dtype)],
            dim=-1,
        )
        tokens_used += 1

    with torch.no_grad():
        while tokens_used < max_new_tokens:
            if state == THINKING:
                if think_count >= max_think_tokens:
                    meta["think_truncated"] = True
                    meta["transition"] = "forced"
                    emit(reg.think_end_id)
                    if tokens_used < max_new_tokens:
                        emit(reg.audio_start_id)
                    state = AUDIO
                    continue

                out = model(input_ids=seq)
                logits = out["logits"][0, -1].float()
                next_id = _sample_from_logits(
                    logits, temperature=temperature, top_p=top_p, top_k=top_k,
                )

                if next_id == reg.think_end_id:
                    meta["transition"] = "think_end"
                    emit(next_id)
                    if tokens_used < max_new_tokens:
                        emit(reg.audio_start_id)
                    state = AUDIO
                elif next_id == reg.audio_start_id:
                    meta["transition"] = "audio_start_direct"
                    emit(reg.think_end_id)
                    if tokens_used < max_new_tokens:
                        emit(next_id)
                    state = AUDIO
                else:
                    think_ids.append(next_id)
                    think_count += 1
                    emit(next_id)

            else:
                out = model(input_ids=seq)
                logits = out["logits"][0, -1].float()
                logits = torch.where(speech_mask, logits, neg_inf)
                next_id = _sample_from_logits(
                    logits, temperature=temperature, top_p=top_p, top_k=top_k,
                )
                speech_ids.append(next_id)
                emit(next_id)

    return think_ids, speech_ids, meta


def generate_speech(
    model,
    prompt: str,
    output_path: str,
    *,
    stage: int = 1,
    max_new_tokens: int = 1500,
    max_think_tokens: int = 200,
    temperature: float = 1.0,
    top_p: float = 1.0,
    top_k: int = 0,
    constrained: bool = False,
    speculative: bool = False,
    codec=None,
    codec_name: str = "xcodec2",
    device: Optional[str] = None,
) -> dict:
    import soundfile as sf
    from codec import load_codec

    if device is None:
        device = getattr(model, "device", "cuda")

    tokenizer = model.tokenizer
    registry = model.token_registry

    input_ids = build_inference_prefix(tokenizer, registry, prompt, stage)
    prefix_len = input_ids.shape[1]
    input_ids = input_ids.to(device)

    cot_meta = None
    spec_meta = None
    think_ids: List[int] = []

    if speculative and stage == 2:
        raise NotImplementedError(
            "speculative=True with stage=2 (CoT-aware decode) is not "
            "supported in Phase 11 — the THINK→AUDIO state machine and "
            "the draft-and-verify loop have multiple interaction points "
            "that need careful design. Use stage=1 + speculative for now."
        )

    if speculative:
        gen_tokens, spec_meta = speculative_generate(
            model, input_ids,
            max_new_tokens=max_new_tokens,
            temperature=0.0,
            speech_only=constrained,
        )
        generated_ids = input_ids[0].tolist() + gen_tokens
        if constrained:
            speech_ids = list(gen_tokens)
        else:
            speech_ids = extract_speech_token_ids(
                generated_ids, registry, prefix_len,
            )
    elif constrained and stage == 2:
        think_ids, speech_ids, cot_meta = _cot_constrained_decode(
            model, input_ids,
            max_new_tokens=max_new_tokens,
            max_think_tokens=max_think_tokens,
            temperature=temperature,
            top_p=top_p,
            top_k=top_k,
        )
        boundary_pair = [registry.think_end_id, registry.audio_start_id]
        if cot_meta and cot_meta.get("transition") == "audio_start_direct":
            pass
        generated_ids = (
            input_ids[0].tolist() + think_ids + boundary_pair + speech_ids
        )
    elif constrained:
        speech_ids = _speech_constrained_decode(
            model, input_ids,
            max_new_tokens=max_new_tokens,
            temperature=temperature,
            top_p=top_p,
            top_k=top_k,
        )
        generated_ids = input_ids[0].tolist() + speech_ids
    else:
        max_length = prefix_len + max_new_tokens
        with torch.no_grad():
            output = model.generate(
                input_ids,
                max_length=max_length,
                temperature=temperature,
                top_p=top_p,
                top_k=top_k,
            )
        generated_ids = _flatten_generation_output(output)
        speech_ids = extract_speech_token_ids(generated_ids, registry, prefix_len)

    if not speech_ids:
        raise ValueError(
            f"No speech tokens generated (prefix_len={prefix_len}, "
            f"total_generated={len(generated_ids)})"
        )

    if codec is None:
        codec = load_codec(codec_name, device=device)

    codec_values = [registry.speech_id_to_value(tid) for tid in speech_ids]
    waveform, sr = codec.decode(codec_values)
    sf.write(output_path, waveform, sr)

    duration = len(waveform) / sr if sr else 0.0
    logger.info(
        "Generated %d speech tokens -> %s (%.2fs)",
        len(speech_ids),
        output_path,
        duration,
    )

    result = {
        "output_path": output_path,
        "num_speech_tokens": len(speech_ids),
        "duration_seconds": duration,
        "sample_rate": sr,
        "token_ids": generated_ids,
    }
    if cot_meta is not None:
        result["cot_meta"] = cot_meta
        result["think_token_count"] = len(think_ids)
    if spec_meta is not None:
        result["spec_meta"] = spec_meta
    return result