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
//! `contiguize` plan — strided→contiguous materialization
//! (`torch.Tensor.contiguous()`). Phase 13.2.
//!
//! Closes the D2H→CPU contiguize→H2D fallback cliff in Fuel's CUDA
//! backend for non-contiguous CUDA inputs. The kernel is byte-level
//! dtype-agnostic: a single sizeof(T)-templated CUDA body covers every
//! byte-aligned dtype (f16, bf16, f32, f64, F32Strict, i32, i64, Bool,
//! S8, U8, Fp8E4M3, Fp8E5M2, Complex32, Complex64). A separate nibble
//! kernel handles S4 / U4 with a documented innermost-stride
//! constraint.
//!
//! Three host-side fast paths land in this trailblazer:
//!   1. Source already contiguous + zero offset → single
//!      `cudaMemcpyAsync(DeviceToDevice)`.
//!   2. Innermost stride == 1 → per-outer-coord contiguous-run kernel.
//!   3. Generic per-element kernel (one thread per output element).
//!
//! No backward — strides are metadata, autograd doesn't observe
//! contiguize.

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

use baracuda_cutlass::{Error, Result};
use baracuda_driver::Stream;
use baracuda_kernels_types::{
    ArchSku, BackendKind, ElementKind, KernelSku, MathPrecision, OpCategory, PlanPreference,
    PrecisionGuarantee, ShapeLayoutKind, TensorMut, TensorRef, Workspace,
};
use baracuda_types::DeviceRepr;

/// Descriptor for a Contiguize op.
///
/// `shape` is the logical output shape (equal to the source's logical
/// shape — Contiguize is a layout op, not a shape transform).
/// `source_strides` are the SIGNED element strides of the source view
/// (signed because Flip ops produce negatives and BroadcastTo produces
/// zeros). `source_offset` is the source-side base offset **in
/// elements**, not bytes.
#[derive(Copy, Clone, Debug)]
pub struct ContiguizeDescriptor<const N: usize> {
    /// Logical tensor shape (source and dest agree).
    pub shape: [i32; N],
    /// Source-view element strides. Signed so Flip-produced negative
    /// strides and BroadcastTo-produced zero strides are both legal.
    pub source_strides: [i64; N],
    /// Source-view base offset, **in elements** (not bytes). The
    /// kernel scales by `sizeof(T)` internally.
    pub source_offset: i64,
    /// Element type.
    pub element: ElementKind,
}

/// Args bundle for a Contiguize launch.
///
/// **Stride-field convention**: the kernel does NOT use `source.stride`
/// — it uses `ContiguizeDescriptor::source_strides`. The `TensorRef`'s
/// `stride` field is a load-bearing descriptor for OTHER ops; here it
/// MAY be the canonical contiguous stride of the underlying buffer or
/// the descriptor's strides. The kernel treats `source.data` as a flat
/// element buffer and indexes it via descriptor strides + element-
/// scaled offset.
///
/// `dest` MUST be a contiguous, zero-offset allocation. This is enforced
/// at `can_implement` time (`dest.is_contiguous()` + no offset is
/// implicit because dest is a raw [`TensorMut`] over a fresh buffer).
pub struct ContiguizeArgs<'a, T: DeviceRepr + Copy + 'static, const N: usize> {
    /// Source: raw byte buffer. The kernel reads
    /// `source.data[source_offset + Σ i_k * source_strides[k]]` for
    /// each output coord `(i_0, …, i_{N-1})`.
    pub source: TensorRef<'a, T, N>,
    /// Destination: contiguous, zero-offset, caller-allocated.
    pub dest: TensorMut<'a, T, N>,
}

/// `contiguize` plan.
///
/// `dest = source.contiguous()` — materializes a row-major contiguous
/// tensor from an arbitrary strided source view. Single fast-path
/// dispatch (already-contig / inner-stride-1 / generic) baked into the
/// CUDA launcher.
///
/// **When to use**: a downstream kernel requires contiguous input but
/// the upstream tensor has a non-canonical layout (transpose, slice,
/// flip, broadcast). Today Fuel's CUDA backend D2H→CPU-contiguize→H2D
/// in this case; this plan keeps the work device-resident.
///
/// **Dtypes**: every byte-aligned dtype baracuda's element bank
/// exposes — `{f16, bf16, f32, f64, F32Strict, i32, i64, Bool, S8, U8,
/// Fp8E4M3, Fp8E5M2, Complex32, Complex64}` — plus nibble-packed
/// `{S4, U4}` with an innermost-stride constraint. `Bin` (1-bit) is
/// **out of scope** for Phase 13.2.
///
/// **Nibble constraint (S4 / U4)**: the source's innermost stride MUST
/// be one of `{1, -1, 2}` — anything else breaks nibble alignment and
/// returns [`Error::Unsupported`] at run time. The dest is always
/// nibble-aligned by construction (zero offset + canonical strides).
///
/// **Shape limits**: rank in `[1, 8]`; `shape[d]` non-negative; strides
/// may be any signed i64 (including 0 and negative).
///
/// **Workspace**: none.
///
/// **Precision guarantee**: deterministic, bit-stable, bit-exact —
/// pure copy, no arithmetic.
pub struct ContiguizePlan<T: DeviceRepr + Copy + 'static, const N: usize> {
    desc: ContiguizeDescriptor<N>,
    sku: KernelSku,
    _marker: PhantomData<T>,
}

impl<T: DeviceRepr + Copy + 'static, const N: usize> ContiguizePlan<T, N> {
    /// Pick a kernel for `desc`.
    ///
    /// Unlike most `*Plan` types in this crate, the type parameter `T`
    /// is bounded by [`baracuda_types::DeviceRepr`] only (no `Element` /
    /// `IntElement` family) so the byte-level-agnostic kernel can serve
    /// `T = S4 / U4 / S8 / U8 / Fp8E4M3 / Fp8E5M2` alongside the
    /// classic `Element`s (`f16` / `bf16` / `f32` / `f64` / `i32` /
    /// `i64` / `Bool` / `Complex32` / `Complex64` / `F32Strict`).
    /// `desc.element` is the source of truth for dispatch.
    pub fn select(
        _stream: &Stream,
        desc: &ContiguizeDescriptor<N>,
        _pref: PlanPreference,
    ) -> Result<Self> {
        // Defensive: caller must size-match T to desc.element. We can't
        // statically verify this without a `KIND` const on every
        // `DeviceRepr` type, so we check that `sizeof::<T>` matches the
        // expected byte width of `desc.element`.
        if !type_size_matches_kind::<T>(desc.element) {
            return Err(Error::Unsupported(
                "baracuda-kernels::ContiguizePlan: sizeof::<T> doesn't match \
                 descriptor element kind (T must be the Rust type that backs \
                 desc.element — see ContiguizeDescriptor docs)",
            ));
        }
        for &d in desc.shape.iter() {
            if d < 0 {
                return Err(Error::InvalidProblem(
                    "baracuda-kernels::ContiguizePlan: shape dims must be non-negative",
                ));
            }
        }
        if N > 8 {
            return Err(Error::Unsupported(
                "baracuda-kernels::ContiguizePlan: tensor rank > 8 not supported",
            ));
        }
        // Coverage matrix — every byte-aligned baracuda dtype plus
        // nibble-packed S4 / U4. Reject Bin (1-bit) — out of scope.
        let supported = matches!(
            desc.element,
            ElementKind::F16
                | ElementKind::Bf16
                | ElementKind::F32
                | ElementKind::F32Strict
                | ElementKind::F64
                | ElementKind::I32
                | ElementKind::I64
                | ElementKind::Bool
                | ElementKind::S8
                | ElementKind::U8
                | ElementKind::Fp8E4M3
                | ElementKind::Fp8E5M2
                | ElementKind::Complex32
                | ElementKind::Complex64
                | ElementKind::S4
                | ElementKind::U4
        );
        if !supported {
            return Err(Error::Unsupported(
                "baracuda-kernels::ContiguizePlan: dtype not in coverage \
                 (Bin is out of scope; everything else byte-aligned or nibble-packed)",
            ));
        }
        // S4 / U4 innermost-stride alignment check — duplicated on the
        // device side defensively, but we surface it earlier here so
        // mis-configured plans fail at select() rather than run().
        if matches!(desc.element, ElementKind::S4 | ElementKind::U4) && N >= 1 {
            let inner = desc.source_strides[N - 1];
            if !(inner == 1 || inner == -1 || inner == 2) {
                return Err(Error::Unsupported(
                    "baracuda-kernels::ContiguizePlan: S4 / U4 source's innermost \
                     stride must be one of {1, -1, 2} for nibble alignment",
                ));
            }
        }
        let precision_guarantee = PrecisionGuarantee {
            math_precision: MathPrecision::F32,
            accumulator: ElementKind::F32,
            // Pure copy — no arithmetic, hence trivially bit-stable.
            bit_stable_on_same_hardware: true,
            deterministic: true,
        };
        let sku = KernelSku {
            category: OpCategory::ShapeLayout,
            op: ShapeLayoutKind::Contiguize as u16,
            element: desc.element,
            aux_element: None,
            layout: None,
            epilogue: None,
            arch: ArchSku::Sm80,
            backend: BackendKind::Bespoke,
            precision_guarantee,
        };
        Ok(Self {
            desc: *desc,
            sku,
            _marker: PhantomData,
        })
    }

    /// Validate args.
    pub fn can_implement(&self, args: &ContiguizeArgs<'_, T, N>) -> Result<()> {
        if args.source.shape != self.desc.shape {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::ContiguizePlan: source shape mismatch with descriptor",
            ));
        }
        if args.dest.shape != self.desc.shape {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::ContiguizePlan: dest shape mismatch with descriptor",
            ));
        }
        if !args.dest.is_contiguous() {
            return Err(Error::InvalidProblem(
                "baracuda-kernels::ContiguizePlan: dest must be canonical contiguous \
                 (the whole point of this op is to MATERIALIZE a contiguous view)",
            ));
        }
        let numel = args.dest.numel();
        let dest_len = args.dest.data.len() as i64;
        // Nibble-packed dtypes (S4/U4) store two logical elements per
        // byte/storage-slot, so the storage requirement is
        // ceil(numel/2), not numel.
        let needed_storage = match self.desc.element {
            ElementKind::S4 | ElementKind::U4 => (numel + 1) / 2,
            _ => numel,
        };
        if dest_len < needed_storage {
            return Err(Error::BufferTooSmall {
                needed: needed_storage as usize,
                got: dest_len as usize,
            });
        }
        // Source buffer length sanity: numel must fit, but we don't
        // tightly bound because the source view may legitimately
        // index well past `source.data.len()` once `source_offset`
        // and signed strides are applied (the buffer behind a strided
        // view is opaque to baracuda-kernels). The caller owns this
        // contract.
        Ok(())
    }

    /// Workspace size in bytes. Always `0`.
    #[inline]
    pub fn workspace_size(&self) -> usize {
        0
    }
    /// Identity of the kernel this plan picked.
    #[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: ContiguizeArgs<'_, T, N>,
    ) -> Result<()> {
        self.can_implement(&args)?;
        let numel = args.dest.numel();
        if numel == 0 {
            return Ok(());
        }
        let source_ptr = args.source.data.as_raw().0 as *const c_void;
        let dest_ptr = args.dest.data.as_raw().0 as *mut c_void;
        let stream_ptr = stream.as_raw() as *mut c_void;
        let shape = self.desc.shape;
        let source_strides = self.desc.source_strides;
        let source_offset = self.desc.source_offset;
        let rank = N as i32;

        // Byte-width dispatch. Each ElementKind picks a single FFI
        // symbol — the kernel body is dtype-agnostic at that point
        // (raw byte memcpy of `ElemBytes`).
        let status = match self.desc.element {
            // 1-byte payloads.
            ElementKind::Bool
            | ElementKind::S8
            | ElementKind::U8
            | ElementKind::Fp8E4M3
            | ElementKind::Fp8E5M2 => unsafe {
                baracuda_kernels_sys::baracuda_kernels_contiguize_b1_run(
                    dest_ptr,
                    source_ptr,
                    shape.as_ptr(),
                    source_strides.as_ptr(),
                    source_offset,
                    rank,
                    stream_ptr,
                )
            },
            // 2-byte payloads.
            ElementKind::F16 | ElementKind::Bf16 => unsafe {
                baracuda_kernels_sys::baracuda_kernels_contiguize_b2_run(
                    dest_ptr,
                    source_ptr,
                    shape.as_ptr(),
                    source_strides.as_ptr(),
                    source_offset,
                    rank,
                    stream_ptr,
                )
            },
            // 4-byte payloads.
            ElementKind::F32 | ElementKind::F32Strict | ElementKind::I32 => unsafe {
                baracuda_kernels_sys::baracuda_kernels_contiguize_b4_run(
                    dest_ptr,
                    source_ptr,
                    shape.as_ptr(),
                    source_strides.as_ptr(),
                    source_offset,
                    rank,
                    stream_ptr,
                )
            },
            // 8-byte payloads — f64 (8 B), i64 (8 B), Complex32 (4+4 B).
            ElementKind::F64 | ElementKind::I64 | ElementKind::Complex32 => unsafe {
                baracuda_kernels_sys::baracuda_kernels_contiguize_b8_run(
                    dest_ptr,
                    source_ptr,
                    shape.as_ptr(),
                    source_strides.as_ptr(),
                    source_offset,
                    rank,
                    stream_ptr,
                )
            },
            // 16-byte payloads — Complex64 (8+8 B).
            ElementKind::Complex64 => unsafe {
                baracuda_kernels_sys::baracuda_kernels_contiguize_b16_run(
                    dest_ptr,
                    source_ptr,
                    shape.as_ptr(),
                    source_strides.as_ptr(),
                    source_offset,
                    rank,
                    stream_ptr,
                )
            },
            // Nibble-packed (S4 / U4) — single symbol with documented
            // innermost-stride constraint enforced device-side.
            ElementKind::S4 | ElementKind::U4 => unsafe {
                baracuda_kernels_sys::baracuda_kernels_contiguize_nibble_run(
                    dest_ptr,
                    source_ptr,
                    shape.as_ptr(),
                    source_strides.as_ptr(),
                    source_offset,
                    rank,
                    stream_ptr,
                )
            },
            _ => {
                return Err(Error::Unsupported(
                    "baracuda-kernels::ContiguizePlan::run: dtype not in coverage \
                     (Bin / unknown — should have been rejected at select())",
                ));
            }
        };
        map_status(status)
    }
}

/// Defensive size-vs-kind check used by `select()`. Returns `true` iff
/// `core::mem::size_of::<T>()` matches the on-device byte width
/// expected for `kind`. The plan's CUDA dispatch picks the byte-width
/// FFI symbol from `desc.element`, so a mismatch between `T` and
/// `desc.element` would silently scribble memory; we reject at select
/// time instead.
fn type_size_matches_kind<T>(kind: ElementKind) -> bool {
    let want = match kind {
        ElementKind::Bool
        | ElementKind::S8
        | ElementKind::U8
        | ElementKind::Fp8E4M3
        | ElementKind::Fp8E5M2
        | ElementKind::S4
        | ElementKind::U4 => 1,
        ElementKind::F16 | ElementKind::Bf16 => 2,
        ElementKind::F32 | ElementKind::F32Strict | ElementKind::I32 => 4,
        ElementKind::F64 | ElementKind::I64 | ElementKind::Complex32 => 8,
        ElementKind::Complex64 => 16,
        ElementKind::Bin => return false, // out of scope
    };
    core::mem::size_of::<T>() == want
}

fn map_status(code: i32) -> Result<()> {
    match code {
        0 => Ok(()),
        1 => Err(Error::MisalignedOperand),
        2 => Err(Error::InvalidProblem(
            "baracuda-kernels-sys reported invalid problem",
        )),
        3 => Err(Error::Unsupported(
            "baracuda-kernels-sys reported unsupported configuration \
             (likely S4 / U4 source innermost stride not in {1, -1, 2})",
        )),
        4 => Err(Error::WorkspaceTooSmall { needed: 0, got: 0 }),
        n => Err(Error::CutlassInternal(n)),
    }
}