Skip to content
Merged
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
6 changes: 4 additions & 2 deletions lrax/common/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@

import equinox as eqx
import jax.numpy as jnp
from jaxtyping import ArrayLike


def affine_initialization(
model: eqx.Module, scale: float, shift: float, where: Callable
model: eqx.Module, scale: float, shift: ArrayLike, where: Callable
):
"""Apply an affine transformation to the chosen `Linear` layer(s) in a model.

Parameters
----------
- `model`: The model to initialize.
- `scale`: Rescaling applied to the selected weights in `model`.
- `shift`: Translation applied to the selected biases in `model`.
- `shift`: Translation applied to the selected biases in `model`, broadcast to
their shape.
- `where`: Function used to get the leaves that should be modified.

Returns
Expand Down
24 changes: 24 additions & 0 deletions lrax/common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ def _next_version(root_dir: Path, name: str) -> int:
return max(versions, default=-1) + 1


def latest_version(root_dir: str | Path, name: str) -> Path | None:
"""Find the run directory of the latest version of a run logged by `FileLogger`.

Parameters
----------
- `root_dir`: The directory containing the versioned run directories.
- `name`: The prefix of the run directory, e.g., `f"{name}_{version}"`.

Returns
-------
The run directory with the highest version, or `None` if there is no run.
"""
root_dir = Path(root_dir)
if not root_dir.exists():
return None

versions = [
int(version)
for path in root_dir.glob(f"{name}_*")
if (version := path.name.removeprefix(f"{name}_")).isdigit()
]
return root_dir / f"{name}_{max(versions)}" if versions else None


class FileLogger(Logger):
"""Logs hyperparameters and metrics to CSV files in a versioned run directory."""

Expand Down
48 changes: 27 additions & 21 deletions lrax/common/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@
_softplus = doc_repr(jnn.softplus, "<function softplus>")


def _vec_to_sym(x: Array, n: int) -> Array:
"""Unpack a vector into a symmetric `(n, n)` matrix."""
diag, off_diag = jnp.split(x, (n,), axis=-1)
i_lower, j_lower = jnp.tril_indices(n, -1)
U = jnp.diag(diag)
U = U.at[i_lower, j_lower].set(off_diag)
return U.at[j_lower, i_lower].set(off_diag)


def _vec_to_tril(x: Array, n: int) -> Array:
"""Unpack a vector into a lower-triangular `(n, n)` matrix."""
i_lower, j_lower = jnp.tril_indices(n)
return jnp.zeros((n, n), x.dtype).at[i_lower, j_lower].set(x)


def _transform_eigenvalues(U: Array, f: Callable[[Array], Array]) -> Array:
"""Apply a function to the eigenvalues of `U`."""
V, w = jax.lax.linalg.eigh(U, sort_eigenvalues=False)
return V @ jnp.diag(f(w)) @ V.T


class SPD(eqx.Module):
"""Symmetric positive-definite network.

Expand Down Expand Up @@ -101,14 +122,8 @@ def __call__(self, x: Array) -> Array:
-------
A JAX array with shape `(diag_size, diag_size)`.
"""
x = self.mlp(x)
diag, off_diag = jnp.split(x, (self.shape[0],), axis=-1)
i_lower, j_lower = jnp.tril_indices(self.shape[0], -1)
U = jnp.diag(diag)
U = U.at[i_lower, j_lower].set(off_diag)
U = U.at[j_lower, i_lower].set(off_diag)
V, w = jax.lax.linalg.eigh(U, sort_eigenvalues=False)
return V @ jnp.diag(self.metric(w)) @ V.T
U = _vec_to_sym(self.mlp(x), self.shape[0])
return _transform_eigenvalues(U, self.metric)


class StackedSPD(eqx.Module):
Expand Down Expand Up @@ -206,13 +221,7 @@ def __call__(self, x: Array) -> Array:

@eqx.filter_vmap
def to_spd(y):
diag, off_diag = jnp.split(y, (self.shape[-1],), axis=-1)
i_lower, j_lower = jnp.tril_indices(self.shape[-1], -1)
U = jnp.diag(diag)
U = U.at[i_lower, j_lower].set(off_diag)
U = U.at[j_lower, i_lower].set(off_diag)
V, w = jax.lax.linalg.eigh(U, sort_eigenvalues=False)
return V @ jnp.diag(self.metric(w)) @ V.T
return _transform_eigenvalues(_vec_to_sym(y, self.shape[-1]), self.metric)

M = jnp.zeros(self.shape, x_full.dtype)
if full_idx:
Expand Down Expand Up @@ -279,9 +288,7 @@ def __call__(self, x: Array) -> Array:
-------
A JAX array with shape `(diag_size, diag_size)`.
"""
x = self.mlp(x)
i_lower, j_lower = jnp.tril_indices(self.shape[0])
L = jnp.zeros(self.shape, x.dtype).at[i_lower, j_lower].set(x)
L = _vec_to_tril(self.mlp(x), self.shape[0])
return L @ L.T


Expand Down Expand Up @@ -1001,10 +1008,9 @@ def __call__(self, x: Array) -> Array:

@partial(jnp.vectorize, signature="(n,n),(m)->(n,n)")
def compose(M0: Array, delta: Array) -> Array:
i_lower, j_lower = jnp.tril_indices(n)
delta_A, delta_D = jnp.split(delta, 2)
A = jnp.eye(n, dtype=delta.dtype).at[i_lower, j_lower].add(delta_A)
D = jnp.zeros((n, n), delta.dtype).at[i_lower, j_lower].set(delta_D)
A = jnp.eye(n, dtype=delta.dtype) + _vec_to_tril(delta_A, n)
D = _vec_to_tril(delta_D, n)
return A @ M0 @ A.T + D @ D.T

return compose(nominal, correction)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "lrax"
version = "0.4.0"
version = "0.5.0"
description = "JAX library for robotics learning research"
readme = "README.md"
requires-python = ">=3.12"
Expand Down
Loading
Loading