Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Fix Qwen VL image preprocessing crash on 4-channel (RGBA) input (#16548)

Jialong(Bruce) Li39cb5a8b10d1
diff --git a/comfy/text_encoders/qwen_vl.py b/comfy/text_encoders/qwen_vl.py
index f97a88061..818b149e6 100644
--- a/comfy/text_encoders/qwen_vl.py
+++ b/comfy/text_encoders/qwen_vl.py
@@ -22,6 +22,7 @@ def process_qwen2vl_images(
if image_std is None:
image_std = [0.26862954, 0.26130258, 0.27577711]
+ images = images[..., :3]
batch_size, height, width, channels = images.shape
device = images.device
# dtype = images.dtype
diff --git a/tests-unit/comfy_test/qwen_vl_image_channels_test.py b/tests-unit/comfy_test/qwen_vl_image_channels_test.py
new file mode 100644
index 000000000..1b856b120
--- /dev/null
+++ b/tests-unit/comfy_test/qwen_vl_image_channels_test.py
@@ -0,0 +1,25 @@
+"""Regression test: RGBA (or other non-RGB) images must not inflate the Qwen VL patch count."""
+
+import torch
+
+from comfy.cli_args import args
+
+if not torch.cuda.is_available():
+ args.cpu = True
+
+from comfy.text_encoders.qwen_vl import process_qwen2vl_images # noqa: E402
+
+
+def test_extra_channels_are_dropped_before_patching():
+ images_rgb = torch.rand(1, 224, 224, 3)
+ images_rgba = torch.rand(1, 224, 224, 4)
+
+ patches_rgb, grid_rgb = process_qwen2vl_images(images_rgb, patch_size=16, image_mean=[0.5] * 3, image_std=[0.5] * 3)
+ patches_rgba, grid_rgba = process_qwen2vl_images(images_rgba, patch_size=16, image_mean=[0.5] * 3, image_std=[0.5] * 3)
+
+ assert patches_rgba.shape == patches_rgb.shape
+ assert torch.equal(grid_rgba, grid_rgb)
+
+ pos_rows = int(grid_rgba[:, 1:].prod(dim=1).sum())
+ patch_embed_rows = patches_rgba.numel() // (3 * 2 * 16 * 16)
+ assert patch_embed_rows == pos_rows