Reduce temporary CPU allocations when converting PIL conditioning batches

Aberta
#15,369 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
3/5
Tempo estimado
Meio dia
Facilidade para iniciantes
35/100
Tipo de issue
Refatoração
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
numpy, python
Domínio
performance

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
Python
Estrelas
35k
Forks
7.5k
Merge médio
2d 22h
PRs com merge (30d)
58

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de huggingface/diffusers

Todas as issues de huggingface/diffusers

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.