We run background removal as an ONNX model inside a container behind a Cloudflare Worker. Last week we split one combined runtime image into two profile images — a "Lite" profile and a "Lite-2K" profile — and shrunk the distributed weights. The interesting part was not the image split. It was proving that the split changed nothing about the output.
Why "the model still loads" proves nothing
ONNX models are frequently stored as a small graph file plus an external data file holding the tensor payload. The graph does not know whether the bytes it points at are the ones you exported. Get the offsets wrong, or ship a re-exported payload, and onnx.load() still succeeds, the session still runs, and the model still returns an image. It just returns slightly different numbers, which is the hardest kind of regression to notice by eye.
So the check has to be on the payload, not on the session.
import hashlib
import onnx
from onnx import numpy_helper
def tensor_digest(path: str) -> str:
model = onnx.load(path) # resolves external data into memory
h = hashlib.sha256()
for init in model.graph.initializer:
arr = numpy_helper.to_array(init)
h.update(init.name.encode())
h.update(arr.tobytes())
return h.hexdigest()
Hashing the initializer names alongside the raw bytes stops a reordering of tensors from producing the same digest. For our size (about 178 MB of tensors) loading everything into memory is fine; for a multi-gigabyte model you would stream the external data file instead and hash the chunks.
The numbers we held ourselves to
- 178,397,184 bytes of exact float32 tensor data, shared across the three Lite-2K graphs.
- Every complete ONNX ModelProto reconstructed byte-identically to its pinned export, after normalizing default storage metadata.
- No quantization, no graph geometry change, no extra segmentation step.
- Per-profile compressed distribution landed at 267 MB and 268 MB, against 760 MB for the previous combined runtime — roughly a 65% reduction in what gets pulled.
Those are the claims I am comfortable repeating because they are properties of files, not of a screenshot.
Byte-identical behaviour, not just byte-identical weights
A matching hash of the weights does not prove the runtime assembles the graph the same way. We ran four real cases — the Lite profile, and the 2K profile on a portrait, a square and a landscape input — serially on Linux amd64 with 4 CPU and 7 GiB of memory, no swap. All four alpha arrays matched byte-for-byte, and so did both the JPEG and PNG encodings.
Peak resident memory was 1.928 GiB for Lite and 5.790 GiB for Lite-2K. That gap is the entire argument for separate images: the 2K profile is what sets the memory budget, and nobody processing a small photo should pay it.
Warming happens per container, not per profile
A container that has just started holds no model. In a real 2K Flask startup, the first square warm loaded only the 2048-square graph, in 1.81 seconds. A repeat warm took 0.001 seconds and kept the same session. The landscape graph did not load at all.
That is a one-sample observation, not a benchmark, but it shaped the code: a batch warms only its first input per profile, because each container retains one session and the graph is chosen from the input's dimensions. Later orientation changes inside the same batch can still load a second graph — so "warmed" is a per-container fact that expires.
A bug the verification exposed
One of the regression images was a rotated JPEG, and it failed. The parser was matching an uppercase EXIF signature and recognised nothing else, so standard Exif bytes were ignored and the image went in with the wrong orientation hint. The browser and server were already rotating pixels correctly; the parser disagreed with them. That is exactly the class of bug that a "the model is unchanged" check alone would have shipped.
What all of this still does not prove
Local CPU samples are not Cloudflare performance. We verified production separately, with two anonymous tasks: a 768×1024 image on Lite warmed in 902 ms and completed in 16.7 s; a 1440×1440 image on Lite-2K warmed in 1454 ms and completed in 29.9 s. Those are single, network-inclusive samples, not P95s, and I would not quote them as latency guarantees.
And byte-identical output is not an accuracy study. It says the new packaging behaves like the old one. It says nothing about whether the old one was good.
The checklist I now run for any model or packaging change
- Pin the source revision and hash the payload, not the filename.
- Reconstruct the model and compare tensor digests against the pinned export.
- Run real inputs across every geometry the runtime can select, and compare output arrays and encodings.
- Measure peak RSS per profile, on the smallest machine you intend to support.
- Treat local timings as smoke tests. Measure the deployed thing separately.
Disclosure: I build CleanWhiteBG, the product these container images serve — a browser tool that turns product photos into white-background JPGs or transparent PNGs.
Top comments (0)