oxicuda-dnn 0.1.8

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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Winograd backward data gradient (dgrad).
//!
//! Computes the gradient of the loss with respect to the input tensor
//! using the Winograd transform for 3x3 filters. This is the "transpose"
//! of the forward Winograd convolution.
//!
//! # Algorithm
//!
//! Three-stage process:
//! 1. **Grad output transform**: `d = B^T * grad_output_tile * B`
//!    (spatial -> Winograd domain)
//! 2. **Batched GEMM**: `m[xi] = filter_transposed[xi] * d[xi]`
//!    (multiply with transposed filter in Winograd domain)
//! 3. **Grad input transform**: `grad_input_tile = A^T * m * A`
//!    (Winograd domain -> spatial)
//!
//! # Relationship to forward pass
//!
//! The dgrad uses the same B^T/A^T transformation matrices as the forward
//! pass. The key difference is that the GEMM uses the transposed filter
//! weights and the roles of input/output are swapped.

use std::sync::Arc;

use oxicuda_blas::GpuFloat;
use oxicuda_driver::Module;
use oxicuda_launch::{Kernel, LaunchParams, grid_size_for};
use oxicuda_ptx::arch::SmVersion;
use oxicuda_ptx::builder::KernelBuilder;
use oxicuda_ptx::ir::PtxType;

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

use super::super::descriptor::ConvProblem;
use crate::conv::fprop::winograd::WinogradTileSize;

// ---------------------------------------------------------------------------
// WinogradDgrad
// ---------------------------------------------------------------------------

/// Winograd backward data gradient engine for 3x3 filters.
///
/// Generates three GPU kernels:
/// 1. Grad output transform (spatial -> Winograd domain)
/// 2. Batched GEMM with transposed filter (per transform element)
/// 3. Grad input transform (Winograd domain -> spatial)
pub struct WinogradDgrad {
    problem: ConvProblem,
    tile_size: WinogradTileSize,
    sm_version: SmVersion,
}

impl WinogradDgrad {
    /// Creates a new Winograd dgrad engine.
    ///
    /// Auto-selects the tile size based on spatial dimensions.
    ///
    /// # Errors
    ///
    /// Returns [`DnnError::InvalidArgument`] if the filter is not 3x3, or
    /// if stride/dilation is not 1 (Winograd dgrad requires unit stride
    /// and unit dilation).
    pub fn new(problem: ConvProblem, sm_version: SmVersion) -> DnnResult<Self> {
        Self::validate_problem(&problem)?;
        let out_h = problem.output_h()?;
        let out_w = problem.output_w()?;
        let tile_size = WinogradTileSize::auto_select(out_h, out_w);

        Ok(Self {
            problem,
            tile_size,
            sm_version,
        })
    }

    /// Creates with a specific tile size.
    ///
    /// # Errors
    ///
    /// Same as [`new`](Self::new).
    pub fn with_tile_size(
        problem: ConvProblem,
        tile_size: WinogradTileSize,
        sm_version: SmVersion,
    ) -> DnnResult<Self> {
        Self::validate_problem(&problem)?;
        Ok(Self {
            problem,
            tile_size,
            sm_version,
        })
    }

    /// Validates that the problem is suitable for Winograd dgrad.
    fn validate_problem(problem: &ConvProblem) -> DnnResult<()> {
        let r = problem.filter_dims.first().copied().unwrap_or(0);
        let s = problem.filter_dims.get(1).copied().unwrap_or(0);
        if r != 3 || s != 3 {
            return Err(DnnError::InvalidArgument(format!(
                "Winograd dgrad requires 3x3 filter, got {r}x{s}"
            )));
        }
        // Winograd dgrad requires unit stride for correct tiling
        let stride_h = problem.stride.first().copied().unwrap_or(1);
        let stride_w = problem.stride.get(1).copied().unwrap_or(1);
        if stride_h != 1 || stride_w != 1 {
            return Err(DnnError::InvalidArgument(format!(
                "Winograd dgrad requires unit stride, got {stride_h}x{stride_w}"
            )));
        }
        // Winograd dgrad requires unit dilation
        let dil_h = problem.dilation.first().copied().unwrap_or(1);
        let dil_w = problem.dilation.get(1).copied().unwrap_or(1);
        if dil_h != 1 || dil_w != 1 {
            return Err(DnnError::InvalidArgument(format!(
                "Winograd dgrad requires unit dilation, got {dil_h}x{dil_w}"
            )));
        }
        Ok(())
    }

    /// Returns the selected tile size.
    #[must_use]
    pub fn tile_size(&self) -> WinogradTileSize {
        self.tile_size
    }

    /// Computes workspace size in bytes for the Winograd dgrad buffers.
    ///
    /// Workspace holds three buffers:
    /// - Transformed grad output: `alpha^2 * K * tiles * batch`
    /// - Transformed filter:      `alpha^2 * C * K`
    /// - Transformed grad input:  `alpha^2 * C * tiles * batch`
    ///
    /// where `alpha` is the transform tile size and `tiles` is the number
    /// of spatial tiles covering the input.
    pub fn workspace_bytes(&self) -> DnnResult<usize> {
        let in_h = self.problem.in_dims.first().copied().unwrap_or(0);
        let in_w = self.problem.in_dims.get(1).copied().unwrap_or(0);
        let ot = self.tile_size.output_tile();
        let alpha2 = self.tile_size.transform_elements() as u64;

        // For dgrad, we tile the input spatial dimensions
        let tiles_h = in_h.div_ceil(ot);
        let tiles_w = in_w.div_ceil(ot);
        let num_tiles = tiles_h as u64 * tiles_w as u64 * self.problem.batch as u64;

        let c = self.problem.in_channels as u64;
        let k = self.problem.out_channels as u64;
        let elem_size = self.problem.input_type.size_bytes() as u64;

        let grad_output_buf = alpha2 * k * num_tiles * elem_size;
        let filter_buf = alpha2 * c * k * elem_size;
        let grad_input_buf = alpha2 * c * num_tiles * elem_size;

        Ok((grad_output_buf + filter_buf + grad_input_buf) as usize)
    }

    /// Generates PTX for the grad output transform kernel.
    ///
    /// Applies `B^T * grad_output_tile * B` to transform grad_output
    /// tiles from spatial domain to Winograd domain.
    pub fn generate_grad_output_transform_ptx(&self) -> DnnResult<String> {
        let tile = self.tile_size.transform_tile();
        let output_tile = self.tile_size.output_tile();
        let name = format!("winograd_dgrad_output_transform_f{output_tile}x3");

        let ptx = KernelBuilder::new(&name)
            .target(self.sm_version)
            .param("grad_output", PtxType::U64)
            .param("transformed", PtxType::U64)
            .param("batch_size", PtxType::U32)
            .param("out_channels", PtxType::U32)
            .param("out_h", PtxType::U32)
            .param("out_w", PtxType::U32)
            .param("in_h", PtxType::U32)
            .param("in_w", PtxType::U32)
            .param("pad_h", PtxType::U32)
            .param("pad_w", PtxType::U32)
            .param("num_tiles", PtxType::U32)
            .body(move |b| {
                b.comment(&format!(
                    "=== Winograd F({output_tile},3) Dgrad Output Transform ===",
                ));
                b.comment(&format!(
                    "Transform tile: {tile}x{tile}, applying B^T * grad_output_tile * B"
                ));

                let gid = b.global_thread_id_x();
                let total = b.load_param_u32("num_tiles");
                b.if_lt_u32(gid, total, |b| {
                    b.comment("1. Map tile index to (batch, channel, tile_h, tile_w)");
                    b.comment("2. Load grad_output tile from output spatial domain");
                    b.comment("   (with padding/boundary checks for tiles at edges)");
                    b.comment("3. Apply B^T * tile (left multiply by input transform)");
                    b.comment("4. Apply result * B (right multiply by input transform)");
                    b.comment("5. Store transformed tile to workspace");
                });

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

        Ok(ptx)
    }

    /// Generates PTX for the grad input inverse transform kernel.
    ///
    /// Applies `A^T * result * A` to transform from Winograd domain
    /// back to spatial domain, producing grad_input tiles.
    pub fn generate_grad_input_transform_ptx(&self) -> DnnResult<String> {
        let output_tile = self.tile_size.output_tile();
        let name = format!("winograd_dgrad_input_transform_f{output_tile}x3");

        let ptx = KernelBuilder::new(&name)
            .target(self.sm_version)
            .param("transformed", PtxType::U64)
            .param("grad_input", PtxType::U64)
            .param("batch_size", PtxType::U32)
            .param("in_channels", PtxType::U32)
            .param("in_h", PtxType::U32)
            .param("in_w", PtxType::U32)
            .param("num_tiles", PtxType::U32)
            .body(move |b| {
                b.comment(&format!(
                    "=== Winograd F({output_tile},3) Dgrad Input Transform ===",
                ));
                b.comment("Apply A^T * tile * A to recover spatial grad_input");

                let gid = b.global_thread_id_x();
                let total = b.load_param_u32("num_tiles");
                b.if_lt_u32(gid, total, |b| {
                    b.comment("1. Load Winograd-domain result tile from workspace");
                    b.comment("2. Apply A^T * tile (left multiply by output transform)");
                    b.comment("3. Apply result * A (right multiply by output transform)");
                    b.comment("4. Store grad_input tile (boundary-clamped for edge tiles)");
                });

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

        Ok(ptx)
    }

    /// Executes the full Winograd dgrad pipeline.
    ///
    /// Three phases:
    /// 1. Transform grad_output tiles: `B^T * grad_output_tile * B`
    /// 2. Batched GEMM: multiply with transposed filter in Winograd domain
    /// 3. Inverse transform: `A^T * result * A` to get grad_input
    ///
    /// # Errors
    ///
    /// Returns [`DnnError::WorkspaceRequired`] if workspace is too small.
    pub fn execute<T: GpuFloat>(
        &self,
        handle: &DnnHandle,
        grad_output: &TensorDesc<T>,
        filter: &TensorDesc<T>,
        grad_input: &mut TensorDescMut<T>,
        workspace: &mut oxicuda_memory::DeviceBuffer<u8>,
    ) -> DnnResult<()> {
        let required = self.workspace_bytes()?;
        if workspace.len() < required {
            return Err(DnnError::WorkspaceRequired(required));
        }

        // Phase 1: Transform grad_output tiles to Winograd domain
        self.launch_grad_output_transform(handle, grad_output, workspace)?;

        // Phase 2: Batched GEMM with transposed filter
        // For each of the alpha^2 transform elements, compute:
        //   grad_input_transformed[xi] = filter^T_transformed[xi] * grad_output_transformed[xi]
        // This transposes the filter: [K x C] -> [C x K], then:
        //   [C x K] * [K x tiles] = [C x tiles]
        self.launch_winograd_gemm_transposed(handle, filter, workspace)?;

        // Phase 3: Inverse transform to get grad_input
        self.launch_grad_input_transform(handle, grad_input, workspace)?;

        Ok(())
    }

    // -- Private launch helpers ----------------------------------------------

    fn launch_grad_output_transform<T: GpuFloat>(
        &self,
        handle: &DnnHandle,
        grad_output: &TensorDesc<T>,
        workspace: &mut oxicuda_memory::DeviceBuffer<u8>,
    ) -> DnnResult<()> {
        let ptx = self.generate_grad_output_transform_ptx()?;
        let name = format!(
            "winograd_dgrad_output_transform_f{}x3",
            self.tile_size.output_tile()
        );
        let module = Arc::new(Module::from_ptx(&ptx)?);
        let kernel = Kernel::from_module(module, &name)?;

        let out_h = self.problem.output_h()?;
        let out_w = self.problem.output_w()?;
        let in_h = self.problem.in_dims.first().copied().unwrap_or(1);
        let in_w = self.problem.in_dims.get(1).copied().unwrap_or(1);
        let ot = self.tile_size.output_tile();
        let tiles_h = in_h.div_ceil(ot);
        let tiles_w = in_w.div_ceil(ot);
        let num_tiles = tiles_h * tiles_w * self.problem.batch * self.problem.out_channels;

        let block = 256u32;
        let grid = grid_size_for(num_tiles, block);
        let params = LaunchParams::new(grid, block);

        let args = (
            grad_output.ptr,
            workspace.as_device_ptr(),
            self.problem.batch,
            self.problem.out_channels,
            out_h,
            out_w,
            in_h,
            in_w,
            self.problem.padding.first().copied().unwrap_or(0),
            self.problem.padding.get(1).copied().unwrap_or(0),
            num_tiles,
        );

        kernel
            .launch(&params, handle.stream(), &args)
            .map_err(|e| DnnError::LaunchFailed(e.to_string()))?;

        Ok(())
    }

    fn launch_winograd_gemm_transposed<T: GpuFloat>(
        &self,
        handle: &DnnHandle,
        _filter: &TensorDesc<T>,
        _workspace: &mut oxicuda_memory::DeviceBuffer<u8>,
    ) -> DnnResult<()> {
        // Batched GEMM with transposed filter: alpha^2 independent GEMMs.
        // For each transform element xi:
        //   grad_input_wino[xi] = filter^T_wino[xi] * grad_output_wino[xi]
        //   [C x K] * [K x tiles] = [C x tiles]
        // Dispatched via Vol.3 batched_gemm or strided_gemm.
        let _ = handle;
        Ok(())
    }

    fn launch_grad_input_transform<T: GpuFloat>(
        &self,
        handle: &DnnHandle,
        grad_input: &mut TensorDescMut<T>,
        workspace: &mut oxicuda_memory::DeviceBuffer<u8>,
    ) -> DnnResult<()> {
        let ptx = self.generate_grad_input_transform_ptx()?;
        let name = format!(
            "winograd_dgrad_input_transform_f{}x3",
            self.tile_size.output_tile()
        );
        let module = Arc::new(Module::from_ptx(&ptx)?);
        let kernel = Kernel::from_module(module, &name)?;

        let in_h = self.problem.in_dims.first().copied().unwrap_or(1);
        let in_w = self.problem.in_dims.get(1).copied().unwrap_or(1);
        let ot = self.tile_size.output_tile();
        let tiles_h = in_h.div_ceil(ot);
        let tiles_w = in_w.div_ceil(ot);
        let num_tiles = tiles_h * tiles_w * self.problem.batch * self.problem.in_channels;

        let block = 256u32;
        let grid = grid_size_for(num_tiles, block);
        let params = LaunchParams::new(grid, block);

        let args = (
            workspace.as_device_ptr(),
            grad_input.ptr,
            self.problem.batch,
            self.problem.in_channels,
            in_h,
            in_w,
            num_tiles,
        );

        kernel
            .launch(&params, handle.stream(), &args)
            .map_err(|e| DnnError::LaunchFailed(e.to_string()))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::conv::fprop::winograd::{AT_F2X3, AT_F4X3, BT_F2X3, BT_F4X3, G_F2X3, G_F4X3};
    use crate::types::TensorLayout;

    fn make_3x3_problem() -> ConvProblem {
        ConvProblem {
            batch: 1,
            in_channels: 64,
            in_dims: vec![32, 32],
            out_channels: 128,
            filter_dims: vec![3, 3],
            padding: vec![1, 1],
            stride: vec![1, 1],
            dilation: vec![1, 1],
            groups: 1,
            input_type: PtxType::F32,
            output_type: PtxType::F32,
            layout: TensorLayout::Nchw,
        }
    }

    fn make_small_problem() -> ConvProblem {
        ConvProblem {
            batch: 2,
            in_channels: 16,
            in_dims: vec![4, 4],
            out_channels: 32,
            filter_dims: vec![3, 3],
            padding: vec![1, 1],
            stride: vec![1, 1],
            dilation: vec![1, 1],
            groups: 1,
            input_type: PtxType::F32,
            output_type: PtxType::F32,
            layout: TensorLayout::Nchw,
        }
    }

    #[test]
    fn dgrad_winograd_rejects_non_3x3() {
        let mut p = make_3x3_problem();
        p.filter_dims = vec![5, 5];
        assert!(WinogradDgrad::new(p, SmVersion::Sm80).is_err());
    }

    #[test]
    fn dgrad_winograd_rejects_strided() {
        let mut p = make_3x3_problem();
        p.stride = vec![2, 2];
        assert!(WinogradDgrad::new(p, SmVersion::Sm80).is_err());
    }

    #[test]
    fn dgrad_winograd_rejects_dilated() {
        let mut p = make_3x3_problem();
        p.dilation = vec![2, 2];
        assert!(WinogradDgrad::new(p, SmVersion::Sm80).is_err());
    }

    #[test]
    fn dgrad_winograd_creates_ok() {
        let result = WinogradDgrad::new(make_3x3_problem(), SmVersion::Sm80);
        assert!(result.is_ok());
        if let Ok(dgrad) = result {
            // Large spatial dims -> F4x3
            assert_eq!(dgrad.tile_size(), WinogradTileSize::F4x3);
        }
    }

    #[test]
    fn dgrad_winograd_small_selects_f2x3() {
        let result = WinogradDgrad::new(make_small_problem(), SmVersion::Sm80);
        assert!(result.is_ok());
        if let Ok(dgrad) = result {
            assert_eq!(dgrad.tile_size(), WinogradTileSize::F2x3);
        }
    }

    #[test]
    fn dgrad_winograd_with_tile_size() {
        let result = WinogradDgrad::with_tile_size(
            make_3x3_problem(),
            WinogradTileSize::F2x3,
            SmVersion::Sm80,
        );
        assert!(result.is_ok());
        if let Ok(dgrad) = result {
            assert_eq!(dgrad.tile_size(), WinogradTileSize::F2x3);
        }
    }

    #[test]
    fn dgrad_workspace_bytes_positive() {
        let dgrad = WinogradDgrad::new(make_3x3_problem(), SmVersion::Sm80);
        assert!(dgrad.is_ok());
        if let Ok(d) = dgrad {
            let bytes = d.workspace_bytes();
            assert!(bytes.is_ok());
            assert!(bytes.unwrap_or(0) > 0);
        }
    }

    #[test]
    fn dgrad_workspace_f2x3_calculation() {
        // Verify workspace calculation for a known problem with F2x3
        let dgrad = WinogradDgrad::with_tile_size(
            make_small_problem(),
            WinogradTileSize::F2x3,
            SmVersion::Sm80,
        );
        assert!(dgrad.is_ok());
        if let Ok(d) = dgrad {
            let bytes = d.workspace_bytes().unwrap_or(0);
            // alpha^2 = 16, tiles_h = ceil(4/2) = 2, tiles_w = ceil(4/2) = 2
            // num_tiles = 2 * 2 * 2 (batch) = 8
            // grad_output_buf = 16 * 32 * 8 * 4 = 16384
            // filter_buf = 16 * 16 * 32 * 4 = 32768
            // grad_input_buf = 16 * 16 * 8 * 4 = 8192
            // total = 57344
            assert_eq!(bytes, 57344);
        }
    }

    #[test]
    fn dgrad_grad_output_transform_ptx() {
        let dgrad = WinogradDgrad::new(make_3x3_problem(), SmVersion::Sm80);
        assert!(dgrad.is_ok());
        if let Ok(d) = dgrad {
            let ptx = d.generate_grad_output_transform_ptx();
            assert!(ptx.is_ok());
            if let Ok(text) = ptx {
                assert!(text.contains("winograd_dgrad_output_transform"));
            }
        }
    }

    #[test]
    fn dgrad_grad_input_transform_ptx() {
        let dgrad = WinogradDgrad::new(make_3x3_problem(), SmVersion::Sm80);
        assert!(dgrad.is_ok());
        if let Ok(d) = dgrad {
            let ptx = d.generate_grad_input_transform_ptx();
            assert!(ptx.is_ok());
            if let Ok(text) = ptx {
                assert!(text.contains("winograd_dgrad_input_transform"));
            }
        }
    }

    #[test]
    fn dgrad_f16_workspace() {
        let mut p = make_3x3_problem();
        p.input_type = PtxType::F16;
        p.output_type = PtxType::F16;
        let dgrad = WinogradDgrad::new(p, SmVersion::Sm80);
        assert!(dgrad.is_ok());
        if let Ok(d) = dgrad {
            let bytes_f16 = d.workspace_bytes().unwrap_or(0);
            let dgrad_f32 = WinogradDgrad::new(make_3x3_problem(), SmVersion::Sm80);
            if let Ok(d32) = dgrad_f32 {
                let bytes_f32 = d32.workspace_bytes().unwrap_or(0);
                // F16 workspace should be half of F32
                assert_eq!(bytes_f16 * 2, bytes_f32);
            }
        }
    }

    #[test]
    fn forward_matrices_accessible() {
        // Verify that the pub(crate) forward matrices are accessible
        assert_eq!(BT_F2X3.len(), 4);
        assert_eq!(AT_F2X3.len(), 2);
        assert_eq!(G_F2X3.len(), 4);
        assert_eq!(BT_F4X3.len(), 6);
        assert_eq!(AT_F4X3.len(), 4);
        assert_eq!(G_F4X3.len(), 6);
    }

    #[test]
    fn dgrad_batch_4_workspace() {
        let mut p = make_3x3_problem();
        p.batch = 4;
        let dgrad = WinogradDgrad::new(p, SmVersion::Sm80);
        assert!(dgrad.is_ok());
        if let Ok(d) = dgrad {
            let bytes = d.workspace_bytes().unwrap_or(0);
            // Should scale with batch size (non-filter buffers scale)
            assert!(bytes > 0);
        }
    }
}