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
36 changes: 35 additions & 1 deletion lrax/common/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import jax
import jax.numpy as jnp
from equinox.internal import doc_repr
from jaxtyping import Array, ArrayLike, Float, Scalar, ScalarLike, Shaped
from jaxtyping import Array, ArrayLike, Float, PyTree, Scalar, ScalarLike, Shaped

from ..typing import DistanceFn, Kernel, Point

Expand Down Expand Up @@ -429,3 +429,37 @@ def row(x: Point) -> Float[Array, " m"]:
raise ValueError(f"`batch_size` must be positive. Got {batch_size}.")

return jax.lax.map(jax.checkpoint(row), xs, batch_size=batch_size)


def product(kernels: PyTree[Kernel]) -> Kernel:
r"""Multiply kernels that each act on one part of a sample.

.. math::

k(x, y) = \prod_{i} k_{i}(x_{i}, y_{i})

where :math:`k_{i}` is the :math:`i`th kernel and :math:`x_{i}` and :math:`y_{i}`
are the parts of the samples it acts on. This builds a kernel on a product space
from kernels on its factors, such as a position paired with an orientation, and
the product of positive definite kernels is positive definite.

Parameters
----------
- `kernels`: The kernels to multiply, arranged in a tree whose structure is a
prefix of the samples. Each kernel receives the subtree of the samples at its
own position, so a kernel may act on a single array or on a whole branch.
Callables are treated as leaves, so kernels that are themselves pytrees, such
as Equinox modules, are not flattened.

Returns
-------
The product kernel, taking two samples and returning a scalar.
"""
if not jax.tree.leaves(kernels, is_leaf=callable):
raise ValueError("`kernels` must hold at least one kernel.")

def kernel(x: Point, y: Point) -> Scalar:
values = jax.tree.map(lambda k, a, b: k(a, b), kernels, x, y, is_leaf=callable)
return jax.tree.reduce(jnp.multiply, values)

return kernel
8 changes: 4 additions & 4 deletions lrax/common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pathlib import Path
from typing import Any

import jax.tree_util as jtu
import jax
import wandb

from ..typing import Metrics
Expand Down Expand Up @@ -149,13 +149,13 @@ def __init__(self, loggers: Sequence[Logger]):
self.loggers = list(loggers)

def log_hyperparams(self, params: dict[str, Any]) -> None:
jtu.tree_map(lambda logger: logger.log_hyperparams(params), self.loggers)
jax.tree.map(lambda logger: logger.log_hyperparams(params), self.loggers)

def log_metrics(self, metrics: Metrics, step: int | None = None) -> None:
jtu.tree_map(lambda logger: logger.log_metrics(metrics, step), self.loggers)
jax.tree.map(lambda logger: logger.log_metrics(metrics, step), self.loggers)

def cleanup(self) -> None:
jtu.tree_map(lambda logger: logger.cleanup(), self.loggers)
jax.tree.map(lambda logger: logger.cleanup(), self.loggers)


class WandbLogger(Logger):
Expand Down
21 changes: 10 additions & 11 deletions lrax/common/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import jax.nn as jnn
import jax.numpy as jnp
import jax.random as jr
import jax.tree_util as jtu
from equinox.internal import doc_repr
from jaxtyping import Array, ArrayLike, PRNGKeyArray, PyTree

Expand Down Expand Up @@ -592,7 +591,7 @@ def __call__(self, *args, **kwargs) -> PyTree[Array]:
```
"""
outputs = [model(*args, **kwargs) for model in self.models] # type: ignore
return jtu.tree_map(lambda *out: jnp.stack(out), *outputs)
return jax.tree.map(lambda *out: jnp.stack(out), *outputs)

def __len__(self) -> int:
return len(self.models)
Expand All @@ -618,7 +617,7 @@ def mean(outputs: PyTree[Array]) -> PyTree[Array]:
-------
A pytree matching a single member's output, each leaf averaged over the members.
"""
return jtu.tree_map(lambda out: jnp.mean(out, axis=0), outputs)
return jax.tree.map(lambda out: jnp.mean(out, axis=0), outputs)

@staticmethod
def std(outputs: PyTree[Array]) -> PyTree[Array]:
Expand All @@ -635,7 +634,7 @@ def std(outputs: PyTree[Array]) -> PyTree[Array]:
-------
A pytree matching a single member's output, each leaf's spread over the members.
"""
return jtu.tree_map(lambda out: jnp.std(out, axis=0), outputs)
return jax.tree.map(lambda out: jnp.std(out, axis=0), outputs)

@staticmethod
def min(outputs: PyTree[Array]) -> PyTree[Array]:
Expand All @@ -650,7 +649,7 @@ def min(outputs: PyTree[Array]) -> PyTree[Array]:
-------
A pytree matching a single member's output, each leaf reduced over the members.
"""
return jtu.tree_map(lambda out: jnp.min(out, axis=0), outputs)
return jax.tree.map(lambda out: jnp.min(out, axis=0), outputs)

@staticmethod
def max(outputs: PyTree[Array]) -> PyTree[Array]:
Expand All @@ -665,7 +664,7 @@ def max(outputs: PyTree[Array]) -> PyTree[Array]:
-------
A pytree matching a single member's output, each leaf reduced over the members.
"""
return jtu.tree_map(lambda out: jnp.max(out, axis=0), outputs)
return jax.tree.map(lambda out: jnp.max(out, axis=0), outputs)

def select(self, indices: Sequence[int]) -> "WrapEnsemble[M]":
return WrapEnsemble([self.models[i] for i in indices])
Expand Down Expand Up @@ -696,7 +695,7 @@ def subsample(
`num_members`.
"""
indices = jr.choice(key, len(self.models), (num_members,), replace=replace)
return jtu.tree_map(lambda out: out[indices], outputs)
return jax.tree.map(lambda out: out[indices], outputs)


class WrapResidual(eqx.Module):
Expand Down Expand Up @@ -753,7 +752,7 @@ def __init__(
if callable(nominal):
self.nominal = nominal
else:
self.nominal = jtu.tree_map(jnp.asarray, nominal)
self.nominal = jax.tree.map(jnp.asarray, nominal)
self.correction = correction
self.freeze_nominal = freeze_nominal

Expand All @@ -780,10 +779,10 @@ def __call__(self, x: Array) -> PyTree[Array]:
nominal = jax.lax.stop_gradient(nominal)
correction = self.correction(x)

same_structure = jtu.tree_structure(nominal) == jtu.tree_structure(correction)
same_structure = jax.tree.structure(nominal) == jax.tree.structure(correction)
if not same_structure or any(
jnp.shape(n) != jnp.shape(c)
for n, c in zip(jtu.tree_leaves(nominal), jtu.tree_leaves(correction))
for n, c in zip(jax.tree.leaves(nominal), jax.tree.leaves(correction))
):
raise ValueError(
"The output of `correction` must have the same pytree structure and "
Expand All @@ -792,7 +791,7 @@ def __call__(self, x: Array) -> PyTree[Array]:
f"`{eqx.tree_pformat(correction)}`."
)

return jtu.tree_map(jnp.add, nominal, correction)
return jax.tree.map(jnp.add, nominal, correction)


class WrapSPDResidual(eqx.Module):
Expand Down
8 changes: 4 additions & 4 deletions lrax/common/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@
import jax
import jax.numpy as jnp
import jax.scipy.linalg as jsl
import jax.tree_util as jtu
from jax.flatten_util import ravel_pytree
from jax.tree_util import KeyPath, keystr
from jaxtyping import Array, ArrayLike, Float, Scalar, ScalarLike, Shaped

from ..typing import Kernel, Point
from .kernels import gaussian


def _get_sample_size(samples: Shaped[Point, "samples"]) -> int:
def check(path: jtu.KeyPath, leaf: ArrayLike) -> int:
def check(path: KeyPath, leaf: ArrayLike) -> int:
shape = jnp.shape(leaf)

if not shape:
raise ValueError(
"Every leaf must carry a leading sample axis. Got a scalar leaf at "
f"`samples{jtu.keystr(path)}`."
f"`samples{keystr(path)}`."
)

return shape[0]

leaves = jtu.tree_leaves_with_path(samples)
leaves = jax.tree.leaves_with_path(samples)

if not leaves:
raise ValueError("`samples` must have at least one array leaf.")
Expand Down
6 changes: 3 additions & 3 deletions lrax/common/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
from typing import Any, Literal, assert_never

import equinox as eqx
import jax
import jax.numpy as jnp
import jax.random as jr
import jax.tree_util as jtu
import optax
from jaxtyping import PRNGKeyArray, PyTree
from lightning.pytorch import LightningDataModule
Expand Down Expand Up @@ -172,7 +172,7 @@ def make_training_step(_model, _opt_state, _batch, _keys):
for batch in tqdm(loader, desc=f"Epoch {epoch}", leave=False):
# split the key into one key per element of the batch; we need this
# when training stochastic models that depend on a key for each rollout
batch_size = jtu.tree_leaves(batch)[0].shape[0]
batch_size = jax.tree.leaves(batch)[0].shape[0]
keys = jr.split(jr.fold_in(key, step), batch_size)

result = make_training_step(model, opt_state, batch, keys)
Expand Down Expand Up @@ -281,7 +281,7 @@ def learn(
# check whether the optimizer is a single optimizer or multiple optimizers
is_optim = lambda x: isinstance(x, optax.GradientTransformation)
params = eqx.filter(model, eqx.is_inexact_array)
opt_state = jtu.tree_map(lambda o: o.init(params), optim, is_leaf=is_optim)
opt_state = jax.tree.map(lambda o: o.init(params), optim, is_leaf=is_optim)

algorithm_state = algorithm.init(model, env, key=init_key)

Expand Down
13 changes: 6 additions & 7 deletions lrax/ppo/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import jax
import jax.numpy as jnp
import jax.random as jr
import jax.tree_util as jtu
from jax import lax
from jaxtyping import Array, ArrayLike, PRNGKeyArray, PyTree, ScalarLike
from optax import OptState
Expand Down Expand Up @@ -312,7 +311,7 @@ def _update(

params, static = eqx.partition(model, eqx.is_inexact_array)

batch_size = jtu.tree_leaves(batch)[0].shape[0]
batch_size = jax.tree.leaves(batch)[0].shape[0]
minibatch_size = batch_size // self.num_minibatches

loss_and_grad = eqx.filter_value_and_grad(self._loss, has_aux=True)
Expand Down Expand Up @@ -345,8 +344,8 @@ def _epoch_step(
) -> tuple[tuple[PyTree, OptState], Metrics]:
params, opt_state = carry
perm = jr.permutation(epoch_key, batch_size)
shuffled = jtu.tree_map(lambda x: x[perm], batch)
minibatches = jtu.tree_map(
shuffled = jax.tree.map(lambda x: x[perm], batch)
minibatches = jax.tree.map(
lambda x: x.reshape(self.num_minibatches, minibatch_size, *x.shape[1:]),
shuffled,
)
Expand All @@ -359,7 +358,7 @@ def _epoch_step(
(params, opt_state), metrics = lax.scan(
_epoch_step, (params, opt_state), epoch_keys
)
metrics = jtu.tree_map(jnp.mean, metrics)
metrics = jax.tree.map(jnp.mean, metrics)
model = eqx.combine(params, static)
return model, opt_state, metrics

Expand Down Expand Up @@ -418,14 +417,14 @@ def step(
advantages=advantages,
returns=returns,
)
batch = jtu.tree_map(lambda x: x.reshape(total_size, *x.shape[2:]), batch)
batch = jax.tree.map(lambda x: x.reshape(total_size, *x.shape[2:]), batch)

model, opt_state, metrics = self._update(
model, opt_state, optim, batch, key=update_key
)

# merge the environment metrics with the loss metrics
metrics.update(jtu.tree_map(jnp.mean, rollouts.metrics))
metrics.update(jax.tree.map(jnp.mean, rollouts.metrics))
metrics["mean_reward"] = jnp.mean(rollouts.rewards)
metrics["done_rate"] = jnp.mean(rollouts.dones)

Expand Down
11 changes: 5 additions & 6 deletions lrax/sac/sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import jax
import jax.numpy as jnp
import jax.random as jr
import jax.tree_util as jtu
from jax import lax
from jaxtyping import Array, PRNGKeyArray, PyTree
from optax import OptState
Expand Down Expand Up @@ -265,7 +264,7 @@ def _grad_step(
critic_target, eqx.is_inexact_array
)
critic_arrays, _ = eqx.partition(model.critic, eqx.is_inexact_array)
target_arrays = jtu.tree_map(
target_arrays = jax.tree.map(
lambda t, c: (1.0 - self.tau) * t + self.tau * c,
target_arrays,
critic_arrays,
Expand All @@ -280,7 +279,7 @@ def _grad_step(
(model_params, state_params, opt_state), metrics = lax.scan(
_grad_step, (model_params, state_params, opt_state), step_keys
)
metrics = jtu.tree_map(jnp.mean, metrics)
metrics = jax.tree.map(jnp.mean, metrics)
model = eqx.combine(model_params, model_static)
alg_state = eqx.combine(state_params, state_static)
return model, alg_state, opt_state, metrics
Expand All @@ -304,7 +303,7 @@ def step(
rollouts, env_state = self._rollout(model, env, env_state, key=rollout_key)

total_size = self.num_steps * env.num_envs
flat = jtu.tree_map(lambda x: x.reshape(total_size, *x.shape[2:]), rollouts)
flat = jax.tree.map(lambda x: x.reshape(total_size, *x.shape[2:]), rollouts)
critic_target, buffer = alg_state
buffer = buffer.add(
flat.obs,
Expand Down Expand Up @@ -361,7 +360,7 @@ def _skip_update(
key: PRNGKeyArray,
) -> tuple[PyTree, PyTree, OptState, Metrics]:
del key
zeros = jtu.tree_map(lambda s: jnp.zeros(s.shape, s.dtype), metrics_shape)
zeros = jax.tree.map(lambda s: jnp.zeros(s.shape, s.dtype), metrics_shape)
return model_params, state_params, opt_state, cast(Metrics, zeros)

model_params, state_params, opt_state, metrics = lax.cond(
Expand All @@ -378,7 +377,7 @@ def _skip_update(

# merge the environment metrics with the loss metrics
metrics = dict(metrics)
metrics.update(jtu.tree_map(jnp.mean, flat.metrics))
metrics.update(jax.tree.map(jnp.mean, flat.metrics))
metrics["mean_reward"] = jnp.mean(flat.rewards)
metrics["done_rate"] = jnp.mean(flat.dones)
metrics["buffer_size"] = buffer.size
Expand Down
13 changes: 6 additions & 7 deletions lrax/shac/shac.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import jax.nn as jnn
import jax.numpy as jnp
import jax.random as jr
import jax.tree_util as jtu
import optax
from jax import lax
from jaxtyping import Array, PRNGKeyArray, PyTree, ScalarLike
Expand Down Expand Up @@ -327,7 +326,7 @@ def _step(
dones=outputs.done,
next_values=outputs.next_value,
metrics={
**jtu.tree_map(jnp.mean, outputs.metrics),
**jax.tree.map(jnp.mean, outputs.metrics),
"mean_reward": jnp.mean(outputs.raw_reward),
"done_rate": jnp.mean(outputs.raw_done),
},
Expand Down Expand Up @@ -382,7 +381,7 @@ def _update_critic(
"""
params, static = eqx.partition(model, eqx.is_inexact_array)

batch_size = jtu.tree_leaves(batch)[0].shape[0]
batch_size = jax.tree.leaves(batch)[0].shape[0]
minibatch_size = batch_size // self.num_minibatches

loss_and_grad = eqx.filter_value_and_grad(self._critic_loss, has_aux=True)
Expand All @@ -404,8 +403,8 @@ def _epoch_step(
) -> tuple[tuple[PyTree, OptState], Metrics]:
params, opt_state = carry
perm = jr.permutation(epoch_key, batch_size)
shuffled = jtu.tree_map(lambda x: x[perm], batch)
minibatches = jtu.tree_map(
shuffled = jax.tree.map(lambda x: x[perm], batch)
minibatches = jax.tree.map(
lambda x: x.reshape(self.num_minibatches, minibatch_size, *x.shape[1:]),
shuffled,
)
Expand All @@ -418,7 +417,7 @@ def _epoch_step(
(params, opt_state), metrics = lax.scan(
_epoch_step, (params, opt_state), epoch_keys
)
metrics = jtu.tree_map(jnp.mean, metrics)
metrics = jax.tree.map(jnp.mean, metrics)
model = eqx.combine(params, static)
return model, opt_state, metrics

Expand Down Expand Up @@ -472,7 +471,7 @@ def step(
alg_state.critic_target, eqx.is_inexact_array
)
critic_arrays, _ = eqx.partition(model.critic, eqx.is_inexact_array)
target_arrays = jtu.tree_map(
target_arrays = jax.tree.map(
lambda t, c: self.target_alpha * t + (1.0 - self.target_alpha) * c,
target_arrays,
critic_arrays,
Expand Down
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.3.0"
version = "0.4.0"
description = "JAX library for robotics learning research"
readme = "README.md"
requires-python = ">=3.12"
Expand Down
Loading
Loading