Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Guidelines for modifications:
* Stefan Van de Mosselaer
* Stephan Pleines
* Sujal
* Sylvester Kaczmarek
* Tiffany Chen
* Trushant Adeshara
* Tsz Ki GAO
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fixed nonfinite gradients in linear interpolation when queries fall outside the sampled range or the curve has one sample.
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
33 changes: 33 additions & 0 deletions source/isaaclab/test/utils/test_linear_interpolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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

import pytest
import torch

from isaaclab.utils.interpolation import LinearInterpolation

pytestmark = [pytest.mark.unit, pytest.mark.kitless]


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))