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

Conversation

marcelroed
Copy link

@marcelroed marcelroed commented Jan 1, 2025

What does this PR do?

This PR adds a type variable for nnx.Optimizer.model to make type inference and IDE autocomplete better.

Before:

class MyModel(nnx.Module):
    ...

optimizer = nnx.Optimizer(MyModel(), tx=...)

optimizer.model  # inferred type: nnx.Module

def use_optimizer(optimizer:​ nnx.Optimizer):
    optimizer.model  # inferred type: nnx.Module

After PR:

class MyModel(nnx.Module):
    ...

optimizer = nnx.Optimizer(MyModel(), tx=...)

optimizer.model  # inferred type: MyModel

def use_optimizer(optimizer:​ nnx.Optimizer[MyModel]):
    optimizer.model  # inferred type: MyModel

# Can also be used as before with generic type hints
def use_optimizer(optimizer: nnx.Optimizer):
    optimizer.model  # inferred type: nnx.Module

Checklist

  • This PR fixes a minor issue.

@marcelroed marcelroed changed the title Add specific model typing for nnx.Optimizer [nnx] Add specific model typing for nnx.Optimizer Jan 1, 2025
@cgarciae
Copy link
Collaborator

cgarciae commented Jan 6, 2025

@marcelroed thanks for doing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants