`unbind` during `init` does not provide a state and cannot be re-`bind`ed
- Dominant language
- Jupyter Notebook
- Stars
- 7.3k
- Forks
- 833
- Avg merge
- 5h 11m
- Merged PRs (30d)
- 5
Description
This is a MWE derived from #2887 . This prevents using `flax` with diffrax.
If you unbind a module during `init` time, you only get the unbounded module but not the state, and you cannot re-bind it.
I'm not sure if it's intended or not, but I think it should work, as it does work during apply...
```python
import flax.linen as nn
import jax.numpy as jnp
import jax
import diffrax as dfx
from typing import Any
class Test(nn.Module):
def setup(self):
self.ode_term = nn.Dense(features=1)
def __call__(self, x):
ma_unbinded, ode_term_vars = self.ode_term.unbind()
print("pars are", ma_unbinded)
print("o are", ode_term_vars)
ma_binded = ma_unbinded.bind(ode_term_vars)
print("ma_binded is", ma_binded)
solution = ma_binded(x)
return solution
ma = Test()
x = jnp.ones(1)
key = jax.random.PRNGKey(1)
pars = ma.init(key, x)
```
Leads to the following error
```python
File ~/Documents/pythonenvs/netket/python-3.10.6/lib/python3.10/site-packages/flax/core/scope.py:815, in Scope.param(self, name, init_fn, unbox, *init_args)
813 if not self.is_mutable_collection('params'):
814 if self.is_collection_empty('params'):
--> 815 raise errors.ScopeCollectionNotFound('params', name, self.path_text)
816 raise errors.ScopeParamNotFoundError(name, self.path_text)
817 value = init_fn(self.make_rng('params'), *init_args)
ScopeCollectionNotFound: Tried to access "kernel" from collection "params" in "/" but the collection is empty. (https://flax.readthedocs.io/en/latest/api_reference/flax.errors.html#flax.errors.ScopeCollectionNotFound)
```
Contributor guide
Research direction
Start by reproducing the provided MWE, then inspect flax/core/scope.py around Scope.param and the module unbind/bind behavior during init. Compare this with the behavior during apply; done means the init-time unbind provides the needed state and rebinding allows the module call without ScopeCollectionNotFound.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100