Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation.  All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
 
"""
Parity test and benchmark performance of SparseAttention. Requires Nvidia GPU of Compute Capability 7.5 or above.
"""
 
import os
import unittest
 
import torch
from benchmark_mha import InputFormats
from onnx import TensorProto, helper
from test_gqa_cpu import smooth_softmax_ref
from torch import Tensor
 
from onnxruntime import InferenceSession, SessionOptions, get_available_providers
from onnxruntime.transformers.io_binding_helper import CudaSession
 
try:
    from gqa_test_helper import (
        AttentionConfig,
        GroupQueryAttentionConfig,
        OrtGroupQueryAttention,
    )
except ImportError:
    import sys
 
    sys.path.insert(0, os.path.dirname(__file__))
    from gqa_test_helper import (
        AttentionConfig,
        GroupQueryAttentionConfig,
        OrtGroupQueryAttention,
    )
 
ENABLE_DEBUG = False
 
 
# AttentionConfig and GroupQueryAttentionConfig moved to gqa_test_helper
 
 
class SparseAttentionConfig(AttentionConfig):
    def __init__(
        self,
        batch_size: int,
        sequence_length: int,
        max_sequence_length: int,
        past_sequence_length: int,
        num_heads: int,
        kv_num_heads: int,
        head_size: int,
        sparse_block_size: int,
        num_layout: int,
        local_blocks: int,
        vert_stride: int,
        softmax_scale=None,
        do_rotary: bool = False,
        rotary_interleaved: bool = False,
        provider: str = "CUDAExecutionProvider",
        device="cuda",
        dtype=torch.float16,
        is_packed_qkv=False,
        max_cache_sequence_length=None,
        max_rotary_sequence_length=None,
    ):
        super().__init__(
            "SparseAttention",
            batch_size=batch_size,
            sequence_length=sequence_length,
            max_sequence_length=max_sequence_length,
            past_sequence_length=past_sequence_length,
            num_heads=num_heads,
            kv_num_heads=kv_num_heads,
            head_size=head_size,
            softmax_scale=softmax_scale,
            do_rotary=do_rotary,
            rotary_interleaved=rotary_interleaved,
            provider=provider,
            device=device,
            dtype=dtype,
            is_packed_qkv=is_packed_qkv,
            max_cache_sequence_length=max_cache_sequence_length,
            max_rotary_sequence_length=max_rotary_sequence_length,
        )
        self.sparse_block_size = sparse_block_size
        self.num_layout = num_layout
        self.local_blocks = local_blocks
        self.vert_stride = vert_stride
        self.max_blocks = max_sequence_length // sparse_block_size
 
    def block_mask(self):
        return get_block_mask(self.num_layout, self.max_blocks, self.local_blocks, self.vert_stride).to(self.device)
 
    def block_indices(self):
        row_indices, col_indices = dense_to_csr(self.block_mask())
        return row_indices.to(torch.int32).to(self.device), col_indices.to(torch.int32).to(self.device)
 
    def dense_mask(self):
        dense_mask = get_dense_mask(
            self.block_mask(), self.total_sequence_length, self.sequence_length, self.sparse_block_size
        )
        return dense_mask.repeat(self.batch_size, self.num_heads // self.num_layout, 1, 1).to(self.device)
 
    def shape_dict(self):
        shapes = super().shape_dict()
        block_row_indices, block_col_indices = self.block_indices()
        shapes.update(
            {
                "block_row_indices": tuple(block_row_indices.shape),
                "block_col_indices": tuple(block_col_indices.shape),
                "key_total_sequence_lengths": (self.batch_size,),
            }
        )
        return shapes
 
    def random_inputs(self):
        feeds = super().random_inputs()
        k_seqlens = torch.ones((self.batch_size,), device=self.device, dtype=torch.int32) * self.total_sequence_length
        block_row_indices, block_col_indices = self.block_indices()
        feeds.update(
            {
                "block_row_indices": block_row_indices,
                "block_col_indices": block_col_indices,
                "key_total_sequence_lengths": k_seqlens,
            }
        )
        return feeds
 
    def get_comparable_ort_gqa_config(self, use_local=False) -> GroupQueryAttentionConfig:
        return GroupQueryAttentionConfig(
            batch_size=self.batch_size,
            sequence_length=self.sequence_length,
            max_sequence_length=self.max_sequence_length,
            past_sequence_length=self.past_sequence_length,
            num_heads=self.num_heads,
            kv_num_heads=self.kv_num_heads,
            head_size=self.head_size,
            softmax_scale=self.softmax_scale,
            do_rotary=self.do_rotary,
            rotary_interleaved=self.rotary_interleaved,
            provider=self.provider,
            device=self.device,
            dtype=self.dtype,
            local_window_size=self.local_blocks * self.sparse_block_size if use_local else -1,
            is_packed_qkv=self.is_packed_qkv,
            max_cache_sequence_length=self.max_cache_sequence_length,
            max_rotary_sequence_length=self.max_rotary_sequence_length,
        )
 
    def get_comparable_torch_gqa_config(self, use_sparse=False) -> GroupQueryAttentionConfig:
        attention_mask = None
 
        if use_sparse is True:
            attention_mask = self.dense_mask()[:, :, : self.total_sequence_length, : self.total_sequence_length]
            if self.past_sequence_length > 0:
                attention_mask = attention_mask[:, :, -self.sequence_length :, :]
 
        return GroupQueryAttentionConfig(
            batch_size=self.batch_size,
            sequence_length=self.sequence_length,
            max_sequence_length=self.max_sequence_length,
            past_sequence_length=self.past_sequence_length,
            num_heads=self.num_heads,
            kv_num_heads=self.kv_num_heads,
            head_size=self.head_size,
            softmax_scale=self.softmax_scale,
            do_rotary=self.do_rotary,
            rotary_interleaved=self.rotary_interleaved,
            provider=self.provider,
            device=self.device,
            dtype=self.dtype,
            attention_mask=attention_mask,
            is_packed_qkv=False,  # torch reference implementation does not support packed qkv.
            max_cache_sequence_length=self.max_cache_sequence_length,
            max_rotary_sequence_length=self.max_rotary_sequence_length,
        )
 
 
def get_block_mask(num_layout, max_blocks, local_blocks, vert_stride):
    q_pos = torch.arange(max_blocks)[None, :, None]
    k_pos = torch.arange(max_blocks)[None, None]
    head_sliding_step = max(1, int(vert_stride / num_layout))
    mask_vert_strided = [
        (torch.arange(max_blocks) + h * head_sliding_step + 1) % vert_stride == 0 for h in range(num_layout)
    ]
    mask_vert_strided = torch.vstack(mask_vert_strided).unsqueeze(1)
    local_mask = q_pos - k_pos < local_blocks
    block_mask = (q_pos >= k_pos) & (local_mask | mask_vert_strided)
    block_mask = block_mask.to(torch.int32)
 
    if ENABLE_DEBUG:
        print(f"{num_layout=} {max_blocks=} {local_blocks=} {vert_stride=}")
        print(f"{block_mask=}")
 
    return block_mask
 
 
def dense_to_csr(x):
    """Turning a 3D torch tensor (x) to CSR rows/cols indexing."""
    assert x.dim() == 3
    pad = -1
    x = [xi.to_sparse_csr() for xi in x]
    row_indices = torch.vstack([xi.crow_indices() for xi in x])
    cols = [xi.col_indices() for xi in x]
    max_cols = max(len(xi) for xi in cols)
    cols = [torch.cat([xi, pad + xi.new_zeros(max_cols - xi.shape[0])]) for xi in cols]
    col_indices = torch.vstack(cols)
    return row_indices, col_indices
 
 
def get_dense_mask(block_mask, total_seq_len, query_seq_len, block_size):
    dense_mask = torch.kron(block_mask, block_mask.new_ones((block_size, block_size)))[
        :, :total_seq_len, :total_seq_len
    ]
    causal_mask = torch.tril(torch.ones(total_seq_len, total_seq_len)).type_as(dense_mask)
    dense_mask = dense_mask * causal_mask[None]
    return dense_mask[..., -query_seq_len:, :total_seq_len]
 
 
def create_sparse_attention_onnx_model(config: SparseAttentionConfig):
    # ORT Python I/O binding API does not support bf16, so always use fp16 as graph inputs/outputs.
    io_float_type = TensorProto.FLOAT if config.dtype == torch.float32 else TensorProto.FLOAT16
 
    suffix = "_bf16" if config.dtype == torch.bfloat16 else ""
    nodes = [
        helper.make_node(
            "SparseAttention",
            [
                "query" + suffix,
                "key" + suffix if not config.is_packed_qkv else "",
                "value" + suffix if not config.is_packed_qkv else "",
                "past_key" + suffix,
                "past_value" + suffix,
                "block_row_indices",  # no suffix since int32 need not cast for bfloat graph.
                "block_col_indices",
                "total_sequence_length" if config.share_buffer else "",
                "key_total_sequence_lengths",
                "cos_cache" + suffix if config.do_rotary else "",
                "sin_cache" + suffix if config.do_rotary else "",
            ],
            ["output" + suffix, "present_key" + suffix, "present_value" + suffix],
            "SparseAttention_0",
            num_heads=config.num_heads,
            kv_num_heads=config.kv_num_heads,
            scale=config.softmax_scale,
            sparse_block_size=config.sparse_block_size,
            do_rotary=1 if config.do_rotary else 0,
            domain="com.microsoft",
        ),
    ]
 
    # When testing bfloat16, we add cast nodes so that SparseAttention is computed in bfloat16.
    if config.dtype == torch.bfloat16:
        nodes.extend(
            [
                helper.make_node("Cast", [input], [input + suffix], f"Cast_{input}", to=TensorProto.BFLOAT16)
                for input in (
                    ["query", "key", "value", "past_key", "past_value"]
                    if not config.is_packed_qkv
                    else ["query", "past_key", "past_value"]
                )
            ]
        )
        if config.do_rotary:
            nodes.extend(
                [
                    helper.make_node("Cast", [input], [input + suffix], f"Cast_{input}", to=TensorProto.BFLOAT16)
                    for input in ["cos_cache", "sin_cache"]
                ]
            )
        nodes.extend(
            [
                helper.make_node("Cast", [output + suffix], [output], f"Cast_{output}", to=TensorProto.FLOAT16)
                for output in ["output", "present_key", "present_value"]
            ]
        )
 
    shape_dict = config.shape_dict()
    graph_input = [
        helper.make_tensor_value_info("query", io_float_type, list(shape_dict["query"])),
    ]
 
    if not config.is_packed_qkv:
        graph_input.extend(
            [
                helper.make_tensor_value_info("key", io_float_type, list(shape_dict["key"])),
                helper.make_tensor_value_info("value", io_float_type, list(shape_dict["value"])),
            ]
        )
 
    graph_input.extend(
        [
            helper.make_tensor_value_info("past_key", io_float_type, list(shape_dict["past_key"])),
            helper.make_tensor_value_info("past_value", io_float_type, list(shape_dict["past_value"])),
            helper.make_tensor_value_info(
                "block_row_indices", TensorProto.INT32, list(shape_dict["block_row_indices"])
            ),
            helper.make_tensor_value_info(
                "block_col_indices", TensorProto.INT32, list(shape_dict["block_col_indices"])
            ),
            helper.make_tensor_value_info(
                "total_sequence_length", TensorProto.INT32, list(shape_dict["total_sequence_length"])
            ),
            helper.make_tensor_value_info(
                "key_total_sequence_lengths", TensorProto.INT32, list(shape_dict["key_total_sequence_lengths"])
            ),
        ]
    )
 
    if config.do_rotary:
        graph_input += [
            helper.make_tensor_value_info("cos_cache", io_float_type, list(shape_dict["cos_cache"])),
            helper.make_tensor_value_info("sin_cache", io_float_type, list(shape_dict["sin_cache"])),
        ]
 
    graph_output = [
        helper.make_tensor_value_info("output", io_float_type, list(shape_dict["output"])),
        helper.make_tensor_value_info("present_key", io_float_type, list(shape_dict["present_key"])),
        helper.make_tensor_value_info("present_value", io_float_type, list(shape_dict["present_value"])),
    ]
 
    graph = helper.make_graph(
        nodes,
        "SparseAttention_Graph",
        graph_input,
        graph_output,
    )
 
    model = helper.make_model(graph)
    return model.SerializeToString()
 
 
# create_group_query_attention_onnx_model moved to gqa_test_helper
 
 
def create_session(onnx_model_str, cuda_provider_options=None) -> InferenceSession:
    session_options = SessionOptions()
    ort_session = InferenceSession(
        onnx_model_str,
        session_options,
        providers=[("CUDAExecutionProvider", cuda_provider_options), "CPUExecutionProvider"],
    )
    return ort_session
 
 
def group_query_attention_reference(
    query: Tensor,
    key: Tensor,
    value: Tensor,
    config: GroupQueryAttentionConfig,
    scale: float | None = None,
    mask: Tensor | None = None,
):
    if scale is None:
        scale = 1.0 / (config.head_size**0.5)
 
    # Query is in BSNH shape, transpose it here. Note that key/value is BNSH format (transposed).
    query = query.transpose(1, 2)
 
    # Expand key and value to have same number of heads as query
    num_key_value_groups = config.num_heads // config.kv_num_heads
    key = torch.repeat_interleave(key, dim=1, repeats=num_key_value_groups)
    value = torch.repeat_interleave(value, dim=1, repeats=num_key_value_groups)
    # Apply multi-head attention.
    attn = torch.einsum("bhmd,bhnd->bhmn", query, key).float() * scale
    if mask is not None:
        attn = attn.masked_fill((1 - mask).bool(), float("-inf"))
 
    if config.use_smooth_softmax:
        attn = smooth_softmax_ref(attn, head_sink=None)
    else:
        attn = attn.softmax(-1)
 
    attn_output = torch.einsum("bhmn,bhnd->bhmd", attn.type_as(value), value)
 
    result = attn_output.transpose(1, 2).contiguous()
 
    if torch.cuda.is_available():
        torch.cuda.synchronize()
 
    return result
 
 
class TorchGroupQueryAttention:
    """A wrapper of Torch GroupQueryAttention to test relevance and performance."""
 
    def __init__(self, config: GroupQueryAttentionConfig):
        self.device = config.device
        self.config = config
        self.feed_dict = config.random_inputs()
        self.dense_mask = config.attention_mask
 
    @staticmethod
    def concat_cache(past_key_cache, new_key):
        """
        Concatenates a new key to a past key cache.
 
        Args:
        - past_key (torch.Tensor): Past key cache with shape (batch_size, num_heads, past_sequence_length, head_dim)
        - new_key (torch.Tensor): New key with shape (batch_size, num_heads, sequence_length, head_dim)
 
        Returns:
        - present_key (torch.Tensor): Concatenated key tensor with shape (batch_size, num_heads, new_length, head_dim)
                                      where new_length = past_sequence_length + sequence_length
        """
        # Check if the past_key_cache and new_key have compatible shapes
        assert past_key_cache.size(0) == new_key.size(0), "Batch sizes do not match"
        assert past_key_cache.size(1) == new_key.size(1), "Number of heads do not match"
        assert past_key_cache.size(3) == new_key.size(3), "Head dimensions do not match"
 
        # Concatenate the keys along the sequence length dimension
        concatenated_keys = torch.cat((past_key_cache, new_key), dim=2)
 
        return concatenated_keys
 
    def infer(self):
        config = self.config
        query = self.feed_dict["query"].view(
            config.batch_size, config.sequence_length, config.num_heads, config.head_size
        )
        key = (
            self.feed_dict["key"]
            .view(config.batch_size, config.sequence_length, config.kv_num_heads, config.head_size)
            .transpose(1, 2)
        )
        value = (
            self.feed_dict["value"]
            .view(config.batch_size, config.sequence_length, config.kv_num_heads, config.head_size)
            .transpose(1, 2)
        )
 
        if config.past_sequence_length > 0:
            past_key = self.feed_dict["past_key"][:, :, : config.past_sequence_length, :]
            past_value = self.feed_dict["past_value"][:, :, : config.past_sequence_length, :]
            present_key = TorchGroupQueryAttention.concat_cache(past_key, key)
            present_value = TorchGroupQueryAttention.concat_cache(past_value, value)
        else:
            present_key = key
            present_value = value
 
        if ENABLE_DEBUG:
            print("query(BSNH, GQA)", query)
            print("present_key(BNSH, GQA)", present_key)
            print("present_value(BNSH, GQA)", present_value)
            print("dense_mask", self.dense_mask)
 
        return group_query_attention_reference(
            query, present_key, present_value, config, scale=config.softmax_scale, mask=self.dense_mask
        )
 
 
def create_ort_session(config: SparseAttentionConfig, session_options=None, enable_cuda_graph=False) -> CudaSession:
    if isinstance(config, SparseAttentionConfig):
        onnx_model_str = create_sparse_attention_onnx_model(config)
    else:
        raise ValueError("Only SparseAttentionConfig is supported directly here.")
 
    if config.provider == "CUDAExecutionProvider":
        device_id = torch.cuda.current_device() if isinstance(config.device, str) else config.device.index
        provider_options = CudaSession.get_cuda_provider_options(
            device_id, enable_cuda_graph=enable_cuda_graph, stream=torch.cuda.current_stream().cuda_stream
        )
        providers = [(config.provider, provider_options), "CPUExecutionProvider"]
    else:
        providers = ["CPUExecutionProvider"]
 
    ort_session = InferenceSession(onnx_model_str, session_options, providers=providers)
    # Note that CudaSession could work with both CUDA and CPU providers.
    cuda_session = CudaSession(ort_session, config.device, enable_cuda_graph=enable_cuda_graph)
    shape_dict = config.shape_dict()
    cuda_session.allocate_buffers(shape_dict)
 
    buffer_sharing = {"past_key": "present_key", "past_value": "present_value"}
    for input_name, output_name in buffer_sharing.items():
        cuda_session.set_buffer_sharing(input_name, output_name)
 
    return cuda_session
 
 
# OrtGroupQueryAttention moved to gqa_test_helper
 
 
class OrtSparseAttention:
    """A wrapper of ORT SparseAttention to test relevance and performance."""
 
    def __init__(self, config: SparseAttentionConfig):
        self.session = create_ort_session(config)
        self.feed_dict = config.random_inputs()
 
        if ENABLE_DEBUG and not config.is_packed_qkv:
            query = self.feed_dict["query"].view(
                config.batch_size, config.sequence_length, config.num_heads, config.head_size
            )
            key = self.feed_dict["key"].view(
                config.batch_size, config.sequence_length, config.kv_num_heads, config.head_size
            )
            value = self.feed_dict["value"].view(
                config.batch_size, config.sequence_length, config.kv_num_heads, config.head_size
            )
            print(vars(config))
            print("query(BSNH, SA)", query)
            print("key(BSNH, SA)", key)
            print("value(BSNH, SA)", value)
            print("block_row_indices", self.feed_dict["block_row_indices"])
            print("block_col_indices", self.feed_dict["block_col_indices"])
            print("total_sequence_length", self.feed_dict["total_sequence_length"])
            print("key_total_sequence_lengths", self.feed_dict["key_total_sequence_lengths"])
 
    def infer(self):
        return self.session.infer(self.feed_dict)
 
 
def get_provider_support_info(provider: str, use_kv_cache: bool):
    if provider == "CUDAExecutionProvider":
        formats = [InputFormats.Q_K_V_BSNH_BSNH_BSNH, InputFormats.QKV_BSN3H]
        device_id = torch.cuda.current_device()
        device = torch.device("cuda", device_id)
        dtype = torch.float16
    else:
        assert provider == "CPUExecutionProvider"
        formats = [InputFormats.Q_K_V_BSNH_BSNH_BSNH, InputFormats.QKV_BSN3H]
        device = torch.device("cpu")
        dtype = torch.float
    return device, dtype, formats
 
 
def has_cuda_support():
    if torch.cuda.is_available() and "CUDAExecutionProvider" in get_available_providers():
        major, minor = torch.cuda.get_device_capability()
        sm = major * 10 + minor
        return sm in [75, 80, 86, 89, 90]
 
    return False
 
 
def get_simple_test_case(provider: str, has_past_kv: bool):
    """A simple test case for debugging purpose."""
    device, dtype, _formats = get_provider_support_info(provider, False)
    if provider == "CPUExecutionProvider":
        # A simple case for debugging purpose.
        max_sequence_length = 16
        sequence_length = 15
        packed_qkv = False
        config = SparseAttentionConfig(
            batch_size=1,
            sequence_length=1 if has_past_kv else sequence_length,
            max_sequence_length=max_sequence_length,
            past_sequence_length=sequence_length if has_past_kv else 0,
            num_heads=4,
            kv_num_heads=2,
            head_size=8,
            sparse_block_size=4,
            num_layout=2,
            local_blocks=2,
            vert_stride=2,
            softmax_scale=0.0,
            provider=provider,
            device=device,
            dtype=dtype,
            is_packed_qkv=packed_qkv,
            max_cache_sequence_length=max_sequence_length,
        )
        yield config
 
 
def get_test_cases(provider: str, has_past_kv: bool, comprehensive: bool, do_rotary=False):
    if provider == "CUDAExecutionProvider" and not has_cuda_support():
        return
        yield
 
    device, dtype, formats = get_provider_support_info(provider, False)
    batch_sizes = [1, 2, 3]
    sequence_lengths = [1, 64, 127, 128, 192, 256]
    heads = [4, 8, 16]
 
    # SparseAttention CUDA kernel only supports head size 128
    head_sizes = [128] if provider == "CUDAExecutionProvider" else [128, 256]
 
    if comprehensive:
        for batch_size in batch_sizes:
            for sequence_length in sequence_lengths:
                for num_heads in heads:
                    for head_size in head_sizes:
                        for format in formats:
                            packed_qkv = format == InputFormats.QKV_BSN3H
 
                            non_prompt_len = 1
                            if provider == "CPUExecutionProvider" and sequence_length > 128 and not do_rotary:
                                # Generate case of sequence_length > 1 when it is not prompt for CPU provider.
                                non_prompt_len = batch_size
 
                            query_sequence_length = non_prompt_len if has_past_kv else sequence_length
                            config = SparseAttentionConfig(
                                batch_size=batch_size,
                                sequence_length=query_sequence_length,
                                max_sequence_length=256,
                                past_sequence_length=(
                                    min(256 - query_sequence_length, sequence_length) if has_past_kv else 0
                                ),
                                num_heads=num_heads,
                                kv_num_heads=num_heads // 2,
                                head_size=head_size,
                                sparse_block_size=64,
                                num_layout=2,
                                local_blocks=2,
                                vert_stride=2,
                                softmax_scale=1.8 / (128**0.5),
                                provider=provider,
                                device=device,
                                dtype=dtype,
                                is_packed_qkv=packed_qkv,
                                do_rotary=do_rotary,
                                rotary_interleaved=do_rotary and sequence_length <= 128,
                                max_cache_sequence_length=None if sequence_length >= 128 else 128,
                            )
                            yield config
    else:
        test_cases = max(len(batch_sizes), len(sequence_lengths), len(heads), len(head_sizes))
        for i in range(test_cases):
            batch_size = batch_sizes[i % len(batch_sizes)]
            sequence_length = sequence_lengths[i % len(sequence_lengths)]
            num_heads = heads[i % len(heads)]
            head_size = head_sizes[i % len(head_sizes)]
            format = formats[i % len(formats)]
            packed_qkv = format == InputFormats.QKV_BSN3H
 
            non_prompt_len = 1
            if provider == "CPUExecutionProvider" and sequence_length > 128 and not do_rotary:
                # Generate case of sequence_length > 1 when it is not prompt for CPU provider.
                non_prompt_len = batch_size
 
            query_sequence_length = non_prompt_len if has_past_kv else sequence_length
 
            config = SparseAttentionConfig(
                batch_size=batch_size,
                sequence_length=query_sequence_length,
                max_sequence_length=256,
                past_sequence_length=min(256 - query_sequence_length, sequence_length) if has_past_kv else 0,
                num_heads=num_heads,
                kv_num_heads=num_heads // 2,
                head_size=head_size,
                sparse_block_size=64,
                num_layout=2,
                local_blocks=2,
                vert_stride=2,
                softmax_scale=1.8 / (128**0.5),
                provider=provider,
                device=device,
                dtype=dtype,
                is_packed_qkv=packed_qkv,
                do_rotary=do_rotary,
                rotary_interleaved=do_rotary and sequence_length <= 128,
                max_cache_sequence_length=None if sequence_length >= 128 else 128,  # test smaller kv cache buffer.
            )
            yield config
 
 
# Do not run too many tests. Change it to True to run all combinations.
comprehensive_mode = False
 
 
@unittest.skipIf(os.getenv("PIPELINE_MODE", "1") == "1", "Skipping tests in pipeline mode.")
class TestSparseAttention(unittest.TestCase):
    @unittest.skipUnless(has_cuda_support(), "cuda not available")
    def test_sparse_attention_cuda(self):
        major, minor = torch.cuda.get_device_capability()
        sm = major * 10 + minor
        self.run_relevance_test(sm)
 
        for config in get_test_cases("CUDAExecutionProvider", True, comprehensive_mode):
            self.run_one_relevance_test(config)
 
        for config in get_test_cases("CUDAExecutionProvider", False, comprehensive_mode):
            self.run_one_relevance_test(config)
 
    def test_sparse_attention_cpu(self):
        for config in get_simple_test_case("CPUExecutionProvider", True):
            self.run_one_relevance_test(config)
 
        for config in get_simple_test_case("CPUExecutionProvider", False):
            self.run_one_relevance_test(config)
 
        for config in get_test_cases("CPUExecutionProvider", True, comprehensive_mode, do_rotary=True):
            # When there is rotary, we use ORT GQA as reference: ORT GQA does not support mask so here we use dense.
            if config.sparse_block_size * config.local_blocks > config.total_sequence_length:
                self.run_one_relevance_test(config)
 
        for config in get_test_cases("CPUExecutionProvider", True, comprehensive_mode):
            self.run_one_relevance_test(config)
 
        for config in get_test_cases("CPUExecutionProvider", False, comprehensive_mode):
            self.run_one_relevance_test(config)
 
    def run_one_relevance_test(self, config: SparseAttentionConfig):
        if (not config.do_rotary) and config.total_sequence_length <= 2048:
            # Run QGA by Torch (support mask, but not packed QKV, rotary and very long sequence)
            gqa_config: GroupQueryAttentionConfig = config.get_comparable_torch_gqa_config(use_sparse=True)
            obj = TorchGroupQueryAttention(gqa_config)
            expected_out = obj.infer()
        else:
            # Run QGA by ORT (support packed QKV, rotary and very long sequence, but no mask so dense only).
            gqa_config: GroupQueryAttentionConfig = config.get_comparable_ort_gqa_config(use_local=False)
            obj = OrtGroupQueryAttention(gqa_config)
            ort_qga_outputs = obj.infer()
            expected_out = ort_qga_outputs["output"].view(
                config.batch_size, config.sequence_length, config.num_heads, config.head_size
            )
 
        # Run SparseAttention by ORT
        obj = OrtSparseAttention(config)
        ort_outputs = obj.infer()
        ort_output = ort_outputs["output"]
        actual_out = ort_output.view(config.batch_size, config.sequence_length, config.num_heads, config.head_size)
 
        red_color = "\033[31m"
        green_color = "\033[32m"
        reset_color = "\033[0m"
 
        passed = torch.allclose(expected_out, actual_out, atol=1e-2, rtol=0)
        if passed:
            print(f"Relevance test {green_color}passed{reset_color}: {vars(config)}")
        else:
            print(f"Relevance test {red_color}failed{reset_color}: {vars(config)}")
            print("ort_output", actual_out)
            print("expected_out", expected_out)
            print("diff", expected_out - actual_out)
 
        self.assertTrue(passed)
 
    def run_relevance_no_past(self, sm: int, device):
        """Test prompt prefilling without past kv cache."""
        for seq_len in [1, 64, 127, 128, 192, 256]:
            for packed_qkv in [False, True]:
                config = SparseAttentionConfig(
                    batch_size=3,
                    sequence_length=seq_len,
                    max_sequence_length=256,
                    past_sequence_length=0,
                    num_heads=8,
                    kv_num_heads=4,
                    head_size=128,
                    sparse_block_size=64,
                    num_layout=2,
                    local_blocks=2,
                    vert_stride=2,
                    softmax_scale=1.8 / (128**0.5),
                    device=device,
                    is_packed_qkv=packed_qkv,
                    max_cache_sequence_length=None if seq_len >= 128 else 128,  # test smaller kv cache buffer.
                )
                self.run_one_relevance_test(config)
 
                if sm >= 80 and not packed_qkv:
                    config.dtype = torch.bfloat16
                    self.run_one_relevance_test(config)
 
    def run_relevance_past(self, sm: int, device, do_rotary: bool):
        """Test token generation with past kv cache."""
        for past_seq_len in [1, 63, 64, 127, 128, 511]:
            for packed_qkv in [False, True]:
                config = SparseAttentionConfig(
                    batch_size=3,
                    sequence_length=1,
                    max_sequence_length=512,
                    past_sequence_length=past_seq_len,
                    num_heads=8,
                    kv_num_heads=4,
                    head_size=128,
                    sparse_block_size=64,
                    num_layout=4,
                    local_blocks=2,
                    vert_stride=4,
                    softmax_scale=None,
                    do_rotary=do_rotary,
                    rotary_interleaved=do_rotary and (past_seq_len % 2 == 1),
                    device=device,
                    is_packed_qkv=packed_qkv,
                    max_rotary_sequence_length=None if past_seq_len >= 128 else 128,  # test smaller rotary buffer.
                )
 
                if do_rotary:
                    # When there is rotary, we use ORT GQA as reference: ORT GQA does not support mask so here we use dense.
                    config.local_blocks = config.max_blocks
 
                self.run_one_relevance_test(config)
 
                if sm >= 80 and not packed_qkv:
                    config.dtype = torch.bfloat16
                    self.run_one_relevance_test(config)
 
    def run_relevance_no_past_128k(self, sm: int, device):
        """Test kernel could support up to 128K sequence length."""
        for seq_len in [131072]:
            for packed_qkv in [False, True]:
                config = SparseAttentionConfig(
                    batch_size=1,
                    sequence_length=seq_len,
                    max_sequence_length=131072,
                    past_sequence_length=0,
                    num_heads=1,
                    kv_num_heads=1,
                    head_size=128,
                    sparse_block_size=64,
                    num_layout=1,
                    local_blocks=2048,  # use dense to compare with GQA
                    vert_stride=8,
                    softmax_scale=None,
                    provider="CUDAExecutionProvider",
                    dtype=torch.float16,
                    device=device,
                    is_packed_qkv=packed_qkv,
                )
                self.run_one_relevance_test(config)
 
                if sm >= 80 and not packed_qkv:
                    config.dtype = torch.bfloat16
                    self.run_one_relevance_test(config)
 
    def run_relevance_past_128k(self, sm: int, device):
        """Test kernel could support up to 128K sequence length."""
        for past_seq_len in [131071]:
            for packed_qkv in [False, True]:
                config = SparseAttentionConfig(
                    batch_size=1,
                    sequence_length=1,
                    max_sequence_length=131072,
                    past_sequence_length=past_seq_len,
                    num_heads=1,
                    kv_num_heads=1,
                    head_size=128,
                    sparse_block_size=64,
                    num_layout=1,
                    local_blocks=2048,  # use dense to compare with GQA
                    vert_stride=8,
                    softmax_scale=None,
                    provider="CUDAExecutionProvider",
                    dtype=torch.float16,
                    device=device,
                    is_packed_qkv=packed_qkv,
                )
                self.run_one_relevance_test(config)
 
                if sm >= 80 and not packed_qkv:
                    config.dtype = torch.bfloat16
                    self.run_one_relevance_test(config)
 
    def run_relevance_test(self, sm: int):
        device_id = torch.cuda.current_device()
        device = torch.device("cuda", device_id)
        with torch.no_grad():
            # Test long sequence when GPU memory is enough (need about 12 GB for 128K sequence length)
            # The 128k tests fails randomly in T4 GPU, increase memory threshold for now.
            if torch.cuda.get_device_properties(device_id).total_memory > 20 * 1024 * 1024 * 1024:
                self.run_relevance_no_past_128k(sm, device)
                self.run_relevance_past_128k(sm, device)
            self.run_relevance_no_past(sm, device)
            self.run_relevance_past(sm, device, do_rotary=False)
            self.run_relevance_past(sm, device, do_rotary=True)
 
 
if __name__ == "__main__":
    unittest.main()