Extend inference prefetch beyond CUDA: MPS, accelerator backends, CPU runtimes and videos
Background
#26319 (ultralytics 8.4.162) overlaps image loading with GPU inference. BasePredictor.stream_inference now decodes the next batch on a worker thread while the GPU runs the current one (_prefetch in ultralytics/engine/predictor.py). It applies to CUDA devices, image-only LoadImagesAndVideos sources, and sources with more than one batch, across every task, backend and tracking. Results are identical to the previous sequential loop.
Measured CUDA gains
RTX PRO 6000 Blackwell, PyTorch 2.7.0, 1,024 COCO images at imgsz=640, median of 3 runs. Each main and PR run pair ran back to back on a GPU shared with other jobs, so the relative gains hold but absolute times are pessimistic:
| Model | Batch | main |
#26319 | Faster |
|---|---|---|---|---|
yolo26n.pt detect |
1 | 8.36 s | 7.33 s | 12% |
yolo26n.pt detect |
16 | 2.81 s | 2.25 s | 20% |
yolo26n-seg.pt segment |
1 | 9.68 s | 8.12 s | 16% |
yolo26n-seg.pt segment |
16 | 3.28 s | 2.84 s | 13% |
yolo26n-cls.pt classify |
16 | 1.90 s | 1.67 s | 12% |
yolo26n-pose.pt pose |
16 | 2.38 s | 2.11 s | 11% |
yolo26n-obb.pt OBB |
16 | 4.67 s | 4.32 s | 8% |
The PR author's detect-only measurements on COCO val2017 (five fresh-process pairs each):
| GPU | Workload | main |
#26319 | Faster |
|---|---|---|---|---|
| NVIDIA L4 | batch=1, 1,000 images |
23.01 s | 19.52 s | 18% |
| NVIDIA L4 | batch=16, 5,000 images |
31.22 s | 27.61 s | 13% |
| RTX 4060 Laptop | batch=1, 1,000 images |
6.42 s | 5.18 s | 24% |
| RTX 4060 Laptop | batch=16, 5,000 images |
21.52 s | 19.00 s | 13% |
Follow-up ideas
Prefetch only helps when inference runs somewhere other than the CPU cores that decode images. Each idea below needs a paired benchmark before its gate widens.
- Apple MPS. MPS queues work asynchronously on the Apple GPU just like CUDA. The likely change is one condition:
self.device.type in {"cuda", "mps"}. - Accelerators that report
device=cpu. OpenVINO on an Intel GPU or NPU, CoreML on the Neural Engine, Edge TPU, and NPU backends such as RKNN and Axelera run inference off the CPU, but the predictor sees a CPU device. Enabling prefetch here needs each backend to report where it actually runs, rather than a list of formats in the predictor. - ONNX Runtime and OpenVINO on CPU. Both let other Python threads run during inference, but they already spread inference across every core, so a loader thread competes with inference instead of using an idle core. Rough estimate: about a 3% gain on 8 cores and possibly a loss on 2–4 core devices such as a Raspberry Pi. Measure at 4 and 8 cores on x86 before enabling it.
- OpenVINO asynchronous inference.
AsyncInferQueuekeeps several requests in flight across CPU streams. It is likely a bigger throughput win for OpenVINO on CPU than prefetch, and it belongs in the OpenVINO backend, not the predictor. - Videos. Prefetch is disabled for videos because
write_resultsand the trackers read the loader's current frame state (mode,frame,count). Carrying the frame index inside each batch would make video sources safe to prefetch.
Contributions are welcome. Please include paired main vs. PR timings, output hashes showing identical results, and the hardware used.
Source: ultralytics/ultralytics