Fourteen times cheaper: distilling our moment encoder for CPU indexing
Every clip uploaded to VideoFinder is cut into half-second segments and embedded by a video–text encoder. Until this spring that encoder had 1.1 billion parameters and needed GPUs. Here is how we replaced it with an 86M-parameter student that runs on the CPUs we already pay for.
Indexing cost is an odd kind of cost. Searching is cheap, because a query touches a few hundred thousand compressed vectors. Indexing is expensive, because every new second of footage has to pass through the encoder, twice per second, forever. With uploads growing by roughly 1.8 million clips a month, the GPU bill for indexing was growing linearly with the library, and it was the largest single line in our infrastructure budget.
We had two options: make the GPUs cheaper, or stop needing them. This post is about the second option, which became the technical report Distilling Video–Text Encoders for CPU-Only Indexing and the open-source release of kipinä.
The teacher and the target
The teacher is our production encoder: a ViT-based visual tower with a temporal transformer over four frames per segment, trained contrastively against editor-written captions. It is good, and it is large. Our target was set by hardware rather than ambition: the search nodes have idle CPU capacity outside peak hours, and we wanted the student to index a full day of uploads in that window. That works out to at least 35 segments per second per core.
| Teacher | Student (kipinä-86m) | |
|---|---|---|
| Parameters | 1.1B | 86M |
| Frames per segment | 4 | 4 |
| Throughput | 310 seg/s per GPU | 41 seg/s per CPU core (int8) |
| HETKI-40K R@5 (IoU 0.5) | 71.8 | 69.0 (96.1%) |
Attempt one: match the features
The obvious approach is feature distillation: run both models over the same segments and train the student to reproduce the teacher’s embedding, with a cosine loss and the teacher frozen. We did this over 40 million unlabelled segments sampled across categories.
It worked suspiciously well on our quick evaluation. Object queries such as “red umbrella” or “ferry in the harbour” were almost as good as the teacher. Then we looked at motion queries, and the student had fallen off a cliff: R@5 on the motion subset of HETKI dropped by 19 points. It could see the ferry, but it could no longer tell whether the ferry was leaving or arriving.
The reason is unglamorous. In the teacher’s embedding space, temporal information is a small part of the total variance. A cosine loss on the full vector rewards the student for getting the big directions right — mostly “what is in the frame” — and it can ignore the small directions that encode change over time at little cost. The loss was going down; the thing we cared about was being thrown away.
Attempt two: distil the relations
We added two terms to the objective.
- Relational loss within a shot. For pairs of segments from the same shot, the student must reproduce the teacher’s similarity between them, not just each embedding. Neighbouring segments in a shot look alike, so the only way to get their similarity right is to represent what differs between them, which is usually motion.
- Short-range temporal contrast. Given a segment, the student must identify its true successor among segments from the same clip played in reverse or shuffled order. This explicitly rewards knowing which way time runs.
With both terms, the motion gap shrank from 19 points to 3.1, and object queries lost nothing. We tried weighting the relational term up further and it began to hurt object queries, so the final weights are a compromise, tuned on the validation split only.
If the metric you care about is a small part of the variance, a loss on the whole vector will happily trade it away.
Quantisation, and the bug that wasn’t a bug
Getting to 41 segments per second needed int8. Post-training quantisation with per-channel weights and calibrated activations got us within 0.4 points of the float student. Then, three weeks after deployment to a shadow index, the retrieval scores started to look strange: the same query against the same clip returned a slightly different score than it had in testing.
Nothing was wrong with the model. Our calibration set had been sampled before a seasonal shift in uploads — far more snow, low sun and dark interiors as autumn set in — and the activation ranges for the darkest footage were being clipped. The ranking barely changed, but the scores did, and our moment-merging step uses absolute score thresholds. The fix was a calibration set stratified by luminance and colour temperature, refreshed monthly, plus a monitor that compares score distributions between the float and int8 models on a fixed probe set every night.
Shot boundaries matter more than you would think
One last detail. The student pools four frames per segment, and segments are fixed half-second windows. When a cut falls inside a window, the pooled feature is an average of two unrelated shots, and both the teacher and student produce mush. The teacher was robust enough to get away with it; the student was not. We now align segment windows to shot boundaries from kehys, which shortens a few segments but makes every one of them a single shot. That change alone was worth 1.2 points R@5 for the student and 0.3 for the teacher.
What it bought us
Indexing cost per clip fell by a factor of 14. The GPU pool that used to run indexing now trains models instead, and new uploads become searchable in about four minutes rather than twenty, because the CPU fleet is larger than the GPU pool ever was. The student has been serving 100% of new uploads since July, and we re-embedded the back catalogue over six weekends.
kipinä’s code and int8 ONNX weights are on our open-source page. If you want to see it live, Priya will index an hour of video on a single core at the winter demo night on 10 December.