tomoxide 0.14.0

GPU/CPU tomographic reconstruction toolkit (tomopy + tomocupy, in Rust)
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! # tomoxide-wgpu
//!
//! A portable GPU backend built on `wgpu`, so the GPU reconstruction path
//! runs on hardware CUDA can't target — notably **Metal** on Apple Silicon, and
//! Vulkan/DX12 elsewhere. Kernels are WGSL ports of the CUDA kernels (see
//! `shaders`).
//!
//! Gated behind the **`gpu-wgpu`** feature because `wgpu` is a heavy
//! dependency; the default workspace build skips it and
//! [`WgpuBackend::new`] reports the backend as unavailable.
#![cfg_attr(not(feature = "gpu-wgpu"), allow(dead_code))]

#[cfg(feature = "gpu-wgpu")]
pub mod shaders;

#[cfg(feature = "gpu-wgpu")]
mod compute;
#[cfg(feature = "gpu-wgpu")]
mod kernels;
#[cfg(feature = "gpu-wgpu")]
mod streaming;

use crate::backend::{Backend, DeviceKind};
use crate::dtype::Dtype;
use crate::error::{Error, Result};

/// Handle to the portable GPU backend.
#[cfg(feature = "gpu-wgpu")]
pub struct WgpuBackend {
    /// The logical device. `Arc` so a streaming reconstructor
    /// ([`AnalyticReconstruct::streaming`](crate::backend::AnalyticReconstruct::streaming))
    /// can share the same GPU device (wgpu's `Device`/`Queue` are not `Clone`).
    pub device: std::sync::Arc<wgpu::Device>,
    /// The command queue (shared via `Arc`, see [`Self::device`]).
    pub queue: std::sync::Arc<wgpu::Queue>,
    /// Compiled compute-pipeline cache, keyed by a hash of `(shader source,
    /// entry point)`. Every `dispatch1d` / `fft_shared_pass` call used to
    /// `create_shader_module` + `create_compute_pipeline` from scratch — i.e.
    /// re-run the naga→SPIR-V compile and driver pipeline build on *every*
    /// dispatch. A single reconstruction issues dozens of dispatches (lprec runs
    /// ~44, fourierrec similar), so that per-call compile dominated wall time vs
    /// CUDA's precompiled kernels. Caching makes each unique kernel compile once
    /// per backend; the source string (with its injected `const`s) is the key, so
    /// size-specialized variants (e.g. fourierrec's `const M`, fft's `const NN`)
    /// each get their own entry.
    pipelines:
        std::sync::Mutex<std::collections::HashMap<u64, std::sync::Arc<wgpu::ComputePipeline>>>,
    /// Whether the device was created with [`wgpu::Features::SHADER_FLOAT32_ATOMIC`]
    /// (native `atomicAdd` on `atomic<f32>` storage — Vulkan
    /// `VK_EXT_shader_atomic_float`). When set, the scatter kernels (forward
    /// projection, fourierrec gather/wrap, lprec gather) accumulate with one
    /// native float atomic per update; otherwise they fall back to the portable
    /// compare-exchange emulation on `atomic<u32>` lanes, which pays a
    /// read-modify-retry loop per update (2–3× the memory traffic).
    pub(crate) f32_atomics: bool,
}

/// Handle to the portable GPU backend (stub: compiled without `gpu-wgpu`).
#[cfg(not(feature = "gpu-wgpu"))]
#[derive(Clone, Copy, Debug, Default)]
pub struct WgpuBackend;

impl WgpuBackend {
    /// Initialise a wgpu device, picking a high-performance adapter.
    ///
    /// Without the `gpu-wgpu` feature this returns [`Error::BackendUnavailable`].
    #[cfg(not(feature = "gpu-wgpu"))]
    pub fn new() -> Result<Self> {
        Err(Error::BackendUnavailable(
            "compiled without the `gpu-wgpu` feature".into(),
        ))
    }

    /// Initialise a wgpu device, picking a high-performance adapter (Metal on
    /// Apple Silicon, Vulkan/DX12 elsewhere). Blocks on the async wgpu requests
    /// via `pollster`; returns [`Error::BackendUnavailable`] if no adapter or
    /// device can be acquired.
    #[cfg(feature = "gpu-wgpu")]
    pub fn new() -> Result<Self> {
        pollster::block_on(Self::new_async())
    }

    #[cfg(feature = "gpu-wgpu")]
    async fn new_async() -> Result<Self> {
        let instance = wgpu::Instance::default();
        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                ..Default::default()
            })
            .await
            .map_err(|e| Error::BackendUnavailable(format!("no wgpu adapter available: {e}")))?;
        // `Limits::default()` is the conservative downlevel/WebGL profile
        // (256 MiB max buffer, 128 MiB max storage binding). A whole-volume
        // reconstruction's filter/FFT buffers blow past that (e.g. a 512²
        // nz=128 FBP needs a ~1 GiB spectrum buffer), so request the adapter's
        // reported maxima — the real hardware capability — instead.
        // Native f32 atomics (VK_EXT_shader_atomic_float) let the scatter
        // kernels accumulate with one atomicAdd instead of a CAS loop; request
        // the feature when the adapter has it, fall back to CAS-emulation
        // shader variants when it doesn't (Metal, older drivers).
        // `TOMOXIDE_WGPU_NO_F32_ATOMICS=1` forces the CAS fallback even on a
        // supporting adapter — for exercising/benchmarking the portable path on
        // hardware that would otherwise never take it.
        let f32_atomics = adapter
            .features()
            .contains(wgpu::Features::SHADER_FLOAT32_ATOMIC)
            && std::env::var_os("TOMOXIDE_WGPU_NO_F32_ATOMICS").is_none();
        let required_features = if f32_atomics {
            wgpu::Features::SHADER_FLOAT32_ATOMIC
        } else {
            wgpu::Features::empty()
        };
        let (device, queue) = adapter
            .request_device(&wgpu::DeviceDescriptor {
                label: Some("tomoxide-wgpu"),
                required_features,
                required_limits: adapter.limits(),
                memory_hints: wgpu::MemoryHints::Performance,
                experimental_features: wgpu::ExperimentalFeatures::disabled(),
                trace: wgpu::Trace::Off,
            })
            .await
            .map_err(|e| Error::BackendUnavailable(format!("wgpu request_device: {e}")))?;
        Ok(Self {
            device: std::sync::Arc::new(device),
            queue: std::sync::Arc::new(queue),
            pipelines: std::sync::Mutex::new(std::collections::HashMap::new()),
            f32_atomics,
        })
    }
}

impl Backend for WgpuBackend {
    fn name(&self) -> &'static str {
        "wgpu"
    }
    fn device(&self) -> DeviceKind {
        DeviceKind::Wgpu
    }
    fn supports(&self, dt: Dtype) -> bool {
        // f16 needs the `shader-f16` device feature; advertise f32 for now.
        dt == Dtype::F32
    }

    /// Elementwise preprocessing (dark/flat correction, `−ln`) runs on the GPU.
    #[cfg(feature = "gpu-wgpu")]
    fn elementwise(&self) -> Option<&dyn crate::backend::Elementwise> {
        Some(self)
    }

    /// Parallel-beam filtered back-projection runs on the GPU.
    #[cfg(feature = "gpu-wgpu")]
    fn backprojector(&self) -> Option<&dyn crate::backend::FilteredBackproject> {
        Some(self)
    }

    /// Parallel-beam forward projection (Radon transform) runs on the GPU.
    #[cfg(feature = "gpu-wgpu")]
    fn projector(&self) -> Option<&dyn crate::backend::ForwardProject> {
        Some(self)
    }

    /// 3-D median / dezinger rank filters run on the GPU (bit-exact with CPU).
    #[cfg(feature = "gpu-wgpu")]
    fn rank_filter(&self) -> Option<&dyn crate::backend::RankFilter> {
        Some(self)
    }

    /// Batched radix-2 FFT (power-of-two lengths) runs on the GPU.
    #[cfg(feature = "gpu-wgpu")]
    fn fft(&self) -> Option<&dyn crate::backend::Fft> {
        Some(self)
    }

    /// FBP apodization-filter application (pad → FFT → ×filter → IFFT → crop)
    /// runs on the GPU, closing the full GPU filtered-back-projection path.
    #[cfg(feature = "gpu-wgpu")]
    fn fbp_filter(&self) -> Option<&dyn crate::backend::FbpFilter> {
        Some(self)
    }

    /// Device-resident Gaussian-USFFT (fourierrec) — the whole gridding + FFT
    /// chain runs on the GPU, replacing the host-gridding fallback.
    #[cfg(feature = "gpu-wgpu")]
    fn fourier_reconstruct(&self) -> Option<&dyn crate::backend::FourierReconstruct> {
        Some(self)
    }

    /// Device-resident log-polar (lprec) — the cubic-B-spline prefilter,
    /// gather/scatter interpolation, and FFT convolution run on the GPU,
    /// replacing the host-interpolation + FFT-round-trip fallback.
    #[cfg(feature = "gpu-wgpu")]
    fn lprec_reconstruct(&self) -> Option<&dyn crate::backend::LpRecReconstruct> {
        Some(self)
    }

    /// Fused analytic path (fbp/linerec/fourierrec): filter → back-projection /
    /// Fourier gridding, keeping the filtered sinogram device-resident so the
    /// recon dispatcher pays one upload / one download instead of round-tripping
    /// the filtered sinogram back to host between the filter and the recon.
    #[cfg(feature = "gpu-wgpu")]
    fn analytic_reconstruct(&self) -> Option<&dyn crate::backend::AnalyticReconstruct> {
        Some(self)
    }

    /// Device-resident iterative reconstruction (SIRT): the whole
    /// forward/back-projection loop and its weight setup run on the GPU with the
    /// volume/sinogram resident across all iterations — one upload, one download —
    /// instead of the generic host solver's per-iteration round-trip. Returns
    /// `None` for algorithms without a device path so they fall back to host.
    #[cfg(feature = "gpu-wgpu")]
    fn iterative_reconstruct(&self) -> Option<&dyn crate::backend::IterativeReconstruct> {
        Some(self)
    }
    // Remaining capability accessors stay `None` until their WGSL kernels land.
}

#[cfg(test)]
mod tests {
    #[cfg(not(feature = "gpu-wgpu"))]
    use super::*;

    #[test]
    #[cfg(not(feature = "gpu-wgpu"))]
    fn unavailable_without_feature() {
        assert!(matches!(
            WgpuBackend::new(),
            Err(Error::BackendUnavailable(_))
        ));
    }
}

/// GPU smoke test — requires a real adapter, so it only builds under
/// `gpu-wgpu` and is skipped by the default workspace test run. Run with:
/// `cargo test -p tomoxide-wgpu --features gpu-wgpu`.
#[cfg(all(test, feature = "gpu-wgpu"))]
mod gpu_tests {
    use super::*;
    use wgpu::util::DeviceExt;

    /// Trivial in-place doubling kernel — exercises the full compute path
    /// (device → shader module → storage buffer → dispatch → readback).
    const DOUBLE_WGSL: &str = r#"
@group(0) @binding(0) var<storage, read_write> data: array<f32>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
    let i = gid.x;
    if (i >= arrayLength(&data)) { return; }
    data[i] = data[i] * 2.0;
}
"#;

    #[test]
    fn device_init_and_compute_roundtrip() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let host: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let n = host.len();
        let bytes = (n * std::mem::size_of::<f32>()) as wgpu::BufferAddress;

        let storage = be
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("data"),
                contents: bytemuck::cast_slice(&host),
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            });
        let staging = be.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("staging"),
            size: bytes,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let module = be
            .device
            .create_shader_module(wgpu::ShaderModuleDescriptor {
                label: Some("double"),
                source: wgpu::ShaderSource::Wgsl(DOUBLE_WGSL.into()),
            });
        let pipeline = be
            .device
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("double"),
                layout: None,
                module: &module,
                entry_point: Some("main"),
                compilation_options: wgpu::PipelineCompilationOptions::default(),
                cache: None,
            });
        let bind_group = be.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout: &pipeline.get_bind_group_layout(0),
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: storage.as_entire_binding(),
            }],
        });
        let mut enc = be
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        {
            let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: None,
                timestamp_writes: None,
            });
            pass.set_pipeline(&pipeline);
            pass.set_bind_group(0, &bind_group, &[]);
            pass.dispatch_workgroups((n as u32).div_ceil(64), 1, 1);
        }
        enc.copy_buffer_to_buffer(&storage, 0, &staging, 0, bytes);
        be.queue.submit(Some(enc.finish()));

        let slice = staging.slice(..);
        let (tx, rx) = std::sync::mpsc::channel();
        slice.map_async(wgpu::MapMode::Read, move |r| {
            let _ = tx.send(r);
        });
        be.device
            .poll(wgpu::PollType::wait_indefinitely())
            .expect("wgpu device poll");
        rx.recv().unwrap().unwrap();
        let out: Vec<f32> = {
            let view = slice.get_mapped_range().expect("mapped range");
            bytemuck::cast_slice(&view).to_vec()
        };
        staging.unmap();

        assert_eq!(out, vec![2.0, 4.0, 6.0, 8.0, 10.0]);
    }

    // --- Elementwise capability: parity vs the CPU backend ------------------
    // GPU f32 transcendentals/divisions differ from libm by a few ULP, so the
    // bar is a small relative+absolute tolerance, not bit-for-bit.
    use crate::backend::Backend;
    use crate::data::{Frames, Layout, Tomo, Volume};
    use crate::dtype::Complex32;
    use crate::geometry::{Angles, Center, Geometry};
    use ndarray::Array3;

    /// Assert two flat f32 sequences agree within a relative+absolute tolerance.
    fn assert_close(gpu: &[f32], cpu: &[f32], rtol: f32, atol: f32) {
        assert_eq!(gpu.len(), cpu.len(), "length mismatch");
        for (i, (&g, &c)) in gpu.iter().zip(cpu.iter()).enumerate() {
            let tol = atol + rtol * c.abs();
            assert!(
                (g - c).abs() <= tol,
                "index {i}: gpu={g} cpu={c} |Δ|={} > tol={tol}",
                (g - c).abs()
            );
        }
    }

    fn ramp_tomo(np: usize, nr: usize, nc: usize, layout: Layout) -> Tomo<f32> {
        let n = np * nr * nc;
        let data: Vec<f32> = (0..n).map(|k| 0.5 + k as f32 * 0.01).collect();
        Tomo::new(Array3::from_shape_vec((np, nr, nc), data).unwrap(), layout)
    }

    #[test]
    fn minus_log_matches_cpu() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;

        let base = ramp_tomo(3, 4, 5, Layout::Projection);
        let mut g = base.clone();
        let mut c = base.clone();
        be.elementwise().unwrap().minus_log(&mut g).unwrap();
        cpu.elementwise().unwrap().minus_log(&mut c).unwrap();

        assert_close(
            g.array.as_slice().unwrap(),
            c.array.as_slice().unwrap(),
            1e-5,
            1e-6,
        );
    }

    #[test]
    fn minus_log_scrubs_nonfinite_like_cpu() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;

        // Values at/below the 1e-6 clamp drive -ln(...) large but finite; the
        // clamp floor itself (1e-6) gives the max magnitude. Mix in zeros and
        // negatives to exercise the clamp path on both backends identically.
        let data = vec![0.0f32, -3.0, 1e-9, 1.0, 2.5, 1e-7];
        let base = Tomo::new(
            Array3::from_shape_vec((1, 2, 3), data).unwrap(),
            Layout::Projection,
        );
        let mut g = base.clone();
        let mut c = base.clone();
        be.elementwise().unwrap().minus_log(&mut g).unwrap();
        cpu.elementwise().unwrap().minus_log(&mut c).unwrap();

        assert_close(
            g.array.as_slice().unwrap(),
            c.array.as_slice().unwrap(),
            1e-5,
            1e-5,
        );
    }

    #[test]
    fn darkflat_matches_cpu_projection_and_sinogram() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;

        // 2 dark + 3 flat frames over a 4×5 plane; data is 3 projections.
        let dark = Frames::new(
            Array3::from_shape_vec(
                (2, 4, 5),
                (0..2 * 4 * 5).map(|k| 0.1 + k as f32 * 0.002).collect(),
            )
            .unwrap(),
        );
        let flat = Frames::new(
            Array3::from_shape_vec(
                (3, 4, 5),
                (0..3 * 4 * 5).map(|k| 1.0 + k as f32 * 0.003).collect(),
            )
            .unwrap(),
        );

        // Build a projection-layout base (3 projections over the 4×5 detector
        // plane) and derive the sinogram case by converting it, so the detector
        // plane stays consistent with the dark/flat planes in both layouts.
        let proj_base = ramp_tomo(3, 4, 5, Layout::Projection);
        for layout in [Layout::Projection, Layout::Sinogram] {
            let base = proj_base.to_layout(layout);
            let mut g = base.clone();
            let mut c = base.clone();
            be.elementwise()
                .unwrap()
                .darkflat(&mut g, &flat, &dark)
                .unwrap();
            cpu.elementwise()
                .unwrap()
                .darkflat(&mut c, &flat, &dark)
                .unwrap();

            assert_eq!(g.layout, c.layout, "layout preserved");
            assert_close(
                g.array.as_standard_layout().as_slice().unwrap(),
                c.array.as_standard_layout().as_slice().unwrap(),
                1e-5,
                1e-6,
            );
        }
    }

    // --- FilteredBackproject capability: parity vs the CPU backend ----------
    #[test]
    fn backproject_matches_cpu() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;

        // [row, angle, col] sinogram with structure: 2 rows, 6 angles, 8 cols.
        let (nz, nang, ncols) = (2usize, 6usize, 8usize);
        let sarr = Array3::from_shape_fn((nz, nang, ncols), |(z, a, col)| {
            ((z * 3 + a) as f32 * 0.3 + col as f32 * 0.17).sin() + 0.05 * col as f32
        });
        let s = Tomo::new(sarr, Layout::Sinogram);
        let mut geom = Geometry::parallel(
            Angles::uniform(nang, 0.0, std::f32::consts::PI),
            ncols,
            nz,
            1.0,
        );

        // Reconstruct an interior 4×4 ROI inside the 8-wide detector (the
        // physically sensible field-of-view): every ray maps to t ∈ ~[0.7, 6.8],
        // safely away from the detector edges 0 and ncols−1. That keeps GPU/CPU
        // divergence to pure multiply-accumulate rounding. A full-detector-width
        // grid lets corner rays graze the hard inclusion cutoff (t≈0 or
        // t≈ncols−1), where a sub-ULP t difference flips a whole edge sample —
        // a legitimate discontinuity, not a rounding error.
        let recon = 4usize;
        // Exercise both center buffer paths: scalar (default 4.0) and per-row.
        for center in [geom.center.clone(), Center::PerRow(vec![3.5, 4.0])] {
            geom.center = center;
            let mut g = Volume::new(Array3::<f32>::zeros((nz, recon, recon)));
            let mut c = Volume::new(Array3::<f32>::zeros((nz, recon, recon)));
            be.backprojector()
                .unwrap()
                .backproject(&s, &geom, &mut g)
                .unwrap();
            cpu.backprojector()
                .unwrap()
                .backproject(&s, &geom, &mut c)
                .unwrap();

            assert_eq!(g.array.dim(), c.array.dim());
            assert_close(
                g.array.as_slice().unwrap(),
                c.array.as_slice().unwrap(),
                1e-4,
                1e-5,
            );
        }
    }

    // --- ForwardProject capability: parity vs the CPU backend --------------
    #[test]
    fn forward_project_matches_cpu() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;

        // Interior 4×4×2 volume projected onto an 8-wide / 6-angle detector. The
        // small grid keeps every pixel's t inside (0, ncols−1), away from the
        // hard splat-inclusion cutoff (same edge-grazing hazard as backproject).
        let (nz, ny, nx) = (2usize, 4usize, 4usize);
        let (nang, ncols) = (6usize, 8usize);
        let varr = Array3::from_shape_fn((nz, ny, nx), |(z, y, x)| {
            0.3 + z as f32 * 0.5 + y as f32 * 0.11 + x as f32 * 0.07
        });
        let v = Volume::new(varr);
        let mut geom = Geometry::parallel(
            Angles::uniform(nang, 0.0, std::f32::consts::PI),
            ncols,
            nz,
            1.0,
        );

        // Exercise both center buffer paths: scalar (default 4.0) and per-row.
        for center in [geom.center.clone(), Center::PerRow(vec![3.5, 4.0])] {
            geom.center = center;
            let mut g = Tomo::new(Array3::<f32>::zeros((nz, nang, ncols)), Layout::Sinogram);
            let mut c = Tomo::new(Array3::<f32>::zeros((nz, nang, ncols)), Layout::Sinogram);
            be.projector().unwrap().project(&v, &geom, &mut g).unwrap();
            cpu.projector().unwrap().project(&v, &geom, &mut c).unwrap();

            assert_eq!(g.layout, Layout::Sinogram);
            assert_eq!(g.array.dim(), c.array.dim());
            assert_close(
                g.array.as_slice().unwrap(),
                c.array.as_slice().unwrap(),
                1e-4,
                1e-5,
            );
        }
    }

    // --- RankFilter capability: BIT-EXACT parity vs the CPU backend --------
    // Pure gather + order statistic + one subtraction → no rounding divergence.
    #[test]
    fn median3d_and_remove_outlier3d_match_cpu_bit_exact() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;

        // Volume with a planted zinger spike so the dezinger threshold path hits.
        let (dz, dy, dx) = (4usize, 5usize, 6usize);
        let mut varr = Array3::from_shape_fn((dz, dy, dx), |(z, y, x)| {
            ((z * 7 + y * 3 + x) as f32 * 0.13).sin() + 0.2 * x as f32
        });
        varr[[2, 2, 3]] = 99.0; // outlier

        // median3d (threshold 0): plain local median everywhere.
        let mut g = Volume::new(varr.clone());
        let mut c = Volume::new(varr.clone());
        be.rank_filter().unwrap().median3d(&mut g, 3).unwrap();
        cpu.rank_filter().unwrap().median3d(&mut c, 3).unwrap();
        assert_eq!(g.array, c.array, "median3d not bit-exact");

        // remove_outlier3d (threshold 1.0): only the spike is replaced.
        let mut g = Tomo::new(varr.clone(), Layout::Projection);
        let mut c = Tomo::new(varr.clone(), Layout::Projection);
        be.rank_filter()
            .unwrap()
            .remove_outlier3d(&mut g, 1.0, 3)
            .unwrap();
        cpu.rank_filter()
            .unwrap()
            .remove_outlier3d(&mut c, 1.0, 3)
            .unwrap();
        assert_eq!(g.array, c.array, "remove_outlier3d not bit-exact");
    }

    #[test]
    fn median3d_rejects_window_over_cap() {
        let be = WgpuBackend::new().expect("wgpu device init");
        // size 9 → diameter 9 → 729 voxels > the 343 GPU window cap.
        let mut vol = Volume::new(Array3::<f32>::zeros((9, 9, 9)));
        assert!(matches!(
            be.rank_filter().unwrap().median3d(&mut vol, 9),
            Err(crate::error::Error::InvalidParam(_))
        ));
    }

    // --- Fft capability: tolerance parity vs CPU (rustfft) + roundtrip -----
    fn assert_complex_close(g: &[Complex32], c: &[Complex32], tol: f32, what: &str) {
        assert_eq!(g.len(), c.len());
        for (i, (a, b)) in g.iter().zip(c.iter()).enumerate() {
            assert!(
                (a.re - b.re).abs() <= tol && (a.im - b.im).abs() <= tol,
                "{what} index {i}: gpu={a:?} cpu={b:?}"
            );
        }
    }

    #[test]
    fn fft_1d_roundtrips_and_matches_cpu() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;
        let (len, batch) = (8usize, 3usize);
        let base: Vec<Complex32> = (0..len * batch)
            .map(|k| Complex32::new((k as f32 * 0.3).sin(), (k as f32 * 0.17).cos()))
            .collect();

        // Forward parity vs rustfft.
        let mut g = base.clone();
        let mut c = base.clone();
        be.fft().unwrap().fft_1d(&mut g, len, batch, false).unwrap();
        cpu.fft()
            .unwrap()
            .fft_1d(&mut c, len, batch, false)
            .unwrap();
        assert_complex_close(&g, &c, 1e-3, "fft_1d forward");

        // Roundtrip: ifft(fft(x)) == x.
        be.fft().unwrap().fft_1d(&mut g, len, batch, true).unwrap();
        assert_complex_close(&g, &base, 1e-3, "fft_1d roundtrip");
    }

    #[test]
    fn fft_2d_roundtrips_and_matches_cpu() {
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;
        let (rows, cols, batch) = (4usize, 8usize, 2usize);
        let base: Vec<Complex32> = (0..rows * cols * batch)
            .map(|k| Complex32::new((k as f32 * 0.21).sin(), (k as f32 * 0.09).cos()))
            .collect();

        let mut g = base.clone();
        let mut c = base.clone();
        be.fft()
            .unwrap()
            .fft_2d(&mut g, rows, cols, batch, false)
            .unwrap();
        cpu.fft()
            .unwrap()
            .fft_2d(&mut c, rows, cols, batch, false)
            .unwrap();
        assert_complex_close(&g, &c, 2e-3, "fft_2d forward");

        be.fft()
            .unwrap()
            .fft_2d(&mut g, rows, cols, batch, true)
            .unwrap();
        assert_complex_close(&g, &base, 2e-3, "fft_2d roundtrip");
    }

    #[test]
    fn fft_2d_non_power_of_two_matches_cpu() {
        // Non-power-of-two 2-D shapes take the separable fallback: a 1-D pass
        // along cols (radix-2 or Bluestein), a host transpose, a 1-D pass along
        // rows, and a transpose back. Each axis may itself be Bluestein, so this
        // exercises pow2×non-pow2 (4×6), non-pow2×pow2 (6×4), and non-pow2×
        // non-pow2 (3×5, 6×10). Must match rustfft's 2-D transform and round-trip.
        // Observed on Metal: forward rel ≈ 1e-6, roundtrip ≈ 1e-5.
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;
        for &(rows, cols, batch) in &[(4usize, 6usize, 2usize), (6, 4, 1), (3, 5, 2), (6, 10, 1)] {
            let base: Vec<Complex32> = (0..rows * cols * batch)
                .map(|k| Complex32::new((k as f32 * 0.21).sin(), (k as f32 * 0.09).cos()))
                .collect();
            let mut g = base.clone();
            let mut c = base.clone();
            be.fft()
                .unwrap()
                .fft_2d(&mut g, rows, cols, batch, false)
                .unwrap();
            cpu.fft()
                .unwrap()
                .fft_2d(&mut c, rows, cols, batch, false)
                .unwrap();
            let peak = c.iter().map(|z| z.norm()).fold(0.0f32, f32::max).max(1.0);
            let err = g
                .iter()
                .zip(&c)
                .map(|(a, b)| (a - b).norm())
                .fold(0.0f32, f32::max);
            be.fft()
                .unwrap()
                .fft_2d(&mut g, rows, cols, batch, true)
                .unwrap();
            let rt = g
                .iter()
                .zip(&base)
                .map(|(a, b)| (a - b).norm())
                .fold(0.0f32, f32::max);
            eprintln!(
                "fft_2d {rows}×{cols}×{batch}: peak={peak:.3}, fwd rel={:.3e}, roundtrip max|Δ|={rt:.3e}",
                err / peak
            );
            assert!(
                err <= 1e-5 * peak,
                "fft_2d forward {rows}×{cols}: max|Δ|={err:.3e} > {:.3e}",
                1e-5 * peak
            );
            assert!(
                rt <= 1e-4,
                "fft_2d roundtrip {rows}×{cols}: max|Δ|={rt:.3e}"
            );
        }
    }

    #[test]
    fn fft_1d_bluestein_matches_cpu_for_non_power_of_two() {
        // Non-power-of-two lengths run the Bluestein chirp-z path on the GPU
        // (3 radix-2 FFTs of length m = next_pow2(2n−1) + chirp multiplies), so
        // they must match rustfft (which also uses Bluestein internally) and
        // round-trip cleanly. Forward error is taken relative to the spectrum's
        // peak magnitude. Observed on Metal: rel ≈ 1e-7, roundtrip ≈ 1e-6; the
        // 1e-5·peak / 1e-4 bars leave ~50× headroom yet are far tighter than a
        // chirp/index bug (wrong sign, broken kernel symmetry) would produce.
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;
        for &(len, batch) in &[(3usize, 2usize), (5, 1), (6, 3), (12, 2), (100, 1)] {
            let base: Vec<Complex32> = (0..len * batch)
                .map(|k| Complex32::new((k as f32 * 0.3).sin(), (k as f32 * 0.17).cos()))
                .collect();
            let mut g = base.clone();
            let mut c = base.clone();
            be.fft().unwrap().fft_1d(&mut g, len, batch, false).unwrap();
            cpu.fft()
                .unwrap()
                .fft_1d(&mut c, len, batch, false)
                .unwrap();
            let peak = c.iter().map(|z| z.norm()).fold(0.0f32, f32::max).max(1.0);
            let err = g
                .iter()
                .zip(&c)
                .map(|(a, b)| (a - b).norm())
                .fold(0.0f32, f32::max);
            // Round-trip: ifft(fft(x)) == x.
            be.fft().unwrap().fft_1d(&mut g, len, batch, true).unwrap();
            let rt = g
                .iter()
                .zip(&base)
                .map(|(a, b)| (a - b).norm())
                .fold(0.0f32, f32::max);
            eprintln!(
                "bluestein len={len}: peak={peak:.3}, fwd rel={:.3e}, roundtrip max|Δ|={rt:.3e}",
                err / peak
            );
            assert!(
                err <= 1e-5 * peak,
                "bluestein forward len={len}: max|Δ|={err:.3e} > {:.3e}",
                1e-5 * peak
            );
            assert!(rt <= 1e-4, "bluestein roundtrip len={len}: max|Δ|={rt:.3e}");
        }
    }

    // --- FBP filter apply: parity vs the CPU backend ------------------------
    // The whole pad → FFT → ×filter → IFFT → crop pipeline runs on the GPU; the
    // bar is a tolerance (f32 twiddles + accumulation order), not bit-exact.

    #[test]
    fn fbp_filter_apply_matches_cpu() {
        use crate::params::FilterName;
        let be = WgpuBackend::new().expect("wgpu device init");
        let cpu = crate::cpu::CpuBackend;

        // 6 detector lanes (3 angles × 2 rows) of width 16; the ramp filter
        // zero-pads each lane to 32 before transforming.
        let base = ramp_tomo(3, 2, 16, Layout::Sinogram);
        let geom = Geometry::parallel(Angles::uniform(3, 0.0, std::f32::consts::PI), 16, 2, 1.0);

        // CPU and GPU build the identical filter (shared make_fbp_filter).
        let filter = cpu
            .fbp_filter()
            .unwrap()
            .make_filter(FilterName::Ramp, 16)
            .unwrap();
        let gfilter = be
            .fbp_filter()
            .unwrap()
            .make_filter(FilterName::Ramp, 16)
            .unwrap();
        assert_eq!(filter, gfilter, "make_filter differs between backends");

        let mut g = base.clone();
        let mut c = base.clone();
        be.fbp_filter()
            .unwrap()
            .apply(&mut g, &filter, &geom)
            .unwrap();
        cpu.fbp_filter()
            .unwrap()
            .apply(&mut c, &filter, &geom)
            .unwrap();

        assert_close(
            g.array.as_slice().unwrap(),
            c.array.as_slice().unwrap(),
            2e-3,
            2e-3,
        );
    }

    #[test]
    fn fbp_filter_apply_rejects_non_power_of_two() {
        let be = WgpuBackend::new().expect("wgpu device init");
        // pad = 24 is ≥ ncols (8) but not a power of two: the radix-2 GPU FFT
        // cannot transform it, so apply must error rather than corrupt data.
        let mut sino = ramp_tomo(1, 1, 8, Layout::Sinogram);
        let filter = vec![1.0f32; 24];
        let geom = Geometry::parallel(Angles::uniform(1, 0.0, std::f32::consts::PI), 8, 1, 1.0);
        assert!(matches!(
            be.fbp_filter().unwrap().apply(&mut sino, &filter, &geom),
            Err(crate::error::Error::InvalidParam(_))
        ));
    }
}