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
4 changes: 2 additions & 2 deletions src/jaxpot/rl/alphazero_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ def __init__(

@override
def reset_metrics(self) -> None:
self._metrics_accumulator.value = AlphaZeroTrainMetrics.zero()
self._metrics_accumulator.set_value(AlphaZeroTrainMetrics.zero())

@override
def compute_metrics(self) -> dict[str, float]:
return self._metrics_accumulator.value.compute(self.training_steps.value)
return self._metrics_accumulator.get_value().compute(self.training_steps[...])

@override
def _create_loss_fn(
Expand Down
4 changes: 2 additions & 2 deletions src/jaxpot/rl/ppo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,13 @@ def auxiliary_loss_names(self) -> tuple[str, ...]:

def get_entropy_coeff(self) -> jnp.ndarray:
"""Compute current entropy coefficient with cosine annealing."""
progress = jnp.minimum(self.iterations.value / self.entropy_decay_iterations, 1.0)
progress = jnp.minimum(self.iterations[...] / self.entropy_decay_iterations, 1.0)
return self.entropy_coeff_end + 0.5 * (
self.entropy_coeff_start - self.entropy_coeff_end
) * (1.0 + jnp.cos(jnp.pi * progress))

def compute_metrics(self) -> dict[str, float]:
metrics = self._metrics_accumulator.value.compute(self.training_steps.value)
metrics = self._metrics_accumulator.get_value().compute(self.training_steps[...])
metrics["entropy_coeff"] = float(self.get_entropy_coeff())
return metrics

Expand Down
16 changes: 8 additions & 8 deletions src/jaxpot/rl/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _create_zero_metrics(self) -> Any:

def reset_metrics(self) -> None:
"""Reset accumulated metrics."""
self._metrics_accumulator.value = self._create_zero_metrics()
self._metrics_accumulator.set_value(self._create_zero_metrics())

@abstractmethod
def compute_metrics(self) -> dict[str, float]:
Expand Down Expand Up @@ -201,14 +201,14 @@ def scan_step(carry, batch_slice):
(_, metrics), grads = grad_fn(params, batch, dynamic_args)
if replicated_sharding is not None:
grads = jax.lax.with_sharding_constraint(grads, replicated_sharding)
grad_norm = optax.global_norm(grads)
grad_norm = optax.tree.norm(grads)
grad_norm_clipped = grad_norm
update_grads = grads
if max_grad_norm is not None:
clip_tx = optax.clip_by_global_norm(max_grad_norm)
clip_state = clip_tx.init(grads)
clipped_grads, _ = clip_tx.update(grads, clip_state)
grad_norm_clipped = optax.global_norm(clipped_grads)
grad_norm_clipped = optax.tree.norm(clipped_grads)
update_grads = clipped_grads

metrics = dataclasses.replace(
Expand Down Expand Up @@ -313,10 +313,10 @@ def _run_all_epochs(
# Variable's metadata (sharding hints, OptVariable vs OptArray
# distinctions, etc.). Same pattern the league restore path uses.
nnx.replace_by_pure_dict(params, nnx.to_pure_dict(new_params_pure))
nnx.update(model, nnx.State.merge(params, non_params))
nnx.update(model, nnx.merge_state(params, non_params))
nnx.update(self.optimizer.opt_state, nnx.state(opt_state))
self._metrics_accumulator.value = final_metrics
self.training_steps.value = final_steps
self._metrics_accumulator.set_value(final_metrics)
self.training_steps[...] = final_steps

def train_epochs(
self,
Expand All @@ -330,7 +330,7 @@ def train_epochs(
``seq_batch_size = batch_size // seq_len``.
"""
self.reset_metrics()
self.training_steps.value = jnp.zeros((), dtype=jnp.int32)
self.training_steps[...] = jnp.zeros((), dtype=jnp.int32)

seq_batch_size = self.batch_size // self.seq_len

Expand All @@ -345,7 +345,7 @@ def train_epochs(
batched_data = jax.device_put(batched_data, self._batched_data_sharding)

self._run_all_epochs(model, batched_data)
self.iterations.value = self.iterations.value + 1
self.iterations[...] = self.iterations[...] + 1
return self.compute_metrics()


Expand Down
2 changes: 1 addition & 1 deletion tests/test_checkpoint_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class _Env:
leaves, treedef = jax.tree_util.tree_flatten(params)
mutated_leaves = [leaf + 0.1234 for leaf in leaves]
mutated_params = jax.tree_util.tree_unflatten(treedef, mutated_leaves)
nnx.update(checkpoint.model, nnx.State.merge(mutated_params, non_params))
nnx.update(checkpoint.model, nnx.merge_state(mutated_params, non_params))

before_params = _flat_params(checkpoint.model)
before_key = jax.device_get(checkpoint.key)
Expand Down
Loading