Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): make bbox_to_mask (RandomErasing) torch.compile fullgraph-safe (#3806)

Edgar Riba3daccd43274d
diff --git a/kornia/geometry/bbox.py b/kornia/geometry/bbox.py
index f0f0e1510..731db2baf 100644
--- a/kornia/geometry/bbox.py
+++ b/kornia/geometry/bbox.py
@@ -224,7 +224,10 @@ def bbox_to_mask(boxes: torch.Tensor, width: int, height: int) -> torch.Tensor:
[0., 0., 0., 0., 0.]]])
"""
- validate_bbox(boxes)
+ # NOTE: `validate_bbox`'s boolean result was previously computed here and discarded β€” it
+ # never raised, so it performed no validation while adding a data-dependent graph break
+ # (`torch.any(...)` -> Python `if`) that blocked torch.compile fullgraph (e.g. RandomErasing,
+ # which builds its mask through this function). Dropped; behaviour is byte-identical.
# zero padding the surroundings
yy = torch.arange(height, device=boxes.device, dtype=boxes.dtype).view(height, 1)
xx = torch.arange(width, device=boxes.device, dtype=boxes.dtype).view(1, width)
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index 892537bcd..1769f0583 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -2567,6 +2567,15 @@ class TestRectangleRandomErasing(BaseTester):
res = f(input)
self.assert_close(res[0], res[1])
+ def test_dynamo(self, device, dtype, torch_optimizer):
+ torch.manual_seed(0)
+ input = torch.rand(2, 3, 11, 7, device=device, dtype=dtype)
+ op = RandomErasing(p=1.0)
+ params = op.forward_parameters(input.shape)
+ expected = op(input, params=params)
+ compiled = torch.compile(op, fullgraph=True)
+ self.assert_close(compiled(input, params=params), expected)
+
@pytest.mark.slow
def test_gradcheck(self, device):
# test parameters