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

Don't create param in normalization layers instead of create None-value params. #4501

Merged
merged 1 commit into from
Jan 25, 2025
Merged
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
35 changes: 21 additions & 14 deletions flax/nnx/nn/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,19 @@ def __init__(
self.mean = nnx.BatchStat(jnp.zeros(feature_shape, jnp.float32))
self.var = nnx.BatchStat(jnp.ones(feature_shape, jnp.float32))

self.scale: nnx.Param[jax.Array] | None
if use_scale:
key = rngs.params()
self.scale = nnx.Param(scale_init(key, feature_shape, param_dtype))
else:
self.scale = nnx.Param(None)
self.scale = None

self.bias: nnx.Param[jax.Array] | None
if use_bias:
key = rngs.params()
self.bias = nnx.Param(bias_init(key, feature_shape, param_dtype))
else:
self.bias = nnx.Param(None)
self.bias = None

self.num_features = num_features
self.use_running_average = use_running_average
Expand Down Expand Up @@ -368,8 +370,8 @@ def __call__(
x,
mean,
var,
self.scale.value,
self.bias.value,
self.scale.value if self.scale else None,
self.bias.value if self.bias else None,
reduction_axes,
feature_axes,
self.dtype,
Expand Down Expand Up @@ -454,17 +456,19 @@ def __init__(
):
feature_shape = (num_features,)

self.scale: nnx.Param[jax.Array] | None
if use_scale:
key = rngs.params()
self.scale = nnx.Param(scale_init(key, feature_shape, param_dtype))
else:
self.scale = nnx.Param(None)
self.scale = None

self.bias: nnx.Param[jax.Array] | None
if use_bias:
key = rngs.params()
self.bias = nnx.Param(bias_init(key, feature_shape, param_dtype))
else:
self.bias = nnx.Param(None)
self.bias = None

self.num_features = num_features
self.epsilon = epsilon
Expand Down Expand Up @@ -503,8 +507,8 @@ def __call__(self, x, *, mask: tp.Optional[jax.Array] = None):
x,
mean,
var,
self.scale.value,
self.bias.value,
self.scale.value if self.scale else None,
self.bias.value if self.bias else None,
self.reduction_axes,
self.feature_axes,
self.dtype,
Expand Down Expand Up @@ -582,11 +586,12 @@ def __init__(
):
feature_shape = (num_features,)

self.scale: nnx.Param[jax.Array] | None
if use_scale:
key = rngs.params()
self.scale = nnx.Param(scale_init(key, feature_shape, param_dtype))
else:
self.scale = nnx.Param(None)
self.scale = None

self.num_features = num_features
self.epsilon = epsilon
Expand Down Expand Up @@ -624,7 +629,7 @@ def __call__(self, x, mask: tp.Optional[jax.Array] = None):
x,
mean,
var,
self.scale.value,
self.scale.value if self.scale else None,
None,
self.reduction_axes,
self.feature_axes,
Expand Down Expand Up @@ -757,17 +762,19 @@ def __init__(
self.group_size = num_features // num_groups

feature_shape = (num_features,)
self.scale: nnx.Param[jax.Array] | None
if use_scale:
key = rngs.params()
self.scale = nnx.Param(scale_init(key, feature_shape, param_dtype))
else:
self.scale = nnx.Param(None)
self.scale = None

self.bias: nnx.Param[jax.Array] | None
if use_bias:
key = rngs.params()
self.bias = nnx.Param(bias_init(key, feature_shape, param_dtype))
else:
self.bias = nnx.Param(None)
self.bias = None

self.epsilon = epsilon
self.dtype = dtype
Expand Down Expand Up @@ -822,8 +829,8 @@ def __call__(self, x, *, mask: tp.Optional[jax.Array] = None):
x,
mean,
var,
self.scale.value,
self.bias.value,
self.scale.value if self.scale else None,
self.bias.value if self.bias else None,
reduction_axes[:-1],
(self.feature_axis,),
self.dtype,
Expand Down
Loading