Skip to content

Commit 7204719

Browse files
committed
tiny fixes to normalization
1 parent f8fac50 commit 7204719

9 files changed

Lines changed: 55 additions & 3 deletions

src/maxdiffusion/configs/base_flux2klein.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ t5xxl_model_name_or_path: 'ariG23498/t5-v1-1-xxl-flax'
3333

3434
# Flux params
3535
flux_name: "flux2klein"
36+
scale_shift_order: "scale_shift"
3637
use_latents: False
3738
max_sequence_length: 512
3839
time_shift: True

src/maxdiffusion/configs/base_flux2klein_9B.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ t5xxl_model_name_or_path: 'ariG23498/t5-v1-1-xxl-flax'
3333

3434
# Flux params
3535
flux_name: "flux2klein_9B"
36+
scale_shift_order: "scale_shift"
3637
use_latents: False
3738
max_sequence_length: 512
3839
time_shift: True

src/maxdiffusion/generate_flux2klein.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def load_or_generate_latents(config):
130130
"""
131131
Loads saved latents if use_latents is True, otherwise generates random latents.
132132
"""
133+
if isinstance(config, dict):
134+
from types import SimpleNamespace
135+
config = SimpleNamespace(**config)
136+
133137
batch_size = config.batch_size
134138
height = config.height
135139
width = config.width
@@ -186,10 +190,21 @@ def get_qwen3_models(repo_id="black-forest-labs/FLUX.2-klein-4B"):
186190
"""
187191
global _tokenizer, _text_encoder
188192
if _tokenizer is None:
193+
import os
189194
import torch
190195
from transformers import Qwen2TokenizerFast, Qwen3ForCausalLM
191196

192-
print(f"Loading Qwen3 models from cached repo: {repo_id}...")
197+
# Resolve absolute local path from HF cache if it exists to bypass buggy from_pretrained resolving
198+
hf_home = os.environ.get("HF_HOME", os.path.expanduser("~/.cache/huggingface"))
199+
cache_dir = os.path.join(hf_home, "hub", f"models--{repo_id.replace('/', '--')}", "snapshots")
200+
if os.path.exists(cache_dir):
201+
snapshots = os.listdir(cache_dir)
202+
if snapshots:
203+
snapshot_dir = os.path.join(cache_dir, snapshots[0])
204+
print(f"Detected local cache directory: {snapshot_dir}")
205+
repo_id = snapshot_dir
206+
207+
print(f"Loading Qwen3 models from repo path: {repo_id}...")
193208
try:
194209
_tokenizer = Qwen2TokenizerFast.from_pretrained(repo_id, local_files_only=True)
195210
except Exception:
@@ -657,6 +672,7 @@ def main(argv):
657672
dtype=jnp.bfloat16 if config.weights_dtype == "bfloat16" else jnp.float32,
658673
weights_dtype=jnp.bfloat16 if config.weights_dtype == "bfloat16" else jnp.float32,
659674
attention_kernel=config.attention,
675+
scale_shift_order=getattr(config, "scale_shift_order", "shift_scale"),
660676
)
661677

662678
# 5b. Instantiate JAX FlaxAutoencoderKL VAE

src/maxdiffusion/generate_flux2klein_9B.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ def load_or_generate_latents(config):
137137
"""
138138
Loads saved latents if use_latents is True, otherwise generates random latents.
139139
"""
140+
if isinstance(config, dict):
141+
from types import SimpleNamespace
142+
config = SimpleNamespace(**config)
143+
140144
batch_size = config.batch_size
141145
height = config.height
142146
width = config.width
@@ -193,10 +197,21 @@ def get_qwen3_models(repo_id="black-forest-labs/FLUX.2-klein-4B"):
193197
"""
194198
global _tokenizer, _text_encoder
195199
if _tokenizer is None:
200+
import os
196201
import torch
197202
from transformers import Qwen2TokenizerFast, Qwen3ForCausalLM
198203

199-
print(f"Loading Qwen3 models from cached repo: {repo_id}...")
204+
# Resolve absolute local path from HF cache if it exists to bypass buggy from_pretrained resolving
205+
hf_home = os.environ.get("HF_HOME", os.path.expanduser("~/.cache/huggingface"))
206+
cache_dir = os.path.join(hf_home, "hub", f"models--{repo_id.replace('/', '--')}", "snapshots")
207+
if os.path.exists(cache_dir):
208+
snapshots = os.listdir(cache_dir)
209+
if snapshots:
210+
snapshot_dir = os.path.join(cache_dir, snapshots[0])
211+
print(f"Detected local cache directory: {snapshot_dir}")
212+
repo_id = snapshot_dir
213+
214+
print(f"Loading Qwen3 models from repo path: {repo_id}...")
200215
try:
201216
_tokenizer = Qwen2TokenizerFast.from_pretrained(repo_id, local_files_only=True)
202217
except Exception:
@@ -702,6 +717,7 @@ def main(argv):
702717
dtype=jnp.bfloat16 if config.weights_dtype == "bfloat16" else jnp.float32,
703718
weights_dtype=jnp.bfloat16 if config.weights_dtype == "bfloat16" else jnp.float32,
704719
attention_kernel=config.attention,
720+
scale_shift_order=getattr(config, "scale_shift_order", "shift_scale"),
705721
)
706722

707723
# 6b. Instantiate JAX FlaxAutoencoderKL VAE

src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ class FluxTransformer2DModel(nn.Module, FlaxModelMixin, ConfigMixin):
453453
flash_min_seq_length: int = 4096
454454
flash_block_sizes: BlockSizes = None
455455
mesh: jax.sharding.Mesh = None
456+
scale_shift_order: str = "shift_scale"
456457
dtype: jnp.dtype = jnp.float32
457458
weights_dtype: jnp.dtype = jnp.float32
458459
precision: jax.lax.Precision = None
@@ -581,6 +582,7 @@ def setup(self):
581582
dtype=self.dtype,
582583
weights_dtype=self.weights_dtype,
583584
precision=self.precision,
585+
scale_shift_order=self.scale_shift_order,
584586
)
585587

586588
self.proj_out = nn.Dense(

src/maxdiffusion/models/normalization_flax.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class AdaLayerNormContinuous(nn.Module):
2929
dtype: jnp.dtype = jnp.float32
3030
weights_dtype: jnp.dtype = jnp.float32
3131
precision: jax.lax.Precision = None
32+
scale_shift_order: str = "shift_scale"
3233

3334
@nn.compact
3435
def __call__(self, x, conditioning_embedding):
@@ -42,7 +43,14 @@ def __call__(self, x, conditioning_embedding):
4243
param_dtype=self.weights_dtype,
4344
precision=self.precision,
4445
)(nn.silu(conditioning_embedding))
45-
scale, shift = jnp.split(emb, 2, axis=1)
46+
47+
if self.scale_shift_order == "scale_shift":
48+
scale, shift = jnp.split(emb, 2, axis=1)
49+
elif self.scale_shift_order == "shift_scale":
50+
shift, scale = jnp.split(emb, 2, axis=1)
51+
else:
52+
raise ValueError(f"Unsupported scale_shift_order: {self.scale_shift_order}")
53+
4654
scale = nn.with_logical_constraint(scale, ("activation_batch", "activation_embed"))
4755
shift = nn.with_logical_constraint(shift, ("activation_batch", "activation_embed"))
4856
x = nn.LayerNorm(epsilon=self.eps, use_bias=self.elementwise_affine, use_scale=self.elementwise_affine)(x)

src/maxdiffusion/tests/flux2klein/generate_flux2klein_9b_parity_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def test_e2e_parity_9b(self):
233233
theta=2000,
234234
mesh=mesh,
235235
dtype=jnp.bfloat16 if config.weights_dtype == "bfloat16" else jnp.float32,
236+
scale_shift_order=getattr(config, "scale_shift_order", "shift_scale"),
236237
)
237238

238239
vae = FlaxAutoencoderKL(

src/maxdiffusion/tests/flux2klein/generate_flux2klein_e2e_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def test_end_to_end_parity_and_offloading(self):
135135
theta=2000,
136136
mesh=mesh,
137137
dtype=jnp.float32,
138+
scale_shift_order=getattr(config, "scale_shift_order", "shift_scale"),
138139
)
139140

140141
# VAE Config

src/maxdiffusion/tests/flux2klein/generate_flux2klein_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def test_context_embedder_projection(self):
134134
"run_name=flux_test",
135135
"output_dir=/tmp/",
136136
"jax_cache_dir=/tmp/cache_dir",
137+
"ici_data_parallelism=1",
138+
"ici_fsdp_parallelism=1",
137139
], unittest=True)
138140
config = pyconfig.config
139141

@@ -372,6 +374,8 @@ def test_attention_blocks_parity(self):
372374
"run_name=flux_test",
373375
"output_dir=/tmp/",
374376
"jax_cache_dir=/tmp/cache_dir",
377+
"ici_data_parallelism=1",
378+
"ici_fsdp_parallelism=1",
375379
], unittest=True)
376380
config = pyconfig.config
377381

@@ -626,6 +630,8 @@ def test_full_transformer_and_multistep_parity(self):
626630
"run_name=flux_test",
627631
"output_dir=/tmp/",
628632
"jax_cache_dir=/tmp/cache_dir",
633+
"ici_data_parallelism=1",
634+
"ici_fsdp_parallelism=1",
629635
], unittest=True)
630636
config = pyconfig.config
631637

0 commit comments

Comments
 (0)