Skip to content

Commit

Permalink
Add schedule free Adam optimizer.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 634762603
  • Loading branch information
ColCarroll authored and The bayeux Authors committed May 17, 2024
1 parent fb1861b commit cb27471
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions bayeux/_src/optimize/optax.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@
from bayeux._src.optimize import shared
import jax
import optax
import optax.contrib


class _OptaxOptimizer(shared.Optimizer):
"""Base class for optax optimizers."""
_base = optax

def get_kwargs(self, **kwargs):
kwargs = self.default_kwargs() | kwargs
optimizer = getattr(optax, self.optimizer)
optimizer = getattr(self._base, self.optimizer)
return {optimizer: shared.get_optimizer_kwargs(optimizer, kwargs),
"extra_parameters": shared.get_extra_kwargs(kwargs)}

def __call__(self, seed, **kwargs):
kwargs = self.get_kwargs(**kwargs)
fun, initial_state, apply_transform = self._prep_args(seed, kwargs)

optimizer_fn = getattr(optax, self.optimizer)
optimizer_fn = getattr(self._base, self.optimizer)
optimizer = optimizer_fn(**kwargs[optimizer_fn])
num_iters = kwargs["extra_parameters"]["num_iters"]
optimizer = functools.partial(
Expand Down Expand Up @@ -183,6 +185,19 @@ def default_kwargs(self) -> dict[str, float]:
return kwargs


class ScheduleFree(_OptaxOptimizer):
_base = optax.contrib
name = "optax_schedule_free"
optimizer = "schedule_free"

def default_kwargs(self) -> dict[str, float]:
kwargs = super().default_kwargs()
base_optimizer = optax.adam(
**shared.get_optimizer_kwargs(optax.adam, kwargs))
kwargs["base_optimizer"] = base_optimizer
return kwargs


class Sgd(_OptaxOptimizer):
name = "optax_sgd"
optimizer = "sgd"
Expand Down
2 changes: 2 additions & 0 deletions bayeux/optimize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
# from bayeux._src.optimize.optax import OptimisticGradientDescent # pylint: disable=line-too-long
from bayeux._src.optimize.optax import Radam
from bayeux._src.optimize.optax import Rmsprop
from bayeux._src.optimize.optax import ScheduleFree
from bayeux._src.optimize.optax import Sgd
from bayeux._src.optimize.optax import Sm3
from bayeux._src.optimize.optax import Yogi
Expand All @@ -66,6 +67,7 @@
# "Dpsgd",
"Radam",
"Rmsprop",
"ScheduleFree",
"Sgd",
"Sm3",
"Yogi",
Expand Down

0 comments on commit cb27471

Please sign in to comment.