From sound to audio tokens
Follow a clip from its waveform to the audio tokens the shared backbone can use.
The highlighted window spans 20 ms. The waveform uses a −1 to 1 amplitude scale.
Prepare audio
The pipeline prepares a 16 kHz waveform and turns it into a log-mel spectrogram: a picture of how the sound’s frequencies change over time.
Subsample
The encoder makes the spectrogram’s time sequence roughly four times shorter, so later layers have fewer steps to process.
Attend
Twelve Conformer layers use local attention. Each query sees itself and up to 11 earlier tokens.
Project
Each audio token is projected to 512 values. The pipeline passes the valid tokens to the shared backbone.
Look closer
How audio becomes tokens
Choose a tier and clip length to follow the audio through the encoder. A tier sets how much audio fits; it doesn’t change the token rate.
- SpectrogramSound over time
- SubsampleShorter sequence
- Encoder12 layers
- Audio tokens512 values each
125 valid audio tokens → 512 values each
79 of 204 slots are padding.
How the numbers work
To make the spectrogram, a 20 ms window moves through the waveform in 10 ms steps. Each complete window produces a frame with 128 frequency bands. A one-second clip gives 99 frames.
Subsampling reduces the frame sequence by roughly four. The original model groups the resulting steps into chunks of 12 for attention; each tier is sized to fit whole chunks. Padding fills unused slots and is left out before the backbone.
At 5.00 s: 80,000 samples → 499 frames → 125 valid tokens. The a8 tier has 816 frame slots and 204 token slots in 17 chunks.
The full tier shapes are (1, 816, 128) at the spectrogram, (1, 204, 1024) after subsampling, (1, 204, 1024) after the encoder layers, and (1, 204, 512) after projection. The first number is the batch size; the others are time steps and values per step.
How audio tokens attend
Select a token to see where it can look: itself and up to 11 earlier tokens, even across a chunk boundary. It cannot look ahead.
What the Core ML export revealed
One extra token changed the result
The attention window included one token too many. The cosine similarity against PyTorch fell to about 0.94; correcting the boundary restored 1.000000 in the standalone check.
Keeping the subsampler aligned
Adding explicit zero biases to its normalization layers kept Core ML aligned with PyTorch. Fixed dimensions also avoided a conversion error.
Working with precision
The checkpoint uses BF16 weights. The export uses FP16 with some calculations kept in FP32; the comparisons below show how closely the resulting outputs match PyTorch.
Gathering attention context
The original model uses unfold to select neighboring positions. For the fixed-size Core ML export, we precompute their indices and gather them instead.
Benchmarks per tier
Core ML runs were measured in isolated processes on Apple Silicon. Physical footprint is the worker’s peak dirty and compressed memory (excluding clean mapped weight files; includes ~290 MB of Python runtime); the end-to-end cases include both the audio encoder and backbone. Cosine similarity compares the Core ML and PyTorch outputs for the same input; values closer to 1 indicate a closer match in direction.
1. Standalone Core ML Audio (models/audio_fp16.mlmodelc)
| Tier | Clip (Valid Tokens) | Warm Load (Cold) | Latency | Physical footprint | Cosine similarity |
|---|---|---|---|---|---|
a8 | 1.5 s (38) | — (1.83 s) | 22.3 ms | 424 MB | 0.999989 |
a8 | 8.0 s (200) | 0.24 s | 15.4 ms | 424 MB | 0.999985 |
a16 | 12.0 s (300) | — (1.77 s) | 24.9 ms | 459 MB | 0.999987 |
a16 | 16.0 s (400) | 0.24 s | 19.4 ms | 459 MB | 0.999975 |
a30 | 25.0 s (625) | — (1.79 s) | 29.4 ms | 510 MB | 0.999867 |
a30 | 30.0 s (750) | 0.24 s | 25.5 ms | 510 MB | 0.999949 |
2. Audio → Backbone compared with PyTorch
The two-clip case runs the audio encoder once per clip, then sends both sets of valid tokens to the backbone together.
| Case | Clips | Backbone Tier | Cosine similarity | Max Abs Error | Physical footprint |
|---|---|---|---|---|---|
single_audio_a8_s512 | 4.0 s (100 toks) | s512 | 0.999999 | 2.30e-04 | 552 MB |
single_audio_a30_s2048 | 25.0 s (625 toks) | s2048 | 0.999990 | 5.46e-04 | 1.0 GB |
multi_audio_a8_a16_s2048 | 3.0 s + 12.0 s (375 toks) | s2048 | 0.999996 | 2.98e-04 | 1.0 GB |
Terms used above
- Waveform
- The sound represented as samples over time. At 16 kHz, there are 16,000 samples each second.
- Log-mel spectrogram
- A representation of how the strength of different frequency ranges changes over time.
- Mel bins
- Frequency ranges in the spectrogram. Each frame records a value for each of the 128 ranges.
- Subsampling
- Reducing the length of the spectrogram’s time sequence before the encoder layers process it.
- Token and padding
- A token is one step in the encoder’s output. Padding fills unused slots in a fixed-size tier and is left out when the pipeline passes tokens to the backbone.
- Conformer
- An encoder layer that combines self-attention with convolution to capture both fine-grained and wider audio context.
- Local causal attention
- Each token can look at itself and up to 11 earlier tokens, but not later ones.
Build and verify
# 1. Verify PyTorch wrapper parity
uv run python -m audio.verify_pytorch
# 2. Export multifunction FP16 Core ML package (561 MB)
uv run python -m audio.export
# 3. Compile to .mlmodelc
xcrun coremlcompiler compile models/audio_fp16.mlpackage models/
# 4. Verify Core ML parity, latency, peak memory, and Audio → Backbone handoff
uv run python -m audio.verify_coreml --model models/audio_fp16.mlmodelc --backbone models/backbone_fp16.mlmodelc