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
//! Native CoreML **CED** (tiny/mini/small/base) AudioSet sound-event tagging —
//! coremlit's first multi-label classifier: 16 kHz mono waveform in, ranked
//! AudioSet predictions out (527 rated classes: name + permanent `SoundEventId`
//! + `/m/…` mid + class index + sigmoid confidence), long clips via windowed
//! chunking + Mean/Max aggregation.
//!
//! CED (Consistent Ensemble Distillation, arXiv 2308.11957; upstream
//! RicherMans/CED, `mispeech/ced-{tiny,mini,small,base}`) is a distilled
//! AudioSet transformer. The four sizes are contract-identical here — one
//! size-invariant mel→logits I/O; they differ only in internal transformer
//! width (see [`CedModel`]). The mel front-end runs in Rust (the private `mel`
//! submodule) and the mel→logits transformer runs natively on Apple silicon as
//! one fp16 `.mlmodelc` — an in-graph STFT/mel is the exact fragility class
//! behind the ORT CoreML EP zeroed-logits bug this feature closes. NO `ort`
//! anywhere.
//!
//! Design spec: `docs/superpowers/specs/2026-07-23-ced-native-ane-design.md`.
//!
//! # Model artifacts
//!
//! No model is bundled (a `.mlmodelc` is a directory artifact). Each size's
//! fp16 CED graph is converted owner-side (Wave B), distributed via Hugging
//! Face, and staged as a gitignored dev-time download under
//! `Models/ced/ced-<size>/` (env override `CED_TEST_MODELS` points at the
//! `Models/ced` family root); per-file SHA-256 and I/O contract are pinned per
//! size by `tests/ced/model_io.rs` once staged. [`CedModel`] owns the repo ids
//! and the `<dir>/<bundle>` path spelling ([`CedModel::mlmodelc_path`]). The
//! four graphs are I/O-identical, so model identity is caller-supplied:
//! coremlit cannot — and does not — detect which size a `.mlmodelc` is.
//!
//! # Rust front-end around an fp16 CoreML graph
//!
//! The graph takes the believed `[1, 64, 1001]` log-mel (`mel`, f32) computed
//! by this module's Rust front-end and emits `[1, 527]` **pre-sigmoid** logits
//! (`logits`, f32); sigmoid, ranking, and long-clip aggregation run in Rust.
//! The believed mel numerics are probe-pinned in Wave B (see the `mel`
//! submodule docs). This `[1, 64, 1001]` shape is shared by all four sizes.
//!
//! Upstream's `target_length = 1012` is NOT this input width: it is the
//! transformer's time positional-embedding capacity and its long-form mel
//! chunk size. A canonical 10 s window is 160 000 samples → 1001 mel frames
//! (hop 160, `center=True`), consumed unpadded with the pos embed sliced to 62
//! of its 63 patch columns; padding to 1012 would compute a different
//! function. So `1001 <= 1012` is on-distribution, not a truncation (verified
//! against RicherMans/CED `audiotransformer.py` and the mispeech feature
//! extractor); the `mel` submodule carries the full derivation.
//!
//! # From window scores to events
//!
//! [`Classifier::classify_windows`] is the seam between this crate and event
//! detection. It returns `Vec<`[`WindowConfidences`]`>`, and
//! [`WindowConfidences`] *is* [`windit::windowed::Windowed`]`<`[`Confidences`]`>`
//! — just as [`Span`] *is* [`windit::plan::Span`]. coremlit's long-clip output
//! is already `windit`'s own value type, so the post-processing stack composes
//! with no adapter and no repacking:
//!
//! ```text
//! Classifier::classify_windows -> Vec<Windowed<Confidences>> coremlit (this module)
//! index one class -> Vec<Windowed<f32>> one slice read per window
//! windit::smooth -> Vec<Windowed<f32>> Ema / CadenceEma
//! zuoer::RunSegmenter -> Run { span, mean, peak } hysteresis + durations
//! ```
//!
//! ## coremlit ships no CED convenience layer, deliberately
//!
//! `audio::vad` offers a one-call `detect_speech` because Silero has
//! *upstream-authored* defaults — 0.5 threshold, 250 ms minimum speech, 100 ms
//! minimum silence — that `zuoer`'s hysteresis derives from, so "the default"
//! there is a real, attributable thing. CED's 527 classes have no equivalent. A
//! threshold and minimum duration right for `Glass` (class 441, a sub-second
//! transient) are wrong for `Music` (class 137, continuous for minutes); a hop
//! fine enough to localize a single bark wastes an order of magnitude of
//! inference on ambient scene tagging. Any defaults coremlit shipped would be
//! silently wrong in some scenario, and silently wrong is the failure mode a
//! classifier can least afford.
//!
//! So the glue below stays in your application. It is about a dozen lines, and
//! **every parameter in it is scenario-dependent** — which is precisely why it
//! is not a coremlit function. This module chooses no threshold, no smoothing
//! constant, no minimum event duration, and no set of classes to watch.
//!
//! ## Per-class orchestration is the consumer's job
//!
//! CED emits 527 *independent* sigmoids per window: not a softmax, and they do
//! not sum to one. An event detector picks the handful of classes it cares
//! about and runs one independent smoother + segmenter per class, each with its
//! own parameters. Running all 527 is possible but rarely wanted — a real
//! recording has a sparse active set at any moment, and 527 segmenters is 527
//! parameter sets nobody has tuned.
//!
//! ## Timestamps are window-resolution, not event-resolution
//!
//! Each score summarizes a whole [`WINDOW_SAMPLES`] (10 s) window, and a
//! segmenter treats it as one point sample placed at that window's start. Run
//! boundaries are therefore quantized to [`WindowPlan::hop_samples`], and the
//! audio a run actually observed is its reported interval extended forward by
//! one whole window — so an event reported at `3 s..8 s` happened somewhere in
//! `3 s..18 s`. A shorter hop buys finer quantization, never a narrower smear.
//!
//! ## Dependencies
//!
//! `windit` and `zuoer` are coremlit's own dependencies. Only [`Span`] and
//! [`WindowConfidences`] cross into *this* module's API, and this module
//! re-exports no smoothing tier, so depend on `windit` directly. (Under the
//! `clap` feature, `embeddings::clap::smooth` does re-export windit's smoothing
//! seam — but only the parts a 512-wide *embedding* can use: the scalar `Ema`
//! and `CadenceEma` this table names are not among them.)
//!
//! ```toml
//! windit = "0.4" # smoothing; already in your graph via `ced`
//! ```
//!
//! `zuoer` is the other case. With the `vad` feature on, `audio::vad`
//! re-exports the whole set needed to drive a segmenter — `Run`,
//! `RunSegmenter`, `RunOptions`, `SampleRate` and its `Error` / `Result` — so
//! the segmenting block below names them through coremlit and needs no direct
//! dependency. Under `ced` alone, `zuoer` is not in your graph at all, and you
//! add it yourself:
//!
//! ```toml
//! zuoer = "0.2" # only for `ced` WITHOUT `vad`
//! ```
//!
//! `windit` also ships its own gate/segment tier
//! (`windit::segment::{Hysteresis, Segmenter, SegmentOptions}`, composed by
//! `windit::decode`) which needs no extra dependency at all. It returns element
//! `Range`s and *no* probability aggregates, so prefer it when a plain interval
//! is enough, and `zuoer::RunSegmenter` when the event needs a confidence
//! attached — which is what the rest of this section shows.
//!
//! ## Scoring the clip
//!
//! Loading a model and running it is the ONE step that needs a staged
//! `.mlmodelc`, so this block — and only this block — is `no_run`:
//! `cargo test --doc` **compiles it and never executes it**. Nothing in it is
//! verified behavior. Everything downstream of it is, because everything
//! downstream of it is arithmetic on the returned numbers:
//!
//! ```no_run
//! use coremlit::audio::ced::{CedModel, Classifier, WindowConfidences, WindowPlan};
//!
//! # let samples_16k: Vec<f32> = Vec::new();
//! let classifier = Classifier::from_file(CedModel::Small.mlmodelc_path("Models/ced"))?;
//! // A 1 s hop across the fixed 10 s window: 90% overlap, one score per second.
//! let plan = WindowPlan::new().with_hop_samples(16_000);
//! let windows: Vec<WindowConfidences> = classifier.classify_windows(&samples_16k, &plan)?;
//! # Ok::<(), coremlit::audio::ced::Error>(())
//! ```
//!
//! ## Projecting one class, and smoothing it
//!
//! [`Confidences::try_from_slice`] builds that `windows` vector by hand, which
//! is what lets the rest of the pipeline **run** here with no model staged —
//! and what lets a consumer unit-test their own event logic the same way:
//!
//! ```
//! use coremlit::audio::ced::{
//! Confidences, Error, NUM_CLASSES, RatedSoundEvent, Span, WINDOW_SAMPLES, WindowConfidences,
//! };
//! use windit::{
//! smooth::{Ema, SmoothPolicy},
//! windowed::Windowed,
//! };
//!
//! let hop = 16_000; // the `WindowPlan` hop the scores were produced at
//! let dog = RatedSoundEvent::from_key("Dog")[0].index();
//! let music = RatedSoundEvent::from_key("Music")[0].index();
//! assert_eq!((dog, music), (74, 137));
//!
//! // Twelve windows of 527 scores, standing in for `classify_windows` output.
//! // `Music` outscores `Dog` in every one of them: the 527 sigmoids are
//! // independent, so two classes can both be loud and they never sum to one.
//! let barks = [0.02, 0.04, 0.71, 0.86, 0.31, 0.90, 0.88, 0.09, 0.03, 0.01, 0.01, 0.02];
//! let windows = barks
//! .iter()
//! .enumerate()
//! .map(|(i, &p)| {
//! let mut scores = vec![0.0; NUM_CLASSES];
//! scores[dog] = p;
//! scores[music] = 0.93;
//! Ok(WindowConfidences::new(
//! Confidences::try_from_slice(&scores)?,
//! Span::new(i * hop, WINDOW_SAMPLES, WINDOW_SAMPLES),
//! ))
//! })
//! .collect::<Result<Vec<WindowConfidences>, Error>>()?;
//!
//! // Stored exactly as handed over: `try_from_slice` takes confidences, not
//! // logits, so it applies no sigmoid (which would read 0.7027 here) and no
//! // renormalization (0.4804 here, and a sum that could never pass one).
//! assert_eq!(windows[3].value().as_slice()[dog], 0.86);
//! assert!(windows[3].value().as_slice().iter().sum::<f32>() > 1.0);
//!
//! // One column out of 527. The span rides along untouched.
//! let track: Vec<Windowed<f32>> = windows
//! .iter()
//! .map(|w| Windowed::new(w.value().as_slice()[dog], w.span()))
//! .collect();
//!
//! // The asked-for class, never the loudest one — projecting an argmax would
//! // have followed `Music` and returned a flat 0.93 track.
//! assert_eq!(*track[3].value(), 0.86);
//! assert_eq!(track[3].span(), windows[3].span());
//!
//! // `Ema`'s alpha is per push; `CadenceEma` denominates its time constant in
//! // input samples instead, so one setting survives an irregular hop.
//! let smoothed = Ema::new(0.6).smooth(&track)?;
//!
//! // Spans are preserved and values rewritten: the lone 0.31 dip at window 4
//! // lifts to ~0.46, so a 0.5/0.35 hysteresis will not tear the event in two.
//! // Unsmoothed that window still reads 0.31; read alpha as the decay weight
//! // rather than the innovation weight and it reads 0.4387.
//! assert_eq!(smoothed[4].span(), track[4].span());
//! assert!((smoothed[4].value() - 0.46).abs() < 5e-3);
//! # Ok::<(), Error>(())
//! ```
//!
//!
//! # Compute placement (measured, never marketed)
//!
//! [`DEFAULT_COMPUTE`] ships as [`crate::ComputeUnits::All`], MEASURED: the
//! Wave-C pass (`tests/ced/placement.rs`) characterized per-unit parity and
//! latency across all four sizes and this default is what it pinned. See
//! [`DEFAULT_COMPUTE`] for the numbers.
//!
//! # Performance: construct once, reuse, prewarm
//!
//! Construction pays model load/specialization; [`Classifier::prewarm`] runs
//! one throwaway inference to absorb first-prediction specialization before
//! serving. Fan-out is one [`Classifier`] per worker ([`crate::Model`] is
//! `Send` but deliberately not `Sync`).
//!
//! macOS only (built on [`crate`]).
use Path;
use crate::;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
use crate;
/// The sample rate this module's contract is defined at: callers decode and
/// resample to **16 kHz mono f32** before calling (sans-I/O — the workspace
/// convention; CED natively matches it).
pub const SAMPLE_RATE_HZ: u32 = 16_000;
/// The fixed inference-window length in samples: 160 000 = 10 s at 16 kHz,
/// CED's training window. The CoreML export is fixed-shape, so this is model
/// geometry, not a knob (soundevents exposes `window_samples` only because its
/// ONNX graph is dynamic-length — recorded non-goal).
pub const WINDOW_SAMPLES: usize = 160_000;
/// Number of AudioSet classes the model scores: the 527 released rated classes.
/// Compile-time-pinned to `RatedSoundEvent::events().len()` below, so the
/// dataset crate and this module can never drift apart silently.
pub const NUM_CLASSES: usize = 527;
const _: = assert!;
/// Default compute placement: [`ComputeUnits::All`].
///
/// MEASURED, not provisional: the Wave-C placement pass
/// (`tests/ced/placement.rs`) characterized every unit (`CpuOnly`,
/// `CpuAndGpu`, `CpuAndNeuralEngine`, `All`) across all four sizes. Every
/// unit agrees with the `CpuOnly` reference at ≥ 0.99999 cosine and is
/// NaN-free, and warm latency is flat across units (~0.6–0.8 s/clip,
/// dominated by the Rust mel front end, not the CoreML forward) — so
/// `CpuAndGpu` is not faster here, contra the spec's original expectation.
/// The default `All` arm is in fact the numerically
/// *tightest* vs the committed PyTorch fp32 goldens
/// (`tests/ced/parity_logits.rs`: worst cos ~0.99999988, max|Δlogit| ~0.03),
/// and unlike siglip's vision tower the `CpuAndNeuralEngine` arm did not
/// collapse either, so `All` stays the default. Only a *measured* per-size
/// divergence would promote this to a per-[`CedModel`] table; Wave-C found
/// none, so one shared default stands.
pub const DEFAULT_COMPUTE: ComputeUnits = All;
/// Declared feature names on the CED `.mlmodelc` (pinned by
/// `tests/ced/model_io.rs`). Wave A DECLARES these; the Wave-B export must
/// emit exactly them (we own the conversion), or they change with the probe —
/// the recorded rework seam.
/// Construction options for the CED [`Classifier`] (rust-options-pattern): a
/// single `compute` knob with one source of truth shared by
/// `const new`/`Default`.
/// CED sound-event classifier: 16 kHz mono `&[f32]` in, ranked AudioSet
/// predictions out. Loads any of the four [`CedModel`] sizes — they share one
/// mel→logits contract, so this type is size-agnostic and stores no identity.
///
/// The front-end is a Rust log-mel port (the private `mel` submodule); the
/// fp16 CoreML transformer maps the believed `[1, 64, 1001]` mel to `[1, 527]`
/// PRE-sigmoid logits, and sigmoid + ranking run in Rust.
///
/// Point [`Self::from_file`] / [`Self::load`] at the size you staged, composing
/// the path with [`CedModel::mlmodelc_path`]:
///
/// ```no_run
/// use coremlit::audio::ced::{CedModel, Classifier};
/// let models_root = "Models/ced";
/// Classifier::from_file(CedModel::Small.mlmodelc_path(models_root))?;
/// # Ok::<(), coremlit::audio::ced::Error>(())
/// ```
///
/// `&self` inference (no mutable scratch): the FFT plan and filterbank are
/// built once at load and per-call buffers are local, so fan-out means one
/// [`Classifier`] per worker over a `Send` [`crate::Model`] (`crate::Model` is
/// deliberately `!Sync`).
/// Reject a per-window input the pipeline must not see: empty (nothing to
/// classify), longer than the fixed window (never silently truncated — long
/// clips are windowed explicitly), or carrying a NaN/±∞ sample (it would
/// silently poison the mel). Free fn so the guards are hermetically testable
/// without a model.
/// Reject a NaN/±∞ sample ([`Error::NonFiniteInput`]) — it would silently
/// poison the mel. The finite-scan shared by [`validate_window_input`] (the
/// single-window path) and `Classifier::classify_long`'s `k == 0` early
/// return, which must skip `validate_window_input`'s `AudioTooLong` bound (a
/// long clip is expected to exceed [`WINDOW_SAMPLES`]) but must still not
/// wave a NaN/∞ clip through as an empty result.
/// Classify a NaN/∞ the CoreML runtime produced as model-output corruption
/// ([`Error::NonFiniteOutput`]) before it can reach sigmoid — a NaN logit
/// would silently rank via `total_cmp` and poison downstream aggregation.
/// The load contract this door states: `mel` `[1, 64, 1001]` f32 in,
/// `logits` `[1, 527]` f32 out, no state.
///
/// Data rather than a sequence of checks, and the ONLY thing
/// [`Classifier::load`] does beyond calling [`Model::load`]. The four
/// hand-written comparisons this replaced — a presence test and a
/// shape-and-dtype test per feature — were each a check `load` could forget to
/// make, and deleting any of them failed no runnable test. A [`Checked`] field
/// turns that mutation into a compile error; what remains here is the door's
/// own numbers.
///
/// Every axis is [`Dim::Exactly`], and that buys more than the numbers.
/// [`crate::FeatureInfo::shape`] reports the DEFAULT shape of a flexible
/// input, so a `RangeDims` graph converted at `[1, 64, 1001]` declares this
/// contract's exact numbers — and a flexible input is what takes a graph off
/// the accelerator. An all-`Exactly` contract therefore requires the whole
/// feature to be [`crate::ShapeConstraint::Fixed`], which is the only thing
/// that separates the two. Nothing here is read back off the artifact: the
/// four CED sizes are contract-identical, so every number is this door's.
///
/// Built rather than `const` because a [`LoadContract`] owns its axes.
/// Map a [`ContractViolation`] into this module's error vocabulary.
///
/// The two "unsatisfiable" clauses keep their own variants — they are about
/// what the door cannot SUPPLY, not about a feature's declared shape — and the
/// per-feature clauses all land in [`Error::ContractMismatch`], which already
/// carries a feature name and a rendered expected/actual pair. An output the
/// model declares OPTIONAL is one of those: it is a fact about the named
/// feature's declaration, so "expected a required output, got optional" is the
/// shape that pair was made for.
///
/// `ContractViolation::rendered` performs that reduction, so a clause added to
/// the checker later lands in the `Feature` arm rather than breaking this
/// function and its five siblings at once.