diff --git a/src/maxdiffusion/models/embeddings_flax.py b/src/maxdiffusion/models/embeddings_flax.py index 17ac2b07a..f147366a6 100644 --- a/src/maxdiffusion/models/embeddings_flax.py +++ b/src/maxdiffusion/models/embeddings_flax.py @@ -546,17 +546,22 @@ class CombinedTimestepGuidanceTextProjEmbeddings(nn.Module): @nn.compact def __call__(self, timestep, guidance, pooled_projection=None): - timesteps_proj = FlaxTimesteps(dim=self.frequency_embedding_size, flip_sin_to_cos=True, freq_shift=0)(timestep) - dtype = pooled_projection.dtype if pooled_projection is not None else jnp.float32 + # timestep and guidance arrive already projected: FluxTransformer2DModel runs + # both through its own timestep_embedding(), which carries a time_factor the + # sinusoidal helper here does not, and the Linen Flux2KleinTransformer2DModel + # projects them with FlaxTimesteps. Projecting again would shape them + # (batch, frequency_embedding_size, embedding_dim) and break the sum with the + # pooled projection below. The NNX variant owns its projection instead. + timesteps_proj = timestep timestep_emb = FlaxTimestepEmbedding( time_embed_dim=self.embedding_dim, dtype=self.dtype, weights_dtype=self.weights_dtype - )(timesteps_proj.astype(dtype)) + )(timesteps_proj) if self.guidance_embeds and guidance is not None: - guidance_proj = FlaxTimesteps(dim=self.frequency_embedding_size, flip_sin_to_cos=True, freq_shift=0)(guidance) + guidance_proj = guidance guidance_emb = FlaxTimestepEmbedding( time_embed_dim=self.embedding_dim, dtype=self.dtype, weights_dtype=self.weights_dtype - )(guidance_proj.astype(dtype)) + )(guidance_proj) time_guidance_emb = timestep_emb + guidance_emb else: time_guidance_emb = timestep_emb diff --git a/src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py b/src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py index 2d536ca1c..d8ce0e941 100644 --- a/src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py +++ b/src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py @@ -37,6 +37,7 @@ CombinedTimestepGuidanceTextProjEmbeddings, CombinedTimestepGuidanceTextProjEmbeddings as CombinedTimestepGuidanceTextEmbeddings, CombinedTimestepTextProjEmbeddings, + FlaxTimesteps, NNXCombinedTimestepGuidanceTextProjEmbeddings, ) from .... import common_types @@ -545,6 +546,9 @@ def setup(self): self.inner_dim, elementwise_affine=False, eps=self.eps, + # FLUX loads adaLN_modulation_1 from the original checkpoint, which emits + # shift before scale. The module default follows Flux.2-Klein instead. + scale_shift_order="shift_scale", dtype=self.dtype, weights_dtype=self.weights_dtype, precision=self.precision, @@ -1049,6 +1053,10 @@ def setup(self): weights_dtype=self.weights_dtype, precision=self.precision, ) + # The shared embeddings take projected timesteps, as FluxTransformer2DModel + # passes them. FlaxTimesteps holds no parameters, so the checkpoint layout + # is unchanged. + self.time_proj = FlaxTimesteps(dim=self.time_text_embed.frequency_embedding_size, flip_sin_to_cos=True, freq_shift=0) if self.use_global_modulation: self.double_stream_modulation_img = nn.Dense( @@ -1177,9 +1185,9 @@ def __call__( intermediates["single_block_outputs"] = [] hidden_states = self.x_embedder(hidden_states) - timestep = timestep * 1000.0 + timestep = self.time_proj(timestep * 1000.0) if guidance is not None: - guidance = guidance * 1000.0 + guidance = self.time_proj(guidance * 1000.0) temb = self.time_text_embed(timestep, guidance, pooled_projections) temb = temb.astype(hidden_states.dtype) diff --git a/src/maxdiffusion/models/flux/util.py b/src/maxdiffusion/models/flux/util.py index 77e34099c..f651368ec 100644 --- a/src/maxdiffusion/models/flux/util.py +++ b/src/maxdiffusion/models/flux/util.py @@ -260,7 +260,7 @@ def load_flow_model(name: str, eval_shapes: dict, device: str, hf_download: bool renamed_pt_key = renamed_pt_key.replace("out_layer", "linear_2") elif "final_layer" in renamed_pt_key: renamed_pt_key = renamed_pt_key.replace("final_layer.linear", "proj_out") - renamed_pt_key = renamed_pt_key.replace("final_layer.adaLN_modulation_1", "norm_out.Dense_0") + renamed_pt_key = renamed_pt_key.replace("final_layer.adaLN_modulation_1", "norm_out.linear") pt_tuple_key = tuple(renamed_pt_key.split(".")) flax_key, flax_tensor = rename_key_and_reshape_tensor(pt_tuple_key, tensor, eval_shapes) diff --git a/src/maxdiffusion/tests/flux_conditioning_test.py b/src/maxdiffusion/tests/flux_conditioning_test.py new file mode 100644 index 000000000..068bfd31b --- /dev/null +++ b/src/maxdiffusion/tests/flux_conditioning_test.py @@ -0,0 +1,129 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for the FLUX conditioning path (CPU backend). + +FLUX shares CombinedTimestepGuidanceTextProjEmbeddings and +AdaLayerNormContinuous with Flux.2-Klein, which reads them with different +conventions. These cover the conventions FLUX needs. +""" + +import os +import unittest + +os.environ.setdefault("JAX_PLATFORMS", "cpu") + +import flax +import jax +import jax.numpy as jnp +import numpy as np + +from maxdiffusion.models.embeddings_flax import CombinedTimestepGuidanceTextProjEmbeddings +from maxdiffusion.models.flux.transformers.transformer_flux_flax import Flux2KleinTransformer2DModel +from maxdiffusion.models.normalization_flax import AdaLayerNormContinuous + + +def _layer_norm(x, eps=1e-5): + mean = np.mean(x, axis=-1, keepdims=True) + variance = np.var(x, axis=-1, keepdims=True) + return (x - mean) / np.sqrt(variance + eps) + + +class FluxConditioningTest(unittest.TestCase): + + def test_guidance_embeddings_accept_projected_timesteps(self): + """FluxTransformer2DModel projects timestep and guidance before this module.""" + batch, frequency_embedding_size, embedding_dim, pooled_projection_dim = 2, 256, 64, 32 + module = CombinedTimestepGuidanceTextProjEmbeddings( + embedding_dim=embedding_dim, pooled_projection_dim=pooled_projection_dim + ) + timestep = jnp.zeros((batch, frequency_embedding_size), jnp.float32) + guidance = jnp.zeros((batch, frequency_embedding_size), jnp.float32) + pooled_projection = jnp.zeros((batch, pooled_projection_dim), jnp.float32) + + variables = module.init(jax.random.PRNGKey(0), timestep, guidance, pooled_projection) + conditioning = module.apply(variables, timestep, guidance, pooled_projection) + + self.assertEqual(conditioning.shape, (batch, embedding_dim)) + + def test_norm_out_dense_is_named_linear(self): + """load_flow_model writes final_layer.adaLN_modulation_1 to norm_out/linear.""" + module = AdaLayerNormContinuous(embedding_dim=4, elementwise_affine=False) + x = jnp.zeros((2, 3, 4), jnp.float32) + conditioning_embedding = jnp.zeros((2, 8), jnp.float32) + + variables = module.init(jax.random.PRNGKey(0), x, conditioning_embedding) + + self.assertIn("linear", variables["params"]) + + def test_shift_scale_order_reads_shift_first(self): + """The original FLUX checkpoint emits shift ahead of scale.""" + embedding_dim, conditioning_dim, batch, sequence = 4, 8, 2, 3 + x = jax.random.normal(jax.random.PRNGKey(1), (batch, sequence, embedding_dim)) + conditioning_embedding = jax.random.normal(jax.random.PRNGKey(2), (batch, conditioning_dim)) + + def modulate(scale_shift_order): + module = AdaLayerNormContinuous( + embedding_dim=embedding_dim, elementwise_affine=False, scale_shift_order=scale_shift_order + ) + variables = flax.linen.meta.unbox(flax.core.unfreeze(module.init(jax.random.PRNGKey(0), x, conditioning_embedding))) + # A zero kernel leaves the bias as the whole modulation, so the halves are + # known: 2.0 in the first, 0.0 in the second. + variables["params"]["linear"]["kernel"] = jnp.zeros_like(variables["params"]["linear"]["kernel"]) + variables["params"]["linear"]["bias"] = jnp.concatenate([jnp.full((embedding_dim,), 2.0), jnp.zeros((embedding_dim,))]) + return module.apply(variables, x, conditioning_embedding) + + normalized = _layer_norm(np.asarray(x)) + + # shift first: (1 + 0) * norm + 2 + np.testing.assert_allclose(np.asarray(modulate("shift_scale")), normalized + 2.0, atol=1e-4) + # scale first: (1 + 2) * norm + 0 + np.testing.assert_allclose(np.asarray(modulate("scale_shift")), 3.0 * normalized, atol=1e-4) + + def test_flux2klein_linen_projects_timesteps_before_embedding(self): + """The Linen Flux.2-Klein model shares the guidance embeddings with FLUX.""" + batch, image_tokens, text_tokens = 2, 4, 3 + model = Flux2KleinTransformer2DModel( + in_channels=4, + num_layers=1, + num_single_layers=1, + attention_head_dim=8, + num_attention_heads=2, + joint_attention_dim=8, + pooled_projection_dim=8, + axes_dim=(2, 2, 2, 2), + axes_dims_rope=(2, 2, 2, 2), + ) + inputs = dict( + hidden_states=jnp.ones((batch, image_tokens, 4), jnp.float32), + encoder_hidden_states=jnp.ones((batch, text_tokens, 8), jnp.float32), + pooled_projections=jnp.ones((batch, 8), jnp.float32), + timestep=jnp.full((batch,), 0.5, jnp.float32), + img_ids=jnp.zeros((image_tokens, 4), jnp.float32), + txt_ids=jnp.zeros((text_tokens, 4), jnp.float32), + guidance=jnp.full((batch,), 0.5, jnp.float32), + return_dict=False, + ) + + variables = model.init(jax.random.PRNGKey(0), **inputs) + time_text_embed = flax.linen.meta.unbox(variables["params"]["time_text_embed"]) + for embedder in ("FlaxTimestepEmbedding_0", "FlaxTimestepEmbedding_1"): + self.assertEqual(time_text_embed[embedder]["linear_1"]["kernel"].shape, (256, 16)) + + (sample,) = model.apply(variables, **inputs) + self.assertEqual(sample.shape, (batch, image_tokens, 4)) + + +if __name__ == "__main__": + unittest.main()