Tract CPU runtime: one file prepares three plans at startup (~5 ms instead of ~2 ms on arm64)
Follow-up from the #1447 split. Nothing here blocks anything; this issue records what's left of CPU startup, with measurements and code to reuse, so whoever picks it up doesn't start from scratch.
Where CPU startup goes after #1489
Measured on an Apple M-series machine: release build, warm, with #1489 applied (commit 193a6b0c), 3 runs each.
One CPU plan (load_model + fusions + prepare_with_options):
| Plan | Load graph | Embedding fold | LayerNorm + conv fusion | tract prepare | Total |
|---|---|---|---|---|---|
| batch 1 | ~0.3 ms | ~0.05 ms | ~0.03 ms | ~1.0 ms | ~1.4 ms |
| batch 8 | ~0.3 ms | ~0.04 ms | ~0.65 ms | ~0.19 ms | ~1.2 ms |
At batch 8 and above, nearly all of the fusion time goes to DirectFusedConvMax1D::new (~0.6 ms). It packs the conv kernel for the matmul kernel tract picks on this host (direct_conv.rs:377). Finding the pattern takes about 1 µs.
A whole CPU runtime (Runtime::with_max_batch(BackendRequest::Cpu, n)):
max_batch |
Plans prepared (arm64) | Time |
|---|---|---|
| 1 | 1 | 1.8–2.2 ms |
| 4 | 1, 4 | 3.6–3.8 ms |
| 8 (CLI default) | 1, 4, 8 | 4.5–5.5 ms |
| 64 | 1, 4, 8, 16, 32, 64 | 9.5–11 ms |
On arm64, with_max_batch prepares every class up to max_batch, all at once in prepare_cpu. x86_64 prepares only the largest class. So the CLI identifying one file prepares three plans and uses one of them.
Options, most useful first
- Prepare CPU plans on first use.
prepare_cpuwould record the classes and prepare each plan the first time a batch routes to it, in aOnceLockper class. Sessions already holdstate: Noneuntil first use, so the same pattern applies one level up. For one file this cuts ~4.5–5.5 ms to ~2 ms, and nothing changes for large runs. The risk is latency moving into the first inference of each class. That matters when the CLI overlaps preparation with I/O (#1490), which would have to trigger the preparation it needs early. - Pack the conv kernel once per process. For batches ≥
DEFAULT_TILE_BATCHES(4), the tile is the same, so tract picks the same matmul kernel and the packed kernel should be identical. The plans for 8, 16, 32 and 64 could share oneArcof it instead of packing four times, saving ~1.8 ms when all four are prepared (library users with largemax_batch). This needs checking: the tile shape and themmmchoice have to match across classes before any sharing, and a test should cover that. - Store the fused graph in the embedded model (what #1447's
artifact.rsdid). This is the most code for the least gain. #1447 stored the unpacked kernel and still calledDirectFusedConvMax1D::newat load, so it paid the 0.6 ms packing too. The packed layout depends on the host's matmul kernel (AVX-512, AVX2, NEON…), so it can't go into a portable embedded file. What storing does save is the LayerNorm fusion and the pattern search, about 0.05–0.1 ms per plan. tract's ownprepare(~1 ms at batch 1) can't be serialized at all.
Code to reuse for option 3
#1447 at b771621d:
rust/tract-runtime/src/artifact.rs:- Export of the fused ops:
Operator::Norm(FusedLayerNorm: axis, epsilon, scale, bias, shape) andOperator::FusedConv(DirectFusedConvMax1D: the six dimensions,channels_last, kernel, bias). - Rebuilding them at load.
- Equivalence tests to port:
cpu_artifact_matches_source_for_every_batch,cpu_artifact_roundoff_must_preserve_decisionsandmetal_artifact_matches_source_for_every_batch(L558-L728).
- Export of the fused ops:
- In #1489's
loader.rs, these would be two moreOpvariants, exported after the fusions. The CPU and GPU graphs fuse differently (fuse_magika_layer_normvsfuse_magika_layer_norm_for_gpuandprepare_gpu_graph), so the file would need one graph per backend family, or the GPU path would keep fusing at load.
Suggestion
Start with option 1: it's the only one that moves the single-file number noticeably, and it adds no new file format. Do option 2 if library users with a large max_batch care about startup. Skip option 3 unless profiling later shows the fusion passes themselves matter.
Source: google/magika