Reduce temporary CPU allocations when converting PIL conditioning batches

未关闭
#15,369 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
半天
新手友好度
35/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
活跃
技术栈
numpy, python
领域
performance

调研方向

The target is VaeImageProcessor.pil_to_numpy in the image processor module, which converts each PIL image to a normalized float32 array before stacking. Read that method and its callers in the ControlNet and video preprocessing paths, then benchmark a small batch before and after. Done means a single float32 batch is built and normalized in place, with existing tests for the image processor still passing and no change to public signatures or outputs. The author already has a local patch, but the issue is unassigned and the maintainers have not yet approved the approach.

由索引模型根据 Issue 内容生成。

描述

Is your feature request related to a problem? Please describe.

VaeImageProcessor.pil_to_numpy currently converts each PIL image into a separate normalized float32 array before stacking them into another float32 batch. This creates unnecessary intermediate allocations when processing multiple images. The method is shared by preprocessing paths used in ControlNet and video pipelines.

Describe the solution you'd like.

Construct a single float32 batch from the input pixel arrays and normalize it in place, avoiding per-image normalized float32 arrays and an additional full-batch copy. The change would preserve existing model computations, public signatures, and supported input/output behavior.

I've already implemented and validated a minimal local patch.

Describe alternatives you've considered.

Keeping the current per-image conversion and stacking preserves existing behavior but retains the unnecessary allocations. The proposed optimization avoids them without changing the API.

Additional context.

I benchmarked the optimization on Diffusers main (c215605a189169236544c1c9828789bfca0891d1) using an Intel Xeon Gold 6248R CPU, one thread, PyTorch 2.7.1+cpu, NumPy 2.2.6, and Pillow 11.3.0.

Workload Original Optimized Improvement
8 × 1024×1024 RGB ControlNet conditioning images 119.35 ms 71.09 ms 40.4% lower latency
49-frame 512×512 normalized video preprocessing 369.81 ms 315.87 ms 14.6% lower latency
Peak sampled process RSS, 8-image workload 584.4 MiB 494.8 MiB 15.3% lower

Measurements included warmups and multiple timed samples. I used codex to help investigate and implement this optimization.

Would the maintainers consider this CPU preprocessing optimization appropriate for Diffusers? I'd be happy to contribute the patch I've already prepared if the approach is approved and the issue is assigned to me.

主要语言
Python
星标
35k
派生
7.5k
平均合并
2 天 22 小时
30 天内合并 PR
58

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

huggingface/diffusers 的其他 Issue

查看 huggingface/diffusers 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。