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

Fixes random_flax_module with flax.linen.BatchNorm #1823

Merged
merged 8 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion numpyro/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,8 @@ def __init__(self, fn=None, data=None, substitute_fn=None):

def process_message(self, msg):
if (
msg["type"] not in ("sample", "param", "mutable", "plate", "deterministic")
msg["type"]
not in ("sample", "param", "mutable", "plate", "deterministic", "prng_key")
juanitorduz marked this conversation as resolved.
Show resolved Hide resolved
) or msg.get("_control_flow_done", False):
if msg["type"] == "control_flow":
if self.data is not None:
Expand Down
26 changes: 10 additions & 16 deletions numpyro/infer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,17 @@ def initialize_model(
has_enumerate_support,
model_trace,
) = _get_model_transforms(substituted_model, model_args, model_kwargs)

# substitute param sites from model_trace to model so
# we don't need to generate again parameters of `numpyro.module`
model = substitute(
model,
data={
k: site["value"]
for k, site in model_trace.items()
if site["type"] in ["param"]
},
)
def substitute_fn(site):
if site["type"] in ["param", "mutable"]:
return site["value"]
elif site["type"] == "prng_key":
return random.PRNGKey(0)

model = substitute(model, substitute_fn=substitute_fn)

juanitorduz marked this conversation as resolved.
Show resolved Hide resolved
constrained_values = {
k: v["value"]
for k, v in model_trace.items()
Expand Down Expand Up @@ -701,14 +702,7 @@ def initialize_model(
prototype_params = transform_fn(inv_transforms, constrained_values, invert=True)
(init_params, pe, grad), is_valid = find_valid_initial_params(
rng_key,
substitute(
model,
data={
k: site["value"]
for k, site in model_trace.items()
if site["type"] in ["plate"]
},
),
substitute(model, substitute_fn=substitute_fn),
init_strategy=init_strategy,
enum=has_enumerate_support,
model_args=model_args,
Expand Down
13 changes: 12 additions & 1 deletion test/contrib/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
random_haiku_module,
)
import numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS
from numpyro.infer import MCMC, NUTS, SVI, Trace_ELBO
from numpyro.infer.autoguide import AutoDelta

pytestmark = pytest.mark.filterwarnings(
"ignore:jax.tree_.+ is deprecated:FutureWarning"
Expand Down Expand Up @@ -256,6 +257,11 @@ def model():
else:
assert set(tr.keys()) == {"nn$params", "x", "y"}

# test svi
guide = AutoDelta(model)
svi = SVI(model, guide, numpyro.optim.Adam(0.01), Trace_ELBO())
svi.run(random.PRNGKey(100), 10)


@pytest.mark.parametrize("dropout", [True, False])
@pytest.mark.parametrize("batchnorm", [True, False])
Expand Down Expand Up @@ -300,3 +306,8 @@ def model():
assert tr["nn$state"]["type"] == "mutable"
else:
assert set(tr.keys()) == {"nn$params", "x", "y"}

# test svi
guide = AutoDelta(model)
svi = SVI(model, guide, numpyro.optim.Adam(0.01), Trace_ELBO())
svi.run(random.PRNGKey(100), 10)
Comment on lines +310 to +313
Copy link
Contributor Author

@juanitorduz juanitorduz Jun 30, 2024

Choose a reason for hiding this comment

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

Without these SVI lines, the tests were passing in the master branch. Once added, we got the error. The suggested fix in the issue does solve it.

Loading