From 34767d87faef4147cfd978e47171be70cfbf8378 Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:14:18 +0000 Subject: [PATCH 1/2] Keep constant extrapolation gradients finite Fixes #9242. Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> --- CONTRIBUTORS.md | 1 + ...-interpolation-boundary-gradient.fixed.rst | 1 + .../interpolation/linear_interpolation.py | 9 ++--- .../test/utils/test_linear_interpolation.py | 35 +++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 source/isaaclab/changelog.d/linear-interpolation-boundary-gradient.fixed.rst create mode 100644 source/isaaclab/test/utils/test_linear_interpolation.py diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 63c581fd6dac..06a9de465bdb 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -224,6 +224,7 @@ Guidelines for modifications: * Stefan Van de Mosselaer * Stephan Pleines * Sujal +* Sylvester Kaczmarek * Tiffany Chen * Trushant Adeshara * Tsz Ki GAO diff --git a/source/isaaclab/changelog.d/linear-interpolation-boundary-gradient.fixed.rst b/source/isaaclab/changelog.d/linear-interpolation-boundary-gradient.fixed.rst new file mode 100644 index 000000000000..16eb12de759c --- /dev/null +++ b/source/isaaclab/changelog.d/linear-interpolation-boundary-gradient.fixed.rst @@ -0,0 +1 @@ +* Fixed nonfinite gradients in linear interpolation when queries fall outside the sampled range or the curve has one sample. diff --git a/source/isaaclab/isaaclab/utils/interpolation/linear_interpolation.py b/source/isaaclab/isaaclab/utils/interpolation/linear_interpolation.py index e3b5363de588..12d2398d11f2 100644 --- a/source/isaaclab/isaaclab/utils/interpolation/linear_interpolation.py +++ b/source/isaaclab/isaaclab/utils/interpolation/linear_interpolation.py @@ -72,10 +72,11 @@ def compute(self, q: torch.Tensor) -> torch.Tensor: # If a point is greater than all x elements, it will assign the last elements' index upper_bound = torch.clamp(num_smaller_elements, max=self._x.numel() - 1) - # compute the weight as: (q_i - x_lb) / (x_ub - x_lb) - weight = (q_1d - self._x[lower_bound]) / (self._x[upper_bound] - self._x[lower_bound]) - # If a point is out of bounds assign weight 0.0 - weight[upper_bound == lower_bound] = 0.0 + # Mask degenerate intervals before division so constant extrapolation also has finite gradients. + same_bound = upper_bound == lower_bound + numerator = (q_1d - self._x[lower_bound]).masked_fill(same_bound, 0) + denominator = (self._x[upper_bound] - self._x[lower_bound]).masked_fill(same_bound, 1) + weight = numerator / denominator # Perform linear interpolation fq = self._y[lower_bound] + weight * (self._y[upper_bound] - self._y[lower_bound]) diff --git a/source/isaaclab/test/utils/test_linear_interpolation.py b/source/isaaclab/test/utils/test_linear_interpolation.py new file mode 100644 index 000000000000..090c9f851b83 --- /dev/null +++ b/source/isaaclab/test/utils/test_linear_interpolation.py @@ -0,0 +1,35 @@ +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). +# All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Regression coverage for piecewise linear interpolation.""" + +import pytest +import torch + +from isaaclab.utils.interpolation import LinearInterpolation + +pytestmark = pytest.mark.unit + + +def test_interpolation_boundary_gradients() -> None: + """Constant extrapolation should have zero, finite query gradients.""" + x = torch.tensor([0.0, 1.0, 3.0]) + y = torch.tensor([1.0, 3.0, 11.0]) + interpolator = LinearInterpolation(x, y, device="cpu") + q = torch.tensor([[-2.0, 0.5], [2.0, 4.0]], requires_grad=True) + result = interpolator.compute(q) + torch.testing.assert_close(result, torch.tensor([[1.0, 2.0], [7.0, 11.0]])) + (grad,) = torch.autograd.grad(result.sum(), q) + torch.testing.assert_close(grad, torch.tensor([[0.0, 2.0], [4.0, 0.0]])) + + +def test_single_sample_interpolation_gradients() -> None: + """A one-sample curve is constant over the entire domain.""" + interpolator = LinearInterpolation(torch.tensor([1.0]), torch.tensor([5.0]), device="cpu") + q = torch.tensor([0.0, 1.0, 2.0], requires_grad=True) + result = interpolator.compute(q) + torch.testing.assert_close(result, torch.full_like(q, 5.0)) + (grad,) = torch.autograd.grad(result.sum(), q) + torch.testing.assert_close(grad, torch.zeros_like(q)) From feaf9344a70f89bd2e781af5b09f81451cc25a83 Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:27:56 +0000 Subject: [PATCH 2/2] Include interpolation regressions in kitless CI --- source/isaaclab/test/utils/test_linear_interpolation.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/isaaclab/test/utils/test_linear_interpolation.py b/source/isaaclab/test/utils/test_linear_interpolation.py index 090c9f851b83..746fa3705173 100644 --- a/source/isaaclab/test/utils/test_linear_interpolation.py +++ b/source/isaaclab/test/utils/test_linear_interpolation.py @@ -3,14 +3,12 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Regression coverage for piecewise linear interpolation.""" - import pytest import torch from isaaclab.utils.interpolation import LinearInterpolation -pytestmark = pytest.mark.unit +pytestmark = [pytest.mark.unit, pytest.mark.kitless] def test_interpolation_boundary_gradients() -> None: