Skip to content

perf(utils): 2.2x faster to_tensor_image range check (one aminmax pass) - #78

Closed
dexhunter wants to merge 1 commit into
gmberton:mainfrom
dexhunter:perf/to-tensor-image-aminmax
Closed

dexhunter wants to merge 1 commit into
gmberton:mainfrom
dexhunter:perf/to-tensor-image-aminmax

Conversation

@dexhunter

Copy link
Copy Markdown

TL;DR

  • This PR makes to_tensor_image in vismatch/utils.py faster. BaseMatcher.forward calls it for each image.
  • The range check now uses one aminmax() pass instead of separate min() and max() passes.
  • Benchmark: 120 forward calls with the stub matcher from test_forward_matcher_confidences (no model): 65.3 ms -> 37.9 ms.
  • Real matchers run a model, so the saving per image is small: about 28 us at 256x256, about 0.7 ms at 1080p.
  • Tested with pytest tests/test_import_sandbox.py vismatch_test.py tests/test_matchers.py tests/test_viz.py: no new failures.

What & Why

Before, the [0, 1] range check read the full image twice. It used one pass for img.min() and one pass for img.max(). Then it compared the two 0-dim tensors with Python floats.

Now, for non-empty float images, the code gets the min and the max in one aminmax() pass. It converts both to Python floats and compares them. If the check passes, the function returns the image.

If the check fails, or the image is not a float image, the code runs the original assert line. So error messages, NaN rejection and results for other dtypes stay the same. The function returns the same tensor object as before.

For real matchers the model time dominates. So the gain matters most for large images and cheap matchers.

Results

Time per to_tensor_image call, one CPU thread, best of 7 runs:

Situation Before After Change
3x128x192 float32 22.6 us 10.0 us 2.3x faster
3x256x256 float32 (benchmark size) 51.5 us 23.3 us 2.2x faster
3x480x640 float32 203.7 us 99.0 us 2.1x faster
3x1080x1920 float32 1343.3 us 671.7 us 2.0x faster
3x256x256 uint8 (no fast path, no change expected) 13.8 us 14.1 us same (within noise)

Why this is safe and easy to maintain

The change adds four lines and keeps the original check as the fallback. Output is unchanged for valid images. Invalid images still raise the same AssertionError text. tests/test_matchers.py covers forward, and these tests pass.

Validation

  • Workload of tests/test_matchers.py::test_forward_matcher_confidences[None-None] (120 forward calls, stub matcher with no model): 65.3 ms -> 37.9 ms (median ratio 0.58, measured 3 times).
  • A separate check compared outputs with the original code. It covered NaN, inf, empty, bool, int, half, bfloat16, float64 and out-of-range images, plus numpy inputs and wrong shapes. All matched.
  • pytest tests/test_import_sandbox.py vismatch_test.py tests/test_matchers.py tests/test_viz.py: 53 passed, 276 skipped, 4 failed. The 4 tests/test_viz.py failures are ModuleNotFoundError: No module named 'lightglue' (submodule not checked out). They fail the same way without this change.
  • Reproduce the per-size numbers:
    import timeit, torch
    from vismatch.utils import to_tensor_image
    img = torch.rand(3, 256, 256)
    print(min(timeit.repeat(lambda: to_tensor_image(img), number=2000, repeat=7)) / 2000)

AI assistance

Codex (gpt-6.1-sol) generated the code change as candidates. Weco Observe tracked them and an automated benchmark harness scored them. A Claude agent picked the smallest and most readable candidate.

Weco share link: https://dashboard.weco.ai/share/OGv03jR2EH1TQkjrBSCm189ldvIhMwJW

Use one aminmax() pass for the [0, 1] range check of non-empty float
images instead of separate min() and max() passes. Other inputs and
failing images still go through the original assert, so error messages
and NaN rejection are unchanged.

The range check alone is about 2.2x faster (28 us saved per 256x256
image, about 0.7 ms at 1080p). Model time dominates for real matchers.

Co-Authored-By: Aiden <aiden@weco.ai>
@gmberton

gmberton commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Closing: an overall speedup of less than 1% doesn't justify the extra code

@gmberton gmberton closed this Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants