Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

perf(geometry): warp_affine accepts a single shared transform for a batch (#3835)

Edgar Riba6dab72621b76
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 5f18751b1..78b1877bb 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -213,6 +213,7 @@ def warp_affine(
raise ValueError(f"Input M must be a Bx2x3 torch.Tensor. Got {M.shape}")
B, C, H, W = src.size()
+ B_M = M.shape[0]
M_3x3: torch.Tensor = convert_affinematrix_to_homography(M)
dst_norm_trans_src_norm: torch.Tensor = normalize_homography(M_3x3, (H, W), dsize)
@@ -221,7 +222,13 @@ def warp_affine(
# Jetson wheel where ``torch.linalg.inv`` dlopen-fails.
src_norm_trans_dst_norm = _inverse_3x3_closed_form(dst_norm_trans_src_norm)
- grid = F.affine_grid(src_norm_trans_dst_norm[:, :2, :], [B, C, dsize[0], dsize[1]], align_corners=align_corners)
+ # Generate the grid from the matrix batch. When a single shared transform is passed for a
+ # batch of images (``M`` is ``1x2x3`` with ``src`` ``BxCxHxW``), build one grid and expand it
+ # (a stride-0 view, no copy) instead of materializing B identical grids β€” this is what makes a
+ # batch-shared warp as cheap as torchvision's. When ``B_M == B`` (per-sample) nothing changes.
+ grid = F.affine_grid(src_norm_trans_dst_norm[:, :2, :], [B_M, C, dsize[0], dsize[1]], align_corners=align_corners)
+ if B_M == 1 and B > 1:
+ grid = grid.expand(B, -1, -1, -1)
if padding_mode == "fill":
if fill_value is None: