From sound to audio tokens

Follow a clip from its waveform to the audio tokens the shared backbone can use.

One second of a rising tone
Waveform
Log-mel spectrogram
0 ms250 ms500 ms750 ms1 s

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.

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.

  1. SpectrogramSound over time
  2. SubsampleShorter sequence
  3. Encoder12 layers
  4. Audio tokens512 values each
Tier up to 8.17 s
5.00 s

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.

First chunk (0–11)  |  Second chunk (12–23) Query q = 14 (Block 1, offset 2) → attends to k = 3..14 (12 keys)
Selected token Earlier tokens it can attend to Other available positions, masked Outside the available positions
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
a81.5 s (38)— (1.83 s)22.3 ms424 MB0.999989
a88.0 s (200)0.24 s15.4 ms424 MB0.999985
a1612.0 s (300)— (1.77 s)24.9 ms459 MB0.999987
a1616.0 s (400)0.24 s19.4 ms459 MB0.999975
a3025.0 s (625)— (1.79 s)29.4 ms510 MB0.999867
a3030.0 s (750)0.24 s25.5 ms510 MB0.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_s5124.0 s (100 toks)s5120.9999992.30e-04552 MB
single_audio_a30_s204825.0 s (625 toks)s20480.9999905.46e-041.0 GB
multi_audio_a8_a16_s20483.0 s + 12.0 s (375 toks)s20480.9999962.98e-041.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