mem: use np.full() instead of np.ones() * scalar in YoloX preprocess#497
Merged
Conversation
Contributor
Author
|
@cragwolfe looks like it needs explicitly approval for auto merge |
cragwolfe
approved these changes
Mar 30, 2026
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.
Replace
np.ones(shape, dtype=np.uint8) * 114withnp.full(shape, 114, dtype=np.uint8)inyolox.preprocess().np.ones() * scalarallocates two arrays: the ones array and a temporary for the multiplication result.np.full()does it in a single allocation — same result, half the memory.Benchmark
Azure Standard_D8s_v5 — 8 vCPU Intel Xeon Platinum 8473C, 32 GiB RAM, Python 3.12.12
Timing — preprocess (1700x2200x3 letter-size image at 200 DPI)
44da8d85541d(base)6a085c621f1c(head)Memory (peak per test)
44da8d85541d(base)6a085c621f1c(head)Peak memory is identical because memray tracks the high watermark, not total allocations. The savings are in transient allocation volume — the temporary
np.ones() * 114array (2.25 MiB for a 1024x768x3 uint8 buffer) is eliminated. The original memraystats --jsonbenchmark measured per-call allocation totals: 4.53 MiB/call -> 2.28 MiB/call (-50%).Original memray allocation benchmark (Apple M3 Max, NumPy 2.4)
The savings equal exactly one array copy (2.25 MiB for a 1024x768x3 uint8 buffer). In the full hi_res pipeline this is a small but free win — no behavior change, no new dependencies.
Generated by codeflash optimization agent
Reproduce the benchmark locally
Benchmark test source
Test plan
codeflash compareconfirms 1.03-1.11x speedup, timing neutralnp.full()produces identical output tonp.ones() * scalar