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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
import os
import warnings

import torch
import torch.nn as nn

try:
    import flash_attn_interface
    FLASH_ATTN_3_AVAILABLE = True
except ModuleNotFoundError:
    FLASH_ATTN_3_AVAILABLE = False

try:
    import flash_attn
    FLASH_ATTN_2_AVAILABLE = True
except ModuleNotFoundError:
    FLASH_ATTN_2_AVAILABLE = False

try:
    major, minor = torch.cuda.get_device_capability(0)
    if f"{major}.{minor}" == "8.0":
        from sageattention_sm80 import sageattn
        SAGE_ATTENTION_AVAILABLE = True
    elif f"{major}.{minor}" == "8.6":
        from sageattention_sm86 import sageattn
        SAGE_ATTENTION_AVAILABLE = True
    elif f"{major}.{minor}" == "8.9":
        from sageattention_sm89 import sageattn
        SAGE_ATTENTION_AVAILABLE = True
    elif f"{major}.{minor}" == "9.0":
        from sageattention_sm90 import sageattn
        SAGE_ATTENTION_AVAILABLE = True
    elif major > 9:
        from sageattention_sm120 import sageattn
        SAGE_ATTENTION_AVAILABLE = True
except Exception:
    try:
        from sageattention import sageattn
        SAGE_ATTENTION_AVAILABLE = True
    except Exception:
        sageattn = None
        SAGE_ATTENTION_AVAILABLE = False


from .attention_kernel import _sparse_linear_attention, get_block_map


def convert_qkv_dtype(q, k, v):
    try:
        """Unify the dtype of q, k, v tensors"""
        dtypes = {q.dtype, k.dtype, v.dtype}
        
        # If any tensor is float16/bfloat16
        if torch.float16 in dtypes or torch.bfloat16 in dtypes:
            target_dtype = torch.bfloat16 if torch.bfloat16 in dtypes else torch.float16
        # If all tensors are float32
        elif dtypes == {torch.float32}:
            target_dtype = torch.bfloat16 if (torch.cuda.is_available() and 
                                            torch.cuda.get_device_capability()[0] >= 8) else torch.float16
        else:
            return q, k, v  # No conversion for other cases
        
        return q.to(target_dtype), k.to(target_dtype), v.to(target_dtype)
    except Exception:
        return q, k, v


def _convert_attn_mask_to_lens(attn_mask):
    """

    Convert attention mask to sequence lengths for Flash Attention.

    

    Args:

        attn_mask: Attention mask, can be:

            - [B, L] with 1=valid, 0=padding

            - [B, 1, L] or [B, 1, 1, L] attention bias with 0=valid, -inf/-10000=padding

            - [B, H, Lq, Lk] full attention mask

    

    Returns:

        k_lens: [B] tensor of valid sequence lengths, or None if not a simple padding mask

    """
    if attn_mask is None:
        return None
    
    # Squeeze to simplest form
    while attn_mask.ndim > 2 and attn_mask.shape[1] == 1:
        attn_mask = attn_mask.squeeze(1)
    
    # Only handle [B, L] case (simple padding mask)
    if attn_mask.ndim != 2:
        return None
    
    # Check if it's attention bias format (0 and -inf/-10000) or binary mask (0/1)
    unique_vals = torch.unique(attn_mask)
    if len(unique_vals) > 2:
        return None  # Complex mask, can't convert
    
    # Determine which value means "valid"
    max_val = unique_vals.max().item()
    
    if max_val <= 0:  # Attention bias format: 0=valid, negative=padding
        valid_mask = (attn_mask >= -1.0)  # 0 is valid
    else:  # Binary format: 1=valid, 0=padding
        valid_mask = (attn_mask > 0.5)
    
    # Check if it's a simple left-padded or right-padded mask
    # For right-padding: [1,1,1,0,0] -> valid tokens are contiguous from start
    k_lens = valid_mask.sum(dim=-1).to(torch.int32)
    
    # Verify it's actually a contiguous padding mask by reconstruction
    B, L = valid_mask.shape
    reconstructed = torch.arange(L, device=valid_mask.device).unsqueeze(0) < k_lens.unsqueeze(1)
    if not torch.all(reconstructed == valid_mask):
        return None  # Not a simple contiguous padding mask
    
    return k_lens


def flash_attention_naive(

    q,

    k,

    v,

    cu_seqlens_q=None,

    cu_seqlens_k=None,

    max_seqlen_q=None,

    max_seqlen_k=None,

):
    # apply attention
    if FLASH_ATTN_3_AVAILABLE:
        # Note: dropout_p, window_size are not supported in FA3 now.
        x = flash_attn_interface.flash_attn_varlen_func(
            q=q,
            k=k,
            v=v,
            cu_seqlens_q=cu_seqlens_q,
            cu_seqlens_k=cu_seqlens_k,
            max_seqlen_q=max_seqlen_q,
            max_seqlen_k=max_seqlen_k,
        )[0]
    else:
        assert FLASH_ATTN_2_AVAILABLE
        x = flash_attn.flash_attn_varlen_func(
            q=q,
            k=k,
            v=v,
            cu_seqlens_q=cu_seqlens_q,
            cu_seqlens_k=cu_seqlens_k,
            max_seqlen_q=max_seqlen_q,
            max_seqlen_k=max_seqlen_k,
        )

    # output
    return x.type(q.dtype)


def flash_attention(

    q,

    k,

    v,

    q_lens=None,

    k_lens=None,

    dropout_p=0.,

    softmax_scale=None,

    q_scale=None,

    causal=False,

    window_size=(-1, -1),

    deterministic=False,

    dtype=torch.bfloat16,

    version=None,

):
    """

    q:              [B, Lq, Nq, C1].

    k:              [B, Lk, Nk, C1].

    v:              [B, Lk, Nk, C2]. Nq must be divisible by Nk.

    q_lens:         [B].

    k_lens:         [B].

    dropout_p:      float. Dropout probability.

    softmax_scale:  float. The scaling of QK^T before applying softmax.

    causal:         bool. Whether to apply causal attention mask.

    window_size:    (left right). If not (-1, -1), apply sliding window local attention.

    deterministic:  bool. If True, slightly slower and uses more memory.

    dtype:          torch.dtype. Apply when dtype of q/k/v is not float16/bfloat16.

    """
    half_dtypes = (torch.float16, torch.bfloat16)
    assert dtype in half_dtypes
    assert q.device.type == 'cuda' and q.size(-1) <= 256

    # params
    b, lq, lk, out_dtype = q.size(0), q.size(1), k.size(1), q.dtype

    def half(x):
        return x if x.dtype in half_dtypes else x.to(dtype)

    # preprocess query
    if q_lens is None:
        q = half(q.flatten(0, 1))
        q_lens = torch.tensor(
            [lq] * b, dtype=torch.int32).to(
                device=q.device, non_blocking=True)
    else:
        q = half(torch.cat([u[:v] for u, v in zip(q, q_lens)]))

    # preprocess key, value
    if k_lens is None:
        k = half(k.flatten(0, 1))
        v = half(v.flatten(0, 1))
        k_lens = torch.tensor(
            [lk] * b, dtype=torch.int32).to(
                device=k.device, non_blocking=True)
    else:
        k = half(torch.cat([u[:v] for u, v in zip(k, k_lens)]))
        v = half(torch.cat([u[:v] for u, v in zip(v, k_lens)]))

    q = q.to(v.dtype)
    k = k.to(v.dtype)

    if q_scale is not None:
        q = q * q_scale

    if version is not None and version == 3 and not FLASH_ATTN_3_AVAILABLE:
        warnings.warn(
            'Flash attention 3 is not available, use flash attention 2 instead.'
        )

    # apply attention
    if (version is None or version == 3) and FLASH_ATTN_3_AVAILABLE:
        # Note: dropout_p, window_size are not supported in FA3 now.
        x = flash_attn_interface.flash_attn_varlen_func(
            q=q,
            k=k,
            v=v,
            cu_seqlens_q=torch.cat([q_lens.new_zeros([1]), q_lens]).cumsum(
                0, dtype=torch.int32).to(q.device, non_blocking=True),
            cu_seqlens_k=torch.cat([k_lens.new_zeros([1]), k_lens]).cumsum(
                0, dtype=torch.int32).to(q.device, non_blocking=True),
            seqused_q=None,
            seqused_k=None,
            max_seqlen_q=lq,
            max_seqlen_k=lk,
            softmax_scale=softmax_scale,
            causal=causal,
            deterministic=deterministic)
        if isinstance(x, (tuple, list)):
            x = x[0].unflatten(0, (b, lq))
        else:
            x = x.unflatten(0, (b, lq))
    else:
        assert FLASH_ATTN_2_AVAILABLE
        x = flash_attn.flash_attn_varlen_func(
            q=q,
            k=k,
            v=v,
            cu_seqlens_q=torch.cat([q_lens.new_zeros([1]), q_lens]).cumsum(
                0, dtype=torch.int32).to(q.device, non_blocking=True),
            cu_seqlens_k=torch.cat([k_lens.new_zeros([1]), k_lens]).cumsum(
                0, dtype=torch.int32).to(q.device, non_blocking=True),
            max_seqlen_q=lq,
            max_seqlen_k=lk,
            dropout_p=dropout_p,
            softmax_scale=softmax_scale,
            causal=causal,
            window_size=window_size,
            deterministic=deterministic).unflatten(0, (b, lq))

    # output
    return x.type(out_dtype)


def attention(

    q,

    k,

    v,

    q_lens=None,

    k_lens=None,

    dropout_p=0.,

    softmax_scale=None,

    q_scale=None,

    causal=False,

    window_size=(-1, -1),

    deterministic=False,

    dtype=torch.bfloat16,

    fa_version=None,

    attention_type=None,

    attn_mask=None,

):
    attention_type = os.environ.get("VIDEOX_ATTENTION_TYPE", "FLASH_ATTENTION") if attention_type is None else attention_type
    if torch.is_grad_enabled() and attention_type == "SAGE_ATTENTION":
        attention_type = "FLASH_ATTENTION"

    # Convert attn_mask to k_lens for Flash Attention if possible
    # Note: flash_attention doesn't support variable-length query, only set k_lens
    if attn_mask is not None and k_lens is None and attention_type == "FLASH_ATTENTION":
        converted_lens = _convert_attn_mask_to_lens(attn_mask)
        if converted_lens is not None:
            k_lens = converted_lens
            attn_mask = None  # Successfully converted, clear the mask
        else:
            # Conversion failed, fallback to SDPA which supports attn_mask
            attention_type = "SDPA"

    if attention_type == "SAGE_ATTENTION" and SAGE_ATTENTION_AVAILABLE:
        if q_lens is not None or k_lens is not None:
            warnings.warn(
                'Padding mask is disabled when using SAGE_ATTENTION. It can have a significant impact on performance.'
            )

        q, k, v = convert_qkv_dtype(q, k, v)
        out = sageattn(
            q, k, v, attn_mask=attn_mask, tensor_layout="NHD", is_causal=causal, dropout_p=dropout_p)

    elif attention_type == "FLASH_ATTENTION" and (FLASH_ATTN_2_AVAILABLE or FLASH_ATTN_3_AVAILABLE):
        return flash_attention(
            q=q,
            k=k,
            v=v,
            q_lens=q_lens,
            k_lens=k_lens,
            dropout_p=dropout_p,
            softmax_scale=softmax_scale,
            q_scale=q_scale,
            causal=causal,
            window_size=window_size,
            deterministic=deterministic,
            dtype=dtype,
            version=fa_version,
        )
    else:
        if q_lens is not None or k_lens is not None:
            warnings.warn(
                'Padding mask is disabled when using scaled_dot_product_attention. It can have a significant impact on performance.'
            )
        q = q.transpose(1, 2)
        k = k.transpose(1, 2)
        v = v.transpose(1, 2)

        out = torch.nn.functional.scaled_dot_product_attention(
            q, k, v, attn_mask=attn_mask, is_causal=causal, dropout_p=dropout_p)

        out = out.transpose(1, 2).contiguous()
    return out


class SparseLinearAttention(nn.Module):
    # Modified from https://github.com/thu-ml/TurboDiffusion/blob/main/turbodiffusion/SLA/kernel.py
    def __init__(self, head_dim, topk, feature_map='softmax', BLKQ=64, BLKK=64, use_bf16=True, tie_feature_map_qk=True):
        R'''

        Args:

            head_dim: dimension of each head.

            topk: ratio of keys selected for sparse attention, shared across all queries.

            feature_map: feature map for linear attention, one of ['hedgehog', 'elu', 'relu', 'softmax'].

            BLKQ: block size for query.

            BLKK: block size for key.

            use_bf16: whether to use bfloat16 (default) or float16 for computation. The conversion to bf16/fp16 is done inside the module.

            tie_feature_map_qk: whether to use the same feature map for query and key.

        '''
        super().__init__()
        self.dtype = torch.bfloat16 if use_bf16 else torch.float16
        self.topk = topk
        self.BLKQ = BLKQ
        self.BLKK = BLKK
        self.proj_l = nn.Linear(head_dim, head_dim, dtype=torch.float32)

        if feature_map == 'elu':
            def elu_feature_map(x):
                return torch.nn.functional.elu(x) + 1
            self.feature_map_q = elu_feature_map
            self.feature_map_k = elu_feature_map
        elif feature_map == 'relu':
            self.feature_map_q = nn.ReLU()
            self.feature_map_k = nn.ReLU()
        elif feature_map == 'softmax':
            def softmax_feature_map(x):
                return torch.nn.functional.softmax(x, dim=-1)
            self.feature_map_q = softmax_feature_map
            self.feature_map_k = softmax_feature_map
        else:
            raise NotImplementedError(f'Not supported feature map {feature_map}.')

        if tie_feature_map_qk:
            self.feature_map_k = self.feature_map_q

        self.init_weights_()

    def init_weights_(self):
        with torch.no_grad():
            nn.init.zeros_(self.proj_l.weight)
            nn.init.zeros_(self.proj_l.bias)

    def forward(self, q, k, v, return_sparsity=False):
        R'''

        Args:

            q: queries  [B, L, H, D]

            k: keys     [B, L, H, D].

            v: values   [B, L, H, D].

            return_sparsity: whether to return the actual sparsity.

        '''
        dtype = q.dtype
        
        q = q.transpose(1, 2).contiguous()
        k = k.transpose(1, 2).contiguous()
        v = v.transpose(1, 2).contiguous()
        
        sparse_map, lut, real_topk = get_block_map(q, k, topk_ratio=self.topk, BLKQ=self.BLKQ, BLKK=self.BLKK)

        q = q.to(self.dtype)
        k = k.to(self.dtype)
        v = v.to(self.dtype)
        o_s = _sparse_linear_attention.apply(q, k, v, sparse_map, lut, real_topk, self.BLKQ, self.BLKK)
        
        q = self.feature_map_q(q).contiguous().to(self.dtype) # c_q
        k = self.feature_map_k(k).contiguous().to(self.dtype) # c_k

        def calc_linear(q, k, v):
            kvsum = k.transpose(-1, -2) @ v
            ksum = torch.sum(k, dim=-2, keepdim=True)
            return (q @ kvsum) / (1e-5 + (q * ksum).sum(dim=-1, keepdim=True))
        o_l = calc_linear(q, k, v)

        with torch.amp.autocast('cuda', dtype=self.dtype):
            o_l = self.proj_l(o_l)
        o = (o_s + o_l).to(dtype).transpose(1, 2)

        if return_sparsity:
            return o, real_topk / sparse_map.shape[-1]
        else:
            return o