oxicuda-vision 0.2.0

Vision Transformer & CLIP primitives for OxiCUDA: ViT patch embedding, multi-head self-attention, CLIP contrastive learning, FPN, RoI align, DETR decoder — pure Rust, zero CUDA SDK dependency.
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//! Mask R-CNN segmentation head.
//!
//! Reference: He et al. 2017 — *"Mask R-CNN"* (ICCV).
//!
//! For each Region-of-Interest (RoI), [`RoIAlign`](super::roi_align()) produces a
//! `(in_channels × roi_size × roi_size)` feature volume. This volume is fed
//! through a tiny **fully convolutional network** (FCN) that predicts a
//! per-class binary mask at **twice** the input resolution:
//!
//! ```text
//!     roi_features                                                            mask_logits
//!   (C_in, R, R)  ─┐                                                        ─►  (K, 2R, 2R)
//!                  │  n_conv × { 3×3 same-pad conv → ReLU }                    (per-class)
//!                  ▼            (first maps C_in → conv_dim, rest conv_dim → conv_dim)
//!            (conv_dim, R, R)
//!                  │  2× transposed-convolution (stride 2, kernel 2)
//!                  ▼            (conv_dim → conv_dim, then ReLU)
//!           (conv_dim, 2R, 2R)
//!                  │  1×1 conv to per-class logits
//!//!           (n_classes, 2R, 2R)
//! ```
//!
//! At inference time, [`MaskHead::predict_mask`] applies a sigmoid on the
//! logits of a chosen class to produce a binary mask in `[0, 1]`.
//!
//! The conv stack is **same-padding** (zero), the transposed conv uses
//! `kernel_size = 2`, `stride = 2`, `padding = 0` so that
//! `out_size = 2 · in_size` exactly, matching He 2017.

use crate::{
    error::{VisionError, VisionResult},
    handle::LcgRng,
};

// ─── MaskHeadConfig ───────────────────────────────────────────────────────────

/// Hyper-parameters for the Mask R-CNN segmentation head.
#[derive(Debug, Clone, PartialEq)]
pub struct MaskHeadConfig {
    /// Input feature channels (the FPN level channel count).
    pub in_channels: usize,
    /// Spatial side of the RoIAlign output (commonly 14 or 28).
    pub roi_size: usize,
    /// Number of foreground classes.
    pub n_classes: usize,
    /// Number of 3×3 conv layers in the stack (commonly 4 in He 2017).
    pub n_conv: usize,
    /// Hidden channel dimension shared by all conv layers and the deconv.
    pub conv_dim: usize,
}

// ─── ConvLayer ────────────────────────────────────────────────────────────────

/// A single 3×3 same-pad convolution layer.
struct ConvLayer {
    /// Kernel weights `[out_c × in_c × 3 × 3]` row-major.
    weight: Vec<f32>,
    /// Bias `[out_c]`.
    bias: Vec<f32>,
    in_c: usize,
    out_c: usize,
}

impl ConvLayer {
    fn new(in_c: usize, out_c: usize, rng: &mut LcgRng) -> Self {
        // He initialisation: scale = sqrt(2 / (in_c · k²)).
        let fan_in = (in_c * 9) as f32;
        let scale = (2.0 / fan_in).sqrt();
        let n = out_c * in_c * 9;
        let mut weight = vec![0.0_f32; n];
        rng.fill_normal(&mut weight);
        for w in &mut weight {
            *w *= scale;
        }
        let bias = vec![0.0_f32; out_c];
        Self {
            weight,
            bias,
            in_c,
            out_c,
        }
    }

    /// 3×3 same-pad conv. Input `(in_c, h, w)`, output `(out_c, h, w)`.
    /// Padding is zero, stride is 1.
    fn forward(&self, x: &[f32], h: usize, w: usize) -> VisionResult<Vec<f32>> {
        let expected = self.in_c * h * w;
        if x.len() != expected {
            return Err(VisionError::DimensionMismatch {
                expected,
                got: x.len(),
            });
        }
        let hw = h * w;
        let mut out = vec![0.0_f32; self.out_c * hw];

        for oc in 0..self.out_c {
            let kernel_base = oc * self.in_c * 9;
            let bias = self.bias[oc];
            for oh in 0..h {
                for ow in 0..w {
                    let mut acc = bias;
                    for ic in 0..self.in_c {
                        let in_base = ic * hw;
                        let ker = kernel_base + ic * 9;
                        // Iterate the 3×3 kernel taps with pad=1.
                        for ki in 0..3usize {
                            let ih = oh as isize + ki as isize - 1;
                            if ih < 0 || ih >= h as isize {
                                continue;
                            }
                            let ih = ih as usize;
                            for kj in 0..3usize {
                                let iw = ow as isize + kj as isize - 1;
                                if iw < 0 || iw >= w as isize {
                                    continue;
                                }
                                let iw = iw as usize;
                                acc += self.weight[ker + ki * 3 + kj] * x[in_base + ih * w + iw];
                            }
                        }
                    }
                    out[oc * hw + oh * w + ow] = acc;
                }
            }
        }
        Ok(out)
    }

    fn n_params(&self) -> usize {
        self.weight.len() + self.bias.len()
    }
}

// ─── DeconvLayer (2× transposed convolution) ──────────────────────────────────

/// 2×2 transposed convolution with stride 2.
///
/// Maps `(in_c, h, w)` → `(out_c, 2h, 2w)` by depositing each input pixel into
/// a `2 × 2` patch of the output via the kernel.
struct DeconvLayer {
    /// Kernel `[in_c × out_c × 2 × 2]` row-major.
    weight: Vec<f32>,
    /// Bias `[out_c]`.
    bias: Vec<f32>,
    in_c: usize,
    out_c: usize,
}

impl DeconvLayer {
    fn new(in_c: usize, out_c: usize, rng: &mut LcgRng) -> Self {
        // He init for transposed conv: fan_in = in_c · k².
        let fan_in = (in_c * 4) as f32;
        let scale = (2.0 / fan_in).sqrt();
        let n = in_c * out_c * 4;
        let mut weight = vec![0.0_f32; n];
        rng.fill_normal(&mut weight);
        for w in &mut weight {
            *w *= scale;
        }
        let bias = vec![0.0_f32; out_c];
        Self {
            weight,
            bias,
            in_c,
            out_c,
        }
    }

    fn forward(&self, x: &[f32], h: usize, w: usize) -> VisionResult<Vec<f32>> {
        let expected = self.in_c * h * w;
        if x.len() != expected {
            return Err(VisionError::DimensionMismatch {
                expected,
                got: x.len(),
            });
        }
        let out_h = h * 2;
        let out_w = w * 2;
        let out_hw = out_h * out_w;
        let mut out = vec![0.0_f32; self.out_c * out_hw];

        // Initialise output to bias.
        for oc in 0..self.out_c {
            let b = self.bias[oc];
            let base = oc * out_hw;
            for p in 0..out_hw {
                out[base + p] = b;
            }
        }

        // For each input pixel (ic, ih, iw), scatter its contribution to the
        // 2×2 output patch starting at (2·ih, 2·iw) for every (oc, ki, kj).
        let hw = h * w;
        for ic in 0..self.in_c {
            let in_base = ic * hw;
            let ker_base = ic * self.out_c * 4;
            for ih in 0..h {
                for iw in 0..w {
                    let v = x[in_base + ih * w + iw];
                    if v == 0.0 {
                        continue;
                    }
                    let oh0 = ih * 2;
                    let ow0 = iw * 2;
                    for oc in 0..self.out_c {
                        let kbase = ker_base + oc * 4;
                        let obase = oc * out_hw;
                        // Unrolled 2×2.
                        out[obase + oh0 * out_w + ow0] += v * self.weight[kbase];
                        out[obase + oh0 * out_w + ow0 + 1] += v * self.weight[kbase + 1];
                        out[obase + (oh0 + 1) * out_w + ow0] += v * self.weight[kbase + 2];
                        out[obase + (oh0 + 1) * out_w + ow0 + 1] += v * self.weight[kbase + 3];
                    }
                }
            }
        }

        Ok(out)
    }

    fn n_params(&self) -> usize {
        self.weight.len() + self.bias.len()
    }
}

// ─── Pointwise (1×1) conv ─────────────────────────────────────────────────────

/// 1×1 convolution: per-position linear map.
struct Pointwise {
    weight: Vec<f32>, // [out_c × in_c]
    bias: Vec<f32>,   // [out_c]
    in_c: usize,
    out_c: usize,
}

impl Pointwise {
    fn new(in_c: usize, out_c: usize, rng: &mut LcgRng) -> Self {
        let fan_in = in_c as f32;
        let scale = (2.0 / fan_in).sqrt();
        let mut weight = vec![0.0_f32; out_c * in_c];
        rng.fill_normal(&mut weight);
        for w in &mut weight {
            *w *= scale;
        }
        let bias = vec![0.0_f32; out_c];
        Self {
            weight,
            bias,
            in_c,
            out_c,
        }
    }

    fn forward(&self, x: &[f32], h: usize, w: usize) -> VisionResult<Vec<f32>> {
        let expected = self.in_c * h * w;
        if x.len() != expected {
            return Err(VisionError::DimensionMismatch {
                expected,
                got: x.len(),
            });
        }
        let hw = h * w;
        let mut out = vec![0.0_f32; self.out_c * hw];
        for oc in 0..self.out_c {
            let wrow = &self.weight[oc * self.in_c..(oc + 1) * self.in_c];
            let b = self.bias[oc];
            for p in 0..hw {
                let mut acc = b;
                for ic in 0..self.in_c {
                    acc += wrow[ic] * x[ic * hw + p];
                }
                out[oc * hw + p] = acc;
            }
        }
        Ok(out)
    }

    fn n_params(&self) -> usize {
        self.weight.len() + self.bias.len()
    }
}

// ─── MaskHead ─────────────────────────────────────────────────────────────────

/// Mask R-CNN segmentation head.
pub struct MaskHead {
    cfg: MaskHeadConfig,
    convs: Vec<ConvLayer>,
    deconv: DeconvLayer,
    mask_pred: Pointwise,
}

impl MaskHead {
    /// Build a mask head with He-initialised weights from `rng`.
    ///
    /// # Errors
    /// [`VisionError::DimensionMismatch`] if any of `in_channels`,
    /// `roi_size`, `n_classes`, `n_conv`, `conv_dim` is zero.
    pub fn new(cfg: MaskHeadConfig, rng: &mut LcgRng) -> VisionResult<Self> {
        if cfg.in_channels == 0 {
            return Err(VisionError::DimensionMismatch {
                expected: 1,
                got: 0,
            });
        }
        if cfg.roi_size == 0 {
            return Err(VisionError::DimensionMismatch {
                expected: 1,
                got: 0,
            });
        }
        if cfg.n_classes == 0 {
            return Err(VisionError::InvalidNumClasses(0));
        }
        if cfg.n_conv == 0 {
            return Err(VisionError::DimensionMismatch {
                expected: 1,
                got: 0,
            });
        }
        if cfg.conv_dim == 0 {
            return Err(VisionError::DimensionMismatch {
                expected: 1,
                got: 0,
            });
        }

        let mut convs = Vec::with_capacity(cfg.n_conv);
        // First conv maps in_channels → conv_dim; the rest are conv_dim → conv_dim.
        convs.push(ConvLayer::new(cfg.in_channels, cfg.conv_dim, rng));
        for _ in 1..cfg.n_conv {
            convs.push(ConvLayer::new(cfg.conv_dim, cfg.conv_dim, rng));
        }
        let deconv = DeconvLayer::new(cfg.conv_dim, cfg.conv_dim, rng);
        let mask_pred = Pointwise::new(cfg.conv_dim, cfg.n_classes, rng);

        Ok(Self {
            cfg,
            convs,
            deconv,
            mask_pred,
        })
    }

    /// Read-only access to the head configuration.
    #[must_use]
    #[inline]
    pub fn config(&self) -> &MaskHeadConfig {
        &self.cfg
    }

    /// Forward the convolution stack only (used as an intermediate testable
    /// API): returns the activation at the input of the deconv layer,
    /// shape `(conv_dim × roi_size × roi_size)`.
    ///
    /// All conv layers use ReLU activation.
    ///
    /// # Errors
    /// [`VisionError::DimensionMismatch`] if `roi_features.len() !=
    /// in_channels · roi_size²`.
    pub fn forward_conv_stack(&self, roi_features: &[f32]) -> VisionResult<Vec<f32>> {
        let r = self.cfg.roi_size;
        let expected = self.cfg.in_channels * r * r;
        if roi_features.len() != expected {
            return Err(VisionError::DimensionMismatch {
                expected,
                got: roi_features.len(),
            });
        }

        let mut feat = roi_features.to_vec();
        for conv in &self.convs {
            let mut next = conv.forward(&feat, r, r)?;
            // In-place ReLU.
            for v in &mut next {
                if *v < 0.0 {
                    *v = 0.0;
                }
            }
            feat = next;
        }
        Ok(feat)
    }

    /// Forward pass: returns per-class mask logits.
    ///
    /// Input `(in_channels × roi_size × roi_size)` → output
    /// `(n_classes × 2·roi_size × 2·roi_size)` row-major.
    ///
    /// # Errors
    /// [`VisionError::DimensionMismatch`] if `roi_features.len() !=
    /// in_channels · roi_size²`.
    pub fn forward(&self, roi_features: &[f32]) -> VisionResult<Vec<f32>> {
        let r = self.cfg.roi_size;
        // Step 1: same-pad conv stack at resolution R×R.
        let after_convs = self.forward_conv_stack(roi_features)?;
        // Step 2: 2× transposed conv → (conv_dim, 2R, 2R), then ReLU.
        let mut after_deconv = self.deconv.forward(&after_convs, r, r)?;
        for v in &mut after_deconv {
            if *v < 0.0 {
                *v = 0.0;
            }
        }
        // Step 3: 1×1 conv → (n_classes, 2R, 2R). No activation: logits.
        let logits = self.mask_pred.forward(&after_deconv, 2 * r, 2 * r)?;
        if logits.iter().any(|v| !v.is_finite()) {
            return Err(VisionError::NonFinite("mask head logits"));
        }
        Ok(logits)
    }

    /// Predict the sigmoid mask for a single class.
    ///
    /// Output length: `(2 · roi_size)²`, values in `[0, 1]`.
    ///
    /// # Errors
    /// - [`VisionError::DimensionMismatch`] if `roi_features` has the wrong
    ///   length.
    /// - [`VisionError::InvalidNumClasses`] if `class_idx >= n_classes`.
    pub fn predict_mask(&self, roi_features: &[f32], class_idx: usize) -> VisionResult<Vec<f32>> {
        if class_idx >= self.cfg.n_classes {
            return Err(VisionError::InvalidNumClasses(class_idx));
        }
        let logits = self.forward(roi_features)?;
        let out_size = 4 * self.cfg.roi_size * self.cfg.roi_size; // (2R)²
        let base = class_idx * out_size;
        let mut mask = Vec::with_capacity(out_size);
        for i in 0..out_size {
            let z = logits[base + i];
            mask.push(sigmoid(z));
        }
        Ok(mask)
    }

    /// Total learnable parameter count.
    #[must_use]
    pub fn n_params(&self) -> usize {
        let conv_params: usize = self.convs.iter().map(ConvLayer::n_params).sum();
        conv_params + self.deconv.n_params() + self.mask_pred.n_params()
    }
}

// ─── Sigmoid ──────────────────────────────────────────────────────────────────

/// Numerically stable sigmoid.
#[inline]
fn sigmoid(z: f32) -> f32 {
    if z >= 0.0 {
        let e = (-z).exp();
        1.0 / (1.0 + e)
    } else {
        let e = z.exp();
        e / (1.0 + e)
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    fn small_cfg() -> MaskHeadConfig {
        // in=4, roi=4, classes=3, n_conv=2, conv_dim=8
        MaskHeadConfig {
            in_channels: 4,
            roi_size: 4,
            n_classes: 3,
            n_conv: 2,
            conv_dim: 8,
        }
    }

    fn make_head(seed: u64) -> MaskHead {
        let mut rng = LcgRng::new(seed);
        MaskHead::new(small_cfg(), &mut rng).expect("ok")
    }

    fn random_roi(in_c: usize, r: usize, seed: u64) -> Vec<f32> {
        let mut rng = LcgRng::new(seed);
        let mut x = vec![0.0_f32; in_c * r * r];
        rng.fill_normal(&mut x);
        x
    }

    // ── Output shapes ─────────────────────────────────────────────────────────

    #[test]
    fn forward_output_length() {
        let head = make_head(1);
        let roi = random_roi(4, 4, 2);
        let out = head.forward(&roi).expect("ok");
        assert_eq!(out.len(), 3 * 8 * 8, "n_classes × 2R × 2R");
    }

    #[test]
    fn predict_mask_length_and_range() {
        let head = make_head(3);
        let roi = random_roi(4, 4, 4);
        for k in 0..3 {
            let m = head.predict_mask(&roi, k).expect("ok");
            assert_eq!(m.len(), 8 * 8);
            for &v in &m {
                assert!(
                    (0.0..=1.0).contains(&v),
                    "sigmoid out of [0,1] for class {k}: {v}"
                );
            }
        }
    }

    #[test]
    fn upsample_doubles_spatial_dims() {
        let cfg = MaskHeadConfig {
            in_channels: 2,
            roi_size: 7,
            n_classes: 1,
            n_conv: 1,
            conv_dim: 4,
        };
        let mut rng = LcgRng::new(5);
        let head = MaskHead::new(cfg, &mut rng).expect("ok");
        let roi = vec![0.0_f32; 2 * 7 * 7];
        let out = head.forward(&roi).expect("ok");
        assert_eq!(out.len(), 14 * 14);
    }

    #[test]
    fn conv_stack_preserves_spatial_size() {
        let head = make_head(6);
        let roi = random_roi(4, 4, 7);
        let mid = head.forward_conv_stack(&roi).expect("ok");
        // Should be (conv_dim, R, R) = (8, 4, 4) = 128.
        assert_eq!(mid.len(), 8 * 4 * 4);
    }

    #[test]
    fn conv_stack_relu_non_negative() {
        let head = make_head(8);
        let roi = random_roi(4, 4, 9);
        let mid = head.forward_conv_stack(&roi).expect("ok");
        for &v in &mid {
            assert!(v >= 0.0, "ReLU should clamp to >= 0; got {v}");
        }
    }

    // ── n_params formula ──────────────────────────────────────────────────────

    #[test]
    fn n_params_positive_and_reasonable() {
        let head = make_head(10);
        let cfg = head.config();
        // Conv0 : conv_dim × in_channels × 9 + conv_dim
        let c0 = cfg.conv_dim * cfg.in_channels * 9 + cfg.conv_dim;
        // Conv1..n_conv-1: conv_dim² × 9 + conv_dim
        let ck = (cfg.n_conv - 1) * (cfg.conv_dim * cfg.conv_dim * 9 + cfg.conv_dim);
        // Deconv: conv_dim × conv_dim × 4 + conv_dim
        let dc = cfg.conv_dim * cfg.conv_dim * 4 + cfg.conv_dim;
        // Mask pred: n_classes × conv_dim + n_classes
        let mp = cfg.n_classes * cfg.conv_dim + cfg.n_classes;
        let expected = c0 + ck + dc + mp;
        assert_eq!(head.n_params(), expected);
        assert!(head.n_params() > 0);
    }

    #[test]
    fn deterministic_given_seed() {
        let head_a = make_head(42);
        let head_b = make_head(42);
        let roi = random_roi(4, 4, 99);
        let oa = head_a.forward(&roi).expect("ok");
        let ob = head_b.forward(&roi).expect("ok");
        assert_eq!(oa, ob);
    }

    #[test]
    fn different_input_changes_output() {
        let head = make_head(11);
        let r1 = random_roi(4, 4, 100);
        let r2 = random_roi(4, 4, 101);
        let o1 = head.forward(&r1).expect("ok");
        let o2 = head.forward(&r2).expect("ok");
        assert_ne!(o1, o2);
    }

    #[test]
    fn n_conv_one_works() {
        let cfg = MaskHeadConfig {
            in_channels: 3,
            roi_size: 5,
            n_classes: 2,
            n_conv: 1,
            conv_dim: 6,
        };
        let mut rng = LcgRng::new(12);
        let head = MaskHead::new(cfg, &mut rng).expect("ok");
        let roi = vec![0.1_f32; 3 * 5 * 5];
        let out = head.forward(&roi).expect("ok");
        assert_eq!(out.len(), 2 * 10 * 10);
    }

    #[test]
    fn single_class_works() {
        let cfg = MaskHeadConfig {
            in_channels: 2,
            roi_size: 3,
            n_classes: 1,
            n_conv: 2,
            conv_dim: 4,
        };
        let mut rng = LcgRng::new(13);
        let head = MaskHead::new(cfg, &mut rng).expect("ok");
        let roi = vec![0.2_f32; 2 * 3 * 3];
        let mask = head.predict_mask(&roi, 0).expect("ok");
        assert_eq!(mask.len(), 6 * 6);
    }

    // ── Error paths ───────────────────────────────────────────────────────────

    #[test]
    fn err_roi_features_wrong_length() {
        let head = make_head(14);
        // expected = 4*4*4 = 64, give 60.
        let roi = vec![0.0_f32; 60];
        let r = head.forward(&roi);
        assert!(matches!(r, Err(VisionError::DimensionMismatch { .. })));
    }

    #[test]
    fn err_class_idx_out_of_range() {
        let head = make_head(15);
        let roi = random_roi(4, 4, 16);
        let r = head.predict_mask(&roi, 99);
        assert!(matches!(r, Err(VisionError::InvalidNumClasses(_))));
    }

    #[test]
    fn err_in_channels_zero() {
        let mut rng = LcgRng::new(17);
        let cfg = MaskHeadConfig {
            in_channels: 0,
            roi_size: 4,
            n_classes: 3,
            n_conv: 2,
            conv_dim: 8,
        };
        let r = MaskHead::new(cfg, &mut rng);
        assert!(matches!(r, Err(VisionError::DimensionMismatch { .. })));
    }

    #[test]
    fn err_roi_size_zero() {
        let mut rng = LcgRng::new(18);
        let cfg = MaskHeadConfig {
            in_channels: 4,
            roi_size: 0,
            n_classes: 3,
            n_conv: 2,
            conv_dim: 8,
        };
        let r = MaskHead::new(cfg, &mut rng);
        assert!(matches!(r, Err(VisionError::DimensionMismatch { .. })));
    }

    #[test]
    fn err_n_classes_zero() {
        let mut rng = LcgRng::new(19);
        let cfg = MaskHeadConfig {
            in_channels: 4,
            roi_size: 4,
            n_classes: 0,
            n_conv: 2,
            conv_dim: 8,
        };
        let r = MaskHead::new(cfg, &mut rng);
        assert!(matches!(r, Err(VisionError::InvalidNumClasses(0))));
    }

    #[test]
    fn err_n_conv_zero() {
        let mut rng = LcgRng::new(20);
        let cfg = MaskHeadConfig {
            in_channels: 4,
            roi_size: 4,
            n_classes: 3,
            n_conv: 0,
            conv_dim: 8,
        };
        let r = MaskHead::new(cfg, &mut rng);
        assert!(matches!(r, Err(VisionError::DimensionMismatch { .. })));
    }

    #[test]
    fn err_conv_dim_zero() {
        let mut rng = LcgRng::new(21);
        let cfg = MaskHeadConfig {
            in_channels: 4,
            roi_size: 4,
            n_classes: 3,
            n_conv: 2,
            conv_dim: 0,
        };
        let r = MaskHead::new(cfg, &mut rng);
        assert!(matches!(r, Err(VisionError::DimensionMismatch { .. })));
    }

    #[test]
    fn logits_finite() {
        let head = make_head(22);
        let roi = random_roi(4, 4, 23);
        let out = head.forward(&roi).expect("ok");
        assert!(out.iter().all(|v| v.is_finite()));
    }
}