perf(inference): skip known-valid image range scans - #1387
Merged
Borda merged 2 commits intoAug 22, 2026
Merged
Conversation
JESUSROYETH
requested review from
Borda,
SkalskiP,
isaacrob and
probicheaux
as code owners
August 22, 2026 00:51
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1387 +/- ##
=======================================
Coverage 86% 86%
=======================================
Files 112 112
Lines 14628 14632 +4
=======================================
+ Hits 12586 12590 +4
Misses 2042 2042 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes inference preprocessing by skipping redundant pixel-range scans for inputs already normalized by conversion.
Changes:
- Skips range reductions for PIL and
uint8NumPy inputs. - Preserves validation for tensors and other NumPy dtypes.
- Adds regression coverage and changelog documentation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/rfdetr/detr.py |
Short-circuits guaranteed-valid range checks. |
tests/inference/test_predict.py |
Adds regression and validation tests. |
CHANGELOG.md |
Documents the inference optimization. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Changes: - Add a mocked successful URL regression that fails if converted image inputs reach pixel-range scans. - Add a mixed PIL/float-NumPy regression and annotate the `tmp_path: Path` fixture. Impact: - Protect the public URL conversion path and per-image range-validation state from regressions. - Resolve the open test typing review thread under the project's mandatory annotation policy. Verification: - `ruff check tests/inference/test_predict.py` — passed. - `ruff format --check tests/inference/test_predict.py` — passed. - `uv run --no-sync mypy src/rfdetr --no-error-summary` — passed. - `uv run --no-sync pytest tests/inference/test_predict.py -n 1 -m "not gpu" --timeout=240 -p no:rerunfailures` — 119 passed, 1 skipped. - `git diff --check` — passed. Residual limits: - Independent read-only specialist review and local CUDA execution remain external runtime gates. --- Co-authored-by: Codex <codex@openai.com>
Borda
approved these changes
Aug 22, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
predict()converts PIL images to 8-bit RGB, andto_tensorscales both those images and uint8 NumPy arrays into[0, 1]. It then runs(img > 1).any()and(img < 0).any()on the resulting tensor anyway.Changes
Falsefor those inputs. This also covers file-path and URL inputs, since both get opened into a PIL image before the check runs.Tensor.any(including under the public defaultinclude_source_image=True), plus float32 NumPy controls that still have to raise on both bounds.Performance
I benchmarked with the public
predict(image, threshold=0.5, include_source_image=False)call, on one real COCO image as auint8NumPy array, withtorch.set_num_threads(1). It alternates baseline and candidate within 101 pairs, repeating each configuration in three independent processes; CUDA numbers ran serially on an RTX 4060 Laptop GPU.include_source_image=Falseisolates the preprocessing path from the source-image copy. Under the public defaultinclude_source_image=True,predict()does more work per call (it also captures the source image), so this same absolute saving becomes a smaller share of a bigger total — the percentages below don't carry over to the default configuration as-is.Each run compares the baseline with itself first, matches boxes by IoU, and compares the matched public detection fields as raw bytes. Baseline and candidate outputs matched in every reported run.
On CPU, the removed reductions take 4.13-4.35 ms per four real images at their original sizes and allocate 6,113,984 bytes per call. The skipped path takes 0.267-0.289 us and allocates no tensor memory. Public CPU
predict()timing was noisy, so I'm not reporting it as a speedup.Validation
tests/inference/test_predict.py: 122 passed.tests/export/test_onnx_notes.py's_export_tiny_modeldoctest. It expects a fixed string, but under-n 2it picks up an ONNX-export INFO log line from other work on the same xdist worker — it doesn't callpredict()or touch this diff's code. I re-ran the same command on unmodifieddevelopthree times and got the identical failure, so it's pre-existing and unrelated.develop, and the open PRs touchingdetr.pydon't change this validation block.COCO mAP wasn't re-run separately. The skip only substitutes a value for
(img > 1).any()/(img < 0).any()thatto_tensor's[0, 1]scaling of PIL and uint8 NumPy input already guarantees — it doesn't change the tensor the model sees. Public detections on real COCO images came out byte-identical to baseline across every Nano/Small, FP32/FP16 configuration I measured. Since mAP is a deterministic function of detections and ground truth, byte-identical detections mean byte-identical mAP.