Skip to content
Open
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
13 changes: 13 additions & 0 deletions flax/nnx/nn/recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,15 @@ def __init__(
kernel_metadata=recurrent_kernel_metadata,
)

# `b_hn` from the docstring. Only the reset-gated term carries a hidden
# bias, which the fused `dense_h` above cannot express: a bias there would
# reach r and z as well. `flax.linen.GRUCell` builds it as a separate `hn`
# bias for the same reason.
self.hn_bias = nnx.Param(
bias_init(rngs.params(), (hidden_features,), self.param_dtype),
**bias_metadata,
)

if carry_init:
warnings.warn(
"carry_init is provided in __init__. "
Expand Down Expand Up @@ -730,6 +739,10 @@ def __call__(self, carry: Array, inputs: Array) -> tuple[Array, Array]: # type:
z = self.gate_fn(xi_z + hh_z)

# Compute n with an additional linear transformation on h
hh_n, hn_bias = self.promote_dtype(
(hh_n, self.hn_bias[...]), dtype=self.dtype
)
hh_n += jnp.reshape(hn_bias, (1,) * (hh_n.ndim - 1) + (-1,))
n = self.activation_fn(xi_n + r * hh_n)

# Update hidden state
Expand Down
Loading