Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SplinePotential device compatibility #138

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions src/torchpme/potentials/spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def __init__(
if len(y_grid) != len(r_grid):
raise ValueError("Length of radial grid and value array mismatch.")

r_grid = r_grid.to(dtype=dtype, device=device)
y_grid = y_grid.to(dtype=dtype, device=device)
PicoCentauri marked this conversation as resolved.
Show resolved Hide resolved

if reciprocal:
if torch.min(r_grid) <= 0.0:
raise ValueError(
Expand All @@ -89,6 +92,8 @@ def __init__(
k_grid = torch.pi * 2 * torch.reciprocal(r_grid).flip(dims=[0])
else:
k_grid = r_grid.clone()
else:
k_grid = k_grid.to(dtype=dtype, device=device)

if yhat_grid is None:
# computes automatically!
Expand All @@ -98,6 +103,8 @@ def __init__(
y_grid,
compute_second_derivatives(r_grid, y_grid),
)
else:
yhat_grid = yhat_grid.to(dtype=dtype, device=device)

# the function is defined for k**2, so we define the grid accordingly
if reciprocal:
Expand All @@ -108,12 +115,16 @@ def __init__(
self._krn_spline = CubicSpline(k_grid**2, yhat_grid)

if y_at_zero is None:
self._y_at_zero = self._spline(torch.tensor([0.0]))
self._y_at_zero = self._spline(
torch.tensor([0.0], dtype=dtype, device=device)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not really matter, but looks cleaner to me. Feel free to ignore.

Suggested change
torch.tensor([0.0], dtype=dtype, device=device)
torch.zeros(1), dtype=dtype, device=device)

)
else:
self._y_at_zero = y_at_zero

if yhat_at_zero is None:
self._yhat_at_zero = self._krn_spline(torch.tensor([0.0]))
self._yhat_at_zero = self._krn_spline(
torch.tensor([0.0], dtype=dtype, device=device)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
torch.tensor([0.0], dtype=dtype, device=device)
torch.zeros(1, dtype=dtype, device=device)

)
else:
self._yhat_at_zero = yhat_at_zero

Expand Down
2 changes: 1 addition & 1 deletion src/torchpme/utils/splines.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def compute_second_derivatives(
d2y = _solve_tridiagonal(a, b, c, d)

# Converts back to the original dtype
return d2y.to(x_points.dtype)
return d2y.to(dtype=x_points.dtype, device=x_points.device)


def compute_spline_ft(
Expand Down
31 changes: 31 additions & 0 deletions tests/test_potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,34 @@ def test_combined_potential_learnable_weights():
loss.backward()
optimizer.step()
assert torch.allclose(combined.weights, weights - 0.1)


@pytest.mark.parametrize("device", ["cpu", "cuda"])
@pytest.mark.parametrize(
"potential_class", [CoulombPotential, InversePowerLawPotential, SplinePotential]
)
def test_potential_device(potential_class, device):
if device == "cuda" and not torch.cuda.is_available():
pytest.skip("CUDA is not available")

smearing = 1.0
exponent = 1.0
dtype = torch.float64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also parametrize over the dtype?


if potential_class == InversePowerLawPotential:
ceriottm marked this conversation as resolved.
Show resolved Hide resolved
potential = potential_class(
exponent=exponent, smearing=smearing, dtype=dtype, device=device
)
elif potential_class == SplinePotential:
x_grid = torch.linspace(0, 20, 100, device=device, dtype=dtype)
y_grid = torch.exp(-(x_grid**2) * 0.5)
potential = potential_class(
r_grid=x_grid, y_grid=y_grid, reciprocal=False, dtype=dtype, device=device
)
else:
potential = potential_class(smearing=smearing, dtype=dtype, device=device)

dists = torch.linspace(0.1, 10.0, 100, device=device, dtype=dtype)
potential_lr = potential.lr_from_dist(dists)

assert potential_lr.device.type == device
Loading