File size: 15,780 Bytes
a066584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2026 Modilify
# SPDX-License-Identifier: LicenseRef-Modilify-Open-Model-1.0
"""Rolling canvas generation for native Modilify Mk1."""

from __future__ import annotations

from dataclasses import dataclass, replace
import math
import time
from typing import Any

import mlx.core as mx

from mlx_vlm.generate.common import wired_limit

from .commit_policy import fused_commit_failure_rate, select_commit_lengths
from .latent_deliberation import LatentDeliberationState
from .modeling import ModilifyMk1ForBlockDiffusion


@dataclass
class ModilifyMk1GenerationOutput:
    sequences: list[int]
    generated_ids: list[int]
    generated_length: int
    denoise_steps: int
    jump_count: int
    average_commit_len: float
    stop_reason: str
    tokens_per_forward: float
    prefill_seconds: float = 0.0
    generate_seconds: float = 0.0
    first_denoise_seconds: float = 0.0
    heavy_denoise_per_second: float = 0.0
    steady_heavy_denoise_per_second: float = 0.0
    tokens_per_second: float = 0.0


@dataclass
class _RollingState:
    canvas: mx.array
    confidence: mx.array
    entropy: mx.array
    age: mx.array
    latent_state: LatentDeliberationState
    history_hidden_state: mx.array | None


def _flatten_token_ids(*values: object) -> tuple[int, ...]:
    token_ids: list[int] = []
    for value in values:
        if value is None:
            continue
        if isinstance(value, int):
            token_ids.append(int(value))
        elif isinstance(value, (list, tuple, set)):
            token_ids.extend(int(token_id) for token_id in value if token_id is not None)
    return tuple(dict.fromkeys(token_ids))


def _shift_prefix(tensor: mx.array, committed: int, fill_value: float | int) -> mx.array:
    if committed <= 0:
        return tensor
    canvas = tensor.shape[1]
    if committed >= canvas:
        return mx.full(tensor.shape, fill_value, dtype=tensor.dtype)
    kept = tensor[:, committed:]
    fill_shape = (tensor.shape[0], committed, *tensor.shape[2:])
    fill = mx.full(fill_shape, fill_value, dtype=tensor.dtype)
    return mx.concatenate([kept, fill], axis=1)


def _shift_state(
    state: _RollingState,
    committed: int,
    *,
    vocab_size: int,
    canvas_length: int,
    unknown_entropy: float,
) -> _RollingState:
    if committed <= 0:
        return state
    tail = mx.random.randint(0, vocab_size, (state.canvas.shape[0], committed))
    canvas = mx.concatenate([state.canvas[:, committed:], tail], axis=1)
    if canvas.shape[1] != canvas_length:
        raise RuntimeError("Canvas shift produced an unexpected length.")
    latent = state.latent_state
    shifted_latent = LatentDeliberationState(
        token_latents=_shift_prefix(latent.token_latents, committed, 0),
        memory_slots=latent.memory_slots,
        confidence=_shift_prefix(latent.confidence, committed, 0),
        entropy=_shift_prefix(latent.entropy, committed, unknown_entropy),
        age=_shift_prefix(latent.age, committed, 0),
        token_changed=_shift_prefix(latent.token_changed, committed, 0),
        confidence_delta=_shift_prefix(latent.confidence_delta, committed, 0),
        entropy_delta=_shift_prefix(latent.entropy_delta, committed, 0),
        ponder_steps=mx.zeros_like(latent.ponder_steps),
        stagnation_steps=mx.zeros_like(latent.stagnation_steps),
    )
    history = state.history_hidden_state
    if history is not None:
        history = _shift_prefix(history, committed, 0)
    return _RollingState(
        canvas=canvas,
        confidence=_shift_prefix(state.confidence, committed, 0),
        entropy=_shift_prefix(state.entropy, committed, unknown_entropy),
        age=_shift_prefix(state.age, committed, 0),
        latent_state=shifted_latent,
        history_hidden_state=history,
    )


def generate(
    model: ModilifyMk1ForBlockDiffusion,
    input_ids: mx.array,
    *,
    max_new_tokens: int | None = None,
    temperature: float | None = None,
    attention_mask: mx.array | None = None,
    pixel_values: mx.array | None = None,
    mm_token_type_ids: mx.array | None = None,
    max_denoising_steps: int | None = None,
    seed: int | None = None,
    profiler=None,
) -> ModilifyMk1GenerationOutput:
    """Generate one response with rolling block diffusion.

    Batch size 1 only. Semantics follow ``generation_modilify_mk1.py``.
    """

    if input_ids.ndim != 2 or input_ids.shape[0] != 1:
        raise ValueError("Native Mk1 generation currently requires shape [1, sequence].")
    if seed is not None:
        mx.random.seed(int(seed))

    config = model.config
    canvas_length = int(config.canvas_length)
    vocab_size = int(config.vocab_size)
    dtype = model.model.decoder.embed_tokens.weight.dtype
    max_new = int(max_new_tokens if max_new_tokens is not None else 256)
    if max_new <= 0:
        raise ValueError("`max_new_tokens` must be positive.")
    denoise_temperature = (
        float(config.denoise_temperature) if temperature is None else float(temperature)
    )
    max_iterations = max(1, max_new * int(config.max_ponder_steps))
    unknown_entropy = math.log(vocab_size)

    if attention_mask is None:
        attention_mask = mx.ones(input_ids.shape, dtype=mx.bool_)
    prefill_started = time.perf_counter()
    cache = model.make_cache(max_size=int(input_ids.shape[1]) + max_new)
    cache = model.prefill(
        input_ids,
        attention_mask=attention_mask,
        cache=cache,
        pixel_values=pixel_values,
        mm_token_type_ids=mm_token_type_ids,
    )
    mx.eval([item for block in cache for item in getattr(block, "state", ())])
    prefill_seconds = time.perf_counter() - prefill_started
    compiled_decoder_step = None

    latent = LatentDeliberationState.empty(
        batch_size=1,
        canvas_length=canvas_length,
        latent_dim=config.latent_dim,
        memory_slots=config.latent_memory_slots,
        dtype=dtype,
    )
    state = _RollingState(
        canvas=mx.random.randint(0, vocab_size, (1, canvas_length)),
        confidence=mx.zeros((1, canvas_length), dtype=mx.float32),
        entropy=mx.full((1, canvas_length), unknown_entropy, dtype=mx.float32),
        age=mx.zeros((1, canvas_length), dtype=mx.int32),
        latent_state=latent,
        history_hidden_state=None,
    )
    stop_token_ids = _flatten_token_ids(config.turn_end_token_id, config.eos_token_id)
    turn_end = int(config.turn_end_token_id)
    generated: list[int] = []
    denoise_steps = 0
    jumps = 0
    shifts = 0
    stop_reason = "episode_watchdog"
    prompt_ids = [int(token) for token in input_ids[0].tolist()]
    generate_started = time.perf_counter()
    first_denoise_seconds = 0.0
    always_active = mx.array([True])

    with wired_limit(model, None):
        while len(generated) < max_new:
                step_started = time.perf_counter()
                remaining = mx.array([max_new - len(generated)], dtype=mx.int32)
                output = model(
                    decoder_input_ids=state.canvas,
                    cache=cache,
                    previous_confidence=state.confidence,
                    previous_entropy=state.entropy,
                    token_age=state.age,
                    latent_state=state.latent_state,
                    history_hidden_state=state.history_hidden_state,
                    return_proposal_statistics=True,
                    denoise_temperature=denoise_temperature,
                    repetition_penalty=float(config.repetition_penalty),
                    compiled_decoder_step=None,
                    profiler=profiler,
                )
                denoise_steps += 1
                proposal = output.proposal
                proposal_confidence = output.proposal_confidence
                token_entropy = output.token_entropy
                greedy_proposal = output.greedy_proposal
                greedy_confidence = output.greedy_confidence
                next_canvas = proposal
                next_confidence = proposal_confidence.astype(mx.float32)
                next_entropy = token_entropy.astype(mx.float32)
                next_latent = replace(
                    output.next_latent_state,
                    confidence=next_confidence,
                    entropy=next_entropy,
                    age=state.age + 1,
                    token_changed=(next_canvas != state.canvas).astype(mx.float32),
                    confidence_delta=next_confidence - state.confidence,
                    entropy_delta=next_entropy - state.entropy,
                )
                next_state = _RollingState(
                    canvas=next_canvas,
                    confidence=next_confidence,
                    entropy=next_entropy,
                    age=state.age + 1,
                    latent_state=next_latent,
                    history_hidden_state=output.heavy_hidden_state,
                )
                if profiler is not None:
                    commit_span = profiler.measure(
                        "commit",
                        proposal,
                        proposal_confidence,
                        token_entropy,
                    )
                policy = select_commit_lengths(
                    sampled_token_ids=proposal,
                    normal_failure_rate=fused_commit_failure_rate(
                        proposal_confidence, token_entropy, vocab_size=vocab_size
                    ),
                    previous_failure_rate=fused_commit_failure_rate(
                        state.confidence, state.entropy, vocab_size=vocab_size
                    ),
                    greedy_token_ids=greedy_proposal,
                    jump_failure_rate=fused_commit_failure_rate(
                        greedy_confidence, token_entropy, vocab_size=vocab_size
                    ),
                    ponder_steps=state.latent_state.ponder_steps,
                    stagnation_steps=state.latent_state.stagnation_steps,
                    active_rows=always_active,
                    remaining_lengths=remaining,
                    failure_budget=float(config.commit_failure_budget),
                    jump_failure_budget=float(config.jump_failure_budget),
                    stop_token_id=stop_token_ids,
                    max_ponder_steps=int(config.max_ponder_steps),
                    stagnation_threshold=int(config.jump_on_no_progress_after),
                    min_progress=float(config.min_trajectory_progress),
                )
                if profiler is not None:
                    commit_span.done(policy.commit_lengths, policy.jump_rows)
                if profiler is not None:
                    span = profiler.measure(
                        "sync",
                        policy.commit_lengths,
                        policy.jump_rows,
                        policy.commit_token_ids,
                    )
                mx.eval(
                    policy.commit_lengths,
                    policy.jump_rows,
                    policy.commit_token_ids,
                )
                commit_len = int(policy.commit_lengths[0].item())
                jump = bool(policy.jump_rows[0].item())
                if profiler is not None:
                    span.done()
                next_state = replace(
                    next_state,
                    latent_state=replace(
                        next_state.latent_state,
                        ponder_steps=policy.ponder_steps,
                        stagnation_steps=policy.stagnation_steps,
                    ),
                )
                if jump:
                    next_state = replace(next_state, canvas=policy.commit_token_ids)
                    jumps += 1
                committed_ids: list[int] = []
                if commit_len:
                    committed_ids = [
                        int(token)
                        for token in policy.commit_token_ids[0, :commit_len].tolist()
                    ]
                    generated.extend(committed_ids)
                    committed_block = policy.commit_token_ids[:, :commit_len]
                    if profiler is not None:
                        span = profiler.measure("update_cache", committed_block)
                    cache = model.update_cache(committed_block, cache=cache)
                    mx.eval(
                        *[
                            block.keys
                            for block in cache
                            if getattr(block, "keys", None) is not None
                        ],
                        *[
                            block.values
                            for block in cache
                            if getattr(block, "values", None) is not None
                        ],
                    )
                    if profiler is not None:
                        span.done()
                    shifts += 1
                state = _shift_state(
                    next_state,
                    commit_len,
                    vocab_size=vocab_size,
                    canvas_length=canvas_length,
                    unknown_entropy=unknown_entropy,
                )
                if profiler is not None:
                    profiler.finish_step()
                if denoise_steps == 1:
                    first_denoise_seconds = time.perf_counter() - step_started
                    generate_started = time.perf_counter()
                if committed_ids:
                    if turn_end in committed_ids:
                        stop_reason = "turn_end"
                        break
                    if any(
                        token in stop_token_ids and token != turn_end
                        for token in committed_ids
                    ):
                        stop_reason = "eos"
                        break
                if len(generated) >= max_new:
                    stop_reason = "max_new_tokens"
                    break
                if (
                    max_denoising_steps is not None
                    and denoise_steps >= max_denoising_steps
                ):
                    stop_reason = "max_denoising_steps"
                    break
                if denoise_steps >= max_iterations:
                    stop_reason = "episode_watchdog"
                    break

    generate_seconds = time.perf_counter() - generate_started
    if denoise_steps <= 1:
        generate_seconds = first_denoise_seconds
        steady_hdps = 0.0
        overall_hdps = (1.0 / first_denoise_seconds) if first_denoise_seconds else 0.0
        tokens_per_second = (
            len(generated) / first_denoise_seconds if first_denoise_seconds else 0.0
        )
    else:
        steady_hdps = (denoise_steps - 1) / max(generate_seconds, 1e-9)
        overall_hdps = denoise_steps / max(
            first_denoise_seconds + generate_seconds, 1e-9
        )
        tokens_per_second = len(generated) / max(
            first_denoise_seconds + generate_seconds, 1e-9
        )
    average_commit = (len(generated) / shifts) if shifts else 0.0
    tpf = (len(generated) / denoise_steps) if denoise_steps else 0.0
    return ModilifyMk1GenerationOutput(
        sequences=prompt_ids + generated,
        generated_ids=generated,
        generated_length=len(generated),
        denoise_steps=denoise_steps,
        jump_count=jumps,
        average_commit_len=average_commit,
        stop_reason=stop_reason,
        tokens_per_forward=tpf,
        prefill_seconds=prefill_seconds,
        generate_seconds=first_denoise_seconds + generate_seconds,
        first_denoise_seconds=first_denoise_seconds,
        heavy_denoise_per_second=overall_hdps,
        steady_heavy_denoise_per_second=steady_hdps,
        tokens_per_second=tokens_per_second,
    )