lab

401 → 163 ms: Stable Audio 3 medium on one TPU v6e chip

Pencil drawing: memory slabs wired to a chip by a tangle of round trips, one clean line through a fused tile, a waveform leaving
Pencil drawing: memory slabs wired to a chip by a tangle of round trips, one clean line through a fused tile, a waveform leaving

Stable Audio 3 medium, a JAX port, bf16, a 120 s clip, batch 1, 8 sampling steps, text encoder and audio decoder included, on one Colab TPU v6e-1. From a frozen baseline of 401.4 ms to 163.4 ms — 2.46×, 1.26× faster than Stability's own TensorRT fp8 engine on an RTX PRO 6000 (205.2 ms). A full 380 s song: 1766 → 674 ms, also ahead of TensorRT fp8 (693 ms). Every kept change passed a quality gate against the official PyTorch output. Numbers are warm p50s around jax.block_until_ready(), each A/B in one process.

1. Result

stage 120 s clip diffusion transformer decoder vs baseline
frozen baseline 401.4 ms 160.4 230.0 —
fused decoder attention 256.0 ms 158.9 90.0 1.57×
decoder tiling + cached cross-attention keys 228.8 ms 153.8 70.8 1.75×
fused transformer attention 198.5 ms 135.5 61.9 2.03×
keys prepared once per head, both heads as one operand 163.4 ms 99.6 60.9 2.46×
Latency gains per change, 401.4 to 163.4 ms
Latency gains per change, 401.4 to 163.4 ms
Diffusion transformer and decoder time, baseline against now
Diffusion transformer and decoder time, baseline against now

For scale, on an RTX PRO 6000 (different hardware — an implementation reference): TensorRT fp16 274.2 ms, TensorRT fp8 205.2 ms, official PyTorch 474.7 ms, diffusers 981 ms. TensorRT renders 5 % fewer latents for the same 120 s.

A 120 s clip end to end, sa3jax against GPU engines
A 120 s clip end to end, sa3jax against GPU engines
Latency by clip length, sa3jax against TensorRT fp8
Latency by clip length, sa3jax against TensorRT fp8

2. A gate before any speed

Tokens are not images: audio quality needs its own instrument. 16 prompts × 2 seeds, 120 s each, with all four random inputs injected identically into both implementations (initial latent, the sampler's per-step re-noise, the decoder's dither and mask noise) — seeds cannot pair runs across PyTorch's and JAX's random generators. Scored with multi-resolution STFT and log-mel distances and MERT embeddings. Thresholds were set from the official code disagreeing with itself (swapping its attention kernel), never picked.

The gate paid for itself before optimisation began. The port scored halfway to "different music". Fed identical inputs in fp32, one transformer forward matched PyTorch to 88.9 dB and the decoder to 99.0 dB — the port was right; precision was not. Op-by-op bisection found it: the decoder's DyT normalisation in bf16. Its trained gains are large and its softmax is nearly an argmax, so four bf16 roundings per call flip which key wins; fp32 DyT alone took decoded audio from 17 to 36 dB SNR.

Median MERT cosine per stage against the quality gate floor
Median MERT cosine per stage against the quality gate floor

3. The decoder was moving data, not computing

The baseline decoder spent 230 ms per clip, of which its matmuls needed ~38. The rest was glue: sliding windows built by padding and concatenating, rotary embedding by slice-and-negate, head transposes, a 17 × 51 softmax padded to the TPU's 8 × 128 tiles — each op a full round trip to HBM, 23 086 tokens × 12 blocks.

One Pallas kernel replaced it (230 → 90 ms). It reads the attention projection once, does normalisation, rotary embedding, both differential attention branches and their subtraction in on-chip memory, and writes one result. No windows: a query sees ±17 keys, so with ≥ 17-row blocks every key lies in the previous, current or next block — three block reads, masked by position. No transposes: the block index maps pick heads straight out of the projection. Two 64-dim heads share one 128-lane tile by masking lanes, keeping every op a plain 2-D tile op.

4. Fewer FLOPs was slower

The first kernel multiplied 768 keys per query to use 35. Cutting that to 96 made it 2× slower.

Decoder time against kernel tiling.

A TPU core pairs a 256 × 256 matrix unit with a vector unit that pays per op. Small slices meant many small ops — mask, exp, max, sum, divide — each too little work to amortise. The fastest tiling was the one with the largest slices (90 → 62 ms), plus two trims that remove whole passes over the scores: fold 1/√64 into the query before the matmul (a power of two — exact), and divide by the softmax sum after multiplying by the values, on 128 lanes instead of every score.

5. The transformer: one hoist, one kernel, one cliff

Cached cross-attention keys. The text conditioning is fixed while sampling, yet its keys and values were recomputed in 24 blocks × 8 steps. Computing them once per clip is loop-invariant code motion — bit-identical. It first made things slower: the hoisted work sat in an eager function, ~100 separately dispatched ops. One jit fixed it.

A fused attention kernel — the decoder idea without the band. A head's 1422 keys fit on chip, so one program holds the whole score row: normalise, rotate, both softmaxes, subtract. It keeps scores in fp32 where the compiler rounded them to bf16, and passed the gate closer to the official output than the baseline.

Transformer time against sequence length.

At first it was not faster than the compiler in general: ~20 % slower up to 90 s, winning only because the compiler's attention falls off a cliff between 1162 and 1422 tokens (1.83× time for 1.22× tokens).

The fix was hiding in the grid order. Each kernel program handles one head pair and one block of 256 queries; the query block was the innermost loop. Keys and values do not depend on the query block — yet every program re-normalised and re-rotated all 1422 keys, for both branches, six times per head pair. By op count that was as much vector work as the softmax itself. Preparing them once per head pair, in the first program, into on-chip scratch that the next five read, cut the transformer from 133.5 to 109.3 ms. Running both heads of a tile as one stacked operand, and using a single 1424-row query block at 120 s, took it to 99.0 ms. The kernel now wins from 838 tokens and ties at 452; the sequence length is static, so the choice is made per clip length at compile time.

6. What the chip decided

This is a runtime for one device, and most of its decisions would be different, or wrong, on a GPU.

v6e fact what it decided
Registers are (8 × 128) tiles; a kernel block's last two dims must be multiples of (8, 128) or the whole array 64-dim heads cannot be addressed alone: two share a 128-lane tile and are separated by masking lanes. The fused q/k preparation kernel that read 64-lane slices would not compile. A block the full length of the sequence (1422) needs no padding.
Matrix unit: a 256 × 256 systolic array per core Small tiles starve it. Slicing the decoder's attention to cut wasted FLOPs made it 2× slower; the fastest tilings were the largest.
Vector unit and matrix unit are separate, and the vector unit pays per operation Once memory traffic was gone, cost tracked the number of vector ops, not FLOPs. Hence: fold the softmax scale into q, normalise after P·V, run two heads as one operand, compute K once. bf16 exp changed nothing.
128 MiB of on-chip VMEM per core (a GPU SM has ~0.2 MiB) A head's full K and V (1422 × 64) fit, so the transformer computes each softmax row in one pass. Flash attention's streaming, built for memory that does not fit, was pure overhead — and slower.
One TensorCore per chip; a kernel grid runs in order K, K_diff and V can be prepared by the first program of a head pair and read from scratch by the rest — the change worth 24 ms. On a GPU those programs run concurrently and cannot share scratch this way.
XLA compiles one program per shape, sampler loop included No host dispatch: the 8-step sampler is one contiguous device loop, where PyTorch launched ~30 000 kernels per request. And the choice of attention kernel is made per length at compile time, for free.
XLA's own attention falls off a cliff between 1162 and 1422 tokens (1.83× the time for 1.22× the tokens) The fused kernel first won only past that cliff; after the K cache it ties at 452 tokens and wins from 838.
An fp32 matmul at default precision is a bf16 matmul on the MXU; highest is 3-pass The decoder's "fp32" scores were already bf16 inputs; the kernel reproduces that on purpose. highest cost +30 % in the decoder and bought no quality, so it is off.
int8 runs at 2× bf16 on the MXU, but quantising activations is vector work int8 gained 2 % and failed the gate.
Pallas kernels ship as Mosaic IR, versioned against libtpu Colab's image (libtpu 0.0.21.1) could not load a jax 0.7.2 kernel; every session pins 0.0.23.

7. What did not work

bf16 against int8 per stage.

8. Where the time goes now

At 120 s: transformer 99.6 ms, decoder 60.9, conditioning 1.7. The bf16 matmuls run at 77–100 % of the chip's measured rate and are now the larger share of both stages; what remains in the kernels is vector work on the attention scores. At batch 1, SA3 on v6e was never limited by its matrix unit: first by HBM round trips between ops, then by repeated vector work.

9. In context

One chip, one clip at a time: ~22 000 two-minute clips or ~5 300 full 380 s songs per hour (≈ 734 hours of audio per wall-clock hour). At Google Cloud's on-demand 2.70perv6echip − hourthatis 0.0005 per full song; Stability's API charges $0.26 per generation.

implementation hardware 120 s clip
this engine 1× TPU v6e 163 ms
TensorRT fp16 (Stability's README) H100 ~150 ms, 5 % fewer latents
TensorRT fp8 (measured) RTX PRO 6000 205 ms
TensorRT fp16 (measured) RTX PRO 6000 274 ms
official PyTorch (Stability's paper) H200 780 ms

Per latent, Stability's H100 TensorRT figure and this engine are a tie (0.116 vs 0.120 ms); its fp8 tier on an H100 or newer is likely faster, and unpublished. No hosted API publishes a latency. For a different kind of model: YuE2-3B, which writes lyrics and vocals autoregressively, renders a 3.6-minute song in ~71 s on an RTX 4090 and ~373 songs per hour on an H800 at 32 concurrent requests.

10. Method, transferable

  1. Build the quality gate first, from the reference disagreeing with itself.
  2. Attribute device time to source lines before touching code.
  3. A/B in one process; toolchains move numbers.
  4. Measure the thing you believe — here, that fewer FLOPs and cheaper arithmetic would help. Both were wrong.
  5. Read the kernel's grid order: the largest single kernel win was work repeated in every program.