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

refactor: better use of autograd for base lens quantities #306

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
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
109 changes: 34 additions & 75 deletions src/caustics/lenses/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,40 +530,17 @@ def _jacobian_effective_deflection_angle_autograd(
Return the jacobian of the effective reduced deflection angle vector field.
This equates to a (2,2) matrix at each (x,y) point.
"""

# Build Jacobian
J = torch.zeros((*x.shape, 2, 2), device=x.device, dtype=x.dtype)

# Compute deflection angle gradients
dax_dx = torch.func.grad(
lambda *a: self.effective_reduced_deflection_angle(*a)[0], argnums=0
)
J[..., 0, 0] = torch.vmap(dax_dx, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

dax_dy = torch.func.grad(
lambda *a: self.effective_reduced_deflection_angle(*a)[0], argnums=1
)
J[..., 0, 1] = torch.vmap(dax_dy, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

day_dx = torch.func.grad(
lambda *a: self.effective_reduced_deflection_angle(*a)[1], argnums=0
)
J[..., 1, 0] = torch.vmap(day_dx, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

day_dy = torch.func.grad(
lambda *a: self.effective_reduced_deflection_angle(*a)[1], argnums=1
)
J[..., 1, 1] = torch.vmap(day_dy, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

return J.detach()
J = torch.vmap(
torch.func.jacfwd(
self.effective_reduced_deflection_angle,
argnums=(0, 1),
randomness="different",
),
in_dims=(0, 0, None),
chunk_size=chunk_size,
)(x.flatten(), y.flatten(), z_s)
J = torch.stack([torch.stack(Jrow, dim=-1) for Jrow in J], dim=-2)
return J.reshape(*x.shape, 2, 2)

@forward
def jacobian_effective_deflection_angle(
Expand Down Expand Up @@ -753,14 +730,12 @@ def reduced_deflection_angle(
*Unit: arcsec*

"""
d_s = self.cosmology.angular_diameter_distance(z_s)
d_ls = self.cosmology.angular_diameter_distance_z1z2(z_l, z_s)
deflection_angle_x, deflection_angle_y = self.physical_deflection_angle(
x, y, z_s
)
return func.reduced_from_physical_deflection_angle(
deflection_angle_x, deflection_angle_y, d_s, d_ls
)
ax, ay = torch.vmap(
torch.func.grad(self.potential, (0, 1)),
in_dims=(0, 0, None),
chunk_size=10000,
)(x.flatten(), y.flatten(), z_s)
return ax.reshape(x.shape), ay.reshape(y.shape)

@forward
def physical_deflection_angle(
Expand Down Expand Up @@ -856,7 +831,14 @@ def convergence(
*Unit: unitless*

"""
...
Psi_H = torch.vmap(
torch.func.hessian(self.potential, (0, 1)),
in_dims=(0, 0, None),
chunk_size=10000,
)(x.flatten(), y.flatten(), z_s)
Psi_H = torch.stack([torch.stack(Hrow, dim=-1) for Hrow in Psi_H], dim=-2)
Psi_H = Psi_H.reshape(*x.shape, 2, 2)
return 0.5 * (Psi_H[..., 0, 0] + Psi_H[..., 1, 1]).reshape(x.shape)

@abstractmethod
@forward
Expand Down Expand Up @@ -1117,39 +1099,16 @@ def _jacobian_deflection_angle_autograd(
Return the jacobian of the deflection angle vector.
This equates to a (2,2) matrix at each (x,y) point.
"""
# Build Jacobian
J = torch.zeros((*x.shape, 2, 2), device=x.device, dtype=x.dtype)

# Compute deflection angle gradients
dax_dx = torch.func.grad(
lambda *a: self.reduced_deflection_angle(*a)[0], argnums=0
)
J[..., 0, 0] = torch.vmap(dax_dx, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

dax_dy = torch.func.grad(
lambda *a: self.reduced_deflection_angle(*a)[0], argnums=1
)
J[..., 0, 1] = torch.vmap(dax_dy, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

day_dx = torch.func.grad(
lambda *a: self.reduced_deflection_angle(*a)[1], argnums=0
)
J[..., 1, 0] = torch.vmap(day_dx, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

day_dy = torch.func.grad(
lambda *a: self.reduced_deflection_angle(*a)[1], argnums=1
)
J[..., 1, 1] = torch.vmap(day_dy, in_dims=(0, 0, None), chunk_size=chunk_size)(
x.flatten(), y.flatten(), z_s
).reshape(x.shape)

return J.detach()
J = torch.vmap(
torch.func.jacfwd(
self.reduced_deflection_angle, argnums=(0, 1), randomness="different"
),
in_dims=(0, 0, None),
chunk_size=chunk_size,
)(x.flatten(), y.flatten(), z_s)
J = torch.stack([torch.stack(Jrow, dim=-1) for Jrow in J], dim=-2)
return J.reshape(*x.shape, 2, 2)

@forward
def jacobian_deflection_angle(
Expand Down
17 changes: 16 additions & 1 deletion src/caustics/lenses/enclosed_mass.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from caskade import forward, Param

from .base import ThinLens, CosmologyType, NameType, ZLType
from .func import physical_deflection_angle_enclosed_mass, convergence_enclosed_mass
from .func import (
physical_deflection_angle_enclosed_mass,
convergence_enclosed_mass,
reduced_from_physical_deflection_angle,
)

__all__ = ("EnclosedMass",)

Expand Down Expand Up @@ -179,6 +183,17 @@ def physical_deflection_angle(
x0, y0, q, phi, lambda r: self.enclosed_mass(r, p), x, y, self.s
)

@forward
def reduced_deflection_angle(self, x, y, z_s, z_l):
d_s = self.cosmology.angular_diameter_distance(z_s)
d_ls = self.cosmology.angular_diameter_distance_z1z2(z_l, z_s)
deflection_angle_x, deflection_angle_y = self.physical_deflection_angle(
x, y, z_s
)
return reduced_from_physical_deflection_angle(
deflection_angle_x, deflection_angle_y, d_s, d_ls
)

@forward
def potential(
self,
Expand Down
11 changes: 11 additions & 0 deletions src/caustics/lenses/nfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ def physical_deflection_angle(
x0, y0, m, c, critical_density, d_l, x, y, _h=self._h, DELTA=DELTA, s=self.s
)

@forward
def reduced_deflection_angle(self, x, y, z_s, z_l):
d_s = self.cosmology.angular_diameter_distance(z_s)
d_ls = self.cosmology.angular_diameter_distance_z1z2(z_l, z_s)
deflection_angle_x, deflection_angle_y = self.physical_deflection_angle(
x, y, z_s
)
return func.reduced_from_physical_deflection_angle(
deflection_angle_x, deflection_angle_y, d_s, d_ls
)

@forward
def convergence(
self,
Expand Down
11 changes: 11 additions & 0 deletions src/caustics/lenses/tnfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,17 @@ def physical_deflection_angle(
x0, y0, scale_radius, tau, x, y, M0, d_l, self._F_mode, self.s
)

@forward
def reduced_deflection_angle(self, x, y, z_s, z_l):
d_s = self.cosmology.angular_diameter_distance(z_s)
d_ls = self.cosmology.angular_diameter_distance_z1z2(z_l, z_s)
deflection_angle_x, deflection_angle_y = self.physical_deflection_angle(
x, y, z_s
)
return func.reduced_from_physical_deflection_angle(
deflection_angle_x, deflection_angle_y, d_s, d_ls
)

@forward
def potential(
self,
Expand Down
46 changes: 16 additions & 30 deletions tests/test_lens_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test_lens_potential_vs_deflection(device):
),
caustics.Multipole(cosmology=cosmo, z_l=z_l, **caustics.Multipole._null_params),
caustics.MassSheet(cosmology=cosmo, z_l=z_l, **caustics.MassSheet._null_params),
caustics.NFW(
cosmology=cosmo,
z_l=z_l,
**caustics.NFW._null_params,
use_case="differentiable",
),
# caustics.NFW(
# cosmology=cosmo,
# z_l=z_l,
# **caustics.NFW._null_params,
# use_case="differentiable",
# ),
caustics.PixelatedConvergence(
cosmology=cosmo,
z_l=z_l,
Expand All @@ -54,12 +54,12 @@ def test_lens_potential_vs_deflection(device):
),
caustics.SIE(cosmology=cosmo, z_l=z_l, **caustics.SIE._null_params),
caustics.SIS(cosmology=cosmo, z_l=z_l, **caustics.SIS._null_params),
caustics.TNFW(
cosmology=cosmo,
z_l=z_l,
**caustics.TNFW._null_params,
use_case="differentiable",
),
# caustics.TNFW(
# cosmology=cosmo,
# z_l=z_l,
# **caustics.TNFW._null_params,
# use_case="differentiable",
# ),
]

# Define a list of lens model names.
Expand All @@ -71,16 +71,8 @@ def test_lens_potential_vs_deflection(device):
# Compute the deflection angle.
ax, ay = lens.reduced_deflection_angle(x, y, z_s)

# Ensure the x,y coordinates track gradients
x = x.detach().requires_grad_()
y = y.detach().requires_grad_()

# Compute the lensing potential.
phi = lens.potential(x, y, z_s)
# Compute the gradient of the lensing potential.
phi_ax, phi_ay = torch.autograd.grad(
phi, (x, y), grad_outputs=torch.ones_like(phi)
)
# Compute deflection angles using the lensing potential.
phi_ax, phi_ay = super(lens.__class__, lens).reduced_deflection_angle(x, y, z_s)

# Check that the gradient of the lensing potential equals the deflection angle.
if name in ["NFW", "TNFW"]:
Expand Down Expand Up @@ -162,14 +154,8 @@ def test_lens_potential_vs_convergence(device):
except NotImplementedError:
continue

# Compute the laplacian of the lensing potential.
phi_H = torch.vmap(
torch.vmap(
torch.func.hessian(lens.potential, (0, 1)), in_dims=(0, 0, None)
),
in_dims=(0, 0, None),
)(x, y, z_s)
phi_kappa = 0.5 * (phi_H[0][0] + phi_H[1][1])
# Compute the convergence from the lensing potential.
phi_kappa = super(lens.__class__, lens).convergence(x, y, z_s)

# Check that the laplacian of the lensing potential equals the convergence.
if name.strip("_0") in ["NFW", "TNFW"]:
Expand Down
Loading