Skip to content

Image preprocessing ~20% slower on Linux since v5.18.0: in-place normalize (#48897) causes extra page faults #50100

Done

Description

@yidezou

System Info

  • transformers 5.18.0 and 5.19.0 (regression); 5.17.0 is fine. main still 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 patchify reshape, and np.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

  1. jzakrzew commented on Oct 29, 2026

    @jzakrzew
    Contributor

    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:

    Device Image size Old time New time Old peak memory New peak memory
    CPU 512×512 69.2–74.0 ms 44.4–46.7 ms 1,219 MiB 962 MiB
    CPU 1120×1120 281.9–312.3 ms 210.9–213.4 ms 3,124 MiB 1,896 MiB
    GPU 512×512 2.72–2.74 ms 2.03–2.04 ms 517.5 MiB 337.5 MiB
    GPU 1120×1120 11.65–11.78 ms 8.48 ms 2,478.3 MiB 1,616.3 MiB

    @zucchini-nlp WDYT?

  2. Rocketknight1 commented on Oct 29, 2026

    @Rocketknight1
    Member

    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

  3. jzakrzew commented on Oct 29, 2026

    @jzakrzew
    Contributor

    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.

  4. zucchini-nlp commented on Oct 30, 2026

    @zucchini-nlp
    Member

    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)

  5. jzakrzew commented on Oct 30, 2026

    @jzakrzew
    Contributor

    For a different no-code workaround, switching the allocator to tcmalloc, as per the Pytorch tuning guide , makes the regression go away and inplace will 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.

  6. zucchini-nlp commented on Nov 2, 2026

    @zucchini-nlp
    Member

    it doesn't even use this code unless you explicitly opt in.

    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

  7. zucchini-nlp commented on Nov 3, 2026

    @zucchini-nlp
    Member

    I will close the issue @yidezou @jzakrzew if you agree with the above, and it doesn't affect vllm in most cases

  8. yidezou commented on Nov 23, 2026

    @yidezou
    Author

    yes, this issue can be closed. thanks anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions