Skip to content

nnx.Embed resolves dtype=None at construction, so it diverges from linen.Embed #5656

Open

Description

@mahdi-shafiei

System information

  • OS Platform and Distribution: Ubuntu 24.04.5 LTS
  • Flax, jax, jaxlib versions: flax 0.12.12, jax 0.11.1, jaxlib 0.11.1 (also reproduced on flax main at 204a819)
  • Python version: 3.12.14
  • GPU/TPU model and memory: none, CPU only
  • CUDA version (if applicable): n/a

Problem you have encountered:

nnx.Embed.__init__ resolves the default dtype when the module is built:

self.dtype = dtype or self.embedding.dtype   # flax/nnx/nn/linear.py:1278 at main 204a819c

linen.Embed keeps dtype=None and lets promote_dtype resolve it on every call. Both document
dtype as "the dtype of the embedding vectors (default: same as embedding)". The line dates from the
first NNX commit (8e9df27, "add experimental nnx").

This gives two observable differences from linen.Embed, both with the default dtype=None:

  1. The table's dtype changes after construction, for example when a model's parameters are cast
    to bfloat16 for inference. nnx.Embed keeps computing in the construction-time dtype and returns
    float32; linen.Embed returns bfloat16.
  2. attend with a query wider than the table. With a float16 table and a float32 query,
    nnx.Embed.attend casts the query down to float16; linen.Embed.attend promotes both to
    float32.

tests/nnx/nn/embed_test.py::test_nnx_linen_equivalence parametrizes dtype over float32 and
float16 only, so the default dtype=None is never compared with linen.

What you expected to happen:

With dtype=None, nnx.Embed computes in the embedding's current dtype and promotes in attend,
as linen.Embed does.

Logs, error messages, etc:

No error; the output dtype differs. Output of the script below:

1 __call__ after cast to bf16: nnx float32 linen bfloat16
2 attend(f32 query), f16 table: nnx float16 linen float32

Steps to reproduce:

import jax
import jax.numpy as jnp
from flax import linen, nnx

labels = jnp.array([0, 1, 2])


def cast_floats(tree, dtype):
    return jax.tree.map(
        lambda a: a.astype(dtype) if jnp.issubdtype(a.dtype, jnp.floating) else a, tree
    )


# 1. __call__ after casting the parameters to bfloat16.
graphdef, state = nnx.split(nnx.Embed(4, 8, rngs=nnx.Rngs(0)))
nnx_cast = nnx.merge(graphdef, cast_floats(state, jnp.bfloat16))
linen_embed = linen.Embed(4, 8)
variables = cast_floats(linen_embed.init(jax.random.key(0), labels), jnp.bfloat16)
print("1 __call__ after cast to bf16: nnx", nnx_cast(labels).dtype,
      "linen", linen_embed.apply(variables, labels).dtype)

# 2. attend() with a float16 table and a float32 query.
nnx_f16 = nnx.Embed(4, 8, param_dtype=jnp.float16, rngs=nnx.Rngs(0))
linen_f16 = linen.Embed(4, 8, param_dtype=jnp.float16)
variables_f16 = linen_f16.init(jax.random.key(0), labels)
query = jnp.ones((2, 8), jnp.float32)
print("2 attend(f32 query), f16 table: nnx", nnx_f16.attend(query).dtype,
      "linen", linen_f16.apply(variables_f16, query, method=linen_f16.attend).dtype)

Proposed fix

Store dtype as given (self.dtype = dtype), as linen.Embed and the other NNX layers in
linear.py do (LinearGeneral, Linear, Einsum, Conv and ConvTranspose all store
self.dtype = dtype). promote_dtype(..., dtype=None, inexact=False) already resolves None to
the embedding's dtype in __call__. I could find no reader of an Embed's .dtype attribute in
flax's tree. With that one-line change, both cases above match linen, and embed_test.py,
graph_utils_test.py and spmd_test.py still pass (197 tests, CPU). I can open a PR with the
change and two tests that fail before it and pass after it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions