edgefirst-decoder 0.22.1

ML model output decoding for YOLO and ModelPack object detection and segmentation
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
// SPDX-FileCopyrightText: Copyright 2026 Au-Zone Technologies
// SPDX-License-Identifier: Apache-2.0

//! Per-FPN-level box decode orchestrators (DFL and LTRB).
//!
//! Each function decodes one level's box tensor into 4*H*W floats
//! `[xc, yc, w, h]` per anchor, anchor-major.

use super::box_primitives::{
    dist2bbox_anchor_f16, dist2bbox_anchor_f32, weighted_sum_4sides_f16, weighted_sum_4sides_f32,
};
use super::dequant::{
    dequant_f16_to_f16, dequant_f16_to_f32, dequant_f32_to_f16, dequant_f32_to_f32,
    dequant_i16_to_f16, dequant_i16_to_f32, dequant_i8_to_f16, dequant_i8_to_f32,
    dequant_u16_to_f16, dequant_u16_to_f32, dequant_u8_to_f16, dequant_u8_to_f32,
};
use super::softmax::{softmax_inplace_f16, softmax_inplace_f32};
use crate::per_scale::plan::LevelPlan;
use crate::Quantization;
use half::f16;

const MAX_REG_MAX: usize = 64;

// ── DFL: dequant → softmax (per side) → weighted_sum → dist2bbox ─────────

/// Generate per-cell DFL level kernels.  Inputs are NHWC `[1, h, w, 4*reg_max]`.
macro_rules! impl_dfl_level_f32 {
    ($name:ident, $i:ty, $dequant:ident) => {
        #[allow(dead_code)]
        pub(crate) fn $name(input: &[$i], q: Quantization, level: &LevelPlan, dst: &mut [f32]) {
            let h = level.h;
            let w = level.w;
            let reg_max = level.reg_max;
            debug_assert_eq!(input.len(), h * w * 4 * reg_max);
            debug_assert_eq!(dst.len(), 4 * h * w);
            debug_assert!(reg_max <= MAX_REG_MAX);

            let mut deq: [f32; 4 * MAX_REG_MAX] = [0.0_f32; 4 * MAX_REG_MAX];
            for anchor in 0..(h * w) {
                let in_base = anchor * 4 * reg_max;
                $dequant(
                    &input[in_base..in_base + 4 * reg_max],
                    q,
                    &mut deq[..4 * reg_max],
                );
                for side in 0..4 {
                    softmax_inplace_f32(&mut deq[side * reg_max..(side + 1) * reg_max]);
                }
                let ltrb = weighted_sum_4sides_f32(&deq[..4 * reg_max], reg_max);
                let gx = level.grid_x[anchor];
                let gy = level.grid_y[anchor];
                let xywh = dist2bbox_anchor_f32(ltrb, gx, gy, level.stride);
                let out_base = anchor * 4;
                dst[out_base..out_base + 4].copy_from_slice(&xywh);
            }
        }
    };
}

macro_rules! impl_dfl_level_f16 {
    ($name:ident, $i:ty, $dequant:ident) => {
        #[allow(dead_code)]
        pub(crate) fn $name(input: &[$i], q: Quantization, level: &LevelPlan, dst: &mut [f16]) {
            let h = level.h;
            let w = level.w;
            let reg_max = level.reg_max;
            debug_assert_eq!(input.len(), h * w * 4 * reg_max);
            debug_assert_eq!(dst.len(), 4 * h * w);
            debug_assert!(reg_max <= MAX_REG_MAX);

            let mut deq: [f16; 4 * MAX_REG_MAX] = [f16::ZERO; 4 * MAX_REG_MAX];
            for anchor in 0..(h * w) {
                let in_base = anchor * 4 * reg_max;
                $dequant(
                    &input[in_base..in_base + 4 * reg_max],
                    q,
                    &mut deq[..4 * reg_max],
                );
                for side in 0..4 {
                    softmax_inplace_f16(&mut deq[side * reg_max..(side + 1) * reg_max]);
                }
                let ltrb = weighted_sum_4sides_f16(&deq[..4 * reg_max], reg_max);
                let gx = f16::from_f32(level.grid_x[anchor]);
                let gy = f16::from_f32(level.grid_y[anchor]);
                let stride = f16::from_f32(level.stride);
                let xywh = dist2bbox_anchor_f16(ltrb, gx, gy, stride);
                let out_base = anchor * 4;
                dst[out_base..out_base + 4].copy_from_slice(&xywh);
            }
        }
    };
}

// 12 DFL cells.
impl_dfl_level_f32!(decode_box_level_dfl_i8_to_f32, i8, dequant_i8_to_f32);
impl_dfl_level_f32!(decode_box_level_dfl_u8_to_f32, u8, dequant_u8_to_f32);
impl_dfl_level_f32!(decode_box_level_dfl_i16_to_f32, i16, dequant_i16_to_f32);
impl_dfl_level_f32!(decode_box_level_dfl_u16_to_f32, u16, dequant_u16_to_f32);
impl_dfl_level_f32!(decode_box_level_dfl_f16_to_f32, f16, dequant_f16_to_f32);
impl_dfl_level_f32!(decode_box_level_dfl_f32_to_f32, f32, dequant_f32_to_f32);

impl_dfl_level_f16!(decode_box_level_dfl_i8_to_f16, i8, dequant_i8_to_f16);
impl_dfl_level_f16!(decode_box_level_dfl_u8_to_f16, u8, dequant_u8_to_f16);
impl_dfl_level_f16!(decode_box_level_dfl_i16_to_f16, i16, dequant_i16_to_f16);
impl_dfl_level_f16!(decode_box_level_dfl_u16_to_f16, u16, dequant_u16_to_f16);
impl_dfl_level_f16!(decode_box_level_dfl_f16_to_f16, f16, dequant_f16_to_f16);
impl_dfl_level_f16!(decode_box_level_dfl_f32_to_f16, f32, dequant_f32_to_f16);

// ── LTRB: dequant → dist2bbox ────────────────────────────────────────────

macro_rules! impl_ltrb_level_f32 {
    ($name:ident, $i:ty, $dequant:ident) => {
        #[allow(dead_code)]
        pub(crate) fn $name(input: &[$i], q: Quantization, level: &LevelPlan, dst: &mut [f32]) {
            let h = level.h;
            let w = level.w;
            debug_assert_eq!(input.len(), h * w * 4);
            debug_assert_eq!(dst.len(), 4 * h * w);

            let mut deq: [f32; 4] = [0.0_f32; 4];
            for anchor in 0..(h * w) {
                let in_base = anchor * 4;
                $dequant(&input[in_base..in_base + 4], q, &mut deq);
                let gx = level.grid_x[anchor];
                let gy = level.grid_y[anchor];
                let xywh = dist2bbox_anchor_f32(deq, gx, gy, level.stride);
                let out_base = anchor * 4;
                dst[out_base..out_base + 4].copy_from_slice(&xywh);
            }
        }
    };
}

macro_rules! impl_ltrb_level_f16 {
    ($name:ident, $i:ty, $dequant:ident) => {
        #[allow(dead_code)]
        pub(crate) fn $name(input: &[$i], q: Quantization, level: &LevelPlan, dst: &mut [f16]) {
            let h = level.h;
            let w = level.w;
            debug_assert_eq!(input.len(), h * w * 4);
            debug_assert_eq!(dst.len(), 4 * h * w);

            let mut deq: [f16; 4] = [f16::ZERO; 4];
            for anchor in 0..(h * w) {
                let in_base = anchor * 4;
                $dequant(&input[in_base..in_base + 4], q, &mut deq);
                let gx = f16::from_f32(level.grid_x[anchor]);
                let gy = f16::from_f32(level.grid_y[anchor]);
                let stride = f16::from_f32(level.stride);
                let xywh = dist2bbox_anchor_f16(deq, gx, gy, stride);
                let out_base = anchor * 4;
                dst[out_base..out_base + 4].copy_from_slice(&xywh);
            }
        }
    };
}

// 12 LTRB cells.
impl_ltrb_level_f32!(decode_box_level_ltrb_i8_to_f32, i8, dequant_i8_to_f32);
impl_ltrb_level_f32!(decode_box_level_ltrb_u8_to_f32, u8, dequant_u8_to_f32);
impl_ltrb_level_f32!(decode_box_level_ltrb_i16_to_f32, i16, dequant_i16_to_f32);
impl_ltrb_level_f32!(decode_box_level_ltrb_u16_to_f32, u16, dequant_u16_to_f32);
impl_ltrb_level_f32!(decode_box_level_ltrb_f16_to_f32, f16, dequant_f16_to_f32);
impl_ltrb_level_f32!(decode_box_level_ltrb_f32_to_f32, f32, dequant_f32_to_f32);

impl_ltrb_level_f16!(decode_box_level_ltrb_i8_to_f16, i8, dequant_i8_to_f16);
impl_ltrb_level_f16!(decode_box_level_ltrb_u8_to_f16, u8, dequant_u8_to_f16);
impl_ltrb_level_f16!(decode_box_level_ltrb_i16_to_f16, i16, dequant_i16_to_f16);
impl_ltrb_level_f16!(decode_box_level_ltrb_u16_to_f16, u16, dequant_u16_to_f16);
impl_ltrb_level_f16!(decode_box_level_ltrb_f16_to_f16, f16, dequant_f16_to_f16);
impl_ltrb_level_f16!(decode_box_level_ltrb_f32_to_f16, f32, dequant_f32_to_f16);

// ────────────────────────────────────────────────────────────────────────
// NEON-baseline (Tier 1) DFL + LTRB box level kernels
//
// **DFL** keeps the per-anchor structure: each anchor's `4 * reg_max`
// channels (typically 64) is exactly 4 chunks of 16 i8 lanes — perfect
// shape for the chunked NEON dequant. Per-anchor: NEON dequant + scalar
// softmax + scalar weighted-sum + scalar dist2bbox. NEON softmax lands
// in task N-10 and replaces the per-side softmax inline.
//
// **LTRB** has only 4 channels per anchor — too few for NEON to amortize
// loop overhead. Instead the NEON variant **batch-dequants** the whole
// `(h * w * 4)` tensor into a heap scratch (sized 25600 f32 max for
// 80x80 LTRB, well under any per-frame budget), then runs a scalar
// per-anchor dist2bbox loop reading from the scratch.
// ────────────────────────────────────────────────────────────────────────

#[cfg(target_arch = "aarch64")]
macro_rules! impl_dfl_level_f32_neon {
    ($name:ident, $i:ty, $dequant_neon:ident, $softmax:ident) => {
        #[allow(dead_code)]
        pub(crate) fn $name(input: &[$i], q: Quantization, level: &LevelPlan, dst: &mut [f32]) {
            let h = level.h;
            let w = level.w;
            let reg_max = level.reg_max;
            debug_assert_eq!(input.len(), h * w * 4 * reg_max);
            debug_assert_eq!(dst.len(), 4 * h * w);
            debug_assert!(reg_max <= MAX_REG_MAX);

            let mut deq: [f32; 4 * MAX_REG_MAX] = [0.0_f32; 4 * MAX_REG_MAX];
            for anchor in 0..(h * w) {
                let in_base = anchor * 4 * reg_max;
                // SAFETY: NEON mandatory on aarch64; dispatch contract.
                // FP16-suffixed variants additionally require f.neon_fp16.
                unsafe {
                    crate::per_scale::kernels::neon_baseline::$dequant_neon(
                        &input[in_base..in_base + 4 * reg_max],
                        q,
                        &mut deq[..4 * reg_max],
                    );
                    for side in 0..4 {
                        crate::per_scale::kernels::neon_baseline::$softmax(
                            &mut deq[side * reg_max..(side + 1) * reg_max],
                        );
                    }
                }
                // NEON vectorized weighted-sum + scalar dist2bbox.
                let ltrb = unsafe {
                    crate::per_scale::kernels::neon_baseline::weighted_sum_4sides_f32_neon(
                        &deq[..4 * reg_max],
                        reg_max,
                    )
                };
                let gx = level.grid_x[anchor];
                let gy = level.grid_y[anchor];
                let xywh = dist2bbox_anchor_f32(ltrb, gx, gy, level.stride);
                let out_base = anchor * 4;
                dst[out_base..out_base + 4].copy_from_slice(&xywh);
            }
        }
    };
}

#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_i8_to_f32_neon,
    i8,
    dequant_i8_to_f32_neon,
    softmax_inplace_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_u8_to_f32_neon,
    u8,
    dequant_u8_to_f32_neon,
    softmax_inplace_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_i16_to_f32_neon,
    i16,
    dequant_i16_to_f32_neon,
    softmax_inplace_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_u16_to_f32_neon,
    u16,
    dequant_u16_to_f32_neon,
    softmax_inplace_f32_neon
);

// FP16 DFL variants — same dequant, FP16 softmax. Reached only when
// CpuFeatures::neon_fp16 is true (gated upstream by dispatch::select).
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_i8_to_f32_neon_fp16,
    i8,
    dequant_i8_to_f32_neon,
    softmax_inplace_f32_neon_fp16
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_u8_to_f32_neon_fp16,
    u8,
    dequant_u8_to_f32_neon,
    softmax_inplace_f32_neon_fp16
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_i16_to_f32_neon_fp16,
    i16,
    dequant_i16_to_f32_neon,
    softmax_inplace_f32_neon_fp16
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon!(
    decode_box_level_dfl_u16_to_f32_neon_fp16,
    u16,
    dequant_u16_to_f32_neon,
    softmax_inplace_f32_neon_fp16
);

#[cfg(target_arch = "aarch64")]
macro_rules! impl_ltrb_level_f32_neon {
    ($name:ident, $i:ty, $dequant_neon:ident) => {
        #[allow(dead_code)]
        pub(crate) fn $name(input: &[$i], q: Quantization, level: &LevelPlan, dst: &mut [f32]) {
            let h = level.h;
            let w = level.w;
            debug_assert_eq!(input.len(), h * w * 4);
            debug_assert_eq!(dst.len(), 4 * h * w);

            let n = h * w * 4;
            let mut deq: Vec<f32> = vec![0.0_f32; n];
            // SAFETY: NEON mandatory on aarch64; dispatch contract.
            unsafe {
                crate::per_scale::kernels::neon_baseline::$dequant_neon(input, q, &mut deq);
            }

            // Batch-4 NEON dist2bbox for the bulk of anchors.
            let n_anchors = h * w;
            let batch4 = n_anchors / 4;
            for b in 0..batch4 {
                let a0 = b * 4;
                let ltrb_base = a0 * 4;
                let gx_batch = [
                    level.grid_x[a0],
                    level.grid_x[a0 + 1],
                    level.grid_x[a0 + 2],
                    level.grid_x[a0 + 3],
                ];
                let gy_batch = [
                    level.grid_y[a0],
                    level.grid_y[a0 + 1],
                    level.grid_y[a0 + 2],
                    level.grid_y[a0 + 3],
                ];
                // SAFETY: NEON mandatory on aarch64. deq is contiguous
                // [l0,t0,r0,b0, l1,t1,r1,b1, ...] — 16 floats per batch.
                unsafe {
                    crate::per_scale::kernels::neon_baseline::dist2bbox_4anchors_f32_neon(
                        &deq[ltrb_base..ltrb_base + 16],
                        &gx_batch,
                        &gy_batch,
                        level.stride,
                        &mut dst[ltrb_base..ltrb_base + 16],
                    );
                }
            }
            // Scalar tail for remaining anchors.
            for anchor in (batch4 * 4)..n_anchors {
                let base = anchor * 4;
                let dq = [deq[base], deq[base + 1], deq[base + 2], deq[base + 3]];
                let gx = level.grid_x[anchor];
                let gy = level.grid_y[anchor];
                let xywh = dist2bbox_anchor_f32(dq, gx, gy, level.stride);
                dst[base..base + 4].copy_from_slice(&xywh);
            }
        }
    };
}

#[cfg(target_arch = "aarch64")]
impl_ltrb_level_f32_neon!(
    decode_box_level_ltrb_i8_to_f32_neon,
    i8,
    dequant_i8_to_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_ltrb_level_f32_neon!(
    decode_box_level_ltrb_u8_to_f32_neon,
    u8,
    dequant_u8_to_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_ltrb_level_f32_neon!(
    decode_box_level_ltrb_i16_to_f32_neon,
    i16,
    dequant_i16_to_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_ltrb_level_f32_neon!(
    decode_box_level_ltrb_u16_to_f32_neon,
    u16,
    dequant_u16_to_f32_neon
);

// ────────────────────────────────────────────────────────────────────────
// Fused DFL box level kernels (online-softmax inspired)
//
// Replaces the separate softmax + weighted_sum calls with a single
// fused kernel that computes Σ(exp(x-m)×i)/Σ(exp(x-m)) in-register.
// Eliminates 256 memory ops per anchor (64 per side × 4 sides):
//   - No intermediate probability store/reload
//   - No normalize pass
//   - Single scalar divide at the end
//
// Used as the default NEON DFL path (replaces the separate-pass version).
// ────────────────────────────────────────────────────────────────────────

#[cfg(target_arch = "aarch64")]
macro_rules! impl_dfl_level_f32_neon_fused {
    ($name:ident, $i:ty, $dequant_neon:ident, $fused_ws:ident) => {
        #[allow(dead_code)]
        pub(crate) fn $name(input: &[$i], q: Quantization, level: &LevelPlan, dst: &mut [f32]) {
            let h = level.h;
            let w = level.w;
            let reg_max = level.reg_max;
            debug_assert_eq!(input.len(), h * w * 4 * reg_max);
            debug_assert_eq!(dst.len(), 4 * h * w);
            debug_assert!(reg_max <= MAX_REG_MAX);

            let mut deq: [f32; 4 * MAX_REG_MAX] = [0.0_f32; 4 * MAX_REG_MAX];
            for anchor in 0..(h * w) {
                let in_base = anchor * 4 * reg_max;
                // SAFETY: NEON mandatory on aarch64; dispatch contract.
                unsafe {
                    crate::per_scale::kernels::neon_baseline::$dequant_neon(
                        &input[in_base..in_base + 4 * reg_max],
                        q,
                        &mut deq[..4 * reg_max],
                    );
                }
                // Fused softmax+weighted_sum — no intermediate store.
                let ltrb = unsafe {
                    crate::per_scale::kernels::neon_baseline::$fused_ws(
                        &deq[..4 * reg_max],
                        reg_max,
                    )
                };
                let gx = level.grid_x[anchor];
                let gy = level.grid_y[anchor];
                let xywh = dist2bbox_anchor_f32(ltrb, gx, gy, level.stride);
                let out_base = anchor * 4;
                dst[out_base..out_base + 4].copy_from_slice(&xywh);
            }
        }
    };
}

// Fused f32 variants (Tier-1 NEON baseline).
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_i8_to_f32_neon_fused,
    i8,
    dequant_i8_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_u8_to_f32_neon_fused,
    u8,
    dequant_u8_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_i16_to_f32_neon_fused,
    i16,
    dequant_i16_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_u16_to_f32_neon_fused,
    u16,
    dequant_u16_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon
);

// Fused FP16 variants (Tier-2 FP16 exp path).
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_i8_to_f32_neon_fused_fp16,
    i8,
    dequant_i8_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon_fp16
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_u8_to_f32_neon_fused_fp16,
    u8,
    dequant_u8_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon_fp16
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_i16_to_f32_neon_fused_fp16,
    i16,
    dequant_i16_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon_fp16
);
#[cfg(target_arch = "aarch64")]
impl_dfl_level_f32_neon_fused!(
    decode_box_level_dfl_u16_to_f32_neon_fused_fp16,
    u16,
    dequant_u16_to_f32_neon,
    softmax_weighted_sum_4sides_f32_neon_fp16
);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::per_scale::kernels::grids::make_anchor_grid;
    use crate::per_scale::plan::LevelPlan;
    use crate::Quantization;

    fn level_1x1(reg_max: usize, stride: f32, box_channels: usize) -> LevelPlan {
        let (gx, gy) = make_anchor_grid(1, 1);
        LevelPlan {
            stride,
            h: 1,
            w: 1,
            reg_max,
            anchor_offset: 0,
            grid_x: gx,
            grid_y: gy,
            box_shape: vec![1, 1, 1, box_channels].into_boxed_slice(),
            score_shape: vec![1, 1, 1, 80].into_boxed_slice(),
            mc_shape: None,
            layout: crate::per_scale::plan::Layout::Nhwc,
        }
    }

    #[test]
    fn dfl_one_hot_at_bin_5_yields_distance_5() {
        // 1x1 grid, reg_max=16, stride=8. One-hot at bin 5 each side
        // → d_left = d_top = d_right = d_bottom = 5
        // → xc = (0.5 + 0)*8 = 4, w = (5+5)*8 = 80
        let mut logits = [0.0_f32; 64];
        for side in 0..4 {
            logits[side * 16 + 5] = 100.0;
        }
        let mut out_boxes = [0.0_f32; 4];
        decode_box_level_dfl_f32_to_f32(
            &logits,
            Quantization::identity(),
            &level_1x1(16, 8.0, 64),
            &mut out_boxes,
        );
        assert!((out_boxes[0] - 4.0).abs() < 1e-3);
        assert!((out_boxes[2] - 80.0).abs() < 1e-3);
    }

    #[test]
    fn ltrb_direct_distances_yield_dist2bbox() {
        // 1x1 grid, stride=8, ltrb=[1,1,3,3] post-dequant
        // → xc = (0.5 + 1)*8 = 12, yc = (0.5 + 1)*8 = 12
        // → w = 4*8 = 32, h = 4*8 = 32
        let logits = [1.0_f32, 1.0, 3.0, 3.0];
        let mut out_boxes = [0.0_f32; 4];
        decode_box_level_ltrb_f32_to_f32(
            &logits,
            Quantization::identity(),
            &level_1x1(1, 8.0, 4),
            &mut out_boxes,
        );
        assert!((out_boxes[0] - 12.0).abs() < 1e-3);
        assert!((out_boxes[2] - 32.0).abs() < 1e-3);
    }

    #[test]
    fn dfl_2x2_uniform_logits_each_anchor_at_centre() {
        // reg_max=4, all bins equal logit → softmax uniform → distance = 1.5 per side
        // 2x2 grid, stride=4. For anchor (0.5, 0.5):
        //   xc = (0.5 + 0)*4 = 2, w = (1.5+1.5)*4 = 12
        let logits = [1.0_f32; 2 * 2 * 4 * 4]; // (h=2, w=2, 4 sides * 4 bins)
        let (gx, gy) = make_anchor_grid(2, 2);
        let lvl = LevelPlan {
            stride: 4.0,
            h: 2,
            w: 2,
            reg_max: 4,
            anchor_offset: 0,
            grid_x: gx,
            grid_y: gy,
            box_shape: vec![1, 2, 2, 16].into_boxed_slice(),
            score_shape: vec![1, 2, 2, 80].into_boxed_slice(),
            mc_shape: None,
            layout: crate::per_scale::plan::Layout::Nhwc,
        };
        let mut out = [0.0_f32; 16]; // 4 anchors * 4 box-coords
        decode_box_level_dfl_f32_to_f32(&logits, Quantization::identity(), &lvl, &mut out);
        // Anchor 0 (0.5, 0.5)
        assert!((out[0] - 2.0).abs() < 1e-3);
        assert!((out[1] - 2.0).abs() < 1e-3);
        assert!((out[2] - 12.0).abs() < 1e-3);
        // Anchor 1 (1.5, 0.5)
        assert!((out[4] - 6.0).abs() < 1e-3, "expected xc=6, got {}", out[4]);
        // Anchor 2 (0.5, 1.5)
        assert!((out[9] - 6.0).abs() < 1e-3, "expected yc=6, got {}", out[9]);
    }

    #[test]
    fn dfl_i8_dequant_then_decode_matches_f32_input() {
        // Verify the i8→f32 path produces same boxes as f32 passthrough
        // when the dequantized logits are equivalent.
        let q = Quantization::new(0.1, -128);
        let mut input_i8 = [-128_i8; 64];
        // Set bin 5 of each side to a saturated value that dequants high.
        for side in 0..4 {
            input_i8[side * 16 + 5] = 127;
        }
        let mut out_i8 = [0.0_f32; 4];
        decode_box_level_dfl_i8_to_f32(&input_i8, q, &level_1x1(16, 8.0, 64), &mut out_i8);

        // Equivalent f32 logits: (-128 - -128)*0.1 = 0; (127 - -128)*0.1 = 25.5
        let mut logits_f32 = [0.0_f32; 64];
        for side in 0..4 {
            logits_f32[side * 16 + 5] = 25.5;
        }
        let mut out_f32 = [0.0_f32; 4];
        decode_box_level_dfl_f32_to_f32(
            &logits_f32,
            Quantization::identity(),
            &level_1x1(16, 8.0, 64),
            &mut out_f32,
        );

        for i in 0..4 {
            assert!(
                (out_i8[i] - out_f32[i]).abs() < 1e-2,
                "box[{i}]: i8={} f32={}",
                out_i8[i],
                out_f32[i]
            );
        }
    }
}