vllm.attention.ops.blocksparse_attention.interface
LocalStridedBlockSparseAttn
¶
Bases: Module
Source code in vllm/attention/ops/blocksparse_attention/interface.py
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 |
|
head_sliding_step
instance-attribute
¶
head_sliding_step = get_head_sliding_step(
n_heads, vert_stride, homo_head
)
__init__
¶
__init__(
n_heads,
max_seqlen,
local_blocks,
vert_stride,
block_size,
device=None,
dtype=None,
homo_head=False,
active_head_range=None,
q_block_size=None,
use_spda=None,
)
Source code in vllm/attention/ops/blocksparse_attention/interface.py
forward
¶
Dispatch to varlen_attn
(Ampere or newer) or
self.spda
(cpu, Volta, Turing or older)based on
the type of device used and cuda compute capability.
q, k, v: shape = (num_tokens, num_heads_q/kv, head_size).
Support grouped attention, with q[:, i*r:(i*r + r)]
is correspondent to k[:, i]
, where r
is the q/k ratio.
cu_seqlens_k: shape=(batch_size + 1,), indicating segment of samples,
e.g., k[cu_seqlen[i]:cu_seqlne[i+1]]
is q of sample i
cu_seqlens_q: shape=(batch_size + 1, ).
Default None: same as cu_seqlens_k for prefilling or
[0, 1, .., batch_size] for decoding.
The only case you need to specify
is when q is a mix of prefilling
and decoding.
sm_scale: softmax scale, default to 1/sqrt(head_size).
return: tensor of shape as q.
Source code in vllm/attention/ops/blocksparse_attention/interface.py
get_attn_pattern
¶
Source code in vllm/attention/ops/blocksparse_attention/interface.py
spda
¶
For CPU, V100 or other older GPUs. NOTE: torch SPDA supports nested tensor, but seems extremely slow. Choose to pad instead.
Source code in vllm/attention/ops/blocksparse_attention/interface.py
transpose_and_pad
staticmethod
¶
:param x: (total_tokens, n_heads, head_size) :return: (batch, n_heads, length, head_size)
Source code in vllm/attention/ops/blocksparse_attention/interface.py
transpose_and_unpad
staticmethod
¶
:param x_padded: (batch, n_heads, length, head_size) :return: (total_tokens, n_heads, head_size)
Source code in vllm/attention/ops/blocksparse_attention/interface.py
varlen_attn
¶
q, k, v: shape = (num_tokens, num_heads_q/kv, head_size).
Support grouped attention, with q[:, i*r:(i*r + r)]
is correspondent to k[:, i]
, where r
is the q/k ratio.
cu_seqlens_k: shape=(batch_size + 1,),
indicating segment of samples,
e.g., k[cu_seqlen[i]:cu_seqlne[i+1]]
is q of sample i
cu_seqlens_q: shape=(batch_size + 1, ).
Default None: same as cu_seqlens_k for prefilling or
[0, 1, .., batch_size] for decoding.
The only case you need to specify is when q is a mix of
prefilling and decoding.
sm_scale: softmax scale, default to 1/sqrt(head_size).
return: tensor of shape as q.