Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

perf(filters): don't format the sigma tensor on every gaussian_blur2d call (#3829)

Edgar Riba35e5aa04854b
diff --git a/kornia/filters/gaussian.py b/kornia/filters/gaussian.py
index 9c8574f03..5a654f9ed 100644
--- a/kornia/filters/gaussian.py
+++ b/kornia/filters/gaussian.py
@@ -101,7 +101,11 @@ def gaussian_blur2d(
KORNIA_CHECK_SHAPE(sigma, ["B", "2"])
# `bool()` on a tensor is untraceable by dynamo; skip the data-dependent check under compile.
if not torch.compiler.is_compiling():
- KORNIA_CHECK(bool((sigma > 0).all()), f"sigma must be positive, got {sigma}")
+ # Only interpolate `sigma` into the message when the check actually fails: a plain
+ # f-string here would format the whole `sigma` tensor (a costly tensor->str) on every
+ # eager call even when it passes β€” which dominated the eager Gaussian-blur runtime.
+ positive = bool((sigma > 0).all())
+ KORNIA_CHECK(positive, "sigma must be positive" if positive else f"sigma must be positive, got {sigma}")
if separable:
ky, kx = _unpack_2d_ks(kernel_size)