tract-core 0.23.9

Tiny, no-nonsense, self contained, TensorFlow and ONNX inference
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
#![allow(dead_code)]

use std::ops::AddAssign;

use crate::internal::*;
use crate::ops::cnn::padding::ComputedPaddedDim;
use crate::ops::cnn::{KernelFormat, PoolSpec};
use crate::ops::nn::DataShape;
use tract_ndarray::prelude::*;
use tract_num_traits::Float;

/*
(N) (G) C   H   W
Reshaped Input (N) (G) C   HW
Kernel         (N) (G) OHkWk   C
Gemm           (N) (G) OHkWk   HW              (Gemm: m: OHkWk k:C n:HW)
DeconvSum      (N) (G) O   H'  W'
*/

// f32, ndarray::indices in order

/// Scatters a deconvolution's `(N) C HkWk HW` gemm into its output and adds the bias.
///
/// The bias has the output's rank and broadcasts to it, unit or full on each axis: a
/// scalar or per-channel bias is never materialised to the output shape when every output
/// pixel receives exactly one tap.
#[derive(Clone, Debug, new, Hash, PartialEq, Eq)]
pub struct DeconvSum {
    pub pool_spec: PoolSpec,
    pub kernel_format: KernelFormat,
    /// shape of the deconvolution input
    pub input_shape: ShapeFact,
    pub adjustments: TVec<usize>,
    pub group: usize,
}

impl Op for DeconvSum {
    fn name(&self) -> StaticName {
        "DeconvSum".into()
    }

    op_as_typed_op!();
}

impl EvalOp for DeconvSum {
    op_out_of_plan!();

    fn eval(&self, ctx: &EvalContext, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
        self.eval_with_values(inputs, ctx.symbols)
    }
}

impl DeconvSum {
    fn eval_with_values(
        &self,
        inputs: TVec<TValue>,
        values: &SymbolValues,
    ) -> TractResult<TVec<TValue>> {
        let (gemm, bias) = args_2!(inputs);
        let input_shape = self.input_shape.eval_to_usize(values)?.into_owned();
        let input_shape = self.pool_spec.data_format.shape(input_shape)?;
        let output_shape =
            super::output_shape(&self.pool_spec, &input_shape.shape, &self.adjustments)?;
        let output_shape = self.pool_spec.data_format.shape(output_shape)?;
        let spatial_output_details = self.pool_spec.padding.compute_for_deconv(
            input_shape.hw_dims(),
            &self.pool_spec.kernel_shape,
            &self.pool_spec.dilations(),
            &self.pool_spec.strides(),
            &self.adjustments,
        )?;
        let hw = *gemm.shape().last().unwrap();
        let n = *output_shape.n().unwrap_or(&1);
        let n_o_hkwk_hw = gemm.into_tensor().into_shape(&[
            n,
            *output_shape.c(),
            self.pool_spec.kernel_shape.iter().product(),
            hw,
        ])?;
        let per_channel_bias = bias.len() == 1 || bias.len() == *output_shape.c();
        let geometry =
            non_overlapping_geometry(self, &input_shape, &output_shape, &spatial_output_details);
        let mut tensor = if let (Some((input_hw, strides)), true) = (geometry, per_channel_bias) {
            let mut tensor = Tensor::zero_dt(n_o_hkwk_hw.datum_type(), &output_shape.shape)?;
            if !self.pool_spec.data_format.has_n() {
                tensor.insert_axis(0)?;
            }
            eval_non_overlapping(
                &n_o_hkwk_hw,
                &bias,
                &mut tensor,
                *output_shape.c(),
                input_hw,
                strides,
                self.pool_spec.data_format.c_is_last(),
            )?;
            tensor
        } else {
            let mut tensor = if bias.shape() == &*output_shape.shape {
                bias.into_tensor()
            } else {
                bias.broadcast_to_shape(&output_shape.shape)?
            };
            if !self.pool_spec.data_format.has_n() {
                tensor.insert_axis(0)?;
            }
            eval(
                self,
                &input_shape,
                &output_shape,
                &spatial_output_details,
                &n_o_hkwk_hw,
                &mut tensor,
            )?;
            tensor
        };
        if !self.pool_spec.data_format.has_n() {
            tensor.remove_axis(0)?;
        }
        Ok(tvec!(tensor.into_tvalue()))
    }
}

impl TypedOp for DeconvSum {
    fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
        ensure!(inputs.len() == 2);
        let shape = super::output_shape(&self.pool_spec, &self.input_shape, &self.adjustments)?;
        ensure!(inputs[1].rank() == shape.len());
        ensure!(
            inputs[1].shape.iter().zip(shape.iter()).all(|(b, o)| b.is_one() || b == o),
            "DeconvSum bias {:?} does not broadcast to output {:?}",
            inputs[1].shape,
            shape
        );
        Ok(tvec!(inputs[0].datum_type.fact(shape)))
    }

    fn set_symbols(
        &self,
        _source: &TypedModel,
        node: &TypedNode,
        target: &mut TypedModel,
        mapping: &HashMap<OutletId, OutletId>,
        subs: &HashMap<Symbol, TDim>,
    ) -> TractResult<TVec<OutletId>> {
        target.wire_node(
            &node.name,
            Self { input_shape: self.input_shape.substitute(subs)?.into_owned(), ..self.clone() },
            &[mapping[&node.inputs[0]], mapping[&node.inputs[1]]],
        )
    }

    as_op!();
}

/// The input extents and strides, rank 1 padded to rank 2 with a leading unit axis, of a
/// deconvolution whose kernel taps never share an output pixel: on every spatial axis the
/// kernel extent equals the stride, with no dilation, no leading padding and an output
/// exactly `input * stride` long. Each output pixel is then one gemm value plus bias.
fn non_overlapping_geometry(
    op: &DeconvSum,
    input_shape: &DataShape,
    output_shape: &DataShape,
    spatial_output_details: &[ComputedPaddedDim<usize>],
) -> Option<([usize; 2], [usize; 2])> {
    let rank = input_shape.hw_rank();
    if rank == 0 || rank > 2 {
        return None;
    }
    let strides = op.pool_spec.strides();
    let dilations = op.pool_spec.dilations();
    let input_hw = input_shape.hw_dims();
    for ax in 0..rank {
        if op.pool_spec.kernel_shape[ax] != strides[ax]
            || dilations[ax] != 1
            || spatial_output_details[ax].pad_before != 0
            || output_shape.hw_dims()[ax] != input_hw[ax] * strides[ax]
        {
            return None;
        }
    }
    Some(if rank == 1 {
        ([1, input_hw[0]], [1, strides[0]])
    } else {
        ([input_hw[0], input_hw[1]], [strides[0], strides[1]])
    })
}

/// Write the output of a deconvolution [`non_overlapping_geometry`] accepts from its
/// `N C HkWk HW` gemm and a bias of one value or one per channel, each pixel once.
/// `output` carries the N axis and is fully overwritten.
fn eval_non_overlapping(
    gemm: &Tensor,
    bias: &Tensor,
    output: &mut Tensor,
    channels: usize,
    input_hw: [usize; 2],
    strides: [usize; 2],
    c_is_last: bool,
) -> TractResult<()> {
    ensure!(bias.len() == 1 || bias.len() == channels);
    let dt = output.datum_type();
    dispatch_floatlike!(write_non_overlapping(dt)(
        gemm,
        bias,
        output,
        channels,
        input_hw,
        strides,
        c_is_last,
        |a, b| a + b
    ))
}

#[allow(clippy::too_many_arguments)]
fn write_non_overlapping<T: Datum + Copy>(
    gemm: &Tensor,
    bias: &Tensor,
    output: &mut Tensor,
    channels: usize,
    input_hw: [usize; 2],
    strides: [usize; 2],
    c_is_last: bool,
    add: impl Fn(T, T) -> T + Copy,
) -> TractResult<()> {
    let gemm_view = gemm.try_as_plain_ram()?;
    let gemm = gemm_view.as_slice::<T>()?;
    let bias_view = bias.try_as_plain_ram()?;
    let bias = bias_view.as_slice::<T>()?;
    let mut output_view = output.try_as_plain_ram_mut()?;
    let output = output_view.as_slice_mut::<T>()?;
    let bias_at = |c: usize| if bias.len() == 1 { bias[0] } else { bias[c] };
    let [ih, iw] = input_hw;
    let [sh, sw] = strides;
    let (oh, ow) = (ih * sh, iw * sw);
    let plane = ih * iw;
    let taps = sh * sw;
    if c_is_last {
        for (gemm_n, output_n) in gemm
            .chunks_exact(channels * taps * plane)
            .zip(output.chunks_exact_mut(oh * ow * channels))
        {
            for (oy, row) in output_n.chunks_exact_mut(ow * channels).enumerate() {
                let (iy, ky) = (oy / sh, oy % sh);
                for (ox, pixel) in row.chunks_exact_mut(channels).enumerate() {
                    let (ix, kx) = (ox / sw, ox % sw);
                    let tap = (ky * sw + kx) * plane + iy * iw + ix;
                    for (c, out) in pixel.iter_mut().enumerate() {
                        *out = add(gemm_n[c * taps * plane + tap], bias_at(c));
                    }
                }
            }
        }
    } else {
        for (nc, (gemm_nc, output_nc)) in
            gemm.chunks_exact(taps * plane).zip(output.chunks_exact_mut(oh * ow)).enumerate()
        {
            let b = bias_at(nc % channels);
            for (oy, row) in output_nc.chunks_exact_mut(ow).enumerate() {
                let (iy, ky) = (oy / sh, oy % sh);
                let tap_row = |kx: usize| &gemm_nc[(ky * sw + kx) * plane + iy * iw..][..iw];
                if sw == 2 {
                    for (pair, (&even, &odd)) in
                        row.chunks_exact_mut(2).zip(tap_row(0).iter().zip(tap_row(1)))
                    {
                        pair[0] = add(even, b);
                        pair[1] = add(odd, b);
                    }
                } else {
                    for kx in 0..sw {
                        for (out, &g) in row[kx..].iter_mut().step_by(sw).zip(tap_row(kx)) {
                            *out = add(g, b);
                        }
                    }
                }
            }
        }
    }
    Ok(())
}

fn eval(
    op: &DeconvSum,
    input_shape: &DataShape,
    output_shape: &DataShape,
    spatial_output_details: &[ComputedPaddedDim<usize>],
    n_o_hkwk_hw: &Tensor,
    output: &mut Tensor,
) -> TractResult<()> {
    let dt = output.datum_type();
    unsafe {
        #[cfg(target_arch = "aarch64")]
        if dt == f16::datum_type() && tract_linalg::arm64::has_fp16() {
            return eval_t_aarch64fp16::<f16>(
                op,
                input_shape,
                output_shape,
                spatial_output_details,
                n_o_hkwk_hw,
                output,
                |a, b| tract_linalg::arm64::add_f16(a, b),
            );
        }
        dispatch_floatlike!(eval_t_generic(dt)(
            op,
            input_shape,
            output_shape,
            spatial_output_details,
            n_o_hkwk_hw,
            output,
            |a, b| a + b
        ))
    }
}

macro_rules! impl_eval {
        ($(#[$meta: meta])* $suffix: ident) => {
            pastey::paste! {
                $(#[$meta])*
                    unsafe fn [<eval_t_ $suffix>]<T: Datum + Float + Copy + AddAssign<T>>(
                        op: &DeconvSum,
                        input_shape: &DataShape,
                        output_shape: &DataShape,
                        spatial_output_details: &[ComputedPaddedDim<usize>],
                        n_o_hkwk_hw: &Tensor,
                        output: &mut Tensor,
                        add: impl Fn(T, T) -> T + Copy + 'static,
                        ) -> TractResult<()> {
                        let mut output_plain = output.try_as_plain_ram_mut()?;
                        let output = output_plain.to_array_view_mut::<T>()?;
                        let n_o_hkwk_hw: ArrayView4<T> = n_o_hkwk_hw.to_plain_array_view::<T>()?.into_dimensionality()?;
                        match input_shape.hw_rank() {
                            1 => [<main_loop_1d_ $suffix>](
                                op,
                                input_shape,
                                output_shape,
                                spatial_output_details,
                                &n_o_hkwk_hw,
                                &mut output.into_dimensionality().unwrap(),
                                add,
                                ),
                            2 => [<main_loop_2d_ $suffix>](
                                op,
                                input_shape,
                                output_shape,
                                spatial_output_details,
                                &n_o_hkwk_hw,
                                &mut output.into_dimensionality().unwrap(),
                                add,
                                ),
                            3 => [<main_loop_3d_ $suffix>](
                                op,
                                input_shape,
                                output_shape,
                                spatial_output_details,
                                &n_o_hkwk_hw,
                                &mut output.into_dimensionality().unwrap(),
                                add,
                                ),
                            _ => [<main_loop_ $suffix>](
                                op,
                                input_shape,
                                output_shape,
                                spatial_output_details,
                                &n_o_hkwk_hw,
                                &mut output.into_dimensionality().unwrap(),
                                add,
                                ),
                        }
                    }

                pub fn [<main_loop_1d_ $suffix>]<T: Datum + Float>(
                    op: &DeconvSum,
                    input_shape: &DataShape,
                    output_shape: &DataShape,
                    spatial_output_details: &[ComputedPaddedDim<usize>],
                    n_o_hkwk_hw: &ArrayView4<T>,
                    output: &mut ArrayViewMut3<T>,
                    add: impl Fn(T, T) -> T + Copy + 'static,
                    ) -> TractResult<()> {
                    let n = *output_shape.n().unwrap_or(&1);
                    let kernel_len = op.pool_spec.kernel_shape[0];
                    let geo_input_len = input_shape.hw_dims()[0];
                    let geo_output_len = output_shape.hw_dims()[0];
                    let x_stride = op.pool_spec.strides().as_ref()[0];
                    let x_dil = op.pool_spec.dilations().as_ref()[0];
                    let x_pad = spatial_output_details[0].pad_before as isize;
                    for n in 0..n {
                        for o in 0..*output_shape.c() {
                            for kx in 0..kernel_len {
                                for gx in 0..geo_input_len {
                                    let x = (kx * x_dil + gx * x_stride) as isize - x_pad;
                                    if x < 0 || x >= geo_output_len as isize {
                                        continue;
                                    }
                                    let coord = if op.pool_spec.data_format.c_is_last() {
                                        [n, x as usize, o]
                                    } else {
                                        [n, o, x as usize]
                                    };
                                    unsafe {
                                        let value = *n_o_hkwk_hw.uget((n, o, kx, gx));
                                        *output.uget_mut(coord) = add(*output.uget(coord), value);
                                    }
                                }
                            }
                        }
                    }
                    Ok(())
                }

                pub fn [<main_loop_2d_ $suffix>]<T: Datum + Float>(
                    op: &DeconvSum,
                    input_shape: &DataShape,
                    output_shape: &DataShape,
                    spatial_output_details: &[ComputedPaddedDim<usize>],
                    n_o_hkwk_hw: &ArrayView4<T>,
                    output: &mut ArrayViewMut4<T>,
                    add: impl Fn(T, T) -> T + Copy + 'static,
                    ) -> TractResult<()> {
                    let n = *output_shape.n().unwrap_or(&1);
                    let x_stride = op.pool_spec.strides().as_ref()[0];
                    let y_stride = op.pool_spec.strides().as_ref()[1];
                    let x_dil = op.pool_spec.dilations().as_ref()[0];
                    let y_dil = op.pool_spec.dilations().as_ref()[1];
                    let x_pad = spatial_output_details[0].pad_before as isize;
                    let y_pad = spatial_output_details[1].pad_before as isize;
                    let output_c = *output_shape.c();
                    let output_c_stride = *output_shape.c_stride() as isize;
                    let output_x_stride = output_shape.hw_strides()[0] as isize;
                    let output_y_stride = output_shape.hw_strides()[1] as isize;
                    let temp_n_stride = n_o_hkwk_hw.strides()[0];
                    let temp_o_stride = n_o_hkwk_hw.strides()[1];
                    let temp_k_stride = n_o_hkwk_hw.strides()[2];
                    let temp_i_stride = n_o_hkwk_hw.strides()[3];
                    let ox_len = output_shape.hw_dims()[0];
                    let oy_len = output_shape.hw_dims()[1];
                    let ix_len = input_shape.hw_dims()[0];
                    let iy_len = input_shape.hw_dims()[1];
                    let kx_len = op.pool_spec.kernel_shape[0];
                    let ky_len = op.pool_spec.kernel_shape[1];
                    if !op.pool_spec.data_format.c_is_last()
                        && iy_len >= 16
                        && let Some(temp) = n_o_hkwk_hw.as_slice()
                        && let Some(output) = output.as_slice_mut()
                    {
                        for n in 0..n {
                            for o in 0..output_c {
                                let output_base = n * *output_shape.n_stride().unwrap_or(&0)
                                    + o * output_c_stride as usize;
                                let temp_base = n * temp_n_stride as usize + o * temp_o_stride as usize;
                                for kx in 0..kx_len {
                                    let x_base = (kx * x_dil) as isize - x_pad;
                                    let ix_start = ((-x_base).max(0) as usize).div_ceil(x_stride);
                                    let ix_end = ((ox_len as isize - x_base).max(0) as usize).div_ceil(x_stride).min(ix_len);
                                    for ky in 0..ky_len {
                                        let y_base = (ky * y_dil) as isize - y_pad;
                                        let iy_start = ((-y_base).max(0) as usize).div_ceil(y_stride);
                                        let iy_end = ((oy_len as isize - y_base).max(0) as usize).div_ceil(y_stride).min(iy_len);
                                        if iy_start >= iy_end {
                                            continue;
                                        }
                                        for ix in ix_start..ix_end {
                                            let ox = (x_base + (ix * x_stride) as isize) as usize;
                                            let oy = (y_base + (iy_start * y_stride) as isize) as usize;
                                            let input_start = temp_base + (kx * ky_len + ky) * temp_k_stride as usize
                                                + ix * iy_len + iy_start;
                                            let output_start = output_base + ox * output_x_stride as usize + oy;
                                            let input = &temp[input_start..input_start + iy_end - iy_start];
                                            let output = &mut output[output_start..output_start + (input.len() - 1) * y_stride + 1];
                                            if y_stride == 1 {
                                                for (output, &input) in output.iter_mut().zip(input) {
                                                    *output = add(*output, input);
                                                }
                                            } else {
                                                let blocks = (input.len() - 1) / 8;
                                                let (input_head, input_tail) = input.split_at(blocks * 8);
                                                let (output_head, output_tail) = output.split_at_mut(blocks * 8 * y_stride);
                                                for (output, input) in output_head.chunks_exact_mut(8 * y_stride).zip(input_head.chunks_exact(8)) {
                                                    for i in 0..8 {
                                                        output[i * y_stride] = add(output[i * y_stride], input[i]);
                                                    }
                                                }
                                                for (output, &input) in output_tail.iter_mut().step_by(y_stride).zip(input_tail) {
                                                    *output = add(*output, input);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        return Ok(());
                    }
                    unsafe {
                        for n in 0..n {
                            let output = output.as_mut_ptr().add(n * *output_shape.n_stride().unwrap_or(&0));
                            let temp = n_o_hkwk_hw.as_ptr().offset(n as isize * temp_n_stride);
                            for kx in 0..kx_len {
                                let temp = temp.offset((kx * ky_len) as isize * temp_k_stride);
                                for ix in 0..ix_len {
                                    let ox = (kx * x_dil + ix * x_stride) as isize - x_pad;
                                    if ox < 0 || ox >= ox_len as isize {
                                        continue;
                                    }
                                    let temp = temp.offset((ix * iy_len) as isize * temp_i_stride);
                                    let output = output.offset(ox * output_x_stride);
                                    for ky in 0..ky_len {
                                        let temp = temp.offset(ky as isize * temp_k_stride);
                                        let oy = (ky * y_dil) as isize - y_pad;
                                        for iy in 0..iy_len {
                                            let oy = oy + (iy * y_stride) as isize;
                                            if oy < 0 || oy >= oy_len as isize {
                                                continue;
                                            }
                                            let temp = temp.offset(iy as isize * temp_i_stride);
                                            let output = output.offset(oy * output_y_stride);
                                            [<main_loop_2d_inner_ $suffix>](
                                                output_c,
                                                temp,
                                                temp_o_stride,
                                                output,
                                                output_c_stride,
                                                add,
                                                )
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Ok(())
                }

                #[inline(never)]
                #[allow(clippy::erasing_op)]
                #[allow(clippy::identity_op)]
                unsafe fn [<main_loop_2d_inner_ $suffix>]<T: Datum + Float>(
                    output_c: usize,
                    temp: *const T,
                    temp_o_stride: isize,
                    output: *mut T,
                    output_c_stride: isize,
                    add: impl Fn(T, T) -> T + Copy + 'static,
                    ) { unsafe {
                    let mut c = 0;
                    let mut right = temp;
                    let mut left = output;
                    while c + 8 < output_c {
                        let mut left0 = *left.offset(0 * output_c_stride);
                        let mut left1 = *left.offset(1 * output_c_stride);
                        let mut left2 = *left.offset(2 * output_c_stride);
                        let mut left3 = *left.offset(3 * output_c_stride);
                        let mut left4 = *left.offset(4 * output_c_stride);
                        let mut left5 = *left.offset(5 * output_c_stride);
                        let mut left6 = *left.offset(6 * output_c_stride);
                        let mut left7 = *left.offset(7 * output_c_stride);
                        let right0 = *right.offset(0 * temp_o_stride);
                        let right1 = *right.offset(1 * temp_o_stride);
                        let right2 = *right.offset(2 * temp_o_stride);
                        let right3 = *right.offset(3 * temp_o_stride);
                        let right4 = *right.offset(4 * temp_o_stride);
                        let right5 = *right.offset(5 * temp_o_stride);
                        let right6 = *right.offset(6 * temp_o_stride);
                        let right7 = *right.offset(7 * temp_o_stride);
                        left0 = add(left0, right0);
                        left1 = add(left1, right1);
                        left2 = add(left2, right2);
                        left3 = add(left3, right3);
                        left4 = add(left4, right4);
                        left5 = add(left5, right5);
                        left6 = add(left6, right6);
                        left7 = add(left7, right7);
                        *left.offset(0 * output_c_stride) = left0;
                        *left.offset(1 * output_c_stride) = left1;
                        *left.offset(2 * output_c_stride) = left2;
                        *left.offset(3 * output_c_stride) = left3;
                        *left.offset(4 * output_c_stride) = left4;
                        *left.offset(5 * output_c_stride) = left5;
                        *left.offset(6 * output_c_stride) = left6;
                        *left.offset(7 * output_c_stride) = left7;
                        c += 8;
                        left = left.offset(8 * output_c_stride);
                        right = right.offset(8 * temp_o_stride);
                    }
                    for c in c..output_c {
                        let value = *temp.offset(c as isize * temp_o_stride);
                        let ptr = output.offset(c as isize * output_c_stride);
                        *ptr = add(*ptr, value);
                    }
                }}

                pub fn [<main_loop_3d_ $suffix>]<T: Datum + Float>(
                    op: &DeconvSum,
                    input_shape: &DataShape,
                    output_shape: &DataShape,
                    spatial_output_details: &[ComputedPaddedDim<usize>],
                    n_o_hkwk_hw: &ArrayView4<T>,
                    output: &mut ArrayViewMut5<T>,
                    add: impl Fn(T, T) -> T + Copy + 'static,
                    ) -> TractResult<()> {
                    let n = *output_shape.n().unwrap_or(&1);
                    let kernel_shape: [usize; 3] =
                        [op.pool_spec.kernel_shape[0], op.pool_spec.kernel_shape[1], op.pool_spec.kernel_shape[2]];
                    let geo_input_shape: [usize; 3] =
                        [input_shape.hw_dims()[0], input_shape.hw_dims()[1], input_shape.hw_dims()[2]];
                    let geo_output_shape: [usize; 3] =
                        [output_shape.hw_dims()[0], output_shape.hw_dims()[1], output_shape.hw_dims()[2]];
                    let x_stride = op.pool_spec.strides().as_ref()[0];
                    let y_stride = op.pool_spec.strides().as_ref()[1];
                    let z_stride = op.pool_spec.strides().as_ref()[2];
                    let x_dil = op.pool_spec.dilations().as_ref()[0];
                    let y_dil = op.pool_spec.dilations().as_ref()[1];
                    let z_dil = op.pool_spec.dilations().as_ref()[2];
                    let x_pad = spatial_output_details[0].pad_before as isize;
                    let y_pad = spatial_output_details[1].pad_before as isize;
                    let z_pad = spatial_output_details[2].pad_before as isize;
                    for n in 0..n {
                        for o in 0..*output_shape.c() {
                            for (kix, (kx, ky, kz)) in tract_ndarray::indices(kernel_shape).into_iter().enumerate()
                            {
                                for (gix, (gx, gy, gz)) in
                                    tract_ndarray::indices(geo_input_shape).into_iter().enumerate()
                                    {
                                        let x = (kx * x_dil + gx * x_stride) as isize - x_pad;
                                        let y = (ky * y_dil + gy * y_stride) as isize - y_pad;
                                        let z = (kz * z_dil + gz * z_stride) as isize - z_pad;
                                        if x < 0
                                            || y < 0
                                                || z < 0
                                                || x >= geo_output_shape[0] as isize
                                                || y >= geo_output_shape[1] as isize
                                                || z >= geo_output_shape[2] as isize
                                                {
                                                    continue;
                                                }
                                        let coord = if op.pool_spec.data_format.c_is_last() {
                                            [n, x as usize, y as usize, z as usize, o]
                                        } else {
                                            [n, o, x as usize, y as usize, z as usize]
                                        };
                                        unsafe {
                                            let value = *n_o_hkwk_hw.uget((n, o, kix, gix));
                                            *output.uget_mut(coord) = add(*output.uget(coord), value);
                                        }
                                    }
                            }
                        }
                    }
                    Ok(())
                }

                pub fn [<main_loop_ $suffix>]<T: Datum + Float>(
                    op: &DeconvSum,
                    input_shape: &DataShape,
                    output_shape: &DataShape,
                    spatial_output_details: &[ComputedPaddedDim<usize>],
                    n_o_hkwk_hw: &ArrayView4<T>,
                    output: &mut ArrayViewMutD<T>,
                    add: impl Fn(T, T) -> T + Copy + 'static,
                    ) -> TractResult<()> {
                    let n = *output_shape.n().unwrap_or(&1);
                    let strides = op.pool_spec.strides();
                    let dilations = op.pool_spec.dilations();
                    for n in 0..n {
                        for o in 0..*output_shape.c() {
                            for (kix, kcoords) in
                                tract_ndarray::indices(&*op.pool_spec.kernel_shape).into_iter().enumerate()
                                {
                                    for (gix, gcoords) in
                                        tract_ndarray::indices(input_shape.hw_dims()).into_iter().enumerate()
                                        {
                                            // h' = stride * hg + dil * hk
                                            let ocoord: TVec<isize> = tract_itertools::izip!(
                                                kcoords.slice(),
                                                gcoords.slice(),
                                                strides.as_ref(),
                                                dilations.as_ref(),
                                                spatial_output_details
                                                )
                                                .map(|(k, g, s, d, details)| {
                                                    (k * d + g * s) as isize - details.pad_before as isize
                                                })
                                            .collect();
                                            if ocoord
                                                .iter()
                                                    .zip(output_shape.hw_dims().iter())
                                                    .all(|(x, dim)| *x >= 0 && (*x as usize) < *dim)
                                                    {
                                                        let ocoord = ocoord.iter().map(|x| *x as usize).collect::<TVec<_>>();
                                                        let ocoord = op.pool_spec.data_format.with_n().from_n_c_hw(n, o, ocoord)?;
                                                        let value = n_o_hkwk_hw[(n, o, kix, gix)];
                                                        output[&*ocoord.shape] = add(output[&*ocoord.shape], value)
                                                    }
                                        }
                                }
                        }
                    }
                    Ok(())
                }
            }
        }
    }

impl_eval!(generic);
impl_eval! {
#[target_feature(enable = "fp16")]
#[cfg(target_arch = "aarch64")]
        aarch64fp16
    }

crate::declare_knob!(
    scopable TRACT_DISABLE_DEPTHWISE_DECONV,
    bool,
    false,
    "Disable the fused depthwise-deconvolution path, falling back to einsum + DeconvSum."
);

/// Fused depthwise deconvolution: `kernel * input` accumulated straight into the
/// output, instead of `EinSum` materialising `[N, C, HkWk, HW]` for `DeconvSum` to
/// scatter.
///
/// For a depthwise ConvTranspose the einsum's contraction dim is 1, so it degenerates
/// to an outer product whose result is larger than either operand — and most of it is
/// discarded: GTCRN materialises `[1, 16, 9, 363]` (3267 products per channel) to
/// produce `[1, 16, 1, 33]` (33 per channel). Roughly 91% of those products fall
/// outside the output and are thrown away. Folding the multiply inside the bounds test
/// skips them and never allocates the intermediate.
///
/// Inputs: `kernel` `[C, HkWk, 1]`, `input` `[N, C, 1, HW]`, `bias` (output-shaped).
#[derive(Clone, Debug, new, Hash, PartialEq, Eq)]
pub struct DepthwiseDeconv {
    pub pool_spec: PoolSpec,
    pub kernel_format: KernelFormat,
    pub input_shape: ShapeFact,
    pub adjustments: TVec<usize>,
    pub group: usize,
}

impl Op for DepthwiseDeconv {
    fn name(&self) -> StaticName {
        "DepthwiseDeconv".into()
    }
    op_as_typed_op!();
}

impl EvalOp for DepthwiseDeconv {
    op_out_of_plan!();

    fn eval(&self, ctx: &EvalContext, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
        let (kernel, input, bias) = args_3!(inputs);
        let input_shape = self.input_shape.eval_to_usize(ctx.symbols)?.into_owned();
        let input_shape = self.pool_spec.data_format.shape(input_shape)?;
        let output_shape =
            super::output_shape(&self.pool_spec, &input_shape.shape, &self.adjustments)?;
        let output_shape = self.pool_spec.data_format.shape(output_shape)?;
        let spatial_output_details = self.pool_spec.padding.compute_for_deconv(
            input_shape.hw_dims(),
            &self.pool_spec.kernel_shape,
            &self.pool_spec.dilations(),
            &self.pool_spec.strides(),
            &self.adjustments,
        )?;
        let mut tensor = bias.into_tensor();
        if !self.pool_spec.data_format.has_n() {
            tensor.insert_axis(0)?;
        }
        dispatch_floatlike!(Self::eval_t(tensor.datum_type())(
            self,
            &input_shape,
            &output_shape,
            &spatial_output_details,
            &kernel,
            &input,
            &mut tensor
        ))?;
        if !self.pool_spec.data_format.has_n() {
            tensor.remove_axis(0)?;
        }
        Ok(tvec!(tensor.into_tvalue()))
    }
}

impl DepthwiseDeconv {
    /// Rank-2 fused path, mirroring `main_loop_2d`'s pointer walk: the geometry
    /// bounds are tested once per (kx, ix, ky, iy) as there, and the innermost loop
    /// runs over channels, where kernel and input are both contiguous.
    #[allow(clippy::too_many_arguments)]
    unsafe fn eval_t_2d<T: Datum + Float + Copy + AddAssign<T>>(
        &self,
        input_shape: &DataShape,
        output_shape: &DataShape,
        spatial_output_details: &[ComputedPaddedDim<usize>],
        kernel: &[T],
        input: &[T],
        output: &mut Tensor,
    ) -> TractResult<()> {
        let n_batch = *output_shape.n().unwrap_or(&1);
        let x_stride = self.pool_spec.strides().as_ref()[0];
        let y_stride = self.pool_spec.strides().as_ref()[1];
        let x_dil = self.pool_spec.dilations().as_ref()[0];
        let y_dil = self.pool_spec.dilations().as_ref()[1];
        let x_pad = spatial_output_details[0].pad_before as isize;
        let y_pad = spatial_output_details[1].pad_before as isize;
        let output_c = *output_shape.c();
        let output_c_stride = *output_shape.c_stride() as isize;
        let output_x_stride = output_shape.hw_strides()[0] as isize;
        let output_y_stride = output_shape.hw_strides()[1] as isize;
        let ox_len = output_shape.hw_dims()[0];
        let oy_len = output_shape.hw_dims()[1];
        let ix_len = input_shape.hw_dims()[0];
        let iy_len = input_shape.hw_dims()[1];
        let kx_len = self.pool_spec.kernel_shape[0];
        let ky_len = self.pool_spec.kernel_shape[1];
        let kvol = kx_len * ky_len;
        let ihw = ix_len * iy_len;
        let mut output_view = output.to_plain_array_view_mut::<T>()?;
        let out_base = output_view.as_mut_ptr();
        unsafe {
            for n in 0..n_batch {
                let out_n = out_base.add(n * *output_shape.n_stride().unwrap_or(&0));
                let in_n = input.as_ptr().add(n * output_c * ihw);
                for kx in 0..kx_len {
                    for ix in 0..ix_len {
                        let ox = (kx * x_dil + ix * x_stride) as isize - x_pad;
                        if ox < 0 || ox >= ox_len as isize {
                            continue;
                        }
                        let out_x = out_n.offset(ox * output_x_stride);
                        for ky in 0..ky_len {
                            let kix = kx * ky_len + ky;
                            let oy0 = (ky * y_dil) as isize - y_pad;
                            // Channels outer, the contiguous axis inner. The output's
                            // channel stride is the whole spatial plane (12.8 KB on
                            // DeepFilterNet3's [1, 64, 100, 32]), so a channel-innermost
                            // loop walks past L1 on every step; oy is stride-1.
                            for c in 0..output_c {
                                let kv = *kernel.get_unchecked(c * kvol + kix);
                                let in_c = in_n.add(c * ihw + ix * iy_len);
                                let out_c = out_x.offset(c as isize * output_c_stride);
                                for iy in 0..iy_len {
                                    let oy = oy0 + (iy * y_stride) as isize;
                                    if oy < 0 || oy >= oy_len as isize {
                                        continue;
                                    }
                                    *out_c.offset(oy * output_y_stride) += kv * *in_c.add(iy);
                                }
                            }
                        }
                    }
                }
            }
        }
        Ok(())
    }

    fn eval_t<T: Datum + Float + Copy + AddAssign<T>>(
        &self,
        input_shape: &DataShape,
        output_shape: &DataShape,
        spatial_output_details: &[ComputedPaddedDim<usize>],
        kernel: &TValue,
        input: &TValue,
        output: &mut Tensor,
    ) -> TractResult<()> {
        let c = *output_shape.c();
        let hw: usize = input_shape.hw_dims().iter().product();
        let kernel_vol: usize = self.pool_spec.kernel_shape.iter().product();
        let kernel = kernel.to_plain_array_view::<T>()?;
        let kernel = kernel.as_slice().context("depthwise deconv kernel must be contiguous")?;
        let input = input.to_plain_array_view::<T>()?;
        let input = input.as_slice().context("depthwise deconv input must be contiguous")?;
        ensure!(kernel.len() == c * kernel_vol);
        let n = *output_shape.n().unwrap_or(&1);
        ensure!(input.len() == n * c * hw);
        // The wiring only selects this op at rank 2 (see wire_with_deconv_sum); the
        // generic loop below stays as a reference implementation for the tests.
        if input_shape.hw_rank() == 2 {
            return unsafe {
                self.eval_t_2d::<T>(
                    input_shape,
                    output_shape,
                    spatial_output_details,
                    kernel,
                    input,
                    output,
                )
            };
        }
        let strides = self.pool_spec.strides();
        let dilations = self.pool_spec.dilations();
        let mut output_plain = output.try_as_plain_ram_mut()?;
        let mut output = output_plain.to_array_view_mut::<T>()?;
        for n in 0..n {
            for o in 0..c {
                for (kix, kcoords) in
                    tract_ndarray::indices(&*self.pool_spec.kernel_shape).into_iter().enumerate()
                {
                    // Hoisted out of the geometry loop: one load per (channel, tap).
                    let kv = kernel[o * kernel_vol + kix];
                    for (gix, gcoords) in
                        tract_ndarray::indices(input_shape.hw_dims()).into_iter().enumerate()
                    {
                        let ocoord: TVec<isize> = tract_itertools::izip!(
                            kcoords.slice(),
                            gcoords.slice(),
                            strides.as_ref(),
                            dilations.as_ref(),
                            spatial_output_details
                        )
                        .map(|(k, g, s, d, details)| {
                            (k * d + g * s) as isize - details.pad_before as isize
                        })
                        .collect();
                        if ocoord
                            .iter()
                            .zip(output_shape.hw_dims().iter())
                            .all(|(x, dim)| *x >= 0 && (*x as usize) < *dim)
                        {
                            // The multiply lives here, inside the bounds test, so the
                            // ~91% of products that fall outside are never computed.
                            let value = kv * input[(n * c + o) * hw + gix];
                            let ocoord = ocoord.iter().map(|x| *x as usize).collect::<TVec<_>>();
                            let ocoord =
                                self.pool_spec.data_format.with_n().from_n_c_hw(n, o, ocoord)?;
                            output[&*ocoord.shape] += value;
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

impl TypedOp for DepthwiseDeconv {
    fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
        ensure!(inputs.len() == 3);
        let shape = super::output_shape(&self.pool_spec, &self.input_shape, &self.adjustments)?;
        ensure!(*inputs[2].shape == *shape);
        Ok(tvec!(inputs[1].datum_type.fact(shape)))
    }

    fn set_symbols(
        &self,
        _source: &TypedModel,
        node: &TypedNode,
        target: &mut TypedModel,
        mapping: &HashMap<OutletId, OutletId>,
        subs: &HashMap<Symbol, TDim>,
    ) -> TractResult<TVec<OutletId>> {
        target.wire_node(
            &node.name,
            Self { input_shape: self.input_shape.substitute(subs)?.into_owned(), ..self.clone() },
            &[mapping[&node.inputs[0]], mapping[&node.inputs[1]], mapping[&node.inputs[2]]],
        )
    }

    as_op!();
}