edgefirst-decoder 0.38.0

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
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
// SPDX-FileCopyrightText: Copyright 2025 Au-Zone Technologies
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::excessive_precision)]

use edgefirst_bench::{run_bench, BenchSuite};
use edgefirst_decoder::{
    byte::{nms_int, postprocess_boxes_multilabel_index_quant, postprocess_boxes_quant},
    configs, dequant_detect_box, dequantize_cpu, dequantize_cpu_chunked, dequantize_ndarray,
    float::{nms_float, postprocess_boxes_float},
    per_scale::DecodeDtype,
    schema::SchemaV2,
    ConfigOutput, DecoderBuilder, Nms, Quantization, XYWH,
};
use edgefirst_tensor::{
    Quantization as TQ, Tensor, TensorDyn, TensorMapTrait, TensorMemory, TensorTrait,
};
use ndarray::s;

const WARMUP: usize = 10;
const ITERATIONS: usize = 100;

fn bench_yolo_quant(suite: &mut BenchSuite) {
    let raw = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let raw = unsafe { std::slice::from_raw_parts(raw.as_ptr() as *const i8, raw.len()) };
    let tensor = TensorDyn::I8(Tensor::<i8>::from_slice(raw, &[1, 84, 8400]).unwrap());

    let detection = configs::Detection {
        decoder: configs::DecoderType::Ultralytics,
        quantization: Some(configs::QuantTuple(0.0040811873, -123)),
        shape: vec![1, 84, 8400],
        dshape: vec![],
        anchors: None,
        normalized: Some(true),
    };
    let decoder = DecoderBuilder::default()
        .with_score_threshold(0.25)
        .with_iou_threshold(0.70)
        .with_nms(Some(Nms::ClassAgnostic))
        .add_output(ConfigOutput::Detection(detection))
        .build()
        .expect("yolo det quant decoder must build");

    let inputs: Vec<&TensorDyn> = vec![&tensor];
    let result = run_bench("decoder/yolo/quant", WARMUP, ITERATIONS, || {
        let mut output_boxes = Vec::with_capacity(50);
        let mut masks = Vec::new();
        decoder
            .decode(&inputs, &mut output_boxes, &mut masks)
            .unwrap();
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_quant_decode_boxes(suite: &mut BenchSuite) {
    let score_threshold = 0.25;
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out = ndarray::Array2::from_shape_vec((84, 8400), out.to_vec()).unwrap();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let score_threshold = (score_threshold / quant.scale + quant.zero_point as f32) as i8;
    let boxes_tensor = out.slice(s![..4, ..,]).reversed_axes();
    let scores_tensor = out.slice(s![4..(80 + 4), ..,]).reversed_axes();

    let result = run_bench("decoder/quant/decode_boxes", WARMUP, ITERATIONS, || {
        let _ = postprocess_boxes_quant::<XYWH, _, _>(
            score_threshold,
            boxes_tensor,
            scores_tensor,
            quant,
        );
    });
    result.print_summary();
    suite.record(&result);
}

/// Multi-label candidate selection on the transposed score view a DMA-BUF
/// output produces, at a deployment and a validation score threshold.
fn bench_quant_decode_boxes_multilabel(suite: &mut BenchSuite) {
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out = ndarray::Array2::from_shape_vec((84, 8400), out.to_vec()).unwrap();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let boxes_tensor = out.slice(s![..4, ..,]).reversed_axes();
    let scores_tensor = out.slice(s![4..(80 + 4), ..,]).reversed_axes();
    for (name, score_threshold) in [
        ("decoder/quant/decode_boxes_multilabel", 0.25f32),
        ("decoder/quant/decode_boxes_multilabel_val", 0.001f32),
    ] {
        let threshold = (score_threshold / quant.scale + quant.zero_point as f32) as i8;
        let result = run_bench(name, WARMUP, ITERATIONS, || {
            let _ = postprocess_boxes_multilabel_index_quant::<XYWH, _, _>(
                threshold,
                boxes_tensor,
                scores_tensor,
                quant,
            );
        });
        result.print_summary();
        suite.record(&result);
    }
}

fn bench_quant_nms(suite: &mut BenchSuite) {
    let score_threshold = 0.01;
    let iou_threshold = 0.70;
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out = out.to_vec();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let mut output_boxes: Vec<_> = Vec::with_capacity(50);
    let out = ndarray::Array2::from_shape_vec((84, 8400), out.clone()).unwrap();
    let score_threshold = (score_threshold / quant.scale + quant.zero_point as f32) as i8;
    let boxes_tensor = out.slice(s![..4, ..,]).reversed_axes();
    let scores_tensor = out.slice(s![4..(80 + 4), ..,]).reversed_axes();
    let boxes =
        postprocess_boxes_quant::<XYWH, _, _>(score_threshold, boxes_tensor, scores_tensor, quant);

    let result = run_bench("decoder/quant/nms", WARMUP, ITERATIONS, || {
        let boxes = boxes.clone();
        let cap = output_boxes.capacity();
        let boxes = nms_int(iou_threshold, (cap > 0).then_some(cap), boxes);
        let len = cap.min(boxes.len());
        output_boxes.clear();
        for b in boxes.iter().take(len) {
            output_boxes.push(dequant_detect_box(b, quant));
        }
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_yolo_f32(suite: &mut BenchSuite) {
    // Pre-dequantize once outside the timed loop. The bench measures
    // the float decode path; mixing dequant cost into each iteration
    // (as the legacy version did) inflated wall-time by the dequant.
    let raw = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let raw = unsafe { std::slice::from_raw_parts(raw.as_ptr() as *const i8, raw.len()) };
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let mut buf = vec![0.0f32; 84 * 8400];
    dequantize_cpu_chunked(raw, quant, &mut buf);
    let tensor = TensorDyn::F32(Tensor::<f32>::from_slice(&buf, &[1, 84, 8400]).unwrap());

    let detection = configs::Detection {
        decoder: configs::DecoderType::Ultralytics,
        quantization: None,
        shape: vec![1, 84, 8400],
        dshape: vec![],
        anchors: None,
        normalized: Some(true),
    };
    let decoder = DecoderBuilder::default()
        .with_score_threshold(0.25)
        .with_iou_threshold(0.70)
        .with_nms(Some(Nms::ClassAgnostic))
        .add_output(ConfigOutput::Detection(detection))
        .build()
        .expect("yolo det f32 decoder must build");

    let inputs: Vec<&TensorDyn> = vec![&tensor];
    let result = run_bench("decoder/yolo/f32", WARMUP, ITERATIONS, || {
        let mut output_boxes = Vec::with_capacity(50);
        let mut masks = Vec::new();
        decoder
            .decode(&inputs, &mut output_boxes, &mut masks)
            .unwrap();
        std::hint::black_box(output_boxes);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_dequantize_i8(suite: &mut BenchSuite) {
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out = out.to_vec();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let buf = vec![0.0; 84 * 8400];

    let result = run_bench("decoder/dequantize/i8", WARMUP, ITERATIONS, || {
        let mut buf = buf.clone();
        dequantize_cpu(&out, quant, &mut buf);
        let out = ndarray::Array2::from_shape_vec((84, 8400), buf).unwrap();
        std::hint::black_box(out);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_dequantize_i8_chunked(suite: &mut BenchSuite) {
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out = out.to_vec();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let buf = vec![0.0; 84 * 8400];

    let result = run_bench("decoder/dequantize/i8_chunked", WARMUP, ITERATIONS, || {
        let mut buf = buf.clone();
        dequantize_cpu_chunked(&out, quant, &mut buf);
        let out = ndarray::Array2::from_shape_vec((84, 8400), buf).unwrap();
        std::hint::black_box(out);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_dequantize_i16(suite: &mut BenchSuite) {
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out: Vec<_> = out.iter().map(|x| *x as i16).collect();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let buf = vec![0.0; 84 * 8400];

    let result = run_bench("decoder/dequantize/i16", WARMUP, ITERATIONS, || {
        let mut buf = buf.clone();
        dequantize_cpu(&out, quant, &mut buf);
        let out = ndarray::Array2::from_shape_vec((84, 8400), buf).unwrap();
        std::hint::black_box(out);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_dequantize_i16_chunked(suite: &mut BenchSuite) {
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out: Vec<_> = out.iter().map(|x| *x as i16).collect();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let buf = vec![0.0; 84 * 8400];

    let result = run_bench("decoder/dequantize/i16_chunked", WARMUP, ITERATIONS, || {
        let mut buf = buf.clone();
        dequantize_cpu_chunked(&out, quant, &mut buf);
        let out = ndarray::Array2::from_shape_vec((84, 8400), buf).unwrap();
        std::hint::black_box(out);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_f32_decode_boxes(suite: &mut BenchSuite) {
    let score_threshold = 0.25;
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out = out.to_vec();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let mut buf = vec![0.0; 84 * 8400];
    dequantize_cpu_chunked(&out, quant, &mut buf);

    let result = run_bench("decoder/f32/decode_boxes", WARMUP, ITERATIONS, || {
        let out = buf.clone();
        let out = ndarray::Array2::from_shape_vec((84, 8400), out).unwrap();
        let boxes_tensor = out.slice(s![..4, ..,]).reversed_axes();
        let scores_tensor = out.slice(s![4..(80 + 4), ..,]).reversed_axes();
        let boxes =
            postprocess_boxes_float::<XYWH, _, _>(score_threshold, boxes_tensor, scores_tensor);
        std::hint::black_box(boxes);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_f32_nms(suite: &mut BenchSuite) {
    let score_threshold = 0.01;
    let iou_threshold = 0.70;
    let out = edgefirst_bench::testdata::read("yolov8s_80_classes.bin");
    let out = unsafe { std::slice::from_raw_parts(out.as_ptr() as *const i8, out.len()) };
    let out = out.to_vec();
    let quant = Quantization {
        scale: 0.0040811873,
        zero_point: -123,
    };
    let mut buf = vec![0.0; 84 * 8400];
    dequantize_cpu_chunked(&out, quant, &mut buf);
    let out = ndarray::Array2::from_shape_vec((84, 8400), buf).unwrap();
    let boxes_tensor = out.slice(s![..4, ..,]).reversed_axes();
    let scores_tensor = out.slice(s![4..(80 + 4), ..,]).reversed_axes();
    let boxes = postprocess_boxes_float::<XYWH, _, _>(score_threshold, boxes_tensor, scores_tensor);

    let result = run_bench("decoder/f32/nms", WARMUP, ITERATIONS, || {
        let boxes = boxes.clone();
        let mut output_boxes: Vec<_> = Vec::with_capacity(50);
        let cap = output_boxes.capacity();
        let boxes = nms_float(iou_threshold, (cap > 0).then_some(cap), boxes);
        let len = cap.min(boxes.len());
        output_boxes.clear();
        for b in boxes.into_iter().take(len) {
            output_boxes.push(b);
        }
        std::hint::black_box(output_boxes);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_modelpack_u8(suite: &mut BenchSuite) {
    let score_threshold = 0.45;
    let iou_threshold = 0.45;
    let boxes = edgefirst_bench::testdata::read("modelpack_boxes_1935x1x4.bin");
    let boxes_tensor = TensorDyn::U8(Tensor::<u8>::from_slice(&boxes, &[1, 1935, 1, 4]).unwrap());

    let scores = edgefirst_bench::testdata::read("modelpack_scores_1935x1.bin");
    let scores_tensor = TensorDyn::U8(Tensor::<u8>::from_slice(&scores, &[1, 1935, 1]).unwrap());

    let quant_boxes: configs::QuantTuple = (0.004656755365431309, 21).into();
    let quant_scores: configs::QuantTuple = (0.0019603664986789227, 0).into();

    let decoder = DecoderBuilder::default()
        .with_config_modelpack_det(
            configs::Boxes {
                decoder: configs::DecoderType::ModelPack,
                quantization: Some(quant_boxes),
                shape: vec![1, 1935, 1, 4],
                dshape: vec![
                    (configs::DimName::Batch, 1),
                    (configs::DimName::NumBoxes, 1935),
                    (configs::DimName::Padding, 1),
                    (configs::DimName::BoxCoords, 4),
                ],
                normalized: Some(true),
            },
            configs::Scores {
                decoder: configs::DecoderType::ModelPack,
                quantization: Some(quant_scores),
                shape: vec![1, 1935, 1],
                dshape: vec![
                    (configs::DimName::Batch, 1),
                    (configs::DimName::NumBoxes, 1935),
                    (configs::DimName::NumClasses, 1),
                ],
            },
        )
        .with_score_threshold(score_threshold)
        .with_iou_threshold(iou_threshold)
        .build()
        .expect("modelpack u8 decoder must build");

    let inputs: Vec<&TensorDyn> = vec![&boxes_tensor, &scores_tensor];
    let result = run_bench("decoder/modelpack/u8", WARMUP, ITERATIONS, || {
        let mut output_boxes = Vec::with_capacity(50);
        let mut output_masks = Vec::with_capacity(50);
        decoder
            .decode(&inputs, &mut output_boxes, &mut output_masks)
            .unwrap();
        std::hint::black_box(output_boxes);
        std::hint::black_box(output_masks);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_modelpack_split_u8(suite: &mut BenchSuite) {
    let score_threshold = 0.45;
    let iou_threshold = 0.45;

    let detect0 = edgefirst_bench::testdata::read("modelpack_split_9x15x18.bin");
    let detect0_tensor =
        TensorDyn::U8(Tensor::<u8>::from_slice(&detect0, &[1, 9, 15, 18]).unwrap());

    let detect1 = edgefirst_bench::testdata::read("modelpack_split_17x30x18.bin");
    let detect1_tensor =
        TensorDyn::U8(Tensor::<u8>::from_slice(&detect1, &[1, 17, 30, 18]).unwrap());

    let detect_config0 = configs::Detection {
        decoder: configs::DecoderType::ModelPack,
        shape: vec![1, 9, 15, 18],
        anchors: Some(vec![
            [0.36666667461395264, 0.31481480598449707],
            [0.38749998807907104, 0.4740740656852722],
            [0.5333333611488342, 0.644444465637207],
        ]),
        quantization: Some((0.08547406643629074, 174).into()),
        dshape: vec![
            (configs::DimName::Batch, 1),
            (configs::DimName::Height, 9),
            (configs::DimName::Width, 15),
            (configs::DimName::NumAnchorsXFeatures, 18),
        ],
        normalized: Some(true),
    };

    let detect_config1 = configs::Detection {
        decoder: configs::DecoderType::ModelPack,
        shape: vec![1, 17, 30, 18],
        anchors: Some(vec![
            [0.13750000298023224, 0.2074074000120163],
            [0.2541666626930237, 0.21481481194496155],
            [0.23125000298023224, 0.35185185074806213],
        ]),
        quantization: Some((0.09929127991199493, 183).into()),
        dshape: vec![
            (configs::DimName::Batch, 1),
            (configs::DimName::Height, 17),
            (configs::DimName::Width, 30),
            (configs::DimName::NumAnchorsXFeatures, 18),
        ],
        normalized: Some(true),
    };

    let decoder = DecoderBuilder::default()
        .with_config_modelpack_det_split(vec![detect_config1, detect_config0])
        .with_score_threshold(score_threshold)
        .with_iou_threshold(iou_threshold)
        .build()
        .expect("modelpack split u8 decoder must build");

    let inputs: Vec<&TensorDyn> = vec![&detect0_tensor, &detect1_tensor];
    let result = run_bench("decoder/modelpack/split_u8", WARMUP, ITERATIONS, || {
        let mut output_boxes = Vec::with_capacity(50);
        let mut output_masks = Vec::with_capacity(50);
        decoder
            .decode(&inputs, &mut output_boxes, &mut output_masks)
            .unwrap();
        std::hint::black_box(output_boxes);
        std::hint::black_box(output_masks);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_masks_f32(suite: &mut BenchSuite) {
    // Pre-dequantize boxes + protos once outside the timed loop. The bench
    // measures the float seg-det decode + mask materialization; the legacy
    // version dequantized each iteration which masked the actual decode time.
    let boxes_raw = edgefirst_bench::testdata::read("yolov8_boxes_116x8400.bin");
    let boxes_raw =
        unsafe { std::slice::from_raw_parts(boxes_raw.as_ptr() as *const i8, boxes_raw.len()) };
    let boxes_arr = ndarray::Array2::from_shape_vec((116, 8400), boxes_raw.to_vec()).unwrap();
    let quant_boxes = Quantization {
        scale: 0.01948494464159012,
        zero_point: 20,
    };
    let boxes_f32 = dequantize_ndarray::<_, _, f32>(boxes_arr.view(), quant_boxes);
    let boxes_tensor = TensorDyn::F32(
        Tensor::<f32>::from_slice(boxes_f32.as_slice().unwrap(), &[1, 116, 8400]).unwrap(),
    );

    let protos_raw = edgefirst_bench::testdata::read("yolov8_protos_160x160x32.bin");
    let protos_raw =
        unsafe { std::slice::from_raw_parts(protos_raw.as_ptr() as *const i8, protos_raw.len()) };
    let protos_arr = ndarray::Array3::from_shape_vec((160, 160, 32), protos_raw.to_vec()).unwrap();
    let quant_protos = Quantization {
        scale: 0.020889872685074806,
        zero_point: -115,
    };
    let protos_f32 = dequantize_ndarray::<_, _, f32>(protos_arr.view(), quant_protos);
    let protos_tensor = TensorDyn::F32(
        Tensor::<f32>::from_slice(protos_f32.as_slice().unwrap(), &[1, 160, 160, 32]).unwrap(),
    );

    let detection = configs::Detection {
        decoder: configs::DecoderType::Ultralytics,
        quantization: None,
        shape: vec![1, 116, 8400],
        dshape: vec![],
        anchors: None,
        normalized: Some(true),
    };
    let protos_cfg = configs::Protos {
        decoder: configs::DecoderType::Ultralytics,
        quantization: None,
        shape: vec![1, 160, 160, 32],
        dshape: vec![],
    };
    let decoder = DecoderBuilder::default()
        .with_score_threshold(0.45)
        .with_iou_threshold(0.45)
        .with_nms(Some(Nms::ClassAgnostic))
        .add_output(ConfigOutput::Detection(detection))
        .add_output(ConfigOutput::Protos(protos_cfg))
        .build()
        .expect("yolo segdet f32 decoder must build");

    let inputs: Vec<&TensorDyn> = vec![&boxes_tensor, &protos_tensor];
    let result = run_bench("decoder/masks/f32", WARMUP, ITERATIONS, || {
        let mut output_boxes = Vec::with_capacity(50);
        let mut output_masks = Vec::with_capacity(50);
        decoder
            .decode(&inputs, &mut output_boxes, &mut output_masks)
            .unwrap();
        std::hint::black_box(output_boxes);
        std::hint::black_box(output_masks);
    });
    result.print_summary();
    suite.record(&result);
}

fn bench_masks_i8(suite: &mut BenchSuite) {
    let boxes_raw = edgefirst_bench::testdata::read("yolov8_boxes_116x8400.bin");
    let boxes_raw =
        unsafe { std::slice::from_raw_parts(boxes_raw.as_ptr() as *const i8, boxes_raw.len()) };
    let boxes_tensor = TensorDyn::I8(Tensor::<i8>::from_slice(boxes_raw, &[1, 116, 8400]).unwrap());

    let protos_raw = edgefirst_bench::testdata::read("yolov8_protos_160x160x32.bin");
    let protos_raw =
        unsafe { std::slice::from_raw_parts(protos_raw.as_ptr() as *const i8, protos_raw.len()) };
    let protos_tensor =
        TensorDyn::I8(Tensor::<i8>::from_slice(protos_raw, &[1, 160, 160, 32]).unwrap());

    let detection = configs::Detection {
        decoder: configs::DecoderType::Ultralytics,
        quantization: Some(configs::QuantTuple(0.01948494464159012, 20)),
        shape: vec![1, 116, 8400],
        dshape: vec![],
        anchors: None,
        normalized: Some(true),
    };
    let protos_cfg = configs::Protos {
        decoder: configs::DecoderType::Ultralytics,
        quantization: Some(configs::QuantTuple(0.020889872685074806, -115)),
        shape: vec![1, 160, 160, 32],
        dshape: vec![],
    };
    let decoder = DecoderBuilder::default()
        .with_score_threshold(0.45)
        .with_iou_threshold(0.45)
        .with_nms(Some(Nms::ClassAgnostic))
        .add_output(ConfigOutput::Detection(detection))
        .add_output(ConfigOutput::Protos(protos_cfg))
        .build()
        .expect("yolo segdet i8 decoder must build");

    let inputs: Vec<&TensorDyn> = vec![&boxes_tensor, &protos_tensor];
    let result = run_bench("decoder/masks/i8", WARMUP, ITERATIONS, || {
        let mut output_boxes = Vec::with_capacity(50);
        let mut output_masks = Vec::with_capacity(50);
        decoder
            .decode(&inputs, &mut output_boxes, &mut output_masks)
            .unwrap();
        std::hint::black_box(output_boxes);
        std::hint::black_box(output_masks);
    });
    result.print_summary();
    suite.record(&result);
}

/// Build a per-scale-capable decoder for benchmarking, plus the 6
/// zero-int8 input tensors (3 box levels + 3 score levels) with
/// attached quantization.
///
/// Schema: yolov8n-style 3-level DFL detection-only (no mc, no
/// protos). Detection-only avoids the legacy validator's mc/protos
/// dshape requirements while still exercising the full DFL kernel
/// path on a realistic 8400-anchor workload.
fn build_dfl_per_scale_bench_inputs(
    out_dtype: DecodeDtype,
) -> (edgefirst_decoder::Decoder, Vec<TensorDyn>) {
    let schema_json = r#"{
        "schema_version": 2,
        "nms": "class_agnostic",
        "input": {
            "shape": [1, 640, 640, 3],
            "dshape": [{"batch": 1}, {"height": 640}, {"width": 640}, {"num_features": 3}],
            "cameraadaptor": "rgb"
        },
        "outputs": [
            {
                "name": "boxes", "type": "boxes",
                "shape": [1, 4, 8400],
                "dshape": [{"batch": 1}, {"box_coords": 4}, {"num_boxes": 8400}],
                "encoding": "dfl", "decoder": "ultralytics", "normalized": true,
                "outputs": [
                    {"name": "boxes_0", "type": "boxes", "stride": 8, "scale_index": 0,
                     "shape": [1, 80, 80, 64], "dshape": [{"batch": 1}, {"height": 80}, {"width": 80}, {"num_features": 64}],
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "boxes_1", "type": "boxes", "stride": 16, "scale_index": 1,
                     "shape": [1, 40, 40, 64], "dshape": [{"batch": 1}, {"height": 40}, {"width": 40}, {"num_features": 64}],
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "boxes_2", "type": "boxes", "stride": 32, "scale_index": 2,
                     "shape": [1, 20, 20, 64], "dshape": [{"batch": 1}, {"height": 20}, {"width": 20}, {"num_features": 64}],
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}}
                ]
            },
            {
                "name": "scores", "type": "scores",
                "shape": [1, 80, 8400],
                "dshape": [{"batch": 1}, {"num_classes": 80}, {"num_boxes": 8400}],
                "score_format": "per_class", "decoder": "ultralytics",
                "outputs": [
                    {"name": "scores_0", "type": "scores", "stride": 8, "scale_index": 0,
                     "shape": [1, 80, 80, 80], "dshape": [{"batch": 1}, {"height": 80}, {"width": 80}, {"num_classes": 80}],
                     "activation_required": "sigmoid",
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "scores_1", "type": "scores", "stride": 16, "scale_index": 1,
                     "shape": [1, 40, 40, 80], "dshape": [{"batch": 1}, {"height": 40}, {"width": 40}, {"num_classes": 80}],
                     "activation_required": "sigmoid",
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "scores_2", "type": "scores", "stride": 32, "scale_index": 2,
                     "shape": [1, 20, 20, 80], "dshape": [{"batch": 1}, {"height": 20}, {"width": 20}, {"num_classes": 80}],
                     "activation_required": "sigmoid",
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}}
                ]
            }
        ]
    }"#;
    let schema: SchemaV2 = serde_json::from_str(schema_json).expect("schema parse");

    let decoder = DecoderBuilder::default()
        .with_schema(schema)
        .with_decode_dtype(out_dtype)
        .with_iou_threshold(0.7)
        .with_score_threshold(0.25)
        .with_nms(Some(Nms::ClassAgnostic))
        .build()
        .expect("decoder build");

    let shapes: [&[usize]; 6] = [
        &[1, 80, 80, 64],
        &[1, 80, 80, 80],
        &[1, 40, 40, 64],
        &[1, 40, 40, 80],
        &[1, 20, 20, 64],
        &[1, 20, 20, 80],
    ];
    let tensors: Vec<TensorDyn> = shapes
        .iter()
        .map(|s| {
            let mut t = Tensor::<i8>::new(s, Some(TensorMemory::Mem), None).unwrap();
            t.set_quantization(TQ::per_tensor(0.1, 0)).unwrap();
            TensorDyn::I8(t)
        })
        .collect();

    (decoder, tensors)
}

/// Build LTRB variant of the same bench setup. Detection-only, 3 levels,
/// 4-channel boxes (yolo26 style).
fn build_ltrb_per_scale_bench_inputs(
    out_dtype: DecodeDtype,
) -> (edgefirst_decoder::Decoder, Vec<TensorDyn>) {
    let schema_json = r#"{
        "schema_version": 2,
        "nms": "class_agnostic",
        "input": {
            "shape": [1, 640, 640, 3],
            "dshape": [{"batch": 1}, {"height": 640}, {"width": 640}, {"num_features": 3}],
            "cameraadaptor": "rgb"
        },
        "outputs": [
            {
                "name": "boxes", "type": "boxes",
                "shape": [1, 4, 8400],
                "dshape": [{"batch": 1}, {"box_coords": 4}, {"num_boxes": 8400}],
                "encoding": "ltrb", "decoder": "ultralytics", "normalized": true,
                "outputs": [
                    {"name": "boxes_0", "type": "boxes", "stride": 8, "scale_index": 0,
                     "shape": [1, 80, 80, 4], "dshape": [{"batch": 1}, {"height": 80}, {"width": 80}, {"box_coords": 4}],
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "boxes_1", "type": "boxes", "stride": 16, "scale_index": 1,
                     "shape": [1, 40, 40, 4], "dshape": [{"batch": 1}, {"height": 40}, {"width": 40}, {"box_coords": 4}],
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "boxes_2", "type": "boxes", "stride": 32, "scale_index": 2,
                     "shape": [1, 20, 20, 4], "dshape": [{"batch": 1}, {"height": 20}, {"width": 20}, {"box_coords": 4}],
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}}
                ]
            },
            {
                "name": "scores", "type": "scores",
                "shape": [1, 80, 8400],
                "dshape": [{"batch": 1}, {"num_classes": 80}, {"num_boxes": 8400}],
                "score_format": "per_class", "decoder": "ultralytics",
                "outputs": [
                    {"name": "scores_0", "type": "scores", "stride": 8, "scale_index": 0,
                     "shape": [1, 80, 80, 80], "dshape": [{"batch": 1}, {"height": 80}, {"width": 80}, {"num_classes": 80}],
                     "activation_required": "sigmoid",
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "scores_1", "type": "scores", "stride": 16, "scale_index": 1,
                     "shape": [1, 40, 40, 80], "dshape": [{"batch": 1}, {"height": 40}, {"width": 40}, {"num_classes": 80}],
                     "activation_required": "sigmoid",
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}},
                    {"name": "scores_2", "type": "scores", "stride": 32, "scale_index": 2,
                     "shape": [1, 20, 20, 80], "dshape": [{"batch": 1}, {"height": 20}, {"width": 20}, {"num_classes": 80}],
                     "activation_required": "sigmoid",
                     "dtype": "int8", "quantization": {"scale": 0.1, "zero_point": 0, "dtype": "int8"}}
                ]
            }
        ]
    }"#;
    let schema: SchemaV2 = serde_json::from_str(schema_json).expect("schema parse");

    let decoder = DecoderBuilder::default()
        .with_schema(schema)
        .with_decode_dtype(out_dtype)
        .with_iou_threshold(0.7)
        .with_score_threshold(0.25)
        .with_nms(Some(Nms::ClassAgnostic))
        .build()
        .expect("decoder build");

    let shapes: [&[usize]; 6] = [
        &[1, 80, 80, 4],
        &[1, 80, 80, 80],
        &[1, 40, 40, 4],
        &[1, 40, 40, 80],
        &[1, 20, 20, 4],
        &[1, 20, 20, 80],
    ];
    let tensors: Vec<TensorDyn> = shapes
        .iter()
        .map(|s| {
            let mut t = Tensor::<i8>::new(s, Some(TensorMemory::Mem), None).unwrap();
            t.set_quantization(TQ::per_tensor(0.1, 0)).unwrap();
            TensorDyn::I8(t)
        })
        .collect();

    (decoder, tensors)
}

fn bench_per_scale_dfl_i8_to_f32(suite: &mut BenchSuite) {
    let (decoder, tensors) = build_dfl_per_scale_bench_inputs(DecodeDtype::F32);
    let inputs: Vec<&TensorDyn> = tensors.iter().collect();

    let result = run_bench(
        "decoder/per_scale/dfl_i8_to_f32",
        WARMUP,
        ITERATIONS,
        || {
            let mut output_boxes = Vec::with_capacity(50);
            let mut masks = Vec::new();
            decoder
                .decode(&inputs, &mut output_boxes, &mut masks)
                .unwrap();
        },
    );
    result.print_summary();
    suite.record(&result);
}

fn bench_per_scale_dfl_i8_to_f16(suite: &mut BenchSuite) {
    let (decoder, tensors) = build_dfl_per_scale_bench_inputs(DecodeDtype::F16);
    let inputs: Vec<&TensorDyn> = tensors.iter().collect();

    let result = run_bench(
        "decoder/per_scale/dfl_i8_to_f16",
        WARMUP,
        ITERATIONS,
        || {
            let mut output_boxes = Vec::with_capacity(50);
            let mut masks = Vec::new();
            decoder
                .decode(&inputs, &mut output_boxes, &mut masks)
                .unwrap();
        },
    );
    result.print_summary();
    suite.record(&result);
}

fn bench_per_scale_ltrb_i8_to_f32(suite: &mut BenchSuite) {
    let (decoder, tensors) = build_ltrb_per_scale_bench_inputs(DecodeDtype::F32);
    let inputs: Vec<&TensorDyn> = tensors.iter().collect();

    let result = run_bench(
        "decoder/per_scale/ltrb_i8_to_f32",
        WARMUP,
        ITERATIONS,
        || {
            let mut output_boxes = Vec::with_capacity(50);
            let mut masks = Vec::new();
            decoder
                .decode(&inputs, &mut output_boxes, &mut masks)
                .unwrap();
        },
    );
    result.print_summary();
    suite.record(&result);
}

/// Process CPU time (all threads) consumed so far, where the platform
/// reports it.
#[cfg(unix)]
fn process_cpu_time() -> Option<std::time::Duration> {
    let mut ts = libc::timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };
    // SAFETY: `ts` is a valid out-pointer; the clock id is a constant
    // every supported Unix defines.
    let rc = unsafe { libc::clock_gettime(libc::CLOCK_PROCESS_CPUTIME_ID, &mut ts) };
    (rc == 0).then(|| std::time::Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32))
}

#[cfg(not(unix))]
fn process_cpu_time() -> Option<std::time::Duration> {
    None
}

/// `run_bench`, also printing the process CPU time per iteration, which
/// counts rayon workers (including their idle spinning) alongside the
/// calling thread.
fn run_bench_cpu<F: FnMut()>(
    suite: &mut BenchSuite,
    name: &str,
    warmup: usize,
    iterations: usize,
    f: F,
) {
    let cpu0 = process_cpu_time();
    let result = run_bench(name, warmup, iterations, f);
    let cpu1 = process_cpu_time();
    result.print_summary();
    if let (Some(cpu0), Some(cpu1)) = (cpu0, cpu1) {
        let cpu = (cpu1 - cpu0) / (warmup + iterations) as u32;
        println!("  {:50} cpu/iter={cpu:>10.1?}", "");
    }
    suite.record(&result);
}

/// Deterministic xorshift generator for synthetic model outputs.
struct XorShift(u64);

impl XorShift {
    fn next(&mut self) -> u32 {
        self.0 ^= self.0 << 13;
        self.0 ^= self.0 >> 7;
        self.0 ^= self.0 << 17;
        (self.0 >> 32) as u32
    }

    fn below(&mut self, n: u32) -> u32 {
        self.next() % n
    }
}

/// Per-scale int8 DFL detection model with a `width x height` input, three
/// levels at strides 8/16/32 and `nc` classes, as the EdgeFirst converter
/// declares it. Quantization matches captures of yolov8n smart exports on
/// the i.MX 95 (boxes about 0.12-0.15 per step, scores about 0.2).
fn realistic_per_scale_schema(width: usize, height: usize, nc: usize) -> SchemaV2 {
    let strides = [8usize, 16, 32];
    let total: usize = strides.iter().map(|s| (width / s) * (height / s)).sum();
    let child = |role: &str, i: usize, s: usize, c: usize, q: (f32, i32)| {
        let (h, w) = (height / s, width / s);
        let last = if role == "boxes" {
            serde_json::json!({"num_features": c})
        } else {
            serde_json::json!({"num_classes": c})
        };
        let mut v = serde_json::json!({
            "name": format!("{role}_{i}"), "type": role, "stride": s, "scale_index": i,
            "shape": [1, h, w, c],
            "dshape": [{"batch": 1}, {"height": h}, {"width": w}, last],
            "dtype": "int8",
            "quantization": {"scale": q.0, "zero_point": q.1, "dtype": "int8"}
        });
        if role == "scores" {
            v["activation_required"] = "sigmoid".into();
        }
        v
    };
    let boxes: Vec<_> = strides
        .iter()
        .enumerate()
        .map(|(i, &s)| child("boxes", i, s, 64, (0.147, 8)))
        .collect();
    let scores: Vec<_> = strides
        .iter()
        .enumerate()
        .map(|(i, &s)| child("scores", i, s, nc, (0.2, 110)))
        .collect();
    let schema = serde_json::json!({
        "schema_version": 2,
        "nms": "class_agnostic",
        "input": {
            "shape": [1, height, width, 3],
            "dshape": [{"batch": 1}, {"height": height}, {"width": width}, {"num_features": 3}],
            "cameraadaptor": "rgb"
        },
        "outputs": [
            {
                "name": "boxes", "type": "boxes", "shape": [1, 4, total],
                "dshape": [{"batch": 1}, {"box_coords": 4}, {"num_boxes": total}],
                "encoding": "dfl", "decoder": "ultralytics", "normalized": true,
                "outputs": boxes
            },
            {
                "name": "scores", "type": "scores", "shape": [1, nc, total],
                "dshape": [{"batch": 1}, {"num_classes": nc}, {"num_boxes": total}],
                "score_format": "per_class", "decoder": "ultralytics",
                "outputs": scores
            }
        ]
    });
    serde_json::from_value(schema).expect("schema parse")
}

/// Synthetic int8 outputs for [`realistic_per_scale_schema`], shaped like
/// captured yolov8n frames: each DFL side peaks at one bin and falls off
/// by about 0.4-1.2 logits per bin, about 0.3% of anchors carry a class
/// above 0.25, another 2% sit between about 0.05 and 0.25, and the rest
/// score below 0.02.
fn realistic_per_scale_inputs(schema: &SchemaV2) -> Vec<TensorDyn> {
    let mut rng = XorShift(0x9E37_79B9_7F4A_7C15);
    let mut tensors = Vec::new();
    for logical in &schema.outputs {
        use edgefirst_decoder::schema::LogicalType;
        let role = match logical.type_ {
            Some(LogicalType::Boxes) => "boxes",
            Some(LogicalType::Scores) => "scores",
            _ => continue,
        };
        for child in &logical.outputs {
            let shape = child.shape.clone();
            let c = shape[3];
            let mut t = Tensor::<i8>::new(&shape, Some(TensorMemory::Mem), None).unwrap();
            {
                let mut m = t.map().unwrap();
                let v = m.as_mut_slice();
                for row in v.chunks_exact_mut(c) {
                    if role == "boxes" {
                        for side in row.chunks_exact_mut(16) {
                            let peak = rng.below(16) as i32;
                            let top = 20 + rng.below(50) as i32;
                            let fall = 3 + rng.below(6) as i32;
                            for (i, b) in side.iter_mut().enumerate() {
                                let noise = rng.below(9) as i32 - 4;
                                let x = top - fall * (i as i32 - peak).abs() + noise;
                                *b = x.clamp(-128, 127) as i8;
                            }
                        }
                    } else {
                        for b in row.iter_mut() {
                            *b = (40 + rng.below(50)) as i8;
                        }
                        let r = rng.below(1000);
                        let class = rng.below(c as u32) as usize;
                        if r < 3 {
                            row[class] = (106 + rng.below(22)) as i8;
                        } else if r < 23 {
                            row[class] = (96 + rng.below(9)) as i8;
                        }
                    }
                }
            }
            let q = child.quantization.as_ref().expect("child quantization");
            t.set_quantization(TQ::per_tensor(
                q.scale[0],
                q.zero_point.as_ref().map_or(0, |z| z[0]),
            ))
            .unwrap();
            tensors.push(TensorDyn::I8(t));
        }
    }
    tensors
}

/// The per-scale DFL pipeline on realistic int8 outputs, for the 960x544
/// four-class model (levels of 8160/2040/510 anchors, none a multiple of
/// the prefilter's pack) and the 640x640 COCO model. `dense` decodes every
/// anchor; `prefilter` passes the 0.25 score threshold the way `decode`
/// does; `decode` adds the candidate filter and NMS.
fn bench_per_scale_realistic(suite: &mut BenchSuite) {
    for (label, width, height, nc) in [("960x544", 960, 544, 4), ("640x640", 640, 640, 80)] {
        let schema = realistic_per_scale_schema(width, height, nc);
        let tensors = realistic_per_scale_inputs(&schema);
        let inputs: Vec<&TensorDyn> = tensors.iter().collect();
        let decoder = DecoderBuilder::default()
            .with_schema(schema)
            .with_decode_dtype(DecodeDtype::F32)
            .with_iou_threshold(0.7)
            .with_score_threshold(0.25)
            .with_nms(Some(Nms::ClassAgnostic))
            .build()
            .expect("decoder build");

        let kept = decoder._bench_run_per_scale(&inputs, Some(0.25)).unwrap();
        let mut boxes = Vec::new();
        decoder
            .decode(&inputs, &mut boxes, &mut Vec::new())
            .unwrap();
        println!(
            "  per_scale {label}: prefilter keeps {kept} anchors, {} detections",
            boxes.len()
        );

        for (mode, threshold) in [("dense", None), ("prefilter", Some(0.25))] {
            run_bench_cpu(
                suite,
                &format!("decoder/per_scale/{label}/{mode}"),
                WARMUP,
                ITERATIONS * 3,
                || {
                    decoder._bench_run_per_scale(&inputs, threshold).unwrap();
                },
            );
        }
        run_bench_cpu(
            suite,
            &format!("decoder/per_scale/{label}/decode"),
            WARMUP,
            ITERATIONS * 3,
            || {
                let mut output_boxes = Vec::with_capacity(50);
                decoder
                    .decode(&inputs, &mut output_boxes, &mut Vec::new())
                    .unwrap();
            },
        );
    }
}

/// One 120x68 level (the largest of a 960x544 input) of realistic DFL bins
/// through each 8-bit DFL box kernel, reported per anchor.
fn bench_per_scale_dfl_kernels(suite: &mut BenchSuite) {
    use edgefirst_decoder::per_scale::bench::{DflKernel, DflLevel};
    let level = DflLevel::new(120, 68, 8.0, 0.147, 8);
    let n = level.anchors();
    let mut rng = XorShift(0x2545_F491_4F6C_DD1D);
    let mut input = vec![0i8; n * 64];
    for side in input.chunks_exact_mut(16) {
        let peak = rng.below(16) as i32;
        let top = 20 + rng.below(50) as i32;
        let fall = 3 + rng.below(6) as i32;
        for (i, b) in side.iter_mut().enumerate() {
            let noise = rng.below(9) as i32 - 4;
            *b = (top - fall * (i as i32 - peak).abs() + noise).clamp(-128, 127) as i8;
        }
    }
    let mut reference = vec![0f32; n * 4];
    level.decode(DflKernel::Softmax, &input, &mut reference);
    let mut dst = vec![0f32; n * 4];
    for kernel in DflKernel::available() {
        let name = format!("decoder/per_scale/dfl_kernel/{kernel:?}");
        let result = run_bench(&name, WARMUP, ITERATIONS, || {
            level.decode(kernel, &input, &mut dst);
        });
        let err = dst
            .iter()
            .zip(&reference)
            .map(|(a, b)| (a - b).abs())
            .fold(0f32, f32::max);
        result.print_summary();
        println!(
            "  {:50} ns/anchor={:>7.1}  max |Δ| vs Softmax={err:.2e} px",
            "",
            result.median().as_nanos() as f64 / n as f64
        );
        suite.record(&result);
    }
}

/// SegFormer-B1-shaped logits (150 classes, 128x168, NCHW) over a Voronoi
/// class field. Calibrated to SegFormer-B1 on ADE20K val: about 7% of
/// low-resolution cells straddle a class boundary, where a few classes score
/// close to the winner (near -2) and the rest sit far below.
fn semantic_logits() -> Vec<f32> {
    const C: usize = 150;
    const H: usize = 128;
    const W: usize = 168;
    let mut seed = 0x2164u64;
    let mut next = || {
        seed = seed
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        (seed >> 33) as u32
    };
    let sites: Vec<(f32, f32, usize)> = (0..24)
        .map(|_| {
            let y = (next() % H as u32) as f32;
            let x = (next() % W as u32) as f32;
            (y, x, next() as usize % C)
        })
        .collect();
    let mut v = vec![0f32; C * H * W];
    let mut dist = [0f32; 24];
    for y in 0..H {
        for x in 0..W {
            for (d, s) in dist.iter_mut().zip(&sites) {
                *d = ((s.0 - y as f32).powi(2) + (s.1 - x as f32).powi(2)).sqrt();
            }
            let nearest = dist.iter().copied().fold(f32::INFINITY, f32::min);
            for c in 0..C {
                let u = (next() % 1024) as f32 / 1024.0;
                v[(c * H + y) * W + x] = u * 12.0 - 30.0;
            }
            // A site's class fades with its distance beyond the nearest site.
            for (d, s) in dist.iter().zip(&sites) {
                let i = (s.2 * H + y) * W + x;
                v[i] = v[i].max(-2.0 - 1.5 * (d - nearest));
            }
        }
    }
    v
}

fn bench_semantic(suite: &mut BenchSuite) {
    use edgefirst_decoder::schema::DecodeMode;
    use edgefirst_decoder::semantic::{decode_semantic, SemanticConfig, SemanticOptions};
    use edgefirst_tensor::DType;

    let shape = [1, 150, 128, 168];
    let logits = semantic_logits();
    let f32_nchw = TensorDyn::F32(Tensor::<f32>::from_slice(&logits, &shape).unwrap());
    // Logits span [-30, -1]: scale 0.125 with zero point 112 fits i8.
    let q: Vec<i8> = logits
        .iter()
        .map(|&x| (x * 8.0 + 112.0).round().clamp(-128.0, 127.0) as i8)
        .collect();
    let mut i8_nchw = TensorDyn::I8(Tensor::<i8>::from_slice(&q, &shape).unwrap());
    i8_nchw
        .set_quantization(TQ::per_tensor(0.125, 112))
        .unwrap();
    let mut nhwc = vec![0u8; q.len()];
    for c in 0..150 {
        for p in 0..128 * 168 {
            nhwc[p * 150 + c] = (q[c * 128 * 168 + p] as i16 + 128) as u8;
        }
    }
    let mut u8_nhwc = TensorDyn::U8(Tensor::<u8>::from_slice(&nhwc, &[1, 128, 168, 150]).unwrap());
    u8_nhwc
        .set_quantization(TQ::per_tensor(0.125, 240))
        .unwrap();

    let nchw_config = SemanticConfig::scores(&shape, 1, 2, 3).unwrap();
    let nhwc_config = SemanticConfig::scores(&[1, 128, 168, 150], 3, 1, 2).unwrap();
    // A 683x512 ADE20K image letterboxed top-left into 512x672.
    let letterbox = [0.0, 0.0, 1.0, 504.0 / 512.0];
    let mut dst = TensorDyn::new(&[512, 683], DType::U8, Some(TensorMemory::Mem), None).unwrap();
    let mut conf = TensorDyn::new(&[512, 683], DType::U8, Some(TensorMemory::Mem), None).unwrap();

    let cases = [
        ("f32_nchw", &f32_nchw, &nchw_config),
        ("i8_nchw", &i8_nchw, &nchw_config),
        ("u8_nhwc", &u8_nhwc, &nhwc_config),
    ];
    for (label, input, config) in cases {
        for (mode_label, mode) in [
            ("accurate", DecodeMode::Accurate),
            ("fast", DecodeMode::Fast),
        ] {
            let options = SemanticOptions::new()
                .with_mode(mode)
                .with_content_region(letterbox);
            let name = format!("decoder/semantic/{label}/{mode_label}");
            let result = run_bench(&name, WARMUP, ITERATIONS, || {
                decode_semantic(input, config, &mut dst, None, &options).unwrap();
            });
            result.print_summary();
            suite.record(&result);
        }
    }
    let options = SemanticOptions::new().with_content_region(letterbox);
    let result = run_bench(
        "decoder/semantic/f32_nchw/accurate_confidence",
        WARMUP,
        ITERATIONS,
        || {
            decode_semantic(&f32_nchw, &nchw_config, &mut dst, Some(&mut conf), &options).unwrap();
        },
    );
    result.print_summary();
    suite.record(&result);
}

/// One benchmark function.
type Bench = fn(&mut BenchSuite);

fn main() {
    env_logger::init();

    println!("Decoder Benchmark — edgefirst-bench in-process harness (no fork)");
    println!("  warmup={WARMUP}  iterations={ITERATIONS}");

    let mut suite = BenchSuite::from_args();
    // `--section <name>` runs one section, matched case-insensitively.
    let only = std::env::args()
        .collect::<Vec<_>>()
        .windows(2)
        .find(|w| w[0] == "--section")
        .map(|w| w[1].to_ascii_lowercase());
    let sections: [(&str, &[Bench]); 10] = [
        ("YOLO", &[bench_yolo_quant, bench_yolo_f32]),
        (
            "Quant",
            &[
                bench_quant_decode_boxes,
                bench_quant_decode_boxes_multilabel,
                bench_quant_nms,
            ],
        ),
        (
            "Dequantize",
            &[
                bench_dequantize_i8,
                bench_dequantize_i8_chunked,
                bench_dequantize_i16,
                bench_dequantize_i16_chunked,
            ],
        ),
        ("Float", &[bench_f32_decode_boxes, bench_f32_nms]),
        ("ModelPack", &[bench_modelpack_u8, bench_modelpack_split_u8]),
        ("Masks", &[bench_masks_f32, bench_masks_i8]),
        ("Semantic", &[bench_semantic]),
        (
            "Per-Scale",
            &[
                bench_per_scale_dfl_i8_to_f32,
                bench_per_scale_dfl_i8_to_f16,
                bench_per_scale_ltrb_i8_to_f32,
            ],
        ),
        ("Per-Scale-Realistic", &[bench_per_scale_realistic]),
        ("DFL-Kernels", &[bench_per_scale_dfl_kernels]),
    ];
    for (name, benches) in sections {
        if only
            .as_ref()
            .is_some_and(|o| *o != name.to_ascii_lowercase())
        {
            continue;
        }
        println!("\n== {name} ==\n");
        for bench in benches {
            bench(&mut suite);
        }
    }

    suite.finish();
    println!("\nDone.");
}