"scripts/bench/run_lightx2v_6_distill.sh" did not exist on "fb686a901397c3dc8069e94457ff408b3e042c8a"
radix_attention.py 5.68 KB
Newer Older
Lianmin Zheng's avatar
Lianmin Zheng committed
1
import torch
2
import numpy as np
Liangsheng Yin's avatar
Liangsheng Yin committed
3
4
from torch import nn

Lianmin Zheng's avatar
Lianmin Zheng committed
5
6
7
from sglang.srt.layers.context_flashattention_nopad import context_attention_fwd
from sglang.srt.layers.extend_attention import extend_attention_fwd
from sglang.srt.layers.token_attention import token_attention_fwd
8
from sglang.srt.managers.controller.model_runner import ForwardMode, InputMetadata
Lianmin Zheng's avatar
Lianmin Zheng committed
9
10
11


class RadixAttention(nn.Module):
12
    def __init__(self, num_heads, head_dim, scaling, num_kv_heads, layer_id, logit_cap=-1):
Lianmin Zheng's avatar
Lianmin Zheng committed
13
14
15
16
17
18
        super().__init__()
        self.tp_q_head_num = num_heads
        self.tp_k_head_num = num_kv_heads
        self.tp_v_head_num = num_kv_heads
        self.head_dim = head_dim
        self.layer_id = layer_id
19
20
21
        self.logit_cap = logit_cap

        assert np.allclose(scaling, 1.0 / (head_dim**0.5))
Lianmin Zheng's avatar
Lianmin Zheng committed
22

23
        from sglang.srt.managers.controller.model_runner import global_server_args_dict
Lianmin Zheng's avatar
Lianmin Zheng committed
24

25
        if global_server_args_dict.get("enable_flashinfer", False):
Lianmin Zheng's avatar
Lianmin Zheng committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
            self.prefill_forward = self.prefill_forward_flashinfer
            self.extend_forward = self.prefill_forward_flashinfer
            self.decode_forward = self.decode_forward_flashinfer
        else:
            self.prefill_forward = self.prefill_forward_triton
            self.extend_forward = self.extend_forward_triton
            self.decode_forward = self.decode_forward_triton

    def prefill_forward_triton(self, q, k, v, input_metadata: InputMetadata):
        o = torch.empty_like(q)

        context_attention_fwd(
            q.view(-1, self.tp_q_head_num, self.head_dim),
            k,
            v,
            o.view(-1, self.tp_q_head_num, self.head_dim),
            input_metadata.start_loc,
            input_metadata.seq_lens,
            input_metadata.max_seq_len,
45
            self.logit_cap,
Lianmin Zheng's avatar
Lianmin Zheng committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        )
        self.store_kv_cache(k, v, input_metadata)

        return o

    def extend_forward_triton(self, q, k, v, input_metadata: InputMetadata):
        o = torch.empty_like(q)
        self.store_kv_cache(k, v, input_metadata)
        extend_attention_fwd(
            q.view(-1, self.tp_q_head_num, self.head_dim),
            k.contiguous(),
            v.contiguous(),
            o.view(-1, self.tp_q_head_num, self.head_dim),
            input_metadata.token_to_kv_pool.get_key_buffer(self.layer_id),
            input_metadata.token_to_kv_pool.get_value_buffer(self.layer_id),
            input_metadata.req_to_token_pool.req_to_token,
            input_metadata.req_pool_indices,
            input_metadata.start_loc,
            input_metadata.seq_lens,
            input_metadata.prefix_lens,
            input_metadata.extend_start_loc,
            input_metadata.extend_seq_lens,
            input_metadata.max_seq_len,
            input_metadata.max_extend_len,
70
            self.logit_cap,
Lianmin Zheng's avatar
Lianmin Zheng committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        )

        return o

    def decode_forward_triton(self, q, k, v, input_metadata: InputMetadata):
        o = torch.empty_like(q)
        self.store_kv_cache(k, v, input_metadata)

        token_attention_fwd(
            q.view(-1, self.tp_q_head_num, self.head_dim),
            input_metadata.token_to_kv_pool.get_key_buffer(self.layer_id),
            input_metadata.token_to_kv_pool.get_value_buffer(self.layer_id),
            o.view(-1, self.tp_q_head_num, self.head_dim),
            input_metadata.req_to_token_pool.req_to_token,
            input_metadata.req_pool_indices,
            input_metadata.start_loc,
            input_metadata.seq_lens,
            input_metadata.max_seq_len,
            input_metadata.other_kv_index,
            input_metadata.total_num_tokens,
91
            self.logit_cap,
Lianmin Zheng's avatar
Lianmin Zheng committed
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
        )

        return o

    def prefill_forward_flashinfer(self, q, k, v, input_metadata: InputMetadata):
        self.store_kv_cache(k, v, input_metadata)

        o = input_metadata.prefill_wrapper.forward(
            q.contiguous().view(-1, self.tp_q_head_num, self.head_dim),
            input_metadata.token_to_kv_pool.kv_data[self.layer_id],
        )

        return o.view(-1, self.tp_q_head_num * self.head_dim)

    def decode_forward_flashinfer(self, q, k, v, input_metadata: InputMetadata):
        self.store_kv_cache(k, v, input_metadata)

        o = input_metadata.decode_wrapper.forward(
            q.contiguous().view(-1, self.tp_q_head_num, self.head_dim),
            input_metadata.token_to_kv_pool.kv_data[self.layer_id],
        )

        return o.view(-1, self.tp_q_head_num * self.head_dim)

    def forward(self, q, k, v, input_metadata: InputMetadata):
        k = k.view(-1, self.tp_k_head_num, self.head_dim)
        v = v.view(-1, self.tp_v_head_num, self.head_dim)

        if input_metadata.forward_mode == ForwardMode.PREFILL:
            return self.prefill_forward(q, k, v, input_metadata)
        elif input_metadata.forward_mode == ForwardMode.EXTEND:
            return self.extend_forward(q, k, v, input_metadata)
        elif input_metadata.forward_mode == ForwardMode.DECODE:
            return self.decode_forward(q, k, v, input_metadata)

    def store_kv_cache(self, cache_k, cache_v, input_metadata: InputMetadata):
        key_buffer = input_metadata.token_to_kv_pool.get_key_buffer(self.layer_id)
        value_buffer = input_metadata.token_to_kv_pool.get_value_buffer(self.layer_id)
        if input_metadata.out_cache_loc is not None:
            key_buffer[input_metadata.out_cache_loc] = cache_k
            value_buffer[input_metadata.out_cache_loc] = cache_v
        elif input_metadata.out_cache_cont_start is not None:
            key_buffer[
                input_metadata.out_cache_cont_start : input_metadata.out_cache_cont_end
            ] = cache_k
            value_buffer[
                input_metadata.out_cache_cont_start : input_metadata.out_cache_cont_end
            ] = cache_v
        else:
            raise RuntimeError()