File size: 37,248 Bytes
4700ca8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the Apache License, Version 2.0
# found in the LICENSE file in the root directory of this source tree.

# References:
#   https://github.com/facebookresearch/dino/blob/master/vision_transformer.py
#   https://github.com/rwightman/pytorch-image-models/tree/master/timm/models/vision_transformer.py

import logging
import os
import math
import warnings
import torch

from torch import Tensor
from torch import nn
import torch.nn.functional as F

from lingbot_map.layers.rope import apply_rotary_emb

from einops import rearrange

# FlashInfer imports (optional - for paged attention)
try:
    import flashinfer
    FLASHINFER_AVAILABLE = True
except ImportError:
    FLASHINFER_AVAILABLE = False
    print("flashinfer not available")

try:
    from torchtitan.distributed.sequence_parallel import (
        gather_seq_scatter_heads,
        gather_heads_scatter_seq,
        pad_tensor,
        slice_input_tensor_scale_grad,
        gather_outputs,
    )
except ImportError:
    print("torchtitan not available for ulysses cp")

def gather_seq_scatter_heads_qkv(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, seq_dim: int, head_dim: int):
    """Gather sequence dimension and scatter head dimension for Q, K, V tensors."""
    q = gather_seq_scatter_heads(q, seq_dim, head_dim)
    k = gather_seq_scatter_heads(k, seq_dim, head_dim)
    v = gather_seq_scatter_heads(v, seq_dim, head_dim)
    return q, k, v

from typing_extensions import List
from typing import Optional, Tuple


class Attention(nn.Module):
    def __init__(
        self,
        dim: int,
        num_heads: int = 8,
        qkv_bias: bool = True,
        proj_bias: bool = True,
        attn_drop: float = 0.0,
        proj_drop: float = 0.0,
        norm_layer: nn.Module = nn.LayerNorm,
        qk_norm: bool = False,
        fused_attn: bool = True,  # use F.scaled_dot_product_attention or not
        rope=None,
    ) -> None:
        super().__init__()
        assert dim % num_heads == 0, "dim should be divisible by num_heads"
        self.num_heads = num_heads
        self.head_dim = dim // num_heads
        self.scale = self.head_dim**-0.5
        self.fused_attn = fused_attn

        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
        self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
        self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
        self.attn_drop = nn.Dropout(attn_drop)
        self.proj = nn.Linear(dim, dim, bias=proj_bias)
        self.proj_drop = nn.Dropout(proj_drop)
        self.rope = rope

    def forward(self, x: Tensor, pos=None, enable_ulysses_cp=False, num_patches=None, num_special=None, num_frames=None, enable_3d_rope=False) -> Tensor:
        B, N, C = x.shape
        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
        q, k, v = qkv.unbind(0)
        q, k = self.q_norm(q), self.k_norm(k)

        if enable_ulysses_cp:
            q, k, v = gather_seq_scatter_heads_qkv(q, k, v, seq_dim=2, head_dim=1)

        if self.rope is not None:
            q = self.rope(q, pos)
            k = self.rope(k, pos)

        if self.fused_attn:
            x = F.scaled_dot_product_attention(q, k, v, dropout_p=self.attn_drop.p if self.training else 0.0)
        else:
            q = q * self.scale
            attn = q @ k.transpose(-2, -1)
            attn = attn.softmax(dim=-1)
            attn = self.attn_drop(attn)
            x = attn @ v

        if enable_ulysses_cp:
            x = gather_heads_scatter_seq(x, seq_dim=2, head_dim=1)

        x = x.transpose(1, 2).reshape(B, -1, self.num_heads * self.head_dim)
        x = self.proj(x)
        x = self.proj_drop(x)
        return x


class CausalAttention(nn.Module):
    """
    Causal self-attention module with KV cache support for streaming inference.
    Used by CasualBlockCamera in camera_head.py.
    """
    def __init__(
        self,
        dim: int,
        num_heads: int = 8,
        qkv_bias: bool = True,
        proj_bias: bool = True,
        attn_drop: float = 0.0,
        proj_drop: float = 0.0,
        norm_layer: nn.Module = nn.LayerNorm,
        qk_norm: bool = False,
        fused_attn: bool = True,  # use F.scaled_dot_product_attention or not
        rope=None,
        elementwise_attn_output_gate=False,
        # KV cache eviction parameters (matching build_attn_mask)
        kv_cache_sliding_window: int =64,
        kv_cache_scale_frames: int = 8,
        kv_cache_cross_frame_special: bool = True,
        kv_cache_include_scale_frames: bool = True,
        kv_cache_camera_only: bool = False,  # If True, only cache camera token (no scale token)
    ) -> None:
        super().__init__()
        assert dim % num_heads == 0, "dim should be divisible by num_heads"
        self.num_heads = num_heads
        self.head_dim = dim // num_heads
        self.scale = self.head_dim**-0.5
        self.fused_attn = fused_attn

        self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
        self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
        self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
        self.attn_drop = nn.Dropout(attn_drop)
        self.proj = nn.Linear(dim, dim, bias=proj_bias)
        self.proj_drop = nn.Dropout(proj_drop)
        self.rope = rope

        self.gate_proj = nn.Linear(dim, dim, bias=True) if elementwise_attn_output_gate else None

        # Store KV cache eviction parameters
        self.kv_cache_sliding_window = kv_cache_sliding_window
        self.kv_cache_scale_frames = kv_cache_scale_frames
        self.kv_cache_cross_frame_special = kv_cache_cross_frame_special
        self.kv_cache_include_scale_frames = kv_cache_include_scale_frames
        self.kv_cache_camera_only = kv_cache_camera_only

    def forward(self, x: Tensor, block_mask=None, pos=None, pos_kv=None, frame_seqlen=None, video_mask=None, kv_cache=None, current_start=0, current_end=0, global_idx=0, num_frame_per_block=1, num_frame_for_scale=-1, enable_3d_rope=False, sliding_window_size=-1, attend_to_scale_frames=False, num_random_frames=0, attend_to_special_tokens=False, num_register_tokens=4, enable_ulysses_cp=False, is_scale_frames=False) -> Tensor:
        B, N, C = x.shape

        # Calculate special token indices
        camera_token_idx = 0
        scale_token_idx = camera_token_idx + num_register_tokens + 1  # camera + register tokens + scale

        # [3, B, num_heads, N, head_dim]
        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
        q, k, v = qkv.unbind(0)

        if self.gate_proj is not None:
            gate_score = self.gate_proj(x).reshape(B, N, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
        if kv_cache is None:
            q, k = self.q_norm(q), self.k_norm(k)
            if enable_ulysses_cp:
                q, k, v = gather_seq_scatter_heads_qkv(q, k, v, seq_dim=2, head_dim=1)
                N = q.shape[2]  # Update N after gather
            if self.rope is not None and not enable_3d_rope:
                q = self.rope(q, pos)
                k = self.rope(k, pos)
            elif enable_3d_rope and pos is not None:
                q = apply_rotary_emb(q, pos)
                k = apply_rotary_emb(k, pos)

            with torch.no_grad():
                block_mask = block_mask.squeeze()[:q.shape[2], :k.shape[2]]
                if block_mask.dim() == 2:
                    block_mask = block_mask.unsqueeze(0).unsqueeze(0)  # [1, 1, N, N]
                block_mask = block_mask.expand(B, 1, block_mask.shape[-2], block_mask.shape[-1])

                video_mask = video_mask.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)  if  video_mask is not None else torch.ones_like(block_mask, device=block_mask.device) # [1, 1, N, N]
                video_mask = video_mask.expand(B, 1, block_mask.shape[-2], block_mask.shape[-1])

                mask = block_mask | ~video_mask

                # Apply sliding window mask if sliding_window_size > 0
                # sliding_window_size is in units of num_frame_per_block
                if sliding_window_size > 0 and frame_seqlen is not None:
                    # Create sliding window mask: each frame can only attend to frames within the window
                    num_frames = N // frame_seqlen
                    sliding_mask = torch.zeros_like(mask, dtype=torch.bool)

                    for i in range(num_frames):
                        q_start = i * frame_seqlen
                        q_end = (i + 1) * frame_seqlen
                        # Calculate the window start: sliding_window_size is in units of num_frame_per_block
                        # So the actual window size in frames is sliding_window_size * num_frame_per_block
                        window_size_in_frames = sliding_window_size * num_frame_per_block
                        window_start_frame = max(0, i - window_size_in_frames + 1)
                        k_start = window_start_frame * frame_seqlen
                        k_end = (i + 1) * frame_seqlen  # Can attend up to current frame (causal)
                        sliding_mask[:, :, q_start:q_end, k_start:k_end] = True

                    # Combine with existing mask: both masks need to allow attention
                    mask = mask & sliding_mask

                    # If attend_to_scale_frames is True, also allow attention to first num_frame_for_scale frames
                    if num_frame_for_scale > 0:
                        for i in range(num_frames):
                            q_start = i * frame_seqlen
                            q_end = (i + 1) * frame_seqlen
                            # Allow attending to first num_frame_for_scale frames (directly set to True, not depending on block_mask)
                            mask[:, :, q_start:q_end, :num_frame_for_scale * frame_seqlen] = True

            ## global attention for the first num_frame_for_scale frames
            if num_frame_for_scale > 0:
                mask[:, :, :num_frame_for_scale * frame_seqlen, :num_frame_for_scale * frame_seqlen] = True

            if self.fused_attn:
                x = F.scaled_dot_product_attention(
                    q,
                    k,
                    v,
                    dropout_p=self.attn_drop.p if self.training else 0.0,
                    attn_mask=mask
                )
        else:
            # Apply RoPE to current k before caching
            q, k = self.q_norm(q), self.k_norm(k)

            if self.rope is not None and not enable_3d_rope:
                q = self.rope(q, pos)
                k = self.rope(k, pos)
            elif enable_3d_rope and pos is not None:
                q = apply_rotary_emb(q, pos)
                k = apply_rotary_emb(k, pos)

            # Check if we should skip appending to cache (non-keyframe in keyframe mode)
            skip_append = kv_cache.get("_skip_append", False)

            k_reshaped = k.view(B, self.num_heads, num_frame_per_block, N // num_frame_per_block, self.head_dim)
            v_reshaped = v.view(B, self.num_heads, num_frame_per_block, N // num_frame_per_block, self.head_dim)

            if not skip_append:
                # KEYFRAME: store in cache (original behavior)
                if kv_cache[f"k_{global_idx}"] is None:
                    kv_cache[f"k_{global_idx}"] = k_reshaped
                    kv_cache[f"v_{global_idx}"] = v_reshaped
                else:
                    num_frame_per_block = k.shape[2] // kv_cache[f"k_{global_idx}"].shape[3]
                    k_reshaped = k.view(B, self.num_heads, num_frame_per_block, N // num_frame_per_block, self.head_dim)
                    v_reshaped = v.view(B, self.num_heads, num_frame_per_block, N // num_frame_per_block, self.head_dim)
                    kv_cache[f"k_{global_idx}"] = torch.cat((kv_cache[f"k_{global_idx}"], k_reshaped), dim=2)
                    kv_cache[f"v_{global_idx}"] = torch.cat((kv_cache[f"v_{global_idx}"], v_reshaped), dim=2)

                # Apply sliding window eviction BEFORE attention to match causal_3drope behavior
                # This ensures current frame only attends to frames within the sliding window
                self._apply_kv_cache_eviction_causal(kv_cache, global_idx, camera_token_idx, scale_token_idx)

                # Retrieve full k, v from cache (already RoPE-applied, already evicted)
                k = kv_cache[f"k_{global_idx}"].clone()
                v = kv_cache[f"v_{global_idx}"].clone()
            else:
                # NON-KEYFRAME: attend to [cached + current] without storing in cache
                if kv_cache[f"k_{global_idx}"] is not None:
                    k = torch.cat((kv_cache[f"k_{global_idx}"], k_reshaped), dim=2)
                    v = torch.cat((kv_cache[f"v_{global_idx}"], v_reshaped), dim=2)
                else:
                    k = k_reshaped
                    v = v_reshaped
            a, b, c, d, e = k.shape

            k = k.reshape(a, b, c*d, e)
            v = v.reshape(a, b, c*d, e)

            # Prepend special tokens (camera + scale) from evicted frames if they exist
            if f"k_{global_idx}_special" in kv_cache and kv_cache[f"k_{global_idx}_special"] is not None:
                special_k = kv_cache[f"k_{global_idx}_special"]  # [B, H, num_evicted_frames, 2, D]
                special_v = kv_cache[f"v_{global_idx}_special"]
                sa, sb, sc, sd, se = special_k.shape
                special_k = special_k.reshape(sa, sb, sc * sd, se)  # [B, H, num_evicted*2, D]
                special_v = special_v.reshape(sa, sb, sc * sd, se)

                # Prepend special tokens (older frames first)
                k = torch.cat([special_k, k], dim=2)
                v = torch.cat([special_v, v], dim=2)

            # Note: k from cache is already RoPE-applied, no need to apply again

            if self.fused_attn:
                # Use mask-based SDPA to ensure same kernel as batch mode
                # The causal constraint is enforced by KV cache contents, not by mask
                mask = torch.ones(B, 1, q.shape[2], k.shape[2], dtype=torch.bool, device=q.device)
                x = F.scaled_dot_product_attention(
                    q,
                    k,
                    v,
                    dropout_p=self.attn_drop.p if self.training else 0.0,
                    attn_mask=mask,
                )

        if self.gate_proj is not None:
            x = x * torch.sigmoid(gate_score)
        if enable_ulysses_cp:
            x = gather_heads_scatter_seq(x, seq_dim=2, head_dim=1)
        # Use actual dimensions from attention output, not original input C
        # x shape: [B, H, seq_len, head_dim] -> [B, seq_len, H*head_dim]
        x = x.transpose(1, 2).reshape(B, -1, self.num_heads * self.head_dim)
        x = self.proj(x)
        x = self.proj_drop(x)
        return x

    def _apply_kv_cache_eviction_causal(self, kv_cache, global_idx, camera_token_idx, scale_token_idx):
        """
        Apply sliding window eviction to KV cache BEFORE attention.

        This ensures current frame only attends to frames within the sliding window,
        matching the behavior of causal_3drope's attention mask.
        """
        sliding_window_frames = self.kv_cache_sliding_window
        scale_frames = self.kv_cache_scale_frames

        if kv_cache[f"k_{global_idx}"].shape[3] > 1:
            num_cached_frames = kv_cache[f"k_{global_idx}"].shape[2]

            if num_cached_frames > sliding_window_frames + scale_frames:
                evict_start = scale_frames
                evict_end = num_cached_frames - sliding_window_frames

                if evict_end > evict_start:
                    evicted_k = kv_cache[f"k_{global_idx}"][:, :, evict_start:evict_end, :, :]
                    evicted_v = kv_cache[f"v_{global_idx}"][:, :, evict_start:evict_end, :, :]

                    if self.kv_cache_cross_frame_special:
                        if self.kv_cache_camera_only:
                            # Only keep camera token
                            new_special_k = evicted_k[:, :, :, camera_token_idx:camera_token_idx+1, :].clone()
                            new_special_v = evicted_v[:, :, :, camera_token_idx:camera_token_idx+1, :].clone()
                        else:
                            # Keep ALL special tokens (camera + register + scale) to match attention_mask behavior
                            # Special tokens are in range [camera_token_idx, scale_token_idx+1)
                            new_special_k = evicted_k[:, :, :, camera_token_idx:scale_token_idx+1, :].clone()
                            new_special_v = evicted_v[:, :, :, camera_token_idx:scale_token_idx+1, :].clone()

                        if f"k_{global_idx}_special" not in kv_cache or kv_cache[f"k_{global_idx}_special"] is None:
                            kv_cache[f"k_{global_idx}_special"] = new_special_k
                            kv_cache[f"v_{global_idx}_special"] = new_special_v
                        else:
                            kv_cache[f"k_{global_idx}_special"] = torch.cat(
                                [kv_cache[f"k_{global_idx}_special"], new_special_k], dim=2)
                            kv_cache[f"v_{global_idx}_special"] = torch.cat(
                                [kv_cache[f"v_{global_idx}_special"], new_special_v], dim=2)

                    if self.kv_cache_include_scale_frames:
                        kv_cache[f"k_{global_idx}"] = torch.cat([
                            kv_cache[f"k_{global_idx}"][:, :, :scale_frames, :, :],
                            kv_cache[f"k_{global_idx}"][:, :, -sliding_window_frames:, :, :]
                        ], dim=2)
                        kv_cache[f"v_{global_idx}"] = torch.cat([
                            kv_cache[f"v_{global_idx}"][:, :, :scale_frames, :, :],
                            kv_cache[f"v_{global_idx}"][:, :, -sliding_window_frames:, :, :]
                        ], dim=2)
                    else:
                        kv_cache[f"k_{global_idx}"] = kv_cache[f"k_{global_idx}"][:, :, -sliding_window_frames:, :, :]
                        kv_cache[f"v_{global_idx}"] = kv_cache[f"v_{global_idx}"][:, :, -sliding_window_frames:, :, :]


class FlashInferAttention(Attention):
    """
    FlashInfer variant of the GCT attention layer.
    Uses FlashInferKVCacheManager for paged KV cache storage and
    FlashInfer attention kernels (BatchPrefillWithPagedKVCacheWrapper).
    Supports the same optimized token layout and KV cache streaming inference.
    """

    def __init__(
        self,
        dim: int,
        num_heads: int = 8,
        qkv_bias: bool = True,
        proj_bias: bool = True,
        attn_drop: float = 0.0,
        proj_drop: float = 0.0,
        norm_layer: nn.Module = nn.LayerNorm,
        qk_norm: bool = False,
        fused_attn: bool = True,
        rope=None,
        # KV cache eviction parameters
        kv_cache_sliding_window: int = 64,
        kv_cache_scale_frames: int = 8,
        kv_cache_cross_frame_special: bool = True,
        kv_cache_include_scale_frames: bool = True,
        kv_cache_camera_only: bool = False,
    ) -> None:
        if not FLASHINFER_AVAILABLE:
            raise RuntimeError("FlashInfer is not available. Please install flashinfer.")

        super().__init__(
            dim=dim,
            num_heads=num_heads,
            qkv_bias=qkv_bias,
            proj_bias=proj_bias,
            attn_drop=attn_drop,
            proj_drop=proj_drop,
            norm_layer=norm_layer,
            qk_norm=qk_norm,
            fused_attn=fused_attn,
            rope=rope,
        )

        # Store KV cache eviction parameters
        self.kv_cache_sliding_window = kv_cache_sliding_window
        self.kv_cache_scale_frames = kv_cache_scale_frames
        self.kv_cache_cross_frame_special = kv_cache_cross_frame_special
        self.kv_cache_include_scale_frames = kv_cache_include_scale_frames
        self.kv_cache_camera_only = kv_cache_camera_only

    def prepare_qkv(self, x: Tensor, pos=None, enable_3d_rope: bool = False) -> tuple:
        """Fused pre-attention ops for single-frame streaming (Phase 2).

        Computes q/k/v from x, applies q_norm/k_norm/RoPE, and converts to
        [tpf, H, D] format ready for append_frame + compute_attention.

        Extracted as a method so torch.compile can capture all pre-attn ops as one
        CUDA graph (qkv linear -> reshape -> unbind -> q_norm -> k_norm -> RoPE x2 ->
        squeeze/permute/contiguous x3).
        """
        B, N, C = x.shape
        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
        q, k, v = qkv.unbind(0)  # Each: [B, num_heads, N, head_dim]
        q, k = self.q_norm(q), self.k_norm(k)
        if self.rope is not None and not enable_3d_rope:
            q = self.rope(q, pos)
            k = self.rope(k, pos)
        elif self.rope is not None:  # enable_3d_rope=True
            q = apply_rotary_emb(q, pos)
            k = apply_rotary_emb(k, pos)
        # Convert to [tpf, H, D] format for FlashInfer (B=1 in streaming mode)
        q_nhd = q.squeeze(0).permute(1, 0, 2).contiguous()
        k_nhd = k.squeeze(0).permute(1, 0, 2).contiguous()
        v_nhd = v.squeeze(0).permute(1, 0, 2).contiguous()
        return q_nhd, k_nhd, v_nhd

    def forward(self, x: Tensor, pos=None, enable_ulysses_cp=False,
                num_patches=None, num_special=None, num_frames=None, enable_3d_rope=False,
                # KV cache parameters (kv_cache is a FlashInferKVCacheManager or None)
                kv_cache=None, global_idx=0, num_frame_per_block=1,
                num_frame_for_scale=-1, num_register_tokens=4) -> Tensor:
        """
        Forward pass with FlashInfer paged KV cache and attention.

        Args:
            x: Input tensor [B, N, C]
            kv_cache: FlashInferKVCacheManager instance or None (batch mode)
            global_idx: Block index for per-block cache access
        """
        from lingbot_map.layers.flashinfer_cache import FlashInferKVCacheManager

        B, N, C = x.shape

        # Detect if using optimized layout
        using_optimized_layout = (num_patches is not None and num_special is not None
                                 and num_frames is not None)

        # ========== Batch Mode (no KV cache manager) ==========
        if not isinstance(kv_cache, FlashInferKVCacheManager):
            # [3, B, num_heads, N, head_dim]
            qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
            q, k, v = qkv.unbind(0)  # Each: [B, num_heads, N, head_dim]
            q, k = self.q_norm(q), self.k_norm(k)

            if enable_ulysses_cp:
                if using_optimized_layout:
                    boundary = num_frames * num_patches
                    q_patch, k_patch, v_patch = q[:, :, :boundary, :], k[:, :, :boundary, :], v[:, :, :boundary, :]
                    q_special, k_special, v_special = q[:, :, boundary:, :], k[:, :, boundary:, :], v[:, :, boundary:, :]
                    q_patch, k_patch, v_patch = gather_seq_scatter_heads_qkv(
                        q_patch, k_patch, v_patch, seq_dim=2, head_dim=1
                    )
                    q_special, k_special, v_special = gather_seq_scatter_heads_qkv(
                        q_special, k_special, v_special, seq_dim=2, head_dim=1
                    )
                    q = torch.cat([q_patch, q_special], dim=2)
                    k = torch.cat([k_patch, k_special], dim=2)
                    v = torch.cat([v_patch, v_special], dim=2)
                else:
                    q, k, v = gather_seq_scatter_heads_qkv(q, k, v, seq_dim=2, head_dim=1)

            if self.rope is not None and not enable_3d_rope:
                q = self.rope(q, pos)
                k = self.rope(k, pos)
            elif self.rope is not None and enable_3d_rope:
                q = apply_rotary_emb(q, pos)
                k = apply_rotary_emb(k, pos)

            # Batch mode: use SDPA for numerical consistency with SDPA variant
            x = F.scaled_dot_product_attention(
                q, k, v,
                dropout_p=self.attn_drop.p if self.training else 0.0,
            )

            if enable_ulysses_cp:
                if using_optimized_layout:
                    seq_global = x.shape[2]
                    seq_local = num_frames * (num_patches + num_special)
                    cp_size = seq_global // seq_local
                    boundary_global = num_frames * cp_size * num_patches
                    x_patch = x[:, :, :boundary_global, :]
                    x_special = x[:, :, boundary_global:, :]
                    x_patch = gather_heads_scatter_seq(x_patch, seq_dim=2, head_dim=1)
                    x_special = gather_heads_scatter_seq(x_special, seq_dim=2, head_dim=1)
                    x = torch.cat([x_patch, x_special], dim=2)
                else:
                    x = gather_heads_scatter_seq(x, seq_dim=2, head_dim=1)

            x = x.transpose(1, 2).reshape(B, N, self.num_heads * self.head_dim)

        # ========== Streaming Mode (with FlashInferKVCacheManager) ==========
        else:
            manager = kv_cache  # FlashInferKVCacheManager

            # Phase 1 (scale frames): num_frames > 1 — multi-frame batch
            # Phase 2 (streaming):    num_frames == 1 — single frame
            is_multi_frame = (num_frames is not None and num_frames > 1)

            if is_multi_frame:
                # Phase 1: compute full self-attention via SDPA (all frames attend to each other),
                # then append each frame's K/V to the paged cache one at a time.
                qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
                q, k, v = qkv.unbind(0)
                q, k = self.q_norm(q), self.k_norm(k)

                # Apply RoPE before caching (RoPE baked into K before append)
                if self.rope is not None and not enable_3d_rope:
                    q = self.rope(q, pos)
                    k = self.rope(k, pos)
                elif self.rope is not None and enable_3d_rope:
                    q = apply_rotary_emb(q, pos)
                    k = apply_rotary_emb(k, pos)

                x = F.scaled_dot_product_attention(
                    q, k, v,
                    dropout_p=self.attn_drop.p if self.training else 0.0,
                )
                x = x.transpose(1, 2).reshape(B, N, self.num_heads * self.head_dim)

                # Append each frame's K/V to the paged cache individually.
                tpf = manager.tokens_per_frame
                k_all = k.squeeze(0).permute(1, 0, 2)  # [num_frames*tpf, H, D]
                v_all = v.squeeze(0).permute(1, 0, 2)
                for f_idx in range(num_frames):
                    s = f_idx * tpf
                    manager.append_frame(global_idx, k_all[s:s+tpf].contiguous(), v_all[s:s+tpf].contiguous())
                    manager.evict_frames(
                        block_idx=global_idx,
                        scale_frames=self.kv_cache_scale_frames,
                        sliding_window=self.kv_cache_sliding_window,
                        cross_frame_special=self.kv_cache_cross_frame_special,
                        include_scale_frames=self.kv_cache_include_scale_frames,
                        camera_only=self.kv_cache_camera_only,
                        num_register_tokens=num_register_tokens,
                    )
            else:
                # Phase 2: single-frame streaming via FlashInfer paged attention.
                q_nhd, k_nhd, v_nhd = self.prepare_qkv(x, pos=pos, enable_3d_rope=enable_3d_rope)

                # 1. Append to paged cache
                manager.append_frame(global_idx, k_nhd, v_nhd)

                # 2. Apply sliding window eviction
                manager.evict_frames(
                    block_idx=global_idx,
                    scale_frames=self.kv_cache_scale_frames,
                    sliding_window=self.kv_cache_sliding_window,
                    cross_frame_special=self.kv_cache_cross_frame_special,
                    include_scale_frames=self.kv_cache_include_scale_frames,
                    camera_only=self.kv_cache_camera_only,
                    num_register_tokens=num_register_tokens,
                )

                # 3. Compute attention via FlashInfer BatchPrefillWithPagedKVCacheWrapper
                x = manager.compute_attention(global_idx, q_nhd)

                # Convert back: [tpf, H, D] -> [B, tpf, C].
                x = x.reshape(B, q_nhd.shape[0], self.num_heads * self.head_dim)

        x = self.proj(x)
        x = self.proj_drop(x)
        return x


class SDPAAttention(Attention):
    """
    SDPA variant for streaming inference.
    Uses F.scaled_dot_product_attention with dict-based KV cache.
    No FlashInfer dependency required — works on any CUDA GPU.
    """

    def __init__(
        self,
        dim: int,
        num_heads: int = 8,
        qkv_bias: bool = True,
        proj_bias: bool = True,
        attn_drop: float = 0.0,
        proj_drop: float = 0.0,
        norm_layer: nn.Module = nn.LayerNorm,
        qk_norm: bool = False,
        fused_attn: bool = True,
        rope=None,
        kv_cache_sliding_window: int = 64,
        kv_cache_scale_frames: int = 8,
        kv_cache_cross_frame_special: bool = True,
        kv_cache_include_scale_frames: bool = True,
        kv_cache_camera_only: bool = False,
    ) -> None:
        super().__init__(
            dim=dim, num_heads=num_heads, qkv_bias=qkv_bias, proj_bias=proj_bias,
            attn_drop=attn_drop, proj_drop=proj_drop, norm_layer=norm_layer,
            qk_norm=qk_norm, fused_attn=fused_attn, rope=rope,
        )
        self.kv_cache_sliding_window = kv_cache_sliding_window
        self.kv_cache_scale_frames = kv_cache_scale_frames
        self.kv_cache_cross_frame_special = kv_cache_cross_frame_special
        self.kv_cache_include_scale_frames = kv_cache_include_scale_frames
        self.kv_cache_camera_only = kv_cache_camera_only

    def forward(self, x: Tensor, pos=None, enable_ulysses_cp=False,
                num_patches=None, num_special=None, num_frames=None, enable_3d_rope=False,
                kv_cache=None, global_idx=0, num_frame_per_block=1,
                num_frame_for_scale=-1, num_register_tokens=4) -> Tensor:
        B, N, C = x.shape
        using_optimized_layout = (num_patches is not None and num_special is not None
                                 and num_frames is not None)

        qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
        q, k, v = qkv.unbind(0)
        q, k = self.q_norm(q), self.k_norm(k)

        # ========== Batch Mode (no KV cache) ==========
        if kv_cache is None:
            if self.rope is not None and not enable_3d_rope:
                q = self.rope(q, pos)
                k = self.rope(k, pos)
            elif self.rope is not None and enable_3d_rope:
                q = apply_rotary_emb(q, pos)
                k = apply_rotary_emb(k, pos)

            x = F.scaled_dot_product_attention(
                q, k, v,
                dropout_p=self.attn_drop.p if self.training else 0.0,
            )
            x = x.transpose(1, 2).reshape(B, N, self.num_heads * self.head_dim)

        # ========== Streaming Mode (with KV cache dict) ==========
        else:
            if self.rope is not None and not enable_3d_rope:
                q = self.rope(q, pos)
                k = self.rope(k, pos)
            elif self.rope is not None and enable_3d_rope:
                q = apply_rotary_emb(q, pos)
                k = apply_rotary_emb(k, pos)

            camera_token_idx = 0
            scale_token_idx = camera_token_idx + num_register_tokens + 1

            if kv_cache[f"k_{global_idx}"] is None:
                kv_cache[f"k_{global_idx}"] = k.view(B, self.num_heads, num_frame_per_block,
                                                     N // num_frame_per_block, self.head_dim)
                kv_cache[f"v_{global_idx}"] = v.view(B, self.num_heads, num_frame_per_block,
                                                     N // num_frame_per_block, self.head_dim)
            else:
                num_frame_per_block = k.shape[2] // kv_cache[f"k_{global_idx}"].shape[3]
                kv_cache[f"k_{global_idx}"] = torch.cat((
                    kv_cache[f"k_{global_idx}"],
                    k.view(B, self.num_heads, num_frame_per_block, N // num_frame_per_block, self.head_dim)
                ), dim=2)
                kv_cache[f"v_{global_idx}"] = torch.cat((
                    kv_cache[f"v_{global_idx}"],
                    v.view(B, self.num_heads, num_frame_per_block, N // num_frame_per_block, self.head_dim)
                ), dim=2)

            self._apply_kv_cache_eviction(
                kv_cache, global_idx, camera_token_idx, scale_token_idx, num_register_tokens
            )

            k_cached = kv_cache[f"k_{global_idx}"].clone()
            v_cached = kv_cache[f"v_{global_idx}"].clone()
            a, b, c, d, e = k_cached.shape
            k_full = k_cached.reshape(a, b, c * d, e)
            v_full = v_cached.reshape(a, b, c * d, e)

            if f"k_{global_idx}_special" in kv_cache and kv_cache[f"k_{global_idx}_special"] is not None:
                special_k = kv_cache[f"k_{global_idx}_special"]
                special_v = kv_cache[f"v_{global_idx}_special"]
                sa, sb, sc, sd, se = special_k.shape
                k_full = torch.cat([special_k.reshape(sa, sb, sc * sd, se), k_full], dim=2)
                v_full = torch.cat([special_v.reshape(sa, sb, sc * sd, se), v_full], dim=2)

            q_seq_len = q.shape[2]
            x = F.scaled_dot_product_attention(
                q, k_full, v_full,
                dropout_p=self.attn_drop.p if self.training else 0.0,
            )
            x = x.transpose(1, 2).reshape(B, q_seq_len, self.num_heads * self.head_dim)

        x = self.proj(x)
        x = self.proj_drop(x)
        return x

    def _apply_kv_cache_eviction(self, kv_cache, global_idx, camera_token_idx, scale_token_idx, num_register_tokens):
        """Apply sliding window eviction to KV cache."""
        sliding_window_frames = self.kv_cache_sliding_window
        scale_frames = self.kv_cache_scale_frames

        if kv_cache[f"k_{global_idx}"].shape[3] > 1:
            num_cached_frames = kv_cache[f"k_{global_idx}"].shape[2]
            if num_cached_frames > sliding_window_frames + scale_frames:
                evict_start = scale_frames
                evict_end = num_cached_frames - sliding_window_frames
                if evict_end > evict_start:
                    evicted_k = kv_cache[f"k_{global_idx}"][:, :, evict_start:evict_end, :, :]
                    evicted_v = kv_cache[f"v_{global_idx}"][:, :, evict_start:evict_end, :, :]

                    if self.kv_cache_cross_frame_special:
                        if self.kv_cache_camera_only:
                            new_special_k = evicted_k[:, :, :, camera_token_idx:camera_token_idx+1, :].clone()
                            new_special_v = evicted_v[:, :, :, camera_token_idx:camera_token_idx+1, :].clone()
                        else:
                            new_special_k = evicted_k[:, :, :, camera_token_idx:scale_token_idx+1, :].clone()
                            new_special_v = evicted_v[:, :, :, camera_token_idx:scale_token_idx+1, :].clone()

                        if f"k_{global_idx}_special" not in kv_cache or kv_cache[f"k_{global_idx}_special"] is None:
                            kv_cache[f"k_{global_idx}_special"] = new_special_k
                            kv_cache[f"v_{global_idx}_special"] = new_special_v
                        else:
                            kv_cache[f"k_{global_idx}_special"] = torch.cat(
                                [kv_cache[f"k_{global_idx}_special"], new_special_k], dim=2)
                            kv_cache[f"v_{global_idx}_special"] = torch.cat(
                                [kv_cache[f"v_{global_idx}_special"], new_special_v], dim=2)

                    if self.kv_cache_include_scale_frames:
                        kv_cache[f"k_{global_idx}"] = torch.cat([
                            kv_cache[f"k_{global_idx}"][:, :, :scale_frames, :, :],
                            kv_cache[f"k_{global_idx}"][:, :, -sliding_window_frames:, :, :]
                        ], dim=2)
                        kv_cache[f"v_{global_idx}"] = torch.cat([
                            kv_cache[f"v_{global_idx}"][:, :, :scale_frames, :, :],
                            kv_cache[f"v_{global_idx}"][:, :, -sliding_window_frames:, :, :]
                        ], dim=2)
                    else:
                        kv_cache[f"k_{global_idx}"] = kv_cache[f"k_{global_idx}"][:, :, -sliding_window_frames:, :, :]
                        kv_cache[f"v_{global_idx}"] = kv_cache[f"v_{global_idx}"][:, :, -sliding_window_frames:, :, :]