#4650·kornia

PatchMix: patch_size > min(H, W) (default 16) draws negative coordinates silently; same_on_batch=True is always the identity

Author: ducha-aikiCreated Sep 18, 2026Updated Sep 18, 2026

Summary

Two silent PatchMix defects.

1. patch_size > min(H, W) draws negative coordinates — and the default is patch_size=16. The generator computes a negative upper bound, draws negative top-left corners, and Python negative slicing then copies arbitrary small rectangles. Nothing raises, nothing is clipped.

python
import torch, kornia.augmentation as K
x = torch.stack([torch.full((1, 8, 10), float(i)) for i in range(4)])
torch.manual_seed(1); a = K.PatchMix(patch_size=16, p=1.); y = a(x)
print(a._params["patch_coords"].tolist())   # [[-5, -7], [-2, -3], [-5, -5], [-3, -2]]
ch = (y != x)[0, 0]; print(int(ch.any(1).sum()), int(ch.any(0).sum()))   # 7 x 5 region, not 16 x 16
# patch_size=9 on the same 8x10 input: an 8x9 non-square "patch", no error

2. same_on_batch=True turns PatchMix into the identity. The pairing is an argsort of draws that are identical under same_on_batch, i.e. the identity permutation, so every image is patched with itself.

python
n = 0
for s in range(100):
    torch.manual_seed(s); xx = torch.rand(4, 1, 8, 8)
    n += torch.equal(K.PatchMix(patch_size=4, p=1., same_on_batch=True)(xx), xx)
print(n)   # 100 / 100

Expected

1: reject patch_size > min(H, W) at forward time with a message (or clip). 2: same_on_batch should share the patch location, not the pairing — cf. the conventions page: "It does not equate batch-pairing indices".

Measured on main @ c643312a3 merged with #4634 (docs-only), torch 2.14.0, CPU, macOS arm64. Found while reviewing #4634 (batch 6d of #4407).

Posted on behalf of @ducha-aiki by Claude (Fable 5.1).