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

[nnx] Add specific model typing for nnx.Optimizer #4470

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions flax/nnx/training/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
from __future__ import annotations

import typing as tp

import jax
import jax.numpy as jnp
import optax
Expand All @@ -23,6 +25,8 @@
from flax.nnx.object import Object
from flax.nnx.variablelib import Variable, VariableState

M = tp.TypeVar('M', bound=nnx.Module)

# TODO: add tests and docstrings


Expand Down Expand Up @@ -101,7 +105,7 @@ def optimizer_update_variables(x, update):
return jax.tree.map(optimizer_update_variables, opt_state, updates)


class Optimizer(Object):
class Optimizer(Object, tp.Generic[M]):
"""Simple train state for the common case with a single Optax optimizer.

Example usage::
Expand Down Expand Up @@ -168,7 +172,7 @@ class Optimizer(Object):

def __init__(
self,
model: nnx.Module,
model: M,
tx: optax.GradientTransformation,
wrt: filterlib.Filter = nnx.Param,
):
Expand Down
Loading