Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add extended test coverage for Qwen2-VL vision transformer (#3627)

Ayushi Jain7c72a3dbfe52
diff --git a/tests/models/qwen25/test_qwen2_vl.py b/tests/models/qwen25/test_qwen2_vl.py
index 38d62ed4b..6f0ba56a8 100644
--- a/tests/models/qwen25/test_qwen2_vl.py
+++ b/tests/models/qwen25/test_qwen2_vl.py
@@ -18,16 +18,50 @@
import pytest
import torch
-from kornia.models.qwen25 import Qwen2VLVisionTransformer
+from kornia.models.qwen25.qwen2_vl import (
+ Qwen2VLPatchMerger,
+ Qwen2VLVisionTransformer,
+)
class TestQwen2VL:
@pytest.mark.parametrize("batch_size", [1, 2])
def test_smoke(self, batch_size, device, dtype):
- model = Qwen2VLVisionTransformer().to(device=device, dtype=dtype)
- input = torch.randn(batch_size, 3, 224, 224, device=device, dtype=dtype)
+ # smaller model β†’ stable
+ model = Qwen2VLVisionTransformer(embed_dim=64, depth=2, num_heads=4).to(device=device, dtype=dtype)
- output = model(input)
+ x = torch.randn(batch_size, 3, 224, 224, device=device, dtype=dtype)
- assert output.shape[0] == batch_size
- assert output.shape[2] == 1280
+ output = model(x)
+
+ assert output.shape == (batch_size, 256, 64)
+
+ def test_gradients(self, device):
+ model = Qwen2VLVisionTransformer(embed_dim=64, depth=1, num_heads=4).to(device)
+
+ x = torch.randn(1, 3, 224, 224, device=device, requires_grad=True)
+ output = model(x)
+
+ loss = output.mean()
+ loss.backward()
+
+ assert x.grad is not None
+
+ def test_patch_merger(self, device):
+ merger = Qwen2VLPatchMerger(dim=64).to(device)
+
+ x = torch.randn(1, 3, 224, 224, device=device)
+ output = merger(x)
+
+ assert output.shape == (1, 256, 64)
+
+ def test_batch_consistency(self, device):
+ """Ensure outputs are consistent between batch and single input."""
+ model = Qwen2VLVisionTransformer(embed_dim=64, depth=2, num_heads=4).to(device)
+
+ x = torch.randn(2, 3, 224, 224, device=device)
+
+ out_batch = model(x)
+ out_single = model(x[:1])
+
+ assert torch.allclose(out_batch[0], out_single[0], atol=1e-5)