Skip to content

Add Stateful Modules guide #2689

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cgarciae
Copy link
Collaborator

@cgarciae cgarciae commented Dec 5, 2022

@cgarciae cgarciae changed the title add statful modules guide Add Stateful Modules guide Dec 6, 2022
@codecov-commenter
Copy link

codecov-commenter commented Dec 6, 2022

Codecov Report

Merging #2689 (ff95c1c) into main (db690a4) will increase coverage by 0.00%.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main    #2689   +/-   ##
=======================================
  Coverage   81.15%   81.15%           
=======================================
  Files          51       51           
  Lines        5492     5493    +1     
=======================================
+ Hits         4457     4458    +1     
  Misses       1035     1035           
Impacted Files Coverage Δ
flax/errors.py 85.71% <0.00%> (+0.13%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

* Show how to use ``bind`` to manually run a ``Sequential`` module layer
by layer.

#. Edge cases of using ``bind``:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add this to the "sharp bits" as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: @8bitmp3


Contents
*********

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would first explain that Flax Modules normally are stateless since Flax is using a functional API, and explain why this is the case. Then say that in some cases it can actually be useful to attach variables to the modules, and that the bind/unbind pattern allows this.

@cgarciae
Copy link
Collaborator Author

@jheek:

  • Bound modules don't compose with any JAX API (cannot be tree_map-ed, vmap, etc).

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@cgarciae
Copy link
Collaborator Author

cgarciae commented Dec 16, 2022

Currently bind fails for cases like this:

import flax.linen as nn
import jax
import jax.numpy as jnp

module = nn.Sequential([
    nn.Sequential([
        nn.Dense(8),
        nn.relu,
        nn.Dense(4),
    ]),
    nn.relu,
    nn.Dense(4),
])
x = jnp.ones((1, 5))
variables = module.init(jax.random.PRNGKey(0), x)
bound_module = module.bind(variables)

print(bound_module.layers[0].layers[0].scope) # None  <== !!!

@levskaya will try to come up with a solution to bind submodules, this guide will have to wait for that to be resolved. Previous attempt to solve this: #2028

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.

4 participants