Repository navigation
Add the missing b_hn bias to nnx.GRUCell - #5645
Open
vineethsaivs wants to merge 1 commit into
Open
vineethsaivs wants to merge 1 commit into
vineethsaivs wants to merge 1 commit into
Conversation
The class docstring gives the cell as n = tanh(W_in x + b_in + r * (W_hn h + b_hn)), and flax.linen.GRUCell builds b_hn as the bias of its `hn` layer. nnx fuses r, z and n into one `dense_h` with use_bias=False, so no hidden bias exists and __call__ computes n = activation_fn(xi_n + r * hh_n). A bias on the fused layer would also reach r and z, so carry it as its own parameter, which is what linen does with a separate `hn` bias. bias_init defaults to zeros, so a freshly initialised cell is unchanged; the divergence only appears once that bias is trained or when weights are ported from linen or torch.nn.GRUCell. The bias is promoted with promote_dtype and reshaped to hh_n's rank, as Linear does, so batched carries do not rely on implicit rank promotion and a bfloat16 carry stays bfloat16.
samanklesaria
approved these changes
Nov 9, 2026
| 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,)) |
Collaborator
There was a problem hiding this comment.
Note: I think the reshape here is unnecessary. This is already what broadcast rules give you for free.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #5594. Replaces #5595, which was closed when main was force-pushed to its branch.
nnx.GRUCelldocumentsn = tanh(W_in x + b_in + r * (W_hn h + b_hn)), andlinen.GRUCellbuildsb_hnas the bias of itshnlayer. nnx fuses r, z and n into one bias-freedense_h, sob_hnwas missing.Fix: add it as its own
hn_biasparameter (a bias on the fuseddense_hwould also reach r and z), promoted and broadcast likeLinear's bias.Test: no new tests, per review. With weights copied from
linen.GRUCelland a non-zerobias_init, the outputs match; on main they do not.tests/nnx/nn/recurrent_test.pypasses on CPU (20 tests).Compatibility: one extra array in the parameter tree, so a checkpoint saved from the old
nnx.GRUCellneeds a fallback to restore.bias_initdefaults to zeros, so a fresh cell is numerically unchanged.