π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
perf(augmentation): faster fullgraph flip transform-matrix construction (#3816)
Edgar Riba383d9944f894
diff --git a/kornia/augmentation/_2d/geometric/horizontal_flip.py b/kornia/augmentation/_2d/geometric/horizontal_flip.pyindex 2d9b6a6c3..3698b30f7 100644--- a/kornia/augmentation/_2d/geometric/horizontal_flip.py+++ b/kornia/augmentation/_2d/geometric/horizontal_flip.py@@ -76,20 +76,16 @@ class RandomHorizontalFlip(GeometricAugmentationBase2D):def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]) -> torch.Tensor:- # Build the 3x3 flip matrix from the width as a *tensor* rather than `int(w)`.- # `int(tensor)` lowers to `.item()`, which breaks torch.compile fullgraph on some torch- # versions (data-dependent scalar). Tensor construction is fullgraph-safe everywhere and- # numerically identical to `[[-1, 0, w - 1], [0, 1, 0], [0, 0, 1]]`.+ # Build the 3x3 flip matrix as a constant literal plus the (tensor) width, rather than+ # `int(w)`. `int(tensor)` lowers to `.item()`, which breaks torch.compile fullgraph on+ # some torch versions; a Python-float literal is fine (constant-folded) and adding the+ # width tensor into entry [0, 2] gives `w - 1` β numerically identical to+ # `[[-1, 0, w - 1], [0, 1, 0], [0, 0, 1]]`, and ~1.8x cheaper than stacking 9 scalars.w = params["forward_input_shape"][-1].to(device=input.device, dtype=input.dtype)- one = torch.ones((), device=input.device, dtype=input.dtype)- zero = torch.zeros((), device=input.device, dtype=input.dtype)- flip_mat: torch.Tensor = torch.stack(- [- torch.stack([-one, zero, w - one]),- torch.stack([zero, one, zero]),- torch.stack([zero, zero, one]),- ]+ flip_mat: torch.Tensor = torch.tensor(+ [[-1.0, 0.0, -1.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], device=input.device, dtype=input.dtype)+ flip_mat[0, 2] = flip_mat[0, 2] + wreturn flip_mat.expand(input.shape[0], 3, 3)diff --git a/kornia/augmentation/_2d/geometric/vertical_flip.py b/kornia/augmentation/_2d/geometric/vertical_flip.pyindex 6c8b46375..0dc7b6441 100644--- a/kornia/augmentation/_2d/geometric/vertical_flip.py+++ b/kornia/augmentation/_2d/geometric/vertical_flip.py@@ -67,20 +67,16 @@ class RandomVerticalFlip(GeometricAugmentationBase2D):def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]) -> torch.Tensor:- # Build the 3x3 flip matrix from the height as a *tensor* rather than `int(h)`.- # `int(tensor)` lowers to `.item()`, which breaks torch.compile fullgraph on some torch- # versions (data-dependent scalar). Tensor construction is fullgraph-safe everywhere and- # numerically identical to `[[1, 0, 0], [0, -1, h - 1], [0, 0, 1]]`.+ # Build the 3x3 flip matrix as a constant literal plus the (tensor) height, rather than+ # `int(h)`. `int(tensor)` lowers to `.item()`, which breaks torch.compile fullgraph on+ # some torch versions; a Python-float literal is fine (constant-folded) and adding the+ # height tensor into entry [1, 2] gives `h - 1` β numerically identical to+ # `[[1, 0, 0], [0, -1, h - 1], [0, 0, 1]]`, and ~1.8x cheaper than stacking 9 scalars.h = params["forward_input_shape"][-2].to(device=input.device, dtype=input.dtype)- one = torch.ones((), device=input.device, dtype=input.dtype)- zero = torch.zeros((), device=input.device, dtype=input.dtype)- flip_mat: torch.Tensor = torch.stack(- [- torch.stack([one, zero, zero]),- torch.stack([zero, -one, h - one]),- torch.stack([zero, zero, one]),- ]+ flip_mat: torch.Tensor = torch.tensor(+ [[1.0, 0.0, 0.0], [0.0, -1.0, -1.0], [0.0, 0.0, 1.0]], device=input.device, dtype=input.dtype)+ flip_mat[1, 2] = flip_mat[1, 2] + hreturn flip_mat.expand(input.shape[0], 3, 3)