baracuda-kernels 0.0.1-alpha.68

Unified ML op facade for the baracuda CUDA ecosystem. Exposes every primitive an ML framework would expect (union of PyTorch torch.* + nn.functional and JAX lax.* / numpy ops) through a single Plan-based Rust surface, internally dispatching to baracuda-cutlass, the baracuda-* NVIDIA-library wrappers, or bespoke baracuda-kernels-sys kernels.
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! CrossEntropyLoss plan — `y = NLLLoss(LogSoftmax(input), target)`.
//!
//! Fused into a single per-row two-pass kernel for numerical stability
//! (max subtraction, sum-of-exp, then `-log_softmax[target]`).
//!
//! Two target formats are supported via [`CrossEntropyTargetKind`]:
//! - [`CrossEntropyTargetKind::ClassIndex`] — target is `i64[N]` class
//!   indices. The original fused FW: `y[n] = -log_softmax(input)[n, t[n]]`.
//! - [`CrossEntropyTargetKind::SoftProb`] — target is `T[N, C]` soft
//!   probability tensor (same dtype as input). Used for label smoothing
//!   / distillation. Formula: `y[n] = -Σ_c target[n,c] · log_softmax(input)[n,c]`.
//!
//! Today wired: `CrossEntropy × {f32, f16, bf16, f64}` × `{None, Mean, Sum}`
//! × `{ClassIndex, SoftProb}`.

use core::ffi::c_void;
use core::marker::PhantomData;

use baracuda_cutlass::{Error, Result};
use baracuda_driver::Stream;
use baracuda_kernels_types::{
    ArchSku, BackendKind, CrossEntropyTargetKind, Element, ElementKind, KernelSku, LossKind,
    LossReduction, MathPrecision, OpCategory, PlanPreference, PrecisionGuarantee, TensorMut,
    TensorRef, Workspace,
};

use super::common::{check_supported_dtype, map_status, unpack_workspace};

/// Descriptor for a CrossEntropyLoss op.
#[derive(Copy, Clone, Debug)]
pub struct CrossEntropyLossDescriptor {
    /// Number of rows / batch size.
    pub n_rows: i32,
    /// Number of classes.
    pub class_extent: i32,
    /// Reduction mode.
    pub reduction: LossReduction,
    /// Target tensor format.
    pub target_kind: CrossEntropyTargetKind,
    /// Element type of input.
    pub element: ElementKind,
}

/// Args bundle for a CrossEntropyLoss FW launch.
///
/// Exactly one of `target` (class-index) or `soft_target` (soft probability)
/// must be `Some` to match the descriptor's `target_kind`.
pub struct CrossEntropyLossArgs<'a, T: Element> {
    /// Input logits `[n_rows, class_extent]`.
    pub input: TensorRef<'a, T, 2>,
    /// Target class indices `[n_rows]` — populated when `target_kind ==
    /// ClassIndex`.
    pub target: Option<TensorRef<'a, i64, 1>>,
    /// Soft-target probability `[n_rows, class_extent]` (dtype `T`) —
    /// populated when `target_kind == SoftProb`.
    pub soft_target: Option<TensorRef<'a, T, 2>>,
    /// Output: `[n_rows]` for None, scalar `[1]` for Mean / Sum.
    pub out: TensorMut<'a, T, 1>,
}

/// CrossEntropyLoss forward plan.
pub struct CrossEntropyLossPlan<T: Element> {
    desc: CrossEntropyLossDescriptor,
    sku: KernelSku,
    _marker: PhantomData<T>,
}

impl<T: Element> CrossEntropyLossPlan<T> {
    /// Pick a kernel.
    pub fn select(
        _stream: &Stream,
        desc: &CrossEntropyLossDescriptor,
        _pref: PlanPreference,
    ) -> Result<Self> {
        if desc.element != T::KIND {
            return Err(Error::Unsupported(
                "baracuda-kernels::CrossEntropyLossPlan: descriptor element != T",
            ));
        }
        check_supported_dtype::<T>()?;
        if desc.n_rows < 0 || desc.class_extent < 0 {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::CrossEntropyLossPlan: n_rows / class_extent must be \
                 non-negative",
            ));
        }
        if desc.class_extent < 1 {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::CrossEntropyLossPlan: class_extent must be ≥ 1",
            ));
        }
        let precision_guarantee = PrecisionGuarantee {
            math_precision: MathPrecision::F32,
            accumulator: if T::KIND == ElementKind::F64 {
                ElementKind::F64
            } else {
                ElementKind::F32
            },
            bit_stable_on_same_hardware: true,
            deterministic: true,
        };
        let sku = KernelSku {
            category: OpCategory::Loss,
            op: LossKind::CrossEntropy as u16,
            element: T::KIND,
            aux_element: None,
            layout: None,
            epilogue: None,
            arch: ArchSku::Sm80,
            backend: BackendKind::Bespoke,
            precision_guarantee,
        };
        Ok(Self {
            desc: *desc,
            sku,
            _marker: PhantomData,
        })
    }

    /// Workspace size — `n_rows · sizeof(T)` for Mean/Sum; 0 for None.
    #[inline]
    pub fn workspace_size(&self) -> usize {
        match self.desc.reduction {
            LossReduction::None => 0,
            LossReduction::Mean | LossReduction::Sum => {
                (self.desc.n_rows as usize).saturating_mul(core::mem::size_of::<T>())
            }
        }
    }
    /// Kernel SKU identity.
    #[inline]
    pub fn sku(&self) -> KernelSku {
        self.sku
    }
    /// Numerical guarantees.
    #[inline]
    pub fn precision_guarantee(&self) -> PrecisionGuarantee {
        self.sku.precision_guarantee
    }

    /// Launch.
    pub fn run(
        &self,
        stream: &Stream,
        workspace: Workspace<'_>,
        args: CrossEntropyLossArgs<'_, T>,
    ) -> Result<()> {
        let n_rows = self.desc.n_rows as i64;
        let class_extent = self.desc.class_extent;
        if n_rows == 0 {
            return Ok(());
        }
        if args.input.shape != [self.desc.n_rows, class_extent] {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::CrossEntropyLossPlan: input shape must be [n_rows, \
                 class_extent]",
            ));
        }
        let row_stride_input: i64 = args.input.stride[0];
        let (ws_ptr, ws_bytes) = unpack_workspace(workspace, self.workspace_size())?;
        let stream_ptr = stream.as_raw() as *mut c_void;
        let input_ptr = args.input.data.as_raw().0 as *const c_void;
        let out_ptr = args.out.data.as_raw().0 as *mut c_void;
        let mode = self.desc.reduction as i32;

        let status = match self.desc.target_kind {
            CrossEntropyTargetKind::ClassIndex => {
                let target = args.target.ok_or(Error::InvalidProblem(
                    "baracuda-kernels::CrossEntropyLossPlan: target_kind=ClassIndex requires \
                     `target` arg",
                ))?;
                if target.shape != [self.desc.n_rows] {
                    return Err(Error::InvalidProblem(
                        "baracuda-kernels::CrossEntropyLossPlan: target shape must be [n_rows]",
                    ));
                }
                let target_ptr = target.data.as_raw().0 as *const c_void;
                match T::KIND {
                    ElementKind::F32 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_f32_run(
                            n_rows, class_extent, row_stride_input, mode, input_ptr, target_ptr,
                            out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    ElementKind::F16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_f16_run(
                            n_rows, class_extent, row_stride_input, mode, input_ptr, target_ptr,
                            out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    ElementKind::Bf16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_bf16_run(
                            n_rows, class_extent, row_stride_input, mode, input_ptr, target_ptr,
                            out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    ElementKind::F64 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_f64_run(
                            n_rows, class_extent, row_stride_input, mode, input_ptr, target_ptr,
                            out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    _ => {
                        return Err(Error::Unsupported(
                            "baracuda-kernels::CrossEntropyLossPlan::run unwired dtype",
                        ));
                    }
                }
            }
            CrossEntropyTargetKind::SoftProb => {
                let soft = args.soft_target.ok_or(Error::InvalidProblem(
                    "baracuda-kernels::CrossEntropyLossPlan: target_kind=SoftProb requires \
                     `soft_target` arg",
                ))?;
                if soft.shape != [self.desc.n_rows, class_extent] {
                    return Err(Error::InvalidProblem(
                        "baracuda-kernels::CrossEntropyLossPlan: soft_target shape must be \
                         [n_rows, class_extent]",
                    ));
                }
                let row_stride_target: i64 = soft.stride[0];
                let target_ptr = soft.data.as_raw().0 as *const c_void;
                match T::KIND {
                    ElementKind::F32 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_f32_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            input_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    ElementKind::F16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_f16_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            input_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    ElementKind::Bf16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_bf16_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            input_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    ElementKind::F64 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_f64_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            input_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
                        )
                    },
                    _ => {
                        return Err(Error::Unsupported(
                            "baracuda-kernels::CrossEntropyLossPlan::run unwired dtype",
                        ));
                    }
                }
            }
        };
        map_status(status)
    }
}

// =============================================================================
// BACKWARD
// =============================================================================

/// Descriptor for a CrossEntropyLoss backward op.
#[derive(Copy, Clone, Debug)]
pub struct CrossEntropyLossBackwardDescriptor {
    /// Number of rows / batch size.
    pub n_rows: i32,
    /// Number of classes.
    pub class_extent: i32,
    /// Reduction mode used in the forward.
    pub reduction: LossReduction,
    /// Target tensor format (must match forward).
    pub target_kind: CrossEntropyTargetKind,
    /// Element type of input.
    pub element: ElementKind,
}

/// Args bundle for a CrossEntropyLoss BW launch.
pub struct CrossEntropyLossBackwardArgs<'a, T: Element> {
    /// Input logits (saved from FW).
    pub input: TensorRef<'a, T, 2>,
    /// Target class indices `[n_rows]` — populated when `target_kind ==
    /// ClassIndex`.
    pub target: Option<TensorRef<'a, i64, 1>>,
    /// Soft-target probability `[n_rows, class_extent]` — populated when
    /// `target_kind == SoftProb`.
    pub soft_target: Option<TensorRef<'a, T, 2>>,
    /// Upstream gradient: `[n_rows]` for None, scalar `[1]` for Mean / Sum.
    pub dy: TensorRef<'a, T, 1>,
    /// Gradient w.r.t. input `[n_rows, class_extent]`.
    pub dinput: TensorMut<'a, T, 2>,
}

/// CrossEntropyLoss backward plan.
pub struct CrossEntropyLossBackwardPlan<T: Element> {
    desc: CrossEntropyLossBackwardDescriptor,
    sku: KernelSku,
    _marker: PhantomData<T>,
}

impl<T: Element> CrossEntropyLossBackwardPlan<T> {
    /// Pick a kernel.
    pub fn select(
        _stream: &Stream,
        desc: &CrossEntropyLossBackwardDescriptor,
        _pref: PlanPreference,
    ) -> Result<Self> {
        if desc.element != T::KIND {
            return Err(Error::Unsupported(
                "baracuda-kernels::CrossEntropyLossBackwardPlan: descriptor element != T",
            ));
        }
        check_supported_dtype::<T>()?;
        if desc.n_rows < 0 || desc.class_extent < 1 {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::CrossEntropyLossBackwardPlan: invalid shape parameters",
            ));
        }
        let precision_guarantee = PrecisionGuarantee {
            math_precision: MathPrecision::F32,
            accumulator: if T::KIND == ElementKind::F64 {
                ElementKind::F64
            } else {
                ElementKind::F32
            },
            bit_stable_on_same_hardware: true,
            deterministic: true,
        };
        let sku = KernelSku {
            category: OpCategory::Loss,
            op: LossKind::CrossEntropy as u16,
            element: T::KIND,
            aux_element: None,
            layout: None,
            epilogue: None,
            arch: ArchSku::Sm80,
            backend: BackendKind::Bespoke,
            precision_guarantee,
        };
        Ok(Self {
            desc: *desc,
            sku,
            _marker: PhantomData,
        })
    }
    /// Workspace size in bytes.
    #[inline]
    pub fn workspace_size(&self) -> usize {
        0
    }
    /// Kernel SKU identity.
    #[inline]
    pub fn sku(&self) -> KernelSku {
        self.sku
    }
    /// Numerical guarantees.
    #[inline]
    pub fn precision_guarantee(&self) -> PrecisionGuarantee {
        self.sku.precision_guarantee
    }

    /// Launch.
    pub fn run(
        &self,
        stream: &Stream,
        _workspace: Workspace<'_>,
        args: CrossEntropyLossBackwardArgs<'_, T>,
    ) -> Result<()> {
        let n_rows = self.desc.n_rows as i64;
        let class_extent = self.desc.class_extent;
        if n_rows == 0 {
            return Ok(());
        }
        if args.input.shape != [self.desc.n_rows, class_extent] {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::CrossEntropyLossBackwardPlan: input shape must be \
                 [n_rows, class_extent]",
            ));
        }
        if args.dinput.shape != [self.desc.n_rows, class_extent] {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::CrossEntropyLossBackwardPlan: dinput shape must be \
                 [n_rows, class_extent]",
            ));
        }
        let row_stride_input: i64 = args.input.stride[0];
        let mode = self.desc.reduction as i32;
        let inv_n_or_one: f32 = match self.desc.reduction {
            LossReduction::None => 0.0,
            LossReduction::Mean => 1.0 / (n_rows as f32),
            LossReduction::Sum => 1.0,
        };
        let stream_ptr = stream.as_raw() as *mut c_void;
        let input_ptr = args.input.data.as_raw().0 as *const c_void;
        let dy_ptr = args.dy.data.as_raw().0 as *const c_void;
        let dinput_ptr = args.dinput.data.as_raw().0 as *mut c_void;

        let status = match self.desc.target_kind {
            CrossEntropyTargetKind::ClassIndex => {
                let target = args.target.ok_or(Error::InvalidProblem(
                    "baracuda-kernels::CrossEntropyLossBackwardPlan: target_kind=ClassIndex \
                     requires `target` arg",
                ))?;
                if target.shape != [self.desc.n_rows] {
                    return Err(Error::InvalidProblem(
                        "baracuda-kernels::CrossEntropyLossBackwardPlan: target shape must be \
                         [n_rows]",
                    ));
                }
                let target_ptr = target.data.as_raw().0 as *const c_void;
                match T::KIND {
                    ElementKind::F32 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_backward_f32_run(
                            n_rows, class_extent, row_stride_input, mode, inv_n_or_one, input_ptr,
                            target_ptr, dy_ptr, dinput_ptr, core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    ElementKind::F16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_backward_f16_run(
                            n_rows, class_extent, row_stride_input, mode, inv_n_or_one, input_ptr,
                            target_ptr, dy_ptr, dinput_ptr, core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    ElementKind::Bf16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_backward_bf16_run(
                            n_rows, class_extent, row_stride_input, mode, inv_n_or_one, input_ptr,
                            target_ptr, dy_ptr, dinput_ptr, core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    ElementKind::F64 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_backward_f64_run(
                            n_rows, class_extent, row_stride_input, mode, inv_n_or_one, input_ptr,
                            target_ptr, dy_ptr, dinput_ptr, core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    _ => {
                        return Err(Error::Unsupported(
                            "baracuda-kernels::CrossEntropyLossBackwardPlan::run unwired dtype",
                        ));
                    }
                }
            }
            CrossEntropyTargetKind::SoftProb => {
                let soft = args.soft_target.ok_or(Error::InvalidProblem(
                    "baracuda-kernels::CrossEntropyLossBackwardPlan: target_kind=SoftProb \
                     requires `soft_target` arg",
                ))?;
                if soft.shape != [self.desc.n_rows, class_extent] {
                    return Err(Error::InvalidProblem(
                        "baracuda-kernels::CrossEntropyLossBackwardPlan: soft_target shape \
                         must be [n_rows, class_extent]",
                    ));
                }
                let row_stride_target: i64 = soft.stride[0];
                let target_ptr = soft.data.as_raw().0 as *const c_void;
                match T::KIND {
                    ElementKind::F32 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_backward_f32_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            inv_n_or_one, input_ptr, target_ptr, dy_ptr, dinput_ptr,
                            core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    ElementKind::F16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_backward_f16_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            inv_n_or_one, input_ptr, target_ptr, dy_ptr, dinput_ptr,
                            core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    ElementKind::Bf16 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_backward_bf16_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            inv_n_or_one, input_ptr, target_ptr, dy_ptr, dinput_ptr,
                            core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    ElementKind::F64 => unsafe {
                        baracuda_kernels_sys::baracuda_kernels_loss_cross_entropy_soft_backward_f64_run(
                            n_rows, class_extent, row_stride_input, row_stride_target, mode,
                            inv_n_or_one, input_ptr, target_ptr, dy_ptr, dinput_ptr,
                            core::ptr::null_mut(), 0, stream_ptr,
                        )
                    },
                    _ => {
                        return Err(Error::Unsupported(
                            "baracuda-kernels::CrossEntropyLossBackwardPlan::run unwired dtype",
                        ));
                    }
                }
            }
        };
        map_status(status)
    }
}