luma-flash-attn 0.3.2

luma flash attn
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
use core::ffi::c_void;

use cudarc::driver::{CudaSlice, CudaStream, DevicePtr, DeviceRepr};
use half::{bf16, f16};
use luma_tensor::FloatDType;

use crate::api::ffi;
use crate::error::FlashAttnError;

/// Storage types supported by the flash-attention kernels.
///
/// Compute always runs in f32; the type parameter only selects the device
/// buffer / C-ABI entry point (f32, f16 or bf16).
pub trait FlashDtype: DeviceRepr + Copy + 'static {
    const DTYPE: FloatDType;

    /// # Safety
    /// All pointers must reference valid device buffers usable on `stream`.
    #[doc(hidden)]
    #[allow(clippy::too_many_arguments)]
    unsafe fn ffi_batch(
        q: *const Self,
        k: *const Self,
        v: *const Self,
        o: *mut Self,
        batch: i32,
        q_seq_len: i32,
        kv_seq_len: i32,
        q_num_heads: i32,
        kv_num_heads: i32,
        head_size: i32,
        start_pos: i32,
        scale: f32,
        attn_mask: *const u8,
        stream: *mut c_void,
    ) -> i32;

    /// # Safety
    /// All pointers must reference valid device buffers usable on `stream`.
    #[doc(hidden)]
    #[allow(clippy::too_many_arguments)]
    unsafe fn ffi_varlen(
        q: *const Self,
        k: *const Self,
        v: *const Self,
        o: *mut Self,
        cu_seqlens_q: *const i32,
        cu_seqlens_k: *const i32,
        block_tables: *const i32,
        num_seqs: i32,
        max_seqlen_q: i32,
        max_seqlen_k: i32,
        q_heads: i32,
        kv_heads: i32,
        head_size: i32,
        block_size: i32,
        max_blocks: i32,
        k_paged: i32,
        scale: f32,
        stream: *mut c_void,
    ) -> i32;

    /// # Safety
    /// All pointers must reference valid device buffers usable on `stream`.
    #[doc(hidden)]
    #[allow(clippy::too_many_arguments)]
    unsafe fn ffi_kvcache(
        q: *const Self,
        k_cache: *const Self,
        v_cache: *const Self,
        o: *mut Self,
        block_tables: *const i32,
        context_lens: *const i32,
        num_seqs: i32,
        q_seq_len: i32,
        q_heads: i32,
        kv_heads: i32,
        head_size: i32,
        block_size: i32,
        max_blocks: i32,
        scale: f32,
        stream: *mut c_void,
    ) -> i32;
}

macro_rules! impl_flash_dtype {
    ($T:ty, $dtype:path, $batch:ident, $varlen:ident, $kvcache:ident) => {
        impl FlashDtype for $T {
            const DTYPE: FloatDType = $dtype;

            unsafe fn ffi_batch(
                q: *const Self, k: *const Self, v: *const Self, o: *mut Self,
                batch: i32, q_seq_len: i32, kv_seq_len: i32, q_num_heads: i32, kv_num_heads: i32,
                head_size: i32, start_pos: i32, scale: f32, attn_mask: *const u8, stream: *mut c_void,
            ) -> i32 {
                unsafe {
                    ffi::$batch(q, k, v, o, batch, q_seq_len, kv_seq_len, q_num_heads, kv_num_heads,
                                head_size, start_pos, scale, attn_mask, stream)
                }
            }

            unsafe fn ffi_varlen(
                q: *const Self, k: *const Self, v: *const Self, o: *mut Self,
                cu_seqlens_q: *const i32, cu_seqlens_k: *const i32, block_tables: *const i32,
                num_seqs: i32, max_seqlen_q: i32, max_seqlen_k: i32,
                q_heads: i32, kv_heads: i32, head_size: i32,
                block_size: i32, max_blocks: i32, k_paged: i32, scale: f32, stream: *mut c_void,
            ) -> i32 {
                unsafe {
                    ffi::$varlen(q, k, v, o, cu_seqlens_q, cu_seqlens_k, block_tables,
                                 num_seqs, max_seqlen_q, max_seqlen_k, q_heads, kv_heads, head_size,
                                 block_size, max_blocks, k_paged, scale, stream)
                }
            }

            unsafe fn ffi_kvcache(
                q: *const Self, k_cache: *const Self, v_cache: *const Self, o: *mut Self,
                block_tables: *const i32, context_lens: *const i32,
                num_seqs: i32, q_seq_len: i32, q_heads: i32, kv_heads: i32, head_size: i32,
                block_size: i32, max_blocks: i32, scale: f32, stream: *mut c_void,
            ) -> i32 {
                unsafe {
                    ffi::$kvcache(q, k_cache, v_cache, o, block_tables, context_lens,
                                  num_seqs, q_seq_len, q_heads, kv_heads, head_size,
                                  block_size, max_blocks, scale, stream)
                }
            }
        }
    };
}

impl_flash_dtype!(f32, FloatDType::F32, flash_attn_f32, flash_attn_varlen_f32, flash_attn_with_kvcache_f32);
impl_flash_dtype!(f16, FloatDType::F16, flash_attn_f16, flash_attn_varlen_f16, flash_attn_with_kvcache_f16);
impl_flash_dtype!(bf16, FloatDType::BF16, flash_attn_bf16, flash_attn_varlen_bf16, flash_attn_with_kvcache_bf16);

/// Low-level batched flash attention forward (causal + optional padding mask, GQA).
///
/// Operates directly on device buffers. Layouts (contiguous):
/// `q/o` are `(batch, q_seq_len, q_num_heads, head_size)`, `k/v` are
/// `(batch, kv_seq_len, kv_num_heads, head_size)`, `attn_mask` is
/// `(batch, kv_seq_len)`.
///
/// The caller must `stream.synchronize()` (or use `clone_dtoh`) before reading
/// the result to observe execution-time errors.
#[allow(clippy::too_many_arguments)]
pub fn flash_attn<T: FlashDtype>(
    q: &impl DevicePtr<T>,
    k: &impl DevicePtr<T>,
    v: &impl DevicePtr<T>,
    o: &mut CudaSlice<T>,
    batch: i32,
    q_seq_len: i32,
    kv_seq_len: i32,
    q_num_heads: i32,
    kv_num_heads: i32,
    head_size: i32,
    start_pos: i32,
    scale: f32,
    attn_mask: Option<&CudaSlice<u8>>,
    stream: &CudaStream,
) -> Result<(), FlashAttnError> {
    validate(q, k, v, &*o, batch, q_seq_len, kv_seq_len, q_num_heads, kv_num_heads, head_size, start_pos, attn_mask)?;

    let (q_ptr, _q_guard) = q.device_ptr(stream);
    let (k_ptr, _k_guard) = k.device_ptr(stream);
    let (v_ptr, _v_guard) = v.device_ptr(stream);
    let (o_ptr, _o_guard) = o.device_ptr(stream);

    let (mask_ptr, _mask_guard) = match attn_mask {
        Some(m) => {
            let (p, guard) = m.device_ptr(stream);
            (p as *const u8, Some(guard))
        }
        None => (std::ptr::null(), None),
    };

    let code = unsafe {
        T::ffi_batch(
            q_ptr as *const T,
            k_ptr as *const T,
            v_ptr as *const T,
            o_ptr as *mut T,
            batch,
            q_seq_len,
            kv_seq_len,
            q_num_heads,
            kv_num_heads,
            head_size,
            start_pos,
            scale,
            mask_ptr,
            stream.cu_stream() as *mut c_void,
        )
    };

    FlashAttnError::from_status(code)
}

/// `flash_attn::<f32>` convenience wrapper, retaining the historical name.
#[allow(clippy::too_many_arguments)]
pub fn flash_attn_f32(
    q: &impl DevicePtr<f32>,
    k: &impl DevicePtr<f32>,
    v: &impl DevicePtr<f32>,
    o: &mut CudaSlice<f32>,
    batch: i32,
    q_seq_len: i32,
    kv_seq_len: i32,
    q_num_heads: i32,
    kv_num_heads: i32,
    head_size: i32,
    start_pos: i32,
    scale: f32,
    attn_mask: Option<&CudaSlice<u8>>,
    stream: &CudaStream,
) -> Result<(), FlashAttnError> {
    flash_attn(q, k, v, o, batch, q_seq_len, kv_seq_len, q_num_heads, kv_num_heads, head_size, start_pos, scale, attn_mask, stream)
}

/// Low-level packed variable-length flash attention (causal, bottom-right aligned).
///
/// `q/o` are `(total_q, q_heads, head_size)` and `cu_seqlens_*` are
/// `(num_seqs + 1,)`.
///
/// - `block_tables == None`: contiguous K/V, `k/v` are
///   `(total_kv, kv_heads, head_size)`.
/// - `block_tables == Some(t)`: paged K/V, `k/v` are a cache
///   `(num_blocks, block_size, kv_heads, head_size)` addressed by `t`
///   `(num_seqs, max_blocks)`; `max_blocks` is inferred as `t.len() / num_seqs`.
#[allow(clippy::too_many_arguments)]
pub fn flash_attn_varlen<T: FlashDtype>(
    q: &impl DevicePtr<T>,
    k: &impl DevicePtr<T>,
    v: &impl DevicePtr<T>,
    o: &mut CudaSlice<T>,
    cu_seqlens_q: &impl DevicePtr<i32>,
    cu_seqlens_k: &impl DevicePtr<i32>,
    num_seqs: i32,
    max_seqlen_q: i32,
    max_seqlen_k: i32,
    q_heads: i32,
    kv_heads: i32,
    head_size: i32,
    scale: f32,
    block_tables: Option<&impl DevicePtr<i32>>,
    block_size: i32,
    stream: &CudaStream,
) -> Result<(), FlashAttnError> {
    let (q_ptr, _q_guard) = q.device_ptr(stream);
    let (k_ptr, _k_guard) = k.device_ptr(stream);
    let (v_ptr, _v_guard) = v.device_ptr(stream);
    let (o_ptr, _o_guard) = o.device_ptr(stream);
    let (cu_q_ptr, _cu_q_guard) = cu_seqlens_q.device_ptr(stream);
    let (cu_k_ptr, _cu_k_guard) = cu_seqlens_k.device_ptr(stream);

    let mut _bt_guard = None;
    let bt_ptr = match block_tables {
        Some(bt) => {
            let (p, g) = bt.device_ptr(stream);
            _bt_guard = Some(g);
            p as *const i32
        }
        None => std::ptr::null(),
    };
    let k_paged = if block_tables.is_some() { 1 } else { 0 };
    let max_blocks = match block_tables {
        Some(bt) if num_seqs > 0 => (bt.len() / num_seqs as usize) as i32,
        _ => 0,
    };

    let code = unsafe {
        T::ffi_varlen(
            q_ptr as *const T,
            k_ptr as *const T,
            v_ptr as *const T,
            o_ptr as *mut T,
            cu_q_ptr as *const i32,
            cu_k_ptr as *const i32,
            bt_ptr,
            num_seqs,
            max_seqlen_q,
            max_seqlen_k,
            q_heads,
            kv_heads,
            head_size,
            block_size,
            max_blocks,
            k_paged,
            scale,
            stream.cu_stream() as *mut c_void,
        )
    };

    FlashAttnError::from_status(code)
}

/// Low-level attention over a paged KV cache.
///
/// `q`/`o` are `(num_seqs, q_seq_len, q_heads, head_size)`; `q_seq_len == 1`
/// selects the decode kernel, `> 1` the prefill kernel. `k_cache`/`v_cache` are
/// `(num_blocks, block_size, kv_heads, head_size)`, `context_lens` is
/// `(num_seqs,)` = total cached length (including the q tokens, which must
/// already be present in the cache). Causal is bottom-right aligned.
#[allow(clippy::too_many_arguments)]
pub fn flash_attn_with_kvcache<T: FlashDtype>(
    q: &impl DevicePtr<T>,
    k_cache: &impl DevicePtr<T>,
    v_cache: &impl DevicePtr<T>,
    o: &mut CudaSlice<T>,
    block_tables: &impl DevicePtr<i32>,
    context_lens: &impl DevicePtr<i32>,
    num_seqs: i32,
    q_seq_len: i32,
    q_heads: i32,
    kv_heads: i32,
    head_size: i32,
    block_size: i32,
    max_blocks: i32,
    scale: f32,
    stream: &CudaStream,
) -> Result<(), FlashAttnError> {
    if q_seq_len <= 0 {
        return Err(FlashAttnError::InvalidShape(format!("q_seq_len={q_seq_len} must be > 0")));
    }
    let q_need = num_seqs as u64 * q_seq_len as u64 * q_heads as u64 * head_size as u64;
    check_len("q", q.len(), q_need)?;
    check_len("o", o.len(), q_need)?;

    let (q_ptr, _q_guard) = q.device_ptr(stream);
    let (k_ptr, _k_guard) = k_cache.device_ptr(stream);
    let (v_ptr, _v_guard) = v_cache.device_ptr(stream);
    let (o_ptr, _o_guard) = o.device_ptr(stream);
    let (bt_ptr, _bt_guard) = block_tables.device_ptr(stream);
    let (lens_ptr, _lens_guard) = context_lens.device_ptr(stream);

    let code = unsafe {
        T::ffi_kvcache(
            q_ptr as *const T,
            k_ptr as *const T,
            v_ptr as *const T,
            o_ptr as *mut T,
            bt_ptr as *const i32,
            lens_ptr as *const i32,
            num_seqs,
            q_seq_len,
            q_heads,
            kv_heads,
            head_size,
            block_size,
            max_blocks,
            scale,
            stream.cu_stream() as *mut c_void,
        )
    };

    FlashAttnError::from_status(code)
}

#[allow(clippy::too_many_arguments)]
fn validate<T: FlashDtype>(
    q: &impl DevicePtr<T>,
    k: &impl DevicePtr<T>,
    v: &impl DevicePtr<T>,
    o: &impl DevicePtr<T>,
    batch: i32,
    q_seq_len: i32,
    kv_seq_len: i32,
    q_num_heads: i32,
    kv_num_heads: i32,
    head_size: i32,
    start_pos: i32,
    attn_mask: Option<&CudaSlice<u8>>,
) -> Result<(), FlashAttnError> {
    if batch <= 0 || q_seq_len <= 0 || kv_seq_len <= 0 || q_num_heads <= 0 || kv_num_heads <= 0 {
        return Err(FlashAttnError::InvalidShape(format!(
            "batch={batch} q_seq_len={q_seq_len} kv_seq_len={kv_seq_len} \
             q_num_heads={q_num_heads} kv_num_heads={kv_num_heads} must be positive"
        )));
    }
    if q_num_heads % kv_num_heads != 0 {
        return Err(FlashAttnError::InvalidShape(format!(
            "q_num_heads={q_num_heads} must be a multiple of kv_num_heads={kv_num_heads} (GQA)"
        )));
    }
    if start_pos < 0 {
        return Err(FlashAttnError::InvalidStartPos(format!("start_pos={start_pos}")));
    }
    if !matches!(head_size, 32 | 64 | 128) {
        return Err(FlashAttnError::InvalidHeadSize(format!(
            "head_size={head_size} (only 32/64/128)"
        )));
    }

    let d = head_size as u64;
    let q_need = batch as u64 * q_seq_len as u64 * q_num_heads as u64 * d;
    let kv_need = batch as u64 * kv_seq_len as u64 * kv_num_heads as u64 * d;
    let mask_need = batch as u64 * kv_seq_len as u64;

    check_len("q", q.len(), q_need)?;
    check_len("k", k.len(), kv_need)?;
    check_len("v", v.len(), kv_need)?;
    check_len("o", o.len(), q_need)?;
    if let Some(m) = attn_mask {
        check_len("attn_mask", m.len(), mask_need)?;
    }
    Ok(())
}

fn check_len(name: &str, actual: usize, need: u64) -> Result<(), FlashAttnError> {
    if actual as u64 >= need {
        return Ok(());
    }
    Err(FlashAttnError::BufferTooSmall(format!(
        "{name}: need at least {need} elements, got {actual}"
    )))
}