π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
docs(agents): clamp is not a portable gradient guard (#4406)
Mohansree Vijayakumar10d764fdb888
diff --git a/AGENTS.md b/AGENTS.mdindex 9b25e49b2..a6acde98e 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -99,6 +99,15 @@ when more than one virtualenv is in play: name the interpreter explicitly rather- MPS does not support float64 gradcheck. MPS autocast can also change the effective dtype; inspect nearby tests before changing tolerances or skips.- The blocking MPS job runs `--device=mps --dtype=float32 --xfail-known-failures`. Its exact strict-xfail baseline is `testing/known_failure_xfails/mps_float32.txt` and is tracked in #4159. A fix removes its manifest line; a rename or reparametrization updates the node ID; a new failure is fixed or explicitly documented before being added. The manifest contract requires a full-suite run, without `-k` or a partial path.- TF32 matmul is disabled by default; `--tf32` enables it. cuDNN convolutions still use PyTorch's TF32 default. Tests marked `tf32` are xfailed at collection unless `--tf32` is passed, and that marker is non-strict, so a default run reports neither their failure nor their recovery.+- `torch.clamp` is not a portable gradient guard: its derivative **at the bound** passes the incoming gradient+ through (`1.0`) on torch 2.5.1 and 2.9.1 and returns `0.0` on 2.14.0. Wherever the bound is also the singular+ point -- `clamp(min=0).sqrt()`, `clamp(-1, 1).acos()`, `clamp(-1, 1).asin()` -- the clamp bounds the value only,+ so the unbounded derivative still reaches the backward pass as `inf` or `nan` on the older half of the supported+ range while 2.14 masks it. A floor below the dtype's smallest subnormal is no guard either: `clamp(min=1e-12)`+ underflows to `0` in `float16`. Substitute a safe argument into the expression that gets differentiated and take+ the value from a `.detach()`ed copy, or from the other arm of a `torch.where`, instead; that is+ version-independent by construction. A CPU float32 run on a recent torch does not exercise any of this -- the+ 2.5.1 leg is what discriminates. The measurements and the audit of the pattern are in #4229.- Preserve device and dtype rather than creating implicit CPU or default-dtype tensors. Use the injected `device` and `dtype` fixtures in tests.## Library preferencesdiff --git a/kornia/enhance/jpeg.py b/kornia/enhance/jpeg.pyindex d77423f69..f2abcd397 100644--- a/kornia/enhance/jpeg.py+++ b/kornia/enhance/jpeg.py@@ -239,7 +239,8 @@ def _jpeg_quality_to_scale(# for a documented input. libjpeg gives quality 0 the quality-1 table, so give it the quality-1 scale.# The guard is exactly the zero point: a fractional quality in (0, 1) is already finite, keeps its own# (larger) scale, and is not touched. ``torch.where`` rather than ``clamp`` also keeps the gradient at- # every unguarded quality independent of the torch version (see #4229).+ # every unguarded quality independent of the torch version -- clamp's derivative at its own bound is not+ # portable across the supported torch range, as measured in PR #4406.strength: torch.Tensor = torch.where(compression_strength == 0.0, torch.ones_like(compression_strength), compression_strength)diff --git a/kornia/geometry/solvers/polynomial_solver.py b/kornia/geometry/solvers/polynomial_solver.pyindex 94cabe2fb..561cb3eaf 100644--- a/kornia/geometry/solvers/polynomial_solver.py+++ b/kornia/geometry/solvers/polynomial_solver.py@@ -316,7 +316,7 @@ def solve_quartic(coeffs: torch.Tensor) -> torch.Tensor:R_sq = torch.gather(R_sq_candidates, -1, best_idx).squeeze(-1)# `clamp(min=0).sqrt()` does not guard the gradient: d(sqrt)/dx is unbounded at 0, and on- # torch < 2.14 clamp passes the incoming gradient straight through at the bound (#4229), so+ # torch < 2.14 clamp passes the incoming gradient straight through at the bound, as measured in PR #4406, so# R_sq == 0 -- a biquadratic such as x^4 - 16 -- gave inf and then nan. Substitute a safe# radicand instead, as solve_quadratic above already does, so sqrt is never differentiated at 0.# On torch >= 2.14 clamp already zeroes the boundary gradient, so the pins for this guard pass