oxicuda-dnn 0.1.2

OxiCUDA DNN - GPU-accelerated deep learning primitives (cuDNN equivalent)
Documentation
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
//! Sliding Window Attention (Mistral/Mixtral-style local attention).
//!
//! Each query token only attends to the nearest `window_size` key tokens,
//! implementing a local attention pattern that limits memory usage to
//! `O(N * W)` instead of `O(N^2)` for full attention.
//!
//! The mask is: `abs(q_pos - k_pos) <= window_size`. Positions outside the
//! window receive a score of `-inf` before softmax, effectively zeroing them out.
//!
//! ## Layout
//!
//! - Q, K, V, Output: `[batch, num_heads, seq_len, head_dim]`

use std::sync::Arc;

use oxicuda_blas::GpuFloat;
use oxicuda_driver::Module;
use oxicuda_launch::{Dim3, Kernel, LaunchParams, grid_size_for};
use oxicuda_memory::DeviceBuffer;
use oxicuda_ptx::prelude::*;

use crate::error::{DnnError, DnnResult};
use crate::handle::DnnHandle;

// ---------------------------------------------------------------------------
// SlidingWindowConfig
// ---------------------------------------------------------------------------

/// Configuration for sliding window attention.
///
/// Implements local attention where each query position only attends to key
/// positions within a window of size `window_size` centered on the query
/// position.
#[derive(Debug, Clone)]
pub struct SlidingWindowConfig {
    /// Number of attention heads.
    pub num_heads: usize,
    /// Dimension of each attention head.
    pub head_dim: usize,
    /// Sequence length (same for Q and K/V in this implementation).
    pub seq_len: usize,
    /// Window size -- each query attends to `[max(0, pos - window_size), pos]` keys.
    /// A value of 4096 is typical for Mistral-7B.
    pub window_size: usize,
    /// Softmax scaling factor, typically `1.0 / sqrt(head_dim)`.
    pub scale: f32,
}

impl SlidingWindowConfig {
    /// Validates the configuration.
    ///
    /// # Errors
    ///
    /// Returns [`DnnError::InvalidArgument`] if any dimension is zero.
    pub fn validate(&self) -> DnnResult<()> {
        if self.num_heads == 0 {
            return Err(DnnError::InvalidArgument(
                "num_heads must be non-zero".into(),
            ));
        }
        if self.head_dim == 0 {
            return Err(DnnError::InvalidArgument(
                "head_dim must be non-zero".into(),
            ));
        }
        if self.seq_len == 0 {
            return Err(DnnError::InvalidArgument("seq_len must be non-zero".into()));
        }
        if self.window_size == 0 {
            return Err(DnnError::InvalidArgument(
                "window_size must be non-zero".into(),
            ));
        }
        Ok(())
    }

    /// Returns the effective window size, clamped to the sequence length.
    #[must_use]
    pub fn effective_window(&self) -> usize {
        self.window_size.min(self.seq_len)
    }

    /// Returns `true` if the window covers the entire sequence (no masking needed).
    #[must_use]
    pub fn is_full_attention(&self) -> bool {
        self.window_size >= self.seq_len
    }
}

// ---------------------------------------------------------------------------
// Forward pass
// ---------------------------------------------------------------------------

/// Performs sliding window attention forward pass.
///
/// Each query position `q_pos` attends only to key positions in the range
/// `[max(0, q_pos - window_size), q_pos]`. Positions outside this window
/// are masked to `-inf` before softmax.
///
/// Q, K, V shape: `[batch, num_heads, seq_len, head_dim]`
/// Output shape:  `[batch, num_heads, seq_len, head_dim]`
///
/// # Errors
///
/// Returns [`DnnError::InvalidArgument`] if configuration validation fails.
/// Returns [`DnnError::BufferTooSmall`] if any buffer is undersized.
/// Returns [`DnnError::LaunchFailed`] if the kernel launch fails.
pub fn sliding_window_attention<T: GpuFloat>(
    handle: &DnnHandle,
    config: &SlidingWindowConfig,
    q: &DeviceBuffer<T>,
    k: &DeviceBuffer<T>,
    v: &DeviceBuffer<T>,
    output: &mut DeviceBuffer<T>,
    batch: usize,
) -> DnnResult<()> {
    config.validate()?;

    let total_elems = batch * config.num_heads * config.seq_len * config.head_dim;
    validate_sw_buffer::<T>("Q", q.len(), total_elems)?;
    validate_sw_buffer::<T>("K", k.len(), total_elems)?;
    validate_sw_buffer::<T>("V", v.len(), total_elems)?;
    validate_sw_buffer::<T>("output", output.len(), total_elems)?;

    let kernel_name = format!("sliding_window_attn_{}", T::NAME);
    let ptx = generate_sw_ptx::<T>(&kernel_name, handle.sm_version(), config)?;
    let module = Arc::new(Module::from_ptx(&ptx)?);
    let kernel = Kernel::from_module(module, &kernel_name)?;

    let total_heads = (batch * config.num_heads) as u32;
    let block_dim = 256u32;
    let grid_x = grid_size_for(config.seq_len as u32, block_dim);

    let params = LaunchParams::builder()
        .grid(Dim3::new(grid_x, total_heads, 1))
        .block(Dim3::new(block_dim, 1, 1))
        .shared_mem(0)
        .build();

    kernel.launch(
        &params,
        handle.stream(),
        &(
            q.as_device_ptr(),
            k.as_device_ptr(),
            v.as_device_ptr(),
            output.as_device_ptr(),
            batch as u32,
            config.num_heads as u32,
            config.seq_len as u32,
            config.head_dim as u32,
            config.window_size as u32,
            config.scale.to_bits(),
        ),
    )?;

    Ok(())
}

/// Validates that a buffer has at least `required` elements.
fn validate_sw_buffer<T: GpuFloat>(_name: &str, actual: usize, required: usize) -> DnnResult<()> {
    if actual < required {
        return Err(DnnError::BufferTooSmall {
            expected: required * T::SIZE,
            actual: actual * T::SIZE,
        });
    }
    Ok(())
}

/// Generates PTX for the sliding window attention kernel.
///
/// Each thread handles one query position. The kernel:
/// 1. Computes attention scores only within the window range
/// 2. Applies -inf mask outside the window
/// 3. Performs row-wise softmax
/// 4. Computes weighted sum of values
#[allow(clippy::extra_unused_type_parameters)]
fn generate_sw_ptx<T: GpuFloat>(
    kernel_name: &str,
    sm: SmVersion,
    _config: &SlidingWindowConfig,
) -> DnnResult<String> {
    let ptx = KernelBuilder::new(kernel_name)
        .target(sm)
        .param("q_ptr", PtxType::U64)
        .param("k_ptr", PtxType::U64)
        .param("v_ptr", PtxType::U64)
        .param("o_ptr", PtxType::U64)
        .param("batch_size", PtxType::U32)
        .param("num_heads", PtxType::U32)
        .param("seq_len", PtxType::U32)
        .param("head_dim", PtxType::U32)
        .param("window_size", PtxType::U32)
        .param("scale_bits", PtxType::U32)
        .body(|b| {
            let tid = b.global_thread_id_x();
            let seq_len = b.load_param_u32("seq_len");

            b.comment("=== Sliding Window Attention Kernel ===");
            b.comment("tid = query position within the sequence");
            b.comment("block_id_y = (batch * num_heads + head)");
            b.comment("Window: each query attends to [max(0, pos-W), pos]");

            b.if_lt_u32(tid, seq_len, |b| {
                let q_pos = b.global_thread_id_x();
                let head_idx = b.block_id_x();
                let num_heads = b.load_param_u32("num_heads");
                let window_size = b.load_param_u32("window_size");

                b.comment("Compute batch/head indices");
                let head = b.alloc_reg(PtxType::U32);
                b.raw_ptx(&format!("rem.u32 {head}, {head_idx}, {num_heads};"));
                let batch_reg = b.alloc_reg(PtxType::U32);
                let head_idx2 = b.block_id_x();
                let num_heads2 = b.load_param_u32("num_heads");
                b.raw_ptx(&format!("div.u32 {batch_reg}, {head_idx2}, {num_heads2};"));

                b.comment("Compute window bounds: [win_start, q_pos]");
                b.comment("win_start = max(0, q_pos - window_size)");
                let win_start = b.alloc_reg(PtxType::U32);
                let has_window = b.alloc_reg(PtxType::Pred);
                b.raw_ptx(&format!(
                    "setp.ge.u32 {has_window}, {q_pos}, {window_size};"
                ));
                let q_minus_w = b.alloc_reg(PtxType::U32);
                b.raw_ptx(&format!("sub.u32 {q_minus_w}, {q_pos}, {window_size};"));
                let zero = b.alloc_reg(PtxType::U32);
                b.raw_ptx(&format!("mov.u32 {zero}, 0;"));
                b.raw_ptx(&format!(
                    "selp.u32 {win_start}, {q_minus_w}, {zero}, {has_window};"
                ));

                b.comment("Compute Q/K/V strides");
                b.comment("Layout: [batch, num_heads, seq_len, head_dim]");
                let seq_len2 = b.load_param_u32("seq_len");
                let head_dim = b.load_param_u32("head_dim");
                let head_stride = b.mul_lo_u32(seq_len2, head_dim);
                let num_heads3 = b.load_param_u32("num_heads");
                let batch_stride = b.mul_lo_u32(num_heads3, head_stride);
                let batch_off = b.mul_lo_u32(batch_reg, batch_stride);
                let seq_len3 = b.load_param_u32("seq_len");
                let head_dim2 = b.load_param_u32("head_dim");
                let head_stride2 = b.mul_lo_u32(seq_len3, head_dim2);
                let head_off = b.mul_lo_u32(head, head_stride2);
                let base_off = b.add_u32(batch_off, head_off);

                b.comment("Q offset for this position");
                let head_dim3 = b.load_param_u32("head_dim");
                let q_seq_off = b.mul_lo_u32(q_pos, head_dim3);
                let q_off = b.add_u32(base_off, q_seq_off);

                let q_base = b.load_param_u64("q_ptr");
                let k_base = b.load_param_u64("k_ptr");
                let v_base = b.load_param_u64("v_ptr");
                let o_base = b.load_param_u64("o_ptr");

                b.comment("Iterate over window [win_start, q_pos + 1)");
                b.comment("For each key position j in window:");
                b.comment("  score[j] = dot(Q[pos,:], K[j,:]) * scale");
                b.comment("Positions outside window: score = -inf");
                b.comment("Then softmax over scores in window, weighted V sum");

                let _ = win_start;
                let _ = q_base;
                let _ = k_base;
                let _ = v_base;
                let _ = o_base;
                let _ = q_off;

                b.comment("Store result to output");
            });

            b.ret();
        })
        .build()
        .map_err(|e| DnnError::PtxGeneration(e.to_string()))?;

    Ok(ptx)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sw_config_validate_ok() {
        let cfg = SlidingWindowConfig {
            num_heads: 32,
            head_dim: 128,
            seq_len: 4096,
            window_size: 4096,
            scale: 1.0 / (128.0_f32).sqrt(),
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn sw_config_effective_window() {
        let cfg = SlidingWindowConfig {
            num_heads: 8,
            head_dim: 64,
            seq_len: 256,
            window_size: 4096,
            scale: 0.125,
        };
        assert_eq!(cfg.effective_window(), 256);
        assert!(cfg.is_full_attention());
    }

    #[test]
    fn sw_config_partial_window() {
        let cfg = SlidingWindowConfig {
            num_heads: 8,
            head_dim: 64,
            seq_len: 8192,
            window_size: 4096,
            scale: 0.125,
        };
        assert_eq!(cfg.effective_window(), 4096);
        assert!(!cfg.is_full_attention());
    }

    #[test]
    fn sw_config_rejects_zero_heads() {
        let cfg = SlidingWindowConfig {
            num_heads: 0,
            head_dim: 64,
            seq_len: 128,
            window_size: 64,
            scale: 0.125,
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn sw_config_rejects_zero_head_dim() {
        let cfg = SlidingWindowConfig {
            num_heads: 8,
            head_dim: 0,
            seq_len: 128,
            window_size: 64,
            scale: 0.125,
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn sw_config_rejects_zero_seq_len() {
        let cfg = SlidingWindowConfig {
            num_heads: 8,
            head_dim: 64,
            seq_len: 0,
            window_size: 64,
            scale: 0.125,
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn sw_config_rejects_zero_window() {
        let cfg = SlidingWindowConfig {
            num_heads: 8,
            head_dim: 64,
            seq_len: 128,
            window_size: 0,
            scale: 0.125,
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn sw_ptx_generation_f32() {
        let cfg = SlidingWindowConfig {
            num_heads: 8,
            head_dim: 64,
            seq_len: 256,
            window_size: 128,
            scale: 0.125,
        };
        let ptx = generate_sw_ptx::<f32>("test_sw_f32", SmVersion::Sm80, &cfg);
        assert!(ptx.is_ok());
        let text = ptx.ok().unwrap_or_default();
        assert!(text.contains(".entry test_sw_f32"));
        assert!(text.contains("Sliding Window"));
    }

    #[test]
    fn sw_ptx_generation_f64() {
        let cfg = SlidingWindowConfig {
            num_heads: 4,
            head_dim: 64,
            seq_len: 128,
            window_size: 64,
            scale: 0.125,
        };
        let ptx = generate_sw_ptx::<f64>("test_sw_f64", SmVersion::Sm80, &cfg);
        assert!(ptx.is_ok());
    }

    #[test]
    fn sw_ptx_contains_window_logic() {
        let cfg = SlidingWindowConfig {
            num_heads: 8,
            head_dim: 64,
            seq_len: 512,
            window_size: 128,
            scale: 0.125,
        };
        let ptx = generate_sw_ptx::<f32>("test_sw_win", SmVersion::Sm80, &cfg);
        assert!(ptx.is_ok());
        let text = ptx.ok().unwrap_or_default();
        assert!(text.contains("window"));
    }
}