File size: 27,688 Bytes
2f86d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# Copyright 2026 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Diffusion sampler."""

import dataclasses
import functools
from typing import cast, override

import flax.struct
from gemma.diffusion import _early_stopping
from gemma.diffusion import _transformer
from gemma.gm.nn.gemma4 import _config
from gemma.gm.text import _sampler_loop
from gemma.gm.typing import _common
import jax
import jax.numpy as jnp
from kauldron.ktyping import Bool, Float, Int, PRNGKey, typechecked  # pylint: disable=g-multiple-import,g-importing-member

# Minimum value for the temperature to ensure numerical stability.
_MIN_TEMP = 1e-12
PAD_TOKEN = 0


Embeddings = Float['*B L D']
Logits = Float['*B L V']
NoiseProportion = Float['*B']
Tokens = Int['*B L']


@dataclasses.dataclass(frozen=True)
class LinearSchedule:
  """Linear noise schedule."""

  def noise_probability(self, noise_proportion: Float) -> Float:  # pyrefly: ignore[not-a-type]
    return noise_proportion

  def derivative_noise_probability(self, noise_proportion: Float) -> Float:  # pyrefly: ignore[not-a-type]
    del noise_proportion
    return jnp.array(1.0)


@dataclasses.dataclass(frozen=True)
class DiffusionProcess:
  """Diffusion process for multinomial diffusion."""

  noise_schedule: LinearSchedule = dataclasses.field(
      default_factory=LinearSchedule
  )

  def get_initial_sample(
      self,
      rng: PRNGKey,
      batch_size: int,
      canvas_length: int,
      text_vocab_size: int,
  ) -> Tokens:
    """Create an initial noisy canvas of random tokens for sampling."""

    return jax.random.randint(
        rng,
        shape=(batch_size, canvas_length),
        minval=0,
        maxval=text_vocab_size,
    )

  def add_noise_to_tokens(
      self,
      rng: PRNGKey,
      canvas_tokens: Tokens,
      noise_proportion: Float['*B'],
      text_vocab_size: int,
  ) -> Tokens:
    """Adds noise to the tokens."""
    rng_mask, rng_tokens = jax.random.split(rng)

    prob_noise = jax.vmap(self.noise_schedule.noise_probability)(
        noise_proportion
    )
    noise_mask = jax.random.bernoulli(
        rng_mask,
        p=prob_noise[:, None],
        shape=canvas_tokens.shape,
    )
    random_tokens = jax.random.randint(
        rng_tokens,
        shape=canvas_tokens.shape,
        minval=0,
        maxval=text_vocab_size,
    )
    return jnp.where(noise_mask, random_tokens, canvas_tokens)


@dataclasses.dataclass(frozen=True)
class SampleFromPredictions:
  """Samples tokens from the predicted logits.

  Selects tokens based on the model's confidence and renoises non-selected
  positions.

  Attributes:
    entropy_bound: Confidence threshold controlling how many tokens are accepted
      per step. Lower values accept fewer tokens (more conservative).
    text_vocab_size: Vocabulary size, needed for renoising non-selected tokens.
  """

  entropy_bound: float = 0.1
  text_vocab_size: int = 0

  def __call__(
      self,
      *,
      rng: PRNGKey,
      denoiser_logits: Logits,
      canvas: Tokens,
      current_noise_proportion: NoiseProportion,
      target_noise_proportion: NoiseProportion,
  ) -> Tokens:
    """Returns the sample step output.

    Args:
      rng: RNG key.
      denoiser_logits: Shaped logits from the denoiser.
      canvas: The current noisy canvas from the previous step.
      current_noise_proportion: The noise level of the current canvas.
      target_noise_proportion: The desired noise level after this step.

    Returns:
      The denoised tokens after applying confidence-based selection and
      renoising non-selected positions.
    """
    del current_noise_proportion

    categorical_rng, noise_rng = jax.random.split(rng)
    denoiser_tokens = jax.random.categorical(
        categorical_rng, denoiser_logits.astype(jnp.float32)
    )
    batch_size = canvas.shape[0]

    # Compute per-token entropy from the logits.
    log_probs = jax.nn.log_softmax(denoiser_logits.astype(jnp.float32))
    probs = jnp.exp(log_probs)
    safe_log_probs = jnp.where(probs == 0, 0.0, log_probs)
    token_entropy = -jnp.sum(safe_log_probs * probs, axis=-1)  # [B, L]

    # Sort tokens by entropy (ascending) and build the selection mask.
    sorted_index = jnp.argsort(token_entropy, axis=-1)
    sorted_entropy = jnp.take_along_axis(token_entropy, sorted_index, axis=-1)
    accumulated_entropy = jnp.cumsum(sorted_entropy, axis=-1)

    # Accept k tokens where accumulated - sorted <= entropy_bound.
    sorted_selection_mask = (
        accumulated_entropy - sorted_entropy
    ) <= self.entropy_bound

    # Scatter the sorted mask back to original positions.
    selection_mask = (
        jnp.zeros_like(sorted_index, dtype=jnp.bool_)
        .at[jnp.arange(batch_size)[:, None], sorted_index]
        .set(sorted_selection_mask)
    )
    is_final_step = target_noise_proportion <= 0.0
    selection_mask = selection_mask | is_final_step[:, None]

    # Renoise all non-selected tokens with uniform random tokens.
    # Selected positions get denoiser tokens.
    random_tokens = jax.random.randint(
        noise_rng,
        shape=canvas.shape,
        minval=0,
        maxval=self.text_vocab_size,
    )
    output_tokens = jnp.where(selection_mask, denoiser_tokens, random_tokens)

    return output_tokens


@flax.struct.dataclass
class SampleStepOutput:
  """Output of the diffusion sampler.

  Attributes:
    sampled_tokens: The tokens sampled in this step.
    sc_embeddings: The self conditioning signal to feed back into the
      transformer.
    logits: The predicted logits from this step.
    modified_tokens_mask: A mask indicating which tokens were modified during
      this sampling step.
  """

  sampled_tokens: Tokens
  sc_embeddings: Embeddings
  logits: Logits
  modified_tokens_mask: Bool['*B L']


@flax.struct.dataclass
class _WhileLoopCarry:
  """Carry state for the jax.lax.while_loop in sample_next_canvas."""

  step: Int['']
  canvas: Tokens
  sc_embeddings: Embeddings
  rng: PRNGKey
  done: Bool['B']  # pyrefly: ignore[unknown-name]


@dataclasses.dataclass(frozen=True, kw_only=True)
class AnnealingTemperatureShaperConfig:
  """Configuration for AnnealingTemperatureShaper.

  Attributes:
    exponent: Controls the shape of the temperature curve as a function of
      `noise_proportion`. The temperature interpolates from `max_temperature`
      (when `noise_proportion`=1) down to `min_temperature` (when
      `noise_proportion`=0) based on the formula: `factor = 1 - (1 -
      noise_proportion)**exponent`. - exponent = 1: Linear decrease in
      temperature. - exponent > 1: Temperature decreases slower initially,
      faster later. - exponent < 1: Temperature decreases faster initially,
      slower later.
    max_temperature: The temperature used at the beginning (noise_proportion=1).
    min_temperature: The temperature used at the end (noise_proportion=0).
  """

  exponent: float = 1.0
  max_temperature: float = 0.8
  min_temperature: float = 0.4

  def __post_init__(self):
    if self.min_temperature < _MIN_TEMP:
      raise ValueError(f'{self.min_temperature=} should be >= {_MIN_TEMP=}')
    if self.max_temperature < self.min_temperature:
      raise ValueError(
          f'{self.max_temperature=} should be >= {self.min_temperature=}'
      )

  def make(self) -> 'AnnealingTemperatureShaper':
    return AnnealingTemperatureShaper(config=self)


@dataclasses.dataclass(frozen=True)
class AnnealingTemperatureShaper:
  """Scales logits by a temperature that anneals based on noise_proportion.

  The temperature decreases from `max_temperature` (when noise_proportion=1)
  down to `min_temperature` (when noise_proportion=0) according to a power law
  controlled by the `exponent` parameter in the config.
  """

  config: AnnealingTemperatureShaperConfig

  @typechecked
  def __call__(
      self,
      logits: Float['*B L V'],
      noise_proportion: Float['*B'],
  ) -> Float['*B L V']:

    # Calculate temperature directly from noise_proportion.
    # noise_proportion goes from ~1 down to ~0.
    # (1 - noise_proportion) goes from ~0 up to ~1.
    # (1 - noise_proportion)**exponent goes from ~0 up to ~1.
    # 1 - (1 - noise_proportion)**exponent goes from ~1 down to ~0.
    # This matches the range needed for the final scaling.
    temperature_fraction = (
        1.0
        - (1.0 - noise_proportion.astype(logits.dtype)) ** self.config.exponent
    )

    # Scale to the final range [min_temperature, max_temperature].
    temperature = (
        temperature_fraction
        * (self.config.max_temperature - self.config.min_temperature)
    ) + self.config.min_temperature  # Shape [Batch]
    temperature = temperature.astype(logits.dtype)

    # Apply temperature scaling.
    out_logits = logits / temperature[:, None, None]

    return out_logits.astype(logits.dtype)


@typechecked
def _truncate_canvas_at_stop_tokens(
    canvas: Tokens,
    *,
    end_tokens: tuple[int, ...],
    canvas_length: int,
    done: Bool['B'],  # pyrefly: ignore[unknown-name]
) -> tuple[Tokens, Bool['B']]:  # pyrefly: ignore[unknown-name]
  """Replaces tokens after the first stop token with PAD_TOKEN."""
  end_tokens_arr = jnp.array(end_tokens, dtype=jnp.int32)
  is_stop_token = jnp.isin(canvas, end_tokens_arr)
  batch_has_stop_token = jnp.any(is_stop_token, axis=-1)

  first_stop_idx = jnp.argmax(is_stop_token, axis=-1)

  seq_idx = jnp.arange(canvas_length)[None, :]
  keep_mask = seq_idx <= jnp.where(
      batch_has_stop_token[:, None],
      first_stop_idx[:, None],
      canvas_length,
  )
  keep_mask = keep_mask & ~done[:, None]
  canvas = jnp.where(keep_mask, canvas, PAD_TOKEN)

  return canvas, batch_has_stop_token


@dataclasses.dataclass(frozen=True, kw_only=True)
class DiffusionSampler(_sampler_loop.SamplerLoop):
  """Diffusion sampler, combining the sampling loop and diffusion algorithm.

  On top of the base SamplerLoop, holds diffusion-specific attributes and
  overrides the `_sample_step` method to implement block-wise diffusion
  sampling. Each `_sample_step` produces a full canvas of tokens.
  """

  diffusion_process: DiffusionProcess = dataclasses.field(
      default_factory=DiffusionProcess
  )
  logit_shaper: AnnealingTemperatureShaper = dataclasses.field(
      default_factory=lambda: AnnealingTemperatureShaper(
          config=AnnealingTemperatureShaperConfig()
      )
  )
  sample_from_predictions: SampleFromPredictions = dataclasses.field(
      default_factory=SampleFromPredictions
  )
  canvas_length: int
  max_denoising_steps: int
  text_vocab_size: int
  sliding_window_size: int | None = None
  early_stop_fn: _early_stopping.EarlyStopFn = dataclasses.field(
      default_factory=_early_stopping.NoEarlyStop
  )

  @typechecked
  def sample_next_canvas(
      self,
      *,
      canvas_length: int,
      max_denoising_steps: int,
      batch_size: int,
      cache: _config.Cache | None,
      params: _common.Params,
      rng: PRNGKey,
      full_attention_mask: Bool['*B CacheLength'] | None = None,
  ) -> Tokens:
    """Samples a complete denoised canvas from an initial noisy canvas.

    This function performs a multi-step denoising process, starting from a
    fully noisy canvas and iteratively refining it over `max_denoising_steps`.

    Args:
      canvas_length: The length of the token sequence to sample.
      max_denoising_steps: The number of denoising steps to perform.
      batch_size: The batch size.
      cache: Optional KV cache for the transformer.
      params: The transformer model parameters.
      rng: JAX PRNGKey.
      full_attention_mask: full attention mask used for previous canvas

    Returns:
      The fully denoised token canvas of shape [*B, canvas_length].
    """
    initial_canvas_rng, step_rng = jax.random.split(rng)
    del rng

    if cache is not None:
      cache_layer = list(cache.values())[0]
      cache_length = cache_layer['k'].shape[1]
      samples_in_cache: Int['*B'] = cache_layer['end_index']
      if full_attention_mask is not None:
        valid_prefix = (
            jnp.arange(cache_length)[None, :] < samples_in_cache[:, None]
        ) & full_attention_mask
        unpadded_offset = jnp.sum(valid_prefix.astype(jnp.int32), axis=-1)
      else:
        unpadded_offset = samples_in_cache
      positions = unpadded_offset[:, None] + jnp.arange(canvas_length)[None, :]
    else:
      cache_length = None
      samples_in_cache = None
      positions = jnp.broadcast_to(
          jnp.arange(canvas_length)[None, :], (batch_size, canvas_length)
      )

    attention_mask = _make_global_attention_mask(
        batch_size=batch_size,
        canvas_length=canvas_length,
        cache_length=cache_length,
        num_valid_tokens=samples_in_cache,
        full_attention_mask=full_attention_mask,
    )

    block_local_mask = _make_block_local_attention_mask(
        batch_size=batch_size,
        canvas_length=canvas_length,
        sliding_window_size=self.sliding_window_size,
        cache_length=cache_length,
        num_valid_tokens=samples_in_cache,
    )

    initial_tokens = self.diffusion_process.get_initial_sample(
        rng=initial_canvas_rng,
        batch_size=batch_size,
        canvas_length=canvas_length,
        text_vocab_size=self.text_vocab_size,
    )

    # Pre-compute noise proportions at each step boundary.
    # noise_proportions[i] = 1.0 - i / max_denoising_steps, so:
    #   noise_proportions[0] = 1.0 (fully noisy)
    #   noise_proportions[max_denoising_steps] = 0.0 (fully denoised)
    # At step i: current = noise_proportions[step],
    #            target  = noise_proportions[step + 1].
    noise_proportions = (
        1.0 - jnp.arange(max_denoising_steps + 1) / max_denoising_steps
    )

    embed_dim = cast(_config.TransformerConfig, self.model.config).embed_dim

    def cond_fn(carry: _WhileLoopCarry) -> Bool['']:
      return jnp.logical_and(
          ~jnp.all(carry.done),
          carry.step < max_denoising_steps,
      )

    def body_fn(carry: _WhileLoopCarry) -> _WhileLoopCarry:
      step = carry.step
      next_rng, sample_rng = jax.random.split(carry.rng)

      current_noise_proportion = jnp.full(
          (batch_size,), noise_proportions[step]
      )
      target_noise_proportion = jnp.full(
          (batch_size,), noise_proportions[step + 1]
      )
      out = self.sample_step(
          canvas=carry.canvas,
          sc_embeddings=carry.sc_embeddings,
          cache=cache,
          positions=positions,
          attention_mask=attention_mask,
          sliding_attention_mask=block_local_mask,
          current_noise_proportion=current_noise_proportion,
          target_noise_proportion=target_noise_proportion,
          params=params,
          rng=sample_rng,
      )

      new_done = jnp.logical_or(
          carry.done,
          self.early_stop_fn.should_stop(
              step=step,
              canvas=out.sampled_tokens,
              previous_canvas=carry.canvas,
              logits=out.logits,
          ),
      )

      # Freeze canvas for done elements. sc_embeddings don't need freezing
      # because done elements' canvases are frozen, so model outputs for
      # them are discarded on the next iteration anyway.
      canvas = jnp.where(carry.done[:, None], carry.canvas, out.sampled_tokens)

      return _WhileLoopCarry(
          step=step + 1,
          canvas=canvas,
          sc_embeddings=out.sc_embeddings.astype(carry.sc_embeddings.dtype),
          rng=next_rng,
          done=new_done,
      )

    init_carry = _WhileLoopCarry(
        step=jnp.int32(0),
        canvas=initial_tokens,
        sc_embeddings=jnp.zeros(
            (batch_size, canvas_length, embed_dim),
            dtype=jnp.bfloat16,
        ),
        rng=step_rng,
        done=jnp.zeros(batch_size, dtype=jnp.bool_),
    )

    final_carry = jax.lax.while_loop(cond_fn, body_fn, init_carry)

    return final_carry.canvas

  @functools.partial(jax.jit, static_argnames=('self',))
  @typechecked
  @override
  def _sample_step(
      self,
      state: _sampler_loop.SamplingState,
      *,
      params: _common.Params,
  ) -> _sampler_loop.SamplingState:
    """Single diffusion sampling step (full canvas, multiple tokens)."""
    next_rng, sample_rng = jax.random.split(state.rng)

    cache = state.cache
    cache_layer = list(cache.values())[0]
    batch_size = cache_layer['end_index'].shape[0]

    canvas = self.sample_next_canvas(
        canvas_length=self.canvas_length,
        max_denoising_steps=self.max_denoising_steps,
        batch_size=batch_size,
        cache=cache,
        params=params,
        rng=sample_rng,
        full_attention_mask=state.full_attention_mask,
    )

    canvas, batch_has_stop_token = _truncate_canvas_at_stop_tokens(
        canvas,
        end_tokens=self.end_tokens,
        canvas_length=self.canvas_length,
        done=state.done,
    )

    cache = self.append_tokens_to_cache(
        tokens=canvas,
        cache=cache,
        params=params,
    )

    done = state.done | batch_has_stop_token

    indices = jnp.arange(self.canvas_length) + state.step
    predicted_tokens = state.predicted_tokens.at[:, indices].set(canvas)

    return _sampler_loop.SamplingState(
        step=state.step + self.canvas_length,
        done=done,
        last_token=canvas[:, -1],
        last_token_pos=state.last_token_pos + self.canvas_length,
        predicted_tokens=predicted_tokens,
        cache=cache,
        rng=next_rng,
        init_cache_length=state.init_cache_length,
        full_attention_mask=state.full_attention_mask,
    )

  @typechecked
  def sample_step(
      self,
      *,
      canvas: Tokens,
      sc_embeddings: Embeddings,
      cache: _config.Cache | None,
      positions: Int['*B L'] | None,
      attention_mask: Bool['*B CanvasLength CachePlusCanvasLength'] | None,
      sliding_attention_mask: (
          Bool['*B CanvasLength CachePlusCanvasLength'] | None
      ) = None,
      current_noise_proportion: NoiseProportion,
      target_noise_proportion: NoiseProportion,
      params: _common.Params,
      rng: PRNGKey,
  ) -> SampleStepOutput:
    """Performs a single sampling step."""

    transformer_output = self.model.apply(
        {'params': params},
        tokens=canvas,
        sc_embeddings=sc_embeddings,  # pyrefly: ignore[unexpected-keyword]
        cache=cache,
        positions=positions,
        attention_mask=attention_mask,
        sliding_attention_mask=sliding_attention_mask,  # pyrefly: ignore[unexpected-keyword]
        method=_transformer.DiffusionMixin.call_with_self_conditioning,  # pyrefly: ignore[unexpected-keyword]
    )

    shaped_prediction = self.logit_shaper(
        logits=transformer_output.logits,  # pyrefly: ignore[missing-attribute]
        noise_proportion=current_noise_proportion,
    )

    sampled = self.sample_from_predictions(
        rng=rng,
        denoiser_logits=shaped_prediction,
        canvas=canvas,
        current_noise_proportion=current_noise_proportion,
        target_noise_proportion=target_noise_proportion,
    )

    # Encode the shaped logits into embeddings for self-conditioning in the
    # next denoising step, using the model's own Embedder.encode_logits method.
    new_sc_embeddings = self.model.apply(
        {'params': params},
        shaped_prediction,
        method=lambda self, x: self.embedder.encode_logits(x),  # pyrefly: ignore[unexpected-keyword]
    )

    return SampleStepOutput(
        sc_embeddings=new_sc_embeddings,
        logits=shaped_prediction,
        sampled_tokens=sampled,
        modified_tokens_mask=sampled != canvas,
    )

  @typechecked
  def append_tokens_to_cache(
      self,
      *,
      tokens: Tokens,
      cache: _config.Cache,
      params: _common.Params,
  ) -> _config.Cache:
    """Inserts tokens into the cache via a transformer forward pass.

    Uses a causal attention mask so that each token can attend to all valid
    cached tokens and to preceding tokens in the input, but not to future
    tokens.

    Args:
      tokens: Tokens to insert, shaped [batch_size, seq_len].
      cache: The current KV cache.
      params: Model parameters.

    Returns:
      The updated cache with the tokens inserted.
    """

    seq_len = tokens.shape[1]

    cache_layer = list(cache.values())[0]
    cache_length = cache_layer['k'].shape[1]
    samples_in_cache: Int['B'] = cache_layer['end_index']  # pyrefly: ignore[unknown-name]
    positions = samples_in_cache[:, None] + jnp.arange(seq_len)[None, :]

    attention_mask = _make_causal_attention_mask(
        batch_size=tokens.shape[0],
        canvas_length=seq_len,
        cache_length=cache_length,
        num_valid_cache_tokens=samples_in_cache,
    )

    output = self.model.apply(
        {'params': params},
        tokens=tokens,
        cache=cache,
        positions=positions,
        attention_mask=attention_mask,
    )

    return output.cache  # pyrefly: ignore[missing-attribute]


@typechecked
def _make_global_attention_mask(
    batch_size: int,
    canvas_length: int,
    cache_length: int | None,
    num_valid_tokens: Int['*B'] | None,
    full_attention_mask: Bool['*B CacheLength'] | None = None,
) -> Bool['*B CanvasLength CacheLength']:
  """Create attention mask for the diffusion sampler.

  The canvas has full self attention.  The cache is left aligned, right padded,
  has 1's for valid samples and 0's for padding.

  The canvas is inserted into the cache before attention so the total mask
  length is just cache length.

  Args:
    batch_size: The batch size.
    canvas_length: The length of the canvas.
    cache_length: The length of the cache. If None, no cache is used.
    num_valid_tokens: The number of valid tokens in the cache. Required if
      cache_length is not None.
    full_attention_mask: The full attention mask for prompt and cache.

  Returns:
    The attention mask.
  """

  if cache_length is None:
    return jnp.ones((batch_size, canvas_length, canvas_length), dtype=jnp.bool_)

  if num_valid_tokens is None:
    raise ValueError(
        'num_valid_samples must be provided if cache_length is set.'
    )

  total_valid = jnp.minimum(num_valid_tokens + canvas_length, cache_length)
  mask = jnp.arange(cache_length)[None, :] < total_valid[:, None]
  if full_attention_mask is not None:
    mask = mask & full_attention_mask

  return jnp.broadcast_to(
      mask[:, None, :], (batch_size, canvas_length, cache_length)
  )


@typechecked
def _make_causal_attention_mask(
    batch_size: int,
    canvas_length: int,
    cache_length: int | None,
    num_valid_cache_tokens: Int['B'] | None,  # pyrefly: ignore[unknown-name]
) -> Bool['B SeqLen CacheLength']:
  """Create a causal attention mask for inserting tokens into the cache.

  Args:
    batch_size: The batch size.
    canvas_length: Number of new tokens being inserted.
    cache_length: Total cache size.
    num_valid_cache_tokens: Per-batch number of samples in the cache before
      inserting new tokens.  If this is larger than cache_length the cache is
      assumed to be full and the oldest samples have been evicted.

  Returns:
    Attention mask of shape [batch_size, canvas_length, cache_length].
  """

  if cache_length is None:
    causal_mask = jnp.tril(
        jnp.ones((canvas_length, canvas_length), dtype=jnp.bool_)
    )
    return jnp.broadcast_to(
        causal_mask[None, :, :], (batch_size, canvas_length, canvas_length)
    )

  if num_valid_cache_tokens is None:
    raise ValueError(
        'num_valid_cache_tokens must be provided if cache_length is set.'
    )

  valid_entries = jnp.minimum(num_valid_cache_tokens, cache_length)

  # 1. Fill base mask up to the number of valid tokens in the cache.
  mask = jnp.broadcast_to(
      jnp.arange(cache_length)[None, None, :] < valid_entries[:, None, None],
      (batch_size, canvas_length, cache_length),
  )

  # 2. Append a lower triangular matrix at the (wrapped) write positions.
  write_indices = (
      num_valid_cache_tokens[:, None] + jnp.arange(canvas_length)[None, :]
  ) % cache_length

  batch_idx = jnp.arange(batch_size)[:, None, None]
  seq_idx = jnp.arange(canvas_length)[None, :, None]
  write_idx = write_indices[:, None, :]

  causal_mask = jnp.tril(
      jnp.ones((canvas_length, canvas_length), dtype=jnp.bool_)
  )

  mask = mask.at[batch_idx, seq_idx, write_idx].set(causal_mask[None, :, :])

  return mask


@typechecked
def _make_block_local_attention_mask(
    batch_size: int,
    canvas_length: int,
    sliding_window_size: int | None,
    cache_length: int | None,
    num_valid_tokens: Int['*B'] | None,
) -> Bool['*B CanvasLength CacheLength'] | None:
  """Create block-local attention mask for LOCAL_SLIDING layers in diffusion.

  Block-local attention semantics: all canvas tokens share
  the same context window and have full self-attention among themselves.

  For each canvas query token, the mask allows attending to:
    - Context tokens in [context_end - sliding_window_size, context_end),
      where context_end is the position of the first canvas token. This window
      is the same for ALL canvas tokens.
    - All other canvas tokens (full bidirectional self-attention).

  Args:
    batch_size: The batch size.
    canvas_length: The length of the canvas.
    sliding_window_size: The sliding window size. If None, returns None (global
      attention layers will use the regular attention_mask).
    cache_length: The length of the cache. If None, no cache is used.
    num_valid_tokens: The number of valid tokens in the cache before inserting
      canvas tokens. Required if cache_length is not None.

  Returns:
    The block-local attention mask, or None if sliding_window_size is None.
  """
  if sliding_window_size is None:
    return None

  if cache_length is None:
    # No cache = no context. Full canvas self-attention.
    return jnp.ones((batch_size, canvas_length, canvas_length), dtype=jnp.bool_)

  if num_valid_tokens is None:
    raise ValueError(
        'num_valid_tokens must be provided if cache_length is set.'
    )

  # Context boundary: first canvas position in the cache.
  # context_end = num_valid_tokens (index of first canvas token)
  context_end = num_valid_tokens  # [B]
  context_start = jnp.maximum(context_end - sliding_window_size, 0)  # [B]

  cache_indices = jnp.arange(cache_length)[None, :]  # [1, cache_length]

  # Context portion: same window for ALL canvas tokens.
  # Attend to context positions in [context_start, context_end).
  context_mask = (cache_indices >= context_start[:, None]) & (
      cache_indices < context_end[:, None]
  )

  # Canvas portion: all canvas tokens attend to all other canvas tokens.
  # Canvas is written at [num_valid_tokens, num_valid_tokens + canvas_length).
  canvas_end = jnp.minimum(num_valid_tokens + canvas_length, cache_length)
  canvas_mask = (cache_indices >= num_valid_tokens[:, None]) & (
      cache_indices < canvas_end[:, None]
  )

  # Combine: attend to context window OR canvas self-attention.
  combined = context_mask | canvas_mask  # [B, cache_length]

  return jnp.broadcast_to(
      combined[:, None, :], (batch_size, canvas_length, cache_length)
  )