Skip to content

Commit 55adaa2

Browse files
committed
cleaned up implementation, some potential merge conflicts to be resolved in next commit
1 parent 8ff6bc5 commit 55adaa2

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

  • src/maxdiffusion/models/flux

src/maxdiffusion/models/flux/util.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,20 @@ def prepare_text_ids(batch_size, seq_len):
302302
# -----------------------------------------------------------------------------
303303

304304

305-
def cast_dict_to_bfloat16_inplace(d):
305+
def cast_dict_to_bfloat16_inplace(d, device=None):
306306
"""Casts a nested dictionary of JAX/numpy arrays to bfloat16 in-place, freeing memory immediately."""
307307
import gc
308308
import jax.numpy as jnp
309+
import jax
309310

310311
for k, v in list(d.items()):
311312
if isinstance(v, dict):
312-
cast_dict_to_bfloat16_inplace(v)
313+
cast_dict_to_bfloat16_inplace(v, device=device)
313314
elif hasattr(v, "astype"):
314-
d[k] = jnp.array(v, dtype=jnp.bfloat16)
315+
if device is not None:
316+
d[k] = jax.device_put(jnp.array(v, dtype=jnp.bfloat16), device=device)
317+
else:
318+
d[k] = jnp.array(v, dtype=jnp.bfloat16)
315319
if hasattr(d[k], "block_until_ready"):
316320
d[k].block_until_ready()
317321
del v
@@ -358,8 +362,8 @@ def cvt(tensor, transpose=False):
358362
return jnp.array(tensor.to(torch.float32).cpu().numpy(), dtype=target_dtype)
359363

360364
# Global layers
361-
params["txt_in"]["kernel"] = cvt(pt_state_dict.pop("context_embedder.weight"), transpose=True)
362-
params["img_in"]["kernel"] = cvt(pt_state_dict.pop("x_embedder.weight"), transpose=True)
365+
params["context_embedder"]["kernel"] = cvt(pt_state_dict.pop("context_embedder.weight"), transpose=True)
366+
params["x_embedder"]["kernel"] = cvt(pt_state_dict.pop("x_embedder.weight"), transpose=True)
363367
params["double_stream_modulation_img"]["kernel"] = cvt(
364368
pt_state_dict.pop("double_stream_modulation_img.linear.weight"), transpose=True
365369
)
@@ -372,7 +376,7 @@ def cvt(tensor, transpose=False):
372376
params["proj_out"]["kernel"] = cvt(pt_state_dict.pop("proj_out.weight"), transpose=True)
373377

374378
# norm_out
375-
params["norm_out"]["Dense_0"]["kernel"] = cvt(pt_state_dict.pop("norm_out.linear.weight"), transpose=True)
379+
params["norm_out"]["linear"]["kernel"] = cvt(pt_state_dict.pop("norm_out.linear.weight"), transpose=True)
376380

377381
# time_text_embed (Timestep Embedding)
378382
if "time_guidance_embed.timestep_embedder.linear_1.weight" in pt_state_dict:
@@ -420,10 +424,10 @@ def cvt(tensor, transpose=False):
420424
jax_db["attn"]["encoder_key_norm"]["scale"] = cvt(pt_state_dict.pop(prefix + "attn.norm_added_k.weight"))
421425

422426
# SwiGLU MLPs
423-
jax_db["img_mlp"]["linear_in"]["kernel"] = cvt(pt_state_dict.pop(prefix + "ff.linear_in.weight"), transpose=True)
424-
jax_db["img_mlp"]["linear_out"]["kernel"] = cvt(pt_state_dict.pop(prefix + "ff.linear_out.weight"), transpose=True)
425-
jax_db["txt_mlp"]["linear_in"]["kernel"] = cvt(pt_state_dict.pop(prefix + "ff_context.linear_in.weight"), transpose=True)
426-
jax_db["txt_mlp"]["linear_out"]["kernel"] = cvt(
427+
jax_db["ff"]["linear_in"]["kernel"] = cvt(pt_state_dict.pop(prefix + "ff.linear_in.weight"), transpose=True)
428+
jax_db["ff"]["linear_out"]["kernel"] = cvt(pt_state_dict.pop(prefix + "ff.linear_out.weight"), transpose=True)
429+
jax_db["ff_context"]["linear_in"]["kernel"] = cvt(pt_state_dict.pop(prefix + "ff_context.linear_in.weight"), transpose=True)
430+
jax_db["ff_context"]["linear_out"]["kernel"] = cvt(
427431
pt_state_dict.pop(prefix + "ff_context.linear_out.weight"), transpose=True
428432
)
429433

0 commit comments

Comments
 (0)