aprender-gpu 0.64.0

Pure Rust PTX generation for NVIDIA CUDA - no LLVM, no nvcc
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
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
//! Kernel Cache (WAPR-PERF-004)
//!
//! Global kernel cache to eliminate PTX recompilation overhead.

#[cfg(feature = "cuda")]
use std::cell::Cell;

#[cfg(feature = "cuda")]
use std::collections::HashMap;
#[cfg(feature = "cuda")]
use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};

#[cfg(feature = "cuda")]
use crate::driver::{CudaContext, CudaModule, CudaStream, LaunchConfig};
#[cfg(feature = "cuda")]
use crate::error::Result;

/// Global kernel cache to eliminate PTX recompilation overhead.
///
/// Each unique kernel configuration (name + parameters) is compiled once
/// and cached for reuse. This eliminates the 24x recompilation per inference
/// that was previously observed.
///
/// ## Keying Strategy
///
/// Keys are strings of format: `"{kernel_name}:{config}"` where config
/// encodes all parameters that affect the PTX output.
///
/// ## Thread Safety
///
/// The cache uses double-locking:
/// - Outer Mutex guards the HashMap
/// - Inner Arc<Mutex<CudaModule>> allows concurrent kernel launches
///
/// ## Example Keys
///
/// - `"softmax:32"` - SoftmaxKernel for row_size=32
/// - `"softmax_long_row:1500"` - LongRowSoftmaxKernel for row_size=1500
/// - `"residual_add:1024"` - ResidualAddKernel for n=1024
#[cfg(feature = "cuda")]
static KERNEL_CACHE: OnceLock<Mutex<HashMap<String, Arc<Mutex<CudaModule>>>>> = OnceLock::new();

// Kernel cache hit/miss statistics, counted **per thread**.
//
// GPU-ORD-2: these were process-global atomics, and every caller uses them the
// same way — reset, do work, read. Sharing them across threads meant one
// caller's window measured every other caller's compilations. Attributing a hit
// or a miss to the thread that asked for the kernel makes cross-thread
// pollution unrepresentable, and matches how the numbers are read.
#[cfg(feature = "cuda")]
thread_local! {
    static KERNEL_CACHE_HITS: Cell<u64> = const { Cell::new(0) };
    static KERNEL_CACHE_MISSES: Cell<u64> = const { Cell::new(0) };
}

/// Serialises *destructive* access to the shared compiled-module map.
///
/// The map itself must stay process-global — it exists so a kernel is JIT
/// compiled once rather than 24 times per inference. What broke
/// `test_kernel_cache_stats_after_operations` was not sharing the map, it was
/// one caller wiping it while another was midway through observing that a
/// repeated kernel does *not* recompile. The tell was two identical
/// `[KERNEL-CACHE] Compiling: gelu:16` lines in a single test's output.
///
/// `clear_kernel_cache` is the only way to empty the map and it takes this
/// lock, so anyone who needs a compile/hit sequence to be atomic against every
/// clear in the process can hold `kernel_cache_exclusive()` across it.
#[cfg(feature = "cuda")]
static CACHE_CLEAR_LOCK: Mutex<()> = Mutex::new(());

/// Get the global kernel cache, initializing if needed
#[cfg(feature = "cuda")]
pub(crate) fn get_kernel_cache() -> &'static Mutex<HashMap<String, Arc<Mutex<CudaModule>>>> {
    KERNEL_CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}

/// Acquire the global cache lock, mapping poison errors to `GpuError`.
///
/// Every production call-site that needs the cache HashMap goes through this
/// single helper, eliminating the repeated `.lock().map_err(…)` boilerplate.
#[cfg(feature = "cuda")]
fn lock_cache(
    cache: &Mutex<HashMap<String, Arc<Mutex<CudaModule>>>>,
) -> Result<std::sync::MutexGuard<'_, HashMap<String, Arc<Mutex<CudaModule>>>>> {
    cache
        .lock()
        .map_err(|e| crate::GpuError::KernelLaunch(format!("Cache lock poisoned: {e}")))
}

/// Acquire a `Mutex<CudaModule>` lock, mapping poison errors to `GpuError`.
#[cfg(feature = "cuda")]
fn lock_module(module: &Mutex<CudaModule>) -> Result<std::sync::MutexGuard<'_, CudaModule>> {
    module
        .lock()
        .map_err(|e| crate::GpuError::KernelLaunch(format!("Module lock poisoned: {e}")))
}

/// Get a cached kernel module, compiling if not present.
///
/// # Arguments
///
/// * `ctx` - CUDA context for compilation
/// * `key` - Cache key (kernel_name:config)
/// * `ptx` - PTX source to compile on cache miss
///
/// # Returns
///
/// Arc to the cached module, wrapped in Mutex for mutable access.
#[cfg(feature = "cuda")]
pub(crate) fn get_or_compile_kernel(
    ctx: &CudaContext,
    key: &str,
    ptx: &str,
) -> Result<Arc<Mutex<CudaModule>>> {
    let cache = get_kernel_cache();

    // Fast path: check if already cached
    {
        let cache_guard = lock_cache(cache)?;
        if let Some(module) = cache_guard.get(key) {
            KERNEL_CACHE_HITS.with(|c| c.set(c.get() + 1));
            return Ok(Arc::clone(module));
        }
    }

    // Cache miss: compile and store
    KERNEL_CACHE_MISSES.with(|c| c.set(c.get() + 1));
    eprintln!("[KERNEL-CACHE] Compiling: {key}");

    let module = CudaModule::from_ptx(ctx, ptx)?;
    let module_arc = Arc::new(Mutex::new(module));

    // Insert into cache
    lock_cache(cache)?.insert(key.to_string(), Arc::clone(&module_arc));

    Ok(module_arc)
}

/// Compile (or fetch from cache), lock the module, and launch the kernel.
///
/// This centralises the repeated resource-management boilerplate that every
/// CUDA operation needs:
///
/// 1. `get_or_compile_kernel` — cache lookup / PTX JIT compilation
/// 2. `module_arc.lock()` — acquire the `Mutex<CudaModule>`
/// 3. `stream.launch_kernel` — unsafe dispatch
///
/// By housing this in `cache.rs` the pattern is written once and all call
/// sites across `elementwise`, `gemm`, `norm_activation`, `linear_bias`,
/// `layout`, and `incremental` can delegate to it.
///
/// # Safety
///
/// The caller must guarantee that `args` contains valid device pointers whose
/// types and count match the kernel signature identified by `kernel_name`.
#[cfg(feature = "cuda")]
pub(crate) fn compile_lock_launch(
    ctx: &CudaContext,
    stream: &CudaStream,
    cache_key: &str,
    ptx: &str,
    kernel_name: &str,
    config: &LaunchConfig,
    args: &mut [*mut std::ffi::c_void],
) -> Result<()> {
    let module_arc = get_or_compile_kernel(ctx, cache_key, ptx)?;
    let mut module = lock_module(&module_arc)?;
    // SAFETY: Caller guarantees args are valid pointers matching kernel signature.
    unsafe {
        stream.launch_kernel(&mut module, kernel_name, config, args)?;
    }
    Ok(())
}

/// Get kernel cache hits attributed to the current thread since last reset
#[cfg(feature = "cuda")]
#[must_use]
pub fn kernel_cache_hits() -> u64 {
    KERNEL_CACHE_HITS.with(Cell::get)
}

/// Get kernel cache misses attributed to the current thread since last reset
#[cfg(feature = "cuda")]
#[must_use]
pub fn kernel_cache_misses() -> u64 {
    KERNEL_CACHE_MISSES.with(Cell::get)
}

/// Reset the current thread's kernel cache statistics
#[cfg(feature = "cuda")]
pub fn reset_kernel_cache_stats() {
    KERNEL_CACHE_HITS.with(|c| c.set(0));
    KERNEL_CACHE_MISSES.with(|c| c.set(0));
}

/// Empty the shared module map. Assumes `CACHE_CLEAR_LOCK` is already held.
#[cfg(feature = "cuda")]
fn clear_kernel_cache_locked() {
    if let Some(cache) = KERNEL_CACHE.get() {
        if let Ok(mut guard) = lock_cache(cache) {
            guard.clear();
        }
    }
    reset_kernel_cache_stats();
}

/// Exclusive access to the shared compiled-kernel cache.
///
/// While this guard is alive no `clear_kernel_cache()` anywhere in the process
/// can take effect, so a sequence like "compile a kernel, run the same kernel
/// again, assert the second run did not recompile" is atomic against every
/// clear. Dropping the guard releases it.
#[cfg(feature = "cuda")]
pub struct KernelCacheExclusive(MutexGuard<'static, ()>);

#[cfg(feature = "cuda")]
impl KernelCacheExclusive {
    /// Empty the cache while holding exclusivity.
    pub fn clear(&self) {
        clear_kernel_cache_locked();
    }
}

/// Acquire exclusive access to the shared compiled-kernel cache.
///
/// Blocks until no other holder and no in-flight `clear_kernel_cache()` call
/// is running. See [`KernelCacheExclusive`].
#[cfg(feature = "cuda")]
#[must_use]
pub fn kernel_cache_exclusive() -> KernelCacheExclusive {
    // A poisoned lock guards no invariant here — it only orders clears — so
    // recovering the guard is correct and keeps an unrelated panic from
    // cascading into every later cache user.
    KernelCacheExclusive(
        CACHE_CLEAR_LOCK
            .lock()
            .unwrap_or_else(PoisonError::into_inner),
    )
}

/// Clear the kernel cache (useful for testing)
///
/// Takes the same exclusivity lock as [`kernel_cache_exclusive`], so it cannot
/// land in the middle of another caller's compile/hit observation.
#[cfg(feature = "cuda")]
pub fn clear_kernel_cache() {
    let _exclusive = kernel_cache_exclusive();
    clear_kernel_cache_locked();
}

// Non-CUDA stubs for compilation without cuda feature

/// Get kernel cache hit count (stub when CUDA disabled)
#[cfg(not(feature = "cuda"))]
#[must_use]
pub fn kernel_cache_hits() -> u64 {
    0
}

/// Get kernel cache miss count (stub when CUDA disabled)
#[cfg(not(feature = "cuda"))]
#[must_use]
pub fn kernel_cache_misses() -> u64 {
    0
}

/// Reset kernel cache statistics (stub when CUDA disabled)
#[cfg(not(feature = "cuda"))]
pub fn reset_kernel_cache_stats() {}

/// Clear the kernel cache (stub when CUDA disabled)
#[cfg(not(feature = "cuda"))]
pub fn clear_kernel_cache() {}

/// Exclusive access to the kernel cache (stub when CUDA disabled)
#[cfg(not(feature = "cuda"))]
pub struct KernelCacheExclusive;

#[cfg(not(feature = "cuda"))]
impl KernelCacheExclusive {
    /// Empty the cache (stub when CUDA disabled)
    pub fn clear(&self) {}
}

/// Acquire exclusive access to the kernel cache (stub when CUDA disabled)
#[cfg(not(feature = "cuda"))]
#[must_use]
pub fn kernel_cache_exclusive() -> KernelCacheExclusive {
    KernelCacheExclusive
}

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

    /// Reset stats and assert both hits and misses are zero.
    fn assert_clean_stats() {
        reset_kernel_cache_stats();
        assert_eq!(kernel_cache_hits(), 0);
        assert_eq!(kernel_cache_misses(), 0);
    }

    #[test]
    fn test_kernel_cache_stats_initial() {
        assert_clean_stats();
    }

    #[test]
    fn test_clear_kernel_cache() {
        // Just verify it doesn't panic
        clear_kernel_cache();
        assert_clean_stats();
    }

    /// Reset and clear are both idempotent and leave stats at zero.
    #[test]
    fn test_idempotent_operations() {
        for _ in 0..3 {
            reset_kernel_cache_stats();
            clear_kernel_cache();
        }
        assert_clean_stats();
    }
}

/// CUDA-specific tests that exercise the cache infrastructure
#[cfg(all(test, feature = "cuda"))]
mod cuda_tests {
    use super::*;

    /// Reset stats and assert both hits and misses are zero.
    fn assert_clean_stats() {
        reset_kernel_cache_stats();
        assert_eq!(kernel_cache_hits(), 0);
        assert_eq!(kernel_cache_misses(), 0);
    }

    /// Increment both counters by the given amounts, assert they match, then reset.
    fn assert_counter_round_trip(hits: u64, misses: u64) {
        assert_clean_stats();
        for _ in 0..hits {
            KERNEL_CACHE_HITS.with(|c| c.set(c.get() + 1));
        }
        for _ in 0..misses {
            KERNEL_CACHE_MISSES.with(|c| c.set(c.get() + 1));
        }
        assert_eq!(kernel_cache_hits(), hits);
        assert_eq!(kernel_cache_misses(), misses);
        assert_clean_stats();
    }

    /// GPU-ORD-2 falsifier (a): another thread's compilations must not land in
    /// this thread's hit/miss window.
    #[test]
    fn test_cache_stats_are_not_polluted_by_other_threads() {
        reset_kernel_cache_stats();
        KERNEL_CACHE_HITS.with(|c| c.set(c.get() + 1));

        std::thread::spawn(|| {
            reset_kernel_cache_stats();
            for _ in 0..500 {
                KERNEL_CACHE_HITS.with(|c| c.set(c.get() + 1));
                KERNEL_CACHE_MISSES.with(|c| c.set(c.get() + 1));
            }
        })
        .join()
        .expect("neighbour thread must not panic");

        assert_eq!(
            (kernel_cache_hits(), kernel_cache_misses()),
            (1, 0),
            "another thread's cache activity was attributed to this thread"
        );
    }

    /// GPU-ORD-2 falsifier (b): a clear cannot land inside someone else's
    /// compile/hit observation.
    ///
    /// This is the interference itself, expressed as behaviour and without
    /// needing a GPU: while exclusivity is held, a `clear_kernel_cache()` on
    /// another thread must not have completed. Before the fix the clear was a
    /// straight `HashMap::clear()` with nothing to wait on, so it landed
    /// immediately — which is exactly how a neighbour wiped `gelu:16` between
    /// the two halves of `test_kernel_cache_stats_after_operations`.
    #[test]
    fn test_clear_cannot_interleave_with_exclusive_holder() {
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::sync::Arc as StdArc;

        let cleared = StdArc::new(AtomicBool::new(false));
        let cleared_bg = StdArc::clone(&cleared);

        let exclusive = kernel_cache_exclusive();

        let bg = std::thread::spawn(move || {
            clear_kernel_cache();
            cleared_bg.store(true, Ordering::SeqCst);
        });

        // Give the background thread ample opportunity to run to completion.
        for _ in 0..50 {
            assert!(
                !cleared.load(Ordering::SeqCst),
                "clear_kernel_cache() completed while another caller held \
                 exclusivity — a clear can still land inside someone else's \
                 compile/hit observation"
            );
            std::thread::sleep(std::time::Duration::from_millis(2));
        }

        drop(exclusive);
        bg.join().expect("clearing thread must not panic");
        assert!(
            cleared.load(Ordering::SeqCst),
            "clear_kernel_cache() never completed after exclusivity was released"
        );
    }

    /// Clear the cache and assert it is empty with zeroed stats.
    ///
    /// Holds exclusivity across the clear *and* the assertion: otherwise a
    /// concurrent test compiling a kernel repopulates the map between the two
    /// and this reads a non-empty cache.
    fn clear_and_assert_empty() {
        let exclusive = kernel_cache_exclusive();
        exclusive.clear();
        let guard = lock_cache(get_kernel_cache()).expect("Cache lock should not be poisoned");
        assert!(guard.is_empty(), "Cache should be empty");
    }

    /// Test get_kernel_cache is idempotent (returns same static reference)
    /// and the lock can be acquired repeatedly.
    #[test]
    fn test_get_kernel_cache_static_and_reentrant() {
        let cache1 = get_kernel_cache();
        let cache2 = get_kernel_cache();
        assert!(std::ptr::eq(cache1, cache2));
        // Lock can be acquired and released multiple times
        for _ in 0..3 {
            let _guard = lock_cache(cache1).expect("lock");
        }
    }

    /// Test clear_kernel_cache empties the hashmap and resets stats.
    #[test]
    fn test_clear_kernel_cache_clears_hashmap() {
        clear_and_assert_empty();
    }

    /// Test atomic counter increment round-trips at multiple scales.
    #[test]
    fn test_atomic_counter_operations() {
        assert_counter_round_trip(5, 3);
        assert_counter_round_trip(100, 50);
    }

    /// Test clear_kernel_cache is safe even if the cache was never
    /// explicitly initialised (covers the `KERNEL_CACHE.get() == None` path).
    #[test]
    fn test_clear_uninitialized_cache() {
        clear_kernel_cache();
        assert_clean_stats();
    }
}