File size: 20,679 Bytes
0433390
 
 
 
 
 
 
 
e8cb6a7
 
0433390
 
e8cb6a7
0433390
 
 
e8cb6a7
 
 
 
 
 
0433390
 
 
 
 
 
 
 
 
 
 
 
 
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0433390
 
 
e8cb6a7
0433390
e8cb6a7
 
 
 
 
 
0433390
 
 
 
e8cb6a7
0433390
 
 
 
 
e8cb6a7
0433390
 
 
 
e8cb6a7
0433390
 
 
 
 
e8cb6a7
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
0433390
 
 
 
 
 
e8cb6a7
 
 
 
 
 
 
 
 
 
 
0433390
 
 
e8cb6a7
0433390
e8cb6a7
 
 
0433390
 
e8cb6a7
 
 
 
0433390
 
e8cb6a7
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
0433390
e8cb6a7
 
 
 
 
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
0433390
 
 
 
 
e8cb6a7
0433390
 
 
 
 
e8cb6a7
 
0433390
 
 
 
 
 
 
e8cb6a7
 
0433390
 
 
 
e8cb6a7
0433390
 
e8cb6a7
 
0433390
 
 
 
 
 
e8cb6a7
 
 
 
 
 
 
 
 
0433390
 
e8cb6a7
0433390
e8cb6a7
0433390
e8cb6a7
0433390
 
e8cb6a7
 
0433390
 
 
e8cb6a7
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0433390
e8cb6a7
 
 
 
 
 
 
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
 
0433390
 
e8cb6a7
 
0433390
 
 
 
 
 
e8cb6a7
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
0433390
e8cb6a7
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0433390
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0433390
 
e8cb6a7
 
 
 
 
 
 
 
 
 
 
0433390
e8cb6a7
 
0433390
 
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
0433390
e8cb6a7
0433390
 
 
 
 
 
e8cb6a7
 
0433390
 
 
 
 
 
 
e8cb6a7
0433390
 
e8cb6a7
0433390
e8cb6a7
 
 
 
0433390
 
 
e8cb6a7
0433390
 
e8cb6a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
"""
Core speculative decoding loop for DFlash on MLX.

Implements the full inference pipeline:
1. Prefill: Target model processes prompt, extracts hidden features
2. Draft: Block diffusion model generates parallel draft tokens
3. Verify: Target model verifies drafts in parallel
4. Accept: Accepted tokens appended, rejected tokens regenerated

Fixed for architecture-agnostic operation across Qwen3, Qwen3.5, LLaMA, Mistral, Gemma.
"""

from typing import Optional, List, Callable, Dict, Any, Tuple
import mlx.core as mx
import mlx.nn as nn
from .model import DFlashDraftModel
from .adapters import (
    LoadedTargetModel,
    load_target_model,
    adapter_for_model_type,
    detect_model_architecture,
)


def sample_greedy(logits: mx.array) -> mx.array:
    """Greedy sampling."""
    return mx.argmax(logits, axis=-1)


def sample_temperature(logits: mx.array, temperature: float) -> mx.array:
    """Temperature sampling."""
    probs = mx.softmax(logits / temperature, axis=-1)
    return mx.random.categorical(mx.log(probs))


def find_first_mismatch(draft: mx.array, target: mx.array) -> int:
    """Find length of matching prefix between draft and target tokens.
    
    Returns the number of consecutive matching tokens from the start.
    """
    matches = draft == target
    # Convert to int for cumsum, find first 0
    match_int = matches.astype(mx.int32)
    # Use argmin to find first mismatch (first 0 in cumprod is actually tricky)
    # Simpler: find first position where match is False
    mismatch_positions = mx.where(matches == False, mx.arange(matches.shape[0]), matches.shape[0])
    first_mismatch = int(mismatch_positions.min())
    return first_mismatch


class DFlashSpeculativeDecoder:
    """DFlash speculative decoder for MLX-converted models.
    
    Architecture-agnostic: works with any MLX causal language model as the target,
    paired with a DFlash block diffusion draft model.
    
    Key improvements over naive implementation:
    - Proper KV cache management with trim/rewind on rejection
    - Architecture-aware hidden state extraction via adapters
    - Correct acceptance logic using first-mismatch detection
    - Streaming support for real-time output
    """

    def __init__(
        self,
        target_model: Any,
        draft_model: DFlashDraftModel,
        tokenizer,
        block_size: int = 16,
        max_seq_length: int = 8192,
        device: str = "metal",
        adapter: Optional[LoadedTargetModel] = None,
    ):
        """Initialize the DFlash speculative decoder.
        
        Args:
            target_model: MLX target LLM (any mlx_lm loaded model) or LoadedTargetModel
            draft_model: DFlash block diffusion draft model
            tokenizer: Tokenizer for encoding/decoding
            block_size: Number of tokens to draft per block
            max_seq_length: Maximum sequence length
            device: MLX device ("cpu" or "metal")
            adapter: Optional pre-built adapter (if target_model is raw mlx_lm model)
        """
        # If target_model is already a LoadedTargetModel, use it directly
        if hasattr(target_model, 'adapter') and hasattr(target_model, 'model'):
            self.loaded_target = target_model
        elif adapter is not None:
            self.loaded_target = adapter
        else:
            # Auto-detect and build adapter
            self.loaded_target = load_target_model(target_model)
        
        self.target_model = self.loaded_target.model
        self.draft_model = draft_model
        self.tokenizer = tokenizer
        self.block_size = block_size
        self.max_seq_length = max_seq_length
        self.device = device
        self.mask_token_id = draft_model.mask_token_id
        
        # Verify compatibility
        self._validate_setup()
    
    def _validate_setup(self):
        """Check that target and draft models are compatible."""
        target_vocab = getattr(self.tokenizer, 'vocab_size', None)
        draft_vocab = self.draft_model.vocab_size
        if target_vocab is not None and target_vocab != draft_vocab:
            print(f"[DFlash] Warning: vocab mismatch target={target_vocab} draft={draft_vocab}")
    
    def _target_forward(
        self,
        input_ids: mx.array,
        cache: Optional[list] = None,
        output_hidden_states: bool = False,
        layer_ids: Optional[List[int]] = None,
    ) -> Dict[str, Any]:
        """Forward pass through target model using adapter.
        
        Args:
            input_ids: Input token IDs [bsz, seq_len]
            cache: Per-layer KV cache (managed by adapter)
            output_hidden_states: Whether to return hidden states for KV injection
            layer_ids: Target layer indices to extract (from draft model config)
        
        Returns:
            Dict with 'logits' and optionally 'hidden_states', 'target_hidden'
        """
        if cache is None:
            cache = self.loaded_target.make_cache()
        
        if layer_ids is None:
            layer_ids = getattr(self.draft_model, 'target_layer_ids', [])
        
        if output_hidden_states and layer_ids:
            # Forward with hidden state extraction at specified layers
            logits, target_hidden, _ = self.loaded_target.forward_with_hidden_states(
                tokens=input_ids,
                cache=cache,
                layer_ids=layer_ids,
                output_rollback_records=False,
            )
            return {
                "logits": logits,
                "target_hidden": target_hidden,
                "cache": cache,
            }
        else:
            # Simple forward without hidden states
            logits, _ = self.loaded_target.forward_with_hidden_states(
                tokens=input_ids,
                cache=cache,
                layer_ids=[],
                output_rollback_records=False,
            )
            return {
                "logits": logits,
                "cache": cache,
            }
    
    def _sample(self, logits: mx.array, temperature: float) -> mx.array:
        """Sample from logits."""
        if temperature < 1e-5:
            return sample_greedy(logits)
        return sample_temperature(logits, temperature)
    
    def spec_generate(
        self,
        input_ids: mx.array,
        max_new_tokens: int,
        temperature: float = 0.0,
        stop_token_ids: Optional[set[int]] = None,
        stream_callback: Optional[Callable[[str, bool], None]] = None,
    ) -> mx.array:
        """Generate tokens using DFlash speculative decoding.
        
        Args:
            input_ids: Prompt token IDs [bsz, seq_len]
            max_new_tokens: Maximum new tokens to generate
            temperature: Sampling temperature (0 for greedy)
            stop_token_ids: Optional set of stop token IDs
            stream_callback: Optional callback(text_delta, finished) for streaming
        
        Returns:
            Generated token IDs [bsz, total_seq_len]
        """
        num_input_tokens = int(input_ids.shape[1])
        max_length = num_input_tokens + max_new_tokens
        block_size = self.block_size
        
        # Initialize output buffer
        output_ids = mx.full(
            (1, max_length + block_size),
            self.mask_token_id,
            dtype=mx.int32,
        )
        position_ids = mx.arange(output_ids.shape[1])
        
        # Create fresh KV cache for target model
        target_cache = self.loaded_target.make_cache()
        
        # Get target layer IDs from draft model config
        layer_ids = getattr(self.draft_model, 'target_layer_ids', [])
        
        # ── Prefill stage ────────────────────────────────────────────────────
        print(f"[DFlash] Prefill: processing {num_input_tokens} prompt tokens...")
        target_output = self._target_forward(
            input_ids,
            cache=target_cache,
            output_hidden_states=True,
            layer_ids=layer_ids,
        )
        
        # Copy prompt tokens to output
        output_ids[:, :num_input_tokens] = input_ids[0]
        
        # Sample first token from target model (position num_input_tokens)
        first_token_logits = target_output["logits"][:, -1:, :]
        first_token = self._sample(first_token_logits, temperature)
        output_ids[:, num_input_tokens] = first_token[0, 0]
        
        # Extract target context features for draft conditioning
        target_hidden = target_output.get("target_hidden")
        if target_hidden is None:
            print("[DFlash] Warning: no hidden states extracted, using fallback")
            # Fallback: project logits to hidden size
            # This will produce poor drafts but allows the loop to continue
            target_hidden = mx.zeros((1, 1, self.draft_model.hidden_size))
        
        # Update cache with the first generated token
        _ = self._target_forward(
            first_token,
            cache=target_cache,
            output_hidden_states=False,
        )
        
        # ── Decode stage: speculative decoding loop ──────────────────────────
        print(f"[DFlash] Starting speculative decoding (block_size={block_size})...")
        acceptance_lengths: List[int] = []
        start = num_input_tokens + 1  # After first target-generated token
        generated_count = 1
        
        # Streaming state
        stream_buffer = ""
        
        while start < max_length and generated_count < max_new_tokens:
            # 1. DRAFT: generate block of tokens with diffusion model
            # Prepare block: first token is last accepted token, rest are masked
            block_slice = output_ids[:, start - 1 : start - 1 + block_size]
            block_output_ids = mx.array(block_slice)
            # Mask all positions after the first (anchor)
            block_output_ids = mx.where(
                mx.arange(block_size) == 0,
                block_output_ids,
                self.mask_token_id,
            )
            block_output_ids = block_output_ids.reshape(1, block_size)
            
            block_position_ids = position_ids[start - 1 : start - 1 + block_size]
            
            # Embed draft tokens (including mask tokens)
            draft_embeddings = self.draft_model.embed_tokens(block_output_ids)
            
            # Run draft model to get predictions for all positions
            draft_hidden = self.draft_model(
                noise_embedding=draft_embeddings,
                target_hidden=target_hidden,
                position_ids=block_position_ids,
            )
            draft_logits = self.draft_model.get_logits(draft_hidden)
            
            # Sample draft tokens (predict all positions)
            draft_tokens = self._sample(draft_logits, temperature)
            
            # Build verification input: anchor + draft predictions
            verify_input = mx.concatenate([
                block_output_ids[:, :1],  # Anchor token
                draft_tokens[:, :-1],      # Draft predictions (excluding last)
            ], axis=1)
            
            # 2. VERIFY: run target model on draft tokens
            verify_output = self._target_forward(
                verify_input,
                cache=target_cache,
                output_hidden_states=True,
                layer_ids=layer_ids,
            )
            verify_logits = verify_output["logits"]
            
            # Target's greedy predictions at each position
            posterior = self._sample(verify_logits, temperature=0.0)
            
            # 3. ACCEPT: compare draft vs target tokens
            # draft_tokens[0, 1:] are the predictions for positions 1..block_size-1
            # posterior[0, :-1] are target's predictions for positions 0..block_size-2
            # We compare draft at position i with target at position i-1 for i>=1
            draft_for_compare = draft_tokens[0, 1:]
            target_for_compare = posterior[0, :-1]
            
            # Find first mismatch in the block
            matches = draft_for_compare == target_for_compare
            match_int = matches.astype(mx.int32)
            # cumprod gives 1 up to first mismatch, then 0
            match_prefix = mx.cumprod(match_int)
            acceptance_length = int(match_prefix.sum())
            
            # Accepted tokens: draft predictions for positions 1..acceptance_length
            # Rejected position: target's prediction at acceptance_length
            num_new_tokens = acceptance_length + 1  # +1 for the bonus token
            
            # Copy accepted tokens
            accepted_tokens = draft_tokens[0, 1:1 + acceptance_length]
            if acceptance_length < verify_input.shape[1] - 1:
                bonus_token = posterior[0, acceptance_length]
                new_tokens = mx.concatenate([accepted_tokens, mx.array([bonus_token])])
            else:
                # All draft tokens accepted, need one more from target
                bonus_logits = verify_output["logits"][:, -1:, :]
                bonus_token = self._sample(bonus_logits, temperature)[0, 0]
                new_tokens = mx.concatenate([accepted_tokens, mx.array([bonus_token])])
            
            # Write new tokens to output
            end_pos = min(start + len(new_tokens), max_length)
            actual_new = end_pos - start
            if actual_new > 0:
                output_ids[:, start:end_pos] = new_tokens[:actual_new].reshape(1, -1)
            
            # 4. KV CACHE: rewind to accepted length
            self.loaded_target.rewind_kv_caches(target_cache, start + actual_new)
            
            # Update counters
            start += actual_new
            generated_count += actual_new
            acceptance_lengths.append(actual_new)
            
            # 5. UPDATE target hidden states for next iteration
            if "target_hidden" in verify_output:
                target_hidden = verify_output["target_hidden"]
                # Keep only up to accepted positions
                if target_hidden.shape[1] > actual_new:
                    target_hidden = target_hidden[:, :actual_new, :]
            
            # Stream output
            if stream_callback is not None:
                new_text = self.tokenizer.decode(new_tokens.tolist()[:actual_new])
                stream_buffer += new_text
                stream_callback(new_text, False)
            
            # Check stop conditions
            if stop_token_ids is not None:
                generated_slice = output_ids[0, num_input_tokens:start]
                generated_list = generated_slice.tolist()
                for i, tid in enumerate(generated_list):
                    if int(tid) in stop_token_ids:
                        start = num_input_tokens + i + 1
                        break
                else:
                    continue
                break
        
        # Final trim
        output_ids = output_ids[:, :start]
        
        # Remove mask tokens
        valid_mask = output_ids[0] != self.mask_token_id
        output_ids = output_ids[:, valid_mask]
        
        # Stats
        if acceptance_lengths:
            avg_acceptance = sum(acceptance_lengths) / len(acceptance_lengths)
            speedup = sum(acceptance_lengths) / len(acceptance_lengths) if acceptance_lengths else 1.0
            print(f"[DFlash] Done. Generated {generated_count} tokens, "
                  f"avg acceptance: {avg_acceptance:.2f}, effective speedup: ~{speedup:.2f}x")
        
        # Final stream callback
        if stream_callback is not None:
            stream_callback("", True)
        
        return output_ids
    
    def generate(
        self,
        prompt: str,
        max_tokens: int = 2048,
        temperature: float = 0.0,
        stop_strings: Optional[List[str]] = None,
        stream: bool = False,
    ) -> str | Any:
        """High-level generate method with string input/output.
        
        Args:
            prompt: Text prompt
            max_tokens: Maximum tokens to generate
            temperature: Sampling temperature
            stop_strings: Optional list of stop strings
            stream: If True, returns a generator yielding text deltas
        
        Returns:
            Generated text string, or generator if stream=True
        """
        # Tokenize via adapter
        input_ids = self.loaded_target.build_prompt(prompt)
        input_ids = input_ids.reshape(1, -1)
        
        # Determine stop token IDs
        stop_token_ids = None
        if stop_strings is not None:
            stop_token_ids = set()
            for s in stop_strings:
                tokens = self.tokenizer.encode(s, add_special_tokens=False)
                stop_token_ids.update(tokens)
        else:
            stop_token_ids = self.loaded_target.stop_token_ids()
        
        if stream:
            # Streaming generator
            stream_buffer: List[str] = []
            
            def callback(delta: str, finished: bool):
                stream_buffer.append(delta)
            
            output_ids = self.spec_generate(
                input_ids=input_ids,
                max_new_tokens=max_tokens,
                temperature=temperature,
                stop_token_ids=stop_token_ids,
                stream_callback=callback,
            )
            
            # Yield accumulated text
            for chunk in stream_buffer:
                yield chunk
        else:
            # One-shot generation
            output_ids = self.spec_generate(
                input_ids=input_ids,
                max_new_tokens=max_tokens,
                temperature=temperature,
                stop_token_ids=stop_token_ids,
            )
            
            # Decode (skip prompt)
            prompt_len = input_ids.shape[1]
            generated_ids = output_ids[0, prompt_len:]
            output_text = self.tokenizer.decode(generated_ids.tolist())
            
            return output_text
    
    def benchmark(
        self,
        prompt: str = "Write a quicksort in Python.",
        max_tokens: int = 512,
        num_runs: int = 5,
    ) -> Dict[str, float]:
        """Benchmark DFlash speculative decoding.
        
        Args:
            prompt: Test prompt
            max_tokens: Tokens per run
            num_runs: Number of benchmark runs
        
        Returns:
            Dict with speedup metrics
        """
        import time
        
        print(f"[Benchmark] Running {num_runs} generations with DFlash...")
        
        # Warmup
        self.generate(prompt, max_tokens=10)
        mx.eval()
        
        # DFlash generation
        dflash_times = []
        for _ in range(num_runs):
            start = time.time()
            self.generate(prompt, max_tokens=max_tokens)
            mx.eval()
            dflash_times.append(time.time() - start)
        
        # Baseline: run target model without speculative decoding
        print(f"[Benchmark] Running {num_runs} baseline generations...")
        baseline_times = []
        
        # Simple baseline using mlx_lm generate
        try:
            from mlx_lm.utils import generate as mlx_generate
            for _ in range(num_runs):
                start = time.time()
                mlx_generate(
                    model=self.target_model,
                    tokenizer=self.tokenizer,
                    prompt=prompt,
                    max_tokens=max_tokens,
                    temp=temperature,
                )
                mx.eval()
                baseline_times.append(time.time() - start)
        except Exception as e:
            print(f"[Benchmark] Baseline generation failed: {e}")
            baseline_times = [t * 2.0 for t in dflash_times]  # Estimate
        
        avg_dflash = sum(dflash_times) / len(dflash_times)
        avg_baseline = sum(baseline_times) / len(baseline_times) if baseline_times else avg_dflash * 2
        
        tokens_per_sec = max_tokens / avg_dflash
        speedup = avg_baseline / avg_dflash if avg_baseline > 0 else 1.0
        
        print(f"[Benchmark] Baseline: {avg_baseline:.2f}s | DFlash: {avg_dflash:.2f}s | Speedup: {speedup:.2f}x | {tokens_per_sec:.1f} tok/s")
        
        return {
            "avg_time_sec": avg_dflash,
            "tokens_per_sec": tokens_per_sec,
            "speedup": speedup,
            "baseline_time_sec": avg_baseline,
            "num_runs": num_runs,
        }