atomr-accel-cuda 0.10.0

GPU acceleration via the actor model. Wraps NVIDIA CUDA libraries (cuBLAS, cuDNN, cuFFT, cuRAND, cuSOLVER, cuSPARSE, cuTENSOR, cuBLASLt, NVRTC, NCCL) as supervised atomr actors with generation-validated buffers and a uniform async surface.
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
465
466
467
468
469
470
471
472
473
474
475
476
//! Distribution dispatchers.
//!
//! [`Distribution<T>`] enumerates every supported distribution; the
//! `T` parameter (`f32` or `f64`) is enforced through
//! [`crate::dtype::RngFloatSupported`]. [`FillRequest<T>`] pairs a
//! distribution with the [`GpuRef<T>`] target and a oneshot reply
//! channel; it implements [`RngDispatch`] so the actor can route any
//! float dtype through a single mailbox variant.
//!
//! ## Coverage matrix
//!
//! | distribution | f32 | f64 | path |
//! |---|---|---|---|
//! | Uniform     | ✓ | ✓ | `cudarc::curand::CudaRng::fill_with_uniform` |
//! | Normal      | ✓ | ✓ | `…fill_with_normal` |
//! | LogNormal   | ✓ | ✓ | `…fill_with_log_normal` |
//! | Poisson     | ✓ | ✓ | u32 fill via `curandGeneratePoisson`, then host-side widen |
//! | Exponential | ✓ | ✓ | uniform fill + caller transform (see note) |
//! | Beta        | ✗ | ✗ | needs a custom kernel — returns `LibraryError` |
//! | Cauchy      | ✗ | ✗ | needs a custom kernel — returns `LibraryError` |
//! | Gamma       | ✗ | ✗ | needs a custom kernel — returns `LibraryError` |
//! | Discrete    | ✗ | ✗ | needs `curandCreatePoissonDistribution` + custom kernel |
//!
//! cuRAND's host-API natively exposes only Uniform / Normal /
//! LogNormal / Poisson; the four "✗" rows depend on either NVRTC-
//! generated kernels or device-API calls. Phase 1's job is to
//! freeze the *type-level* surface so callers can write code today
//! that auto-grows when those paths land. Each unsupported variant
//! returns a clearly-tagged
//! `GpuError::LibraryError { lib: "curand", msg: "<dist> not yet wired (Phase 1: needs custom kernel)" }`
//! so users get one consistent error to match on.

use std::sync::Arc;

use cudarc::curand::result::{LogNormalFill, NormalFill, UniformFill};
use cudarc::curand::sys;
use tokio::sync::oneshot;

use crate::completion::CompletionStrategy;
use crate::dtype::RngFloatSupported;
use crate::error::GpuError;
use crate::gpu_ref::GpuRef;
use crate::kernel::dispatch::RngDispatch;
use crate::kernel::envelope;

use super::LIB;

/// Every distribution the cuRAND surface is *intended* to expose.
/// Variants that aren't yet wired to a kernel return a tagged
/// `LibraryError` from [`FillRequest::fill`].
pub enum Distribution<T: RngFloatSupported> {
    Uniform {
        lo: T::Scalar,
        hi: T::Scalar,
    },
    Normal {
        mean: T::Scalar,
        std: T::Scalar,
    },
    LogNormal {
        mean: T::Scalar,
        std: T::Scalar,
    },
    /// cuRAND's Poisson is parameterised by a f64 lambda regardless of
    /// the float output dtype — preserving that here so the type lines
    /// up with `curandGeneratePoisson`.
    Poisson {
        lambda: f64,
    },
    Exponential {
        lambda: T::Scalar,
    },
    Beta {
        alpha: T::Scalar,
        beta: T::Scalar,
    },
    Cauchy {
        loc: T::Scalar,
        scale: T::Scalar,
    },
    Gamma {
        shape: T::Scalar,
        scale: T::Scalar,
    },
    Discrete {
        weights: GpuRef<f32>,
    },
}

/// Single typed fill request: `RngActor` accepts any
/// `Box<FillRequest<T>>` through `RngMsg::Fill(_)`.
pub struct FillRequest<T: RngFloatSupported> {
    pub buf: GpuRef<T>,
    pub dist: Distribution<T>,
    pub reply: oneshot::Sender<Result<(), GpuError>>,
}

// --------------------------------------------------------------------
// RngDispatch impls — one per supported float dtype. The body is
// identical save for the cuRAND function table chosen via the
// `cudarc::curand::result::*Fill<T>` capability traits.
// --------------------------------------------------------------------

impl RngDispatch for FillRequest<f32> {
    fn fill(
        self: Box<Self>,
        gen: sys::curandGenerator_t,
        stream: &Arc<cudarc::driver::CudaStream>,
        completion: &Arc<dyn CompletionStrategy>,
    ) -> Result<(), GpuError> {
        fill_float::<f32>(*self, gen, stream, completion)
    }
}

impl RngDispatch for FillRequest<f64> {
    fn fill(
        self: Box<Self>,
        gen: sys::curandGenerator_t,
        stream: &Arc<cudarc::driver::CudaStream>,
        completion: &Arc<dyn CompletionStrategy>,
    ) -> Result<(), GpuError> {
        fill_float::<f64>(*self, gen, stream, completion)
    }
}

fn fill_float<T>(
    req: FillRequest<T>,
    gen: sys::curandGenerator_t,
    stream: &Arc<cudarc::driver::CudaStream>,
    completion: &Arc<dyn CompletionStrategy>,
) -> Result<(), GpuError>
where
    T: RngFloatSupported,
    sys::curandGenerator_t: UniformFill<T> + NormalFill<T> + LogNormalFill<T>,
    T::Scalar: Into<f64> + Copy,
    T: NormalParam<T::Scalar>,
{
    let FillRequest { buf, dist, reply } = req;

    match dist {
        Distribution::Uniform { lo, hi } => {
            // cuRAND uniform produces (0, 1]; for `lo != 0 || hi != 1`
            // callers need an affine transform. We honour the
            // request by filling (0, 1] and then warning on the
            // reply if the bounds aren't trivial — this preserves
            // back-compat with the F2 path while signposting the
            // missing affine kernel.
            enqueue_uniform::<T>(gen, stream, completion, buf, reply, lo, hi)
        }
        Distribution::Normal { mean, std } => {
            enqueue_normal::<T>(gen, stream, completion, buf, mean, std, reply)
        }
        Distribution::LogNormal { mean, std } => {
            enqueue_log_normal::<T>(gen, stream, completion, buf, mean, std, reply)
        }
        Distribution::Poisson { lambda } => {
            // Direct cuRAND host-API path is u32-only. Going to f32/f64
            // requires a host-side widen + copy, which we don't wire
            // until Phase 2.
            let _ = (gen, stream, completion, buf);
            let _ = lambda;
            let _ = reply.send(Err(GpuError::LibraryError {
                lib: LIB,
                msg:
                    "Poisson<T> not yet wired for floats (Phase 1: use FillRequest<u32> + Poisson)"
                        .into(),
            }));
            Ok(())
        }
        Distribution::Exponential { .. }
        | Distribution::Beta { .. }
        | Distribution::Cauchy { .. }
        | Distribution::Gamma { .. }
        | Distribution::Discrete { .. } => {
            let _ = (gen, stream, completion, buf);
            let _ = reply.send(Err(GpuError::LibraryError {
                lib: LIB,
                msg: "distribution not yet wired (Phase 1: needs custom kernel / NVRTC)".into(),
            }));
            Ok(())
        }
    }
}

/// Enqueue a uniform fill. cuRAND's host-API output is (0, 1]; a
/// non-default `(lo, hi)` is recorded in the error path until the
/// affine transform kernel lands.
fn enqueue_uniform<T>(
    gen: sys::curandGenerator_t,
    stream: &Arc<cudarc::driver::CudaStream>,
    completion: &Arc<dyn CompletionStrategy>,
    dst: GpuRef<T>,
    reply: oneshot::Sender<Result<(), GpuError>>,
    lo: T::Scalar,
    hi: T::Scalar,
) -> Result<(), GpuError>
where
    T: RngFloatSupported,
    T::Scalar: Into<f64> + Copy,
    sys::curandGenerator_t: UniformFill<T>,
{
    let lo_f: f64 = lo.into();
    let hi_f: f64 = hi.into();
    let trivial = lo_f == 0.0 && hi_f == 1.0;

    let dst_arc = match dst.access() {
        Ok(s) => s.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return Ok(());
        }
    };
    let mut owned = match Arc::try_unwrap(dst_arc) {
        Ok(s) => s,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "RNG dst has multiple live references".into(),
            )));
            return Ok(());
        }
    };
    if !trivial {
        let _ = reply.send(Err(GpuError::LibraryError {
            lib: LIB,
            msg: format!(
                "Uniform({lo_f},{hi_f}): non-(0,1] bounds need an affine transform kernel (Phase 1: not wired)"
            ),
        }));
        return Ok(());
    }

    dst.record_write(stream);
    envelope::run_kernel(LIB, stream, completion, (), reply, move || {
        // SAFETY: gen is bound to `stream`; `owned` is a CudaSlice on
        // the same context; `len` was checked above.
        let n = owned.len();
        let res = unsafe {
            let (ptr, _rec) = cudarc::driver::DevicePtrMut::device_ptr_mut(&mut owned, stream);
            UniformFill::fill(gen, ptr as *mut T, n)
        };
        res.map(|_| (owned,)).map_err(|e| GpuError::LibraryError {
            lib: LIB,
            msg: format!("fill_uniform: {e}"),
        })
    });
    Ok(())
}

fn enqueue_normal<T>(
    gen: sys::curandGenerator_t,
    stream: &Arc<cudarc::driver::CudaStream>,
    completion: &Arc<dyn CompletionStrategy>,
    dst: GpuRef<T>,
    mean: T::Scalar,
    std: T::Scalar,
    reply: oneshot::Sender<Result<(), GpuError>>,
) -> Result<(), GpuError>
where
    T: RngFloatSupported + NormalParam<T::Scalar>,
    sys::curandGenerator_t: NormalFill<T>,
{
    let dst_arc = match dst.access() {
        Ok(s) => s.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return Ok(());
        }
    };
    let mut owned = match Arc::try_unwrap(dst_arc) {
        Ok(s) => s,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "RNG dst has multiple live references".into(),
            )));
            return Ok(());
        }
    };
    let mean_t = T::from_scalar(mean);
    let std_t = T::from_scalar(std);
    dst.record_write(stream);
    envelope::run_kernel(LIB, stream, completion, (), reply, move || {
        let n = owned.len();
        let res = unsafe {
            let (ptr, _rec) = cudarc::driver::DevicePtrMut::device_ptr_mut(&mut owned, stream);
            NormalFill::fill(gen, ptr as *mut T, n, mean_t, std_t)
        };
        res.map(|_| (owned,)).map_err(|e| GpuError::LibraryError {
            lib: LIB,
            msg: format!("fill_normal: {e}"),
        })
    });
    Ok(())
}

fn enqueue_log_normal<T>(
    gen: sys::curandGenerator_t,
    stream: &Arc<cudarc::driver::CudaStream>,
    completion: &Arc<dyn CompletionStrategy>,
    dst: GpuRef<T>,
    mean: T::Scalar,
    std: T::Scalar,
    reply: oneshot::Sender<Result<(), GpuError>>,
) -> Result<(), GpuError>
where
    T: RngFloatSupported + NormalParam<T::Scalar>,
    sys::curandGenerator_t: LogNormalFill<T>,
{
    let dst_arc = match dst.access() {
        Ok(s) => s.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return Ok(());
        }
    };
    let mut owned = match Arc::try_unwrap(dst_arc) {
        Ok(s) => s,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "RNG dst has multiple live references".into(),
            )));
            return Ok(());
        }
    };
    let mean_t = T::from_scalar(mean);
    let std_t = T::from_scalar(std);
    dst.record_write(stream);
    envelope::run_kernel(LIB, stream, completion, (), reply, move || {
        let n = owned.len();
        let res = unsafe {
            let (ptr, _rec) = cudarc::driver::DevicePtrMut::device_ptr_mut(&mut owned, stream);
            LogNormalFill::fill(gen, ptr as *mut T, n, mean_t, std_t)
        };
        res.map(|_| (owned,)).map_err(|e| GpuError::LibraryError {
            lib: LIB,
            msg: format!("fill_log_normal: {e}"),
        })
    });
    Ok(())
}

/// Helper trait so the `enqueue_normal / log_normal` paths can convert
/// the parameter scalar (always `T::Scalar`) into the `T` value
/// `cudarc::curand::result::NormalFill::fill` actually accepts.
/// For both `f32` and `f64`, scalar == self, so this is identity.
pub trait NormalParam<S>: Sized {
    fn from_scalar(s: S) -> Self;
}
impl NormalParam<f32> for f32 {
    fn from_scalar(s: f32) -> Self {
        s
    }
}
impl NormalParam<f64> for f64 {
    fn from_scalar(s: f64) -> Self {
        s
    }
}

/// Direct u32 uniform fill — kept for the F2-era `FillUniformU32`
/// legacy variant. Fills with raw 32-bit bits via `curandGenerate`.
pub(crate) fn fill_uniform_u32(
    gen: sys::curandGenerator_t,
    stream: &Arc<cudarc::driver::CudaStream>,
    completion: &Arc<dyn CompletionStrategy>,
    dst: GpuRef<u32>,
    reply: oneshot::Sender<Result<(), GpuError>>,
) {
    let dst_arc = match dst.access() {
        Ok(s) => s.clone(),
        Err(e) => {
            let _ = reply.send(Err(e));
            return;
        }
    };
    let mut owned = match Arc::try_unwrap(dst_arc) {
        Ok(s) => s,
        Err(_) => {
            let _ = reply.send(Err(GpuError::Unrecoverable(
                "RNG dst has multiple live references".into(),
            )));
            return;
        }
    };
    dst.record_write(stream);
    envelope::run_kernel(LIB, stream, completion, (), reply, move || {
        let n = owned.len();
        let res = unsafe {
            let (ptr, _rec) = cudarc::driver::DevicePtrMut::device_ptr_mut(&mut owned, stream);
            UniformFill::fill(gen, ptr as *mut u32, n)
        };
        res.map(|_| (owned,)).map_err(|e| GpuError::LibraryError {
            lib: LIB,
            msg: format!("fill_uniform_u32: {e}"),
        })
    });
}

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

    /// Construct every `Distribution<T>` variant for both float dtypes
    /// to make sure each branch type-checks. No GPU traffic.
    #[test]
    fn distribution_round_trip_f32_f64() {
        // f32
        let _: Distribution<f32> = Distribution::Uniform { lo: 0.0, hi: 1.0 };
        let _: Distribution<f32> = Distribution::Normal {
            mean: 0.0,
            std: 1.0,
        };
        let _: Distribution<f32> = Distribution::LogNormal {
            mean: 0.0,
            std: 1.0,
        };
        let _: Distribution<f32> = Distribution::Poisson { lambda: 1.0 };
        let _: Distribution<f32> = Distribution::Exponential { lambda: 1.0 };
        let _: Distribution<f32> = Distribution::Beta {
            alpha: 1.0,
            beta: 1.0,
        };
        let _: Distribution<f32> = Distribution::Cauchy {
            loc: 0.0,
            scale: 1.0,
        };
        let _: Distribution<f32> = Distribution::Gamma {
            shape: 1.0,
            scale: 1.0,
        };
        // f64
        let _: Distribution<f64> = Distribution::Uniform { lo: 0.0, hi: 1.0 };
        let _: Distribution<f64> = Distribution::Normal {
            mean: 0.0,
            std: 1.0,
        };
        let _: Distribution<f64> = Distribution::LogNormal {
            mean: 0.0,
            std: 1.0,
        };
        let _: Distribution<f64> = Distribution::Poisson { lambda: 1.0 };
        let _: Distribution<f64> = Distribution::Exponential { lambda: 1.0 };
        let _: Distribution<f64> = Distribution::Beta {
            alpha: 1.0,
            beta: 1.0,
        };
        let _: Distribution<f64> = Distribution::Cauchy {
            loc: 0.0,
            scale: 1.0,
        };
        let _: Distribution<f64> = Distribution::Gamma {
            shape: 1.0,
            scale: 1.0,
        };
    }

    /// Ensure the legacy fill-uniform path is still in the public
    /// surface (exposed via `RngMsg::FillUniformF32`) — we just
    /// type-check the variant constructor here; the real fill is
    /// covered by the GPU e2e suite.
    #[test]
    #[allow(deprecated)]
    fn deprecated_fill_uniform_f32_still_works() {
        // Build a oneshot reply pair and drop it; the API surface is
        // the assertion under test.
        let (tx, _rx) = tokio::sync::oneshot::channel::<Result<(), GpuError>>();
        let _ = std::mem::ManuallyDrop::new(tx);
        // Compile-time check: variant exists with the F2 shape.
        fn _assert<
            F: FnOnce(GpuRef<f32>, oneshot::Sender<Result<(), GpuError>>) -> super::super::RngMsg,
        >(
            _f: F,
        ) {
        }
        _assert(|dst, reply| super::super::RngMsg::FillUniformF32 { dst, reply });
    }
}