Repository navigation
Description
System Info
- transformers 5.18.0 and 5.19.0 (regression); 5.17.0 is fine.
mainstill has the change. - Linux x86_64 (glibc), Intel Xeon Platinum 8488C, Python 3.14
- torch 2.13.0+cpu, torchvision 0.28.0+cpu, Pillow 12.3.0
- 8 torch threads
Who can help?
@jzakrzew @Rocketknight1 (author and co-author of #48897)
Reproduction
Since v5.18.0, the Qwen3-VL image processor (Qwen2VLImageProcessor, torchvision backend) is ~20% slower on CPU for a multi-image request. The output is identical, so this is perf only.
I bisected the 300 commits between the v5.17.0 and v5.19.0 merge-bases on main. The first bad commit is 207fca7, #48897 "Skip an unnecessary image copy in the torchvision image normalization path". Reverting only its change to image_processing_backends.py on top of v5.19.0 restores the old latency.
import resource, statistics, time
import numpy as np, torch
from PIL import Image
from transformers import AutoImageProcessor
torch.set_num_threads(8)
proc = AutoImageProcessor.from_pretrained("Qwen/Qwen3-VL-32B-Instruct")
rng = np.random.default_rng(0)
batch = lambda: [Image.fromarray(rng.integers(0, 256, (512, 512, 3), dtype=np.uint8)) for _ in range(30)]
for _ in range(3):
proc(images=batch(), return_tensors="pt")
ts, fl = [], []
for _ in range(40):
imgs = batch()
f0 = resource.getrusage(resource.RUSAGE_SELF).ru_minflt
t = time.perf_counter()
out = proc(images=imgs, return_tensors="pt")
ts.append((time.perf_counter() - t) * 1e3)
fl.append(resource.getrusage(resource.RUSAGE_SELF).ru_minflt - f0)
del out
print(f"median {statistics.median(ts):.1f} ms, min {min(ts):.1f} ms, minor faults/call {statistics.median(fl):.0f}")Results (30 x 512x512 images per call, pixel_values shape (30720, 1536) in every case):
| transformers | min ms | median ms | minor page faults / call |
|---|---|---|---|
| main @ c210bb3 (parent of #48897) | 37.8 - 39.9 | 49 - 55 | 87k - 92k |
| main @ 207fca7 (#48897) | 50.1 - 51.8 | 59 - 61 | 106k |
| v5.19.0 base | 49.6 - 50.2 | 55 - 56 | 95k |
| v5.19.0 base + revert of #48897 | 38.2 - 41.2 | 45 - 52 | 66k - 93k |
We first saw this in vLLM. vLLM's multimodal processor timing (apply_hf_processor, Qwen3-VL, 30 images/request) goes from 84.9 ms (5.17.0) to 104.4 ms (5.19.0), and back to 86.1 ms with only #48897 reverted.
Analysis. The extra time is page faults, not compute:
- In the vLLM path, minor page faults per request rise in exact steps of 23,040. That is 30 images x 768 pages, i.e. one extra freshly mapped 3 MB float32 image buffer per image (sometimes two).
- The slower ops are memory-bound copies on fresh memory:
Tensor.to(float32), the patchifyreshape, andnp.array(PIL). - Setting
MALLOC_MMAP_THRESHOLD_=268435456 MALLOC_TRIM_THRESHOLD_=1073741824(glibc) removes the faults. Both versions then run at the same speed, ~53 ms in the vLLM path.
So with glibc malloc, the in-place normalize changes the allocation/free pattern. Before #48897, the extra temporary from out-of-place normalize was freed early, and later ~3 MB buffers reused that memory. Now more of them come from new mmaps and fault on first touch. I have not traced the exact malloc sequence. The effect is likely allocator-specific, so it may not show on macOS.
BaseVideoProcessor and the other TorchvisionBackend image processors call the same rescale_and_normalize, so other models may be affected too. The size of the effect depends on image size and count.
Expected behavior
No slowdown: #48897 was intended as a speedup. Possible fixes: revert #48897, or make the in-place normalize opt-in.
Activity
jzakrzew commentedon Oct 29, 2026
Seems like the change in #48897 triggers some pathological allocator behavior in Qwen's patch-buffer allocation. Instead of reverting #48897, we could assemble the patches into a preallocated output buffer: jzakrzew@1edce80
This improves both perf and memory use for the 30 image case:
@zucchini-nlp WDYT?
Rocketknight1 commentedon Oct 29, 2026
My take is that our preference is for code readability and image preprocessing is rarely the bottleneck, so we generally try to avoid heavy optimization of low level stuff like memory allocation! If there's a simple fix that restores performance we can review it, but if we add a bunch of arcane buffer hacking then it's probably not worth it
jzakrzew commentedon Oct 29, 2026
The previous fix I linked was more complicated than needed, here is a simplified version: 0879f2f
I agree though, having to workaround allocator behavior is a bit awkward.
zucchini-nlp commentedon Oct 30, 2026
Totally agree with @Rocketknight1 , we don't need any more code. If the slowing down is significant to actually affect overall perf, then we can revert the prev PR
For acually better and optimized processing, we are adding kernels which hopefully will be merged soon (#46771)
jzakrzew commentedon Oct 30, 2026
For a different no-code workaround, switching the allocator to
tcmalloc, as per the Pytorch tuning guide , makes the regression go away andinplacewill be faster for large images.As for vLLM, the most recent release does normalization for Qwen in vision encoder forward by default, so it doesn't even use this code unless you explicitly opt in.
zucchini-nlp commentedon Nov 2, 2026
even better! IMO we can close the issue then and let users to opt-in (understanding the consequences). Quite sure that with processing kernels release, it will become the new default for better perfs
zucchini-nlp commentedon Nov 3, 2026
I will close the issue @yidezou @jzakrzew if you agree with the above, and it doesn't affect vllm in most cases
yidezou commentedon Nov 23, 2026
yes, this issue can be closed. thanks anyway.