@@ -266,7 +266,6 @@ def load_flow_model(name: str, eval_shapes: dict, device: str, hf_download: bool
266266 jax .clear_caches ()
267267 return flax_state_dict
268268
269-
270269# -----------------------------------------------------------------------------
271270# Latent Packing & Unpacking Helpers
272271# -----------------------------------------------------------------------------
@@ -375,20 +374,27 @@ def prepare_text_ids(batch_size, seq_len):
375374# -----------------------------------------------------------------------------
376375
377376
378- def cast_dict_to_bfloat16_inplace (d , device = None ):
379- """Casts a nested dictionary of JAX/numpy arrays to bfloat16 in-place, freeing memory immediately."""
377+ def cast_dict_to_bfloat16_inplace (d , device = None , exclude_keywords = None , parent_key = "" ):
378+ """Casts a nested dictionary of JAX/numpy arrays to bfloat16 in-place, freeing memory immediately.
379+
380+ Optionally keeps parameters matching any keyword in `exclude_keywords` in float32 for numerical stability.
381+ """
380382 import gc
381383 import jax .numpy as jnp
382384 import jax
383385
384386 for k , v in list (d .items ()):
387+ current_key = f"{ parent_key } .{ k } " if parent_key else str (k )
385388 if isinstance (v , dict ):
386- cast_dict_to_bfloat16_inplace (v , device = device )
389+ cast_dict_to_bfloat16_inplace (v , device = device , exclude_keywords = exclude_keywords , parent_key = current_key )
387390 elif hasattr (v , "astype" ):
391+ is_excluded = exclude_keywords and any (kw .lower () in current_key .lower () for kw in exclude_keywords )
392+ target_dtype = jnp .float32 if is_excluded else jnp .bfloat16
393+
388394 if device is not None :
389- d [k ] = jax .device_put (jnp .array (v , dtype = jnp . bfloat16 ), device = device )
395+ d [k ] = jax .device_put (jnp .array (v , dtype = target_dtype ), device = device )
390396 else :
391- d [k ] = jnp .array (v , dtype = jnp . bfloat16 )
397+ d [k ] = jnp .array (v , dtype = target_dtype )
392398 if hasattr (d [k ], "block_until_ready" ):
393399 d [k ].block_until_ready ()
394400 del v
@@ -434,9 +440,7 @@ def convert_and_transpose_tensor(tensor, transpose=False):
434440 return jnp .array (tensor , dtype = target_dtype )
435441
436442 # Global layers
437- params ["context_embedder" ]["kernel" ] = convert_and_transpose_tensor (
438- pt_state_dict .pop ("context_embedder.weight" ), transpose = True
439- )
443+ params ["context_embedder" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop ("context_embedder.weight" ), transpose = True )
440444 params ["x_embedder" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop ("x_embedder.weight" ), transpose = True )
441445 params ["double_stream_modulation_img" ]["kernel" ] = convert_and_transpose_tensor (
442446 pt_state_dict .pop ("double_stream_modulation_img.linear.weight" ), transpose = True
@@ -450,9 +454,7 @@ def convert_and_transpose_tensor(tensor, transpose=False):
450454 params ["proj_out" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop ("proj_out.weight" ), transpose = True )
451455
452456 # norm_out
453- params ["norm_out" ]["linear" ]["kernel" ] = convert_and_transpose_tensor (
454- pt_state_dict .pop ("norm_out.linear.weight" ), transpose = True
455- )
457+ params ["norm_out" ]["linear" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop ("norm_out.linear.weight" ), transpose = True )
456458
457459 # time_text_embed (Timestep Embedding)
458460 if "time_guidance_embed.timestep_embedder.linear_1.weight" in pt_state_dict :
@@ -490,30 +492,18 @@ def convert_and_transpose_tensor(tensor, transpose=False):
490492 jax_db ["attn" ]["e_qkv" ]["kernel" ] = jnp .array (np .concatenate ([add_q , add_k , add_v ], axis = 1 ), dtype = target_dtype )
491493
492494 # Projections out
493- jax_db ["attn" ]["i_proj" ]["kernel" ] = convert_and_transpose_tensor (
494- pt_state_dict .pop (prefix + "attn.to_out.0.weight" ), transpose = True
495- )
496- jax_db ["attn" ]["e_proj" ]["kernel" ] = convert_and_transpose_tensor (
497- pt_state_dict .pop (prefix + "attn.to_add_out.weight" ), transpose = True
498- )
495+ jax_db ["attn" ]["i_proj" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "attn.to_out.0.weight" ), transpose = True )
496+ jax_db ["attn" ]["e_proj" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "attn.to_add_out.weight" ), transpose = True )
499497
500498 # Norm scales
501499 jax_db ["attn" ]["query_norm" ]["scale" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "attn.norm_q.weight" ))
502500 jax_db ["attn" ]["key_norm" ]["scale" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "attn.norm_k.weight" ))
503- jax_db ["attn" ]["encoder_query_norm" ]["scale" ] = convert_and_transpose_tensor (
504- pt_state_dict .pop (prefix + "attn.norm_added_q.weight" )
505- )
506- jax_db ["attn" ]["encoder_key_norm" ]["scale" ] = convert_and_transpose_tensor (
507- pt_state_dict .pop (prefix + "attn.norm_added_k.weight" )
508- )
501+ jax_db ["attn" ]["encoder_query_norm" ]["scale" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "attn.norm_added_q.weight" ))
502+ jax_db ["attn" ]["encoder_key_norm" ]["scale" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "attn.norm_added_k.weight" ))
509503
510504 # SwiGLU MLPs
511- jax_db ["ff" ]["linear_in" ]["kernel" ] = convert_and_transpose_tensor (
512- pt_state_dict .pop (prefix + "ff.linear_in.weight" ), transpose = True
513- )
514- jax_db ["ff" ]["linear_out" ]["kernel" ] = convert_and_transpose_tensor (
515- pt_state_dict .pop (prefix + "ff.linear_out.weight" ), transpose = True
516- )
505+ jax_db ["ff" ]["linear_in" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "ff.linear_in.weight" ), transpose = True )
506+ jax_db ["ff" ]["linear_out" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop (prefix + "ff.linear_out.weight" ), transpose = True )
517507 jax_db ["ff_context" ]["linear_in" ]["kernel" ] = convert_and_transpose_tensor (
518508 pt_state_dict .pop (prefix + "ff_context.linear_in.weight" ), transpose = True
519509 )
@@ -528,12 +518,8 @@ def convert_and_transpose_tensor(tensor, transpose=False):
528518 s_prefix = f"single_transformer_blocks.{ block_idx } ."
529519
530520 # Joint projections
531- jax_sb ["linear1" ]["kernel" ] = convert_and_transpose_tensor (
532- pt_state_dict .pop (s_prefix + "attn.to_qkv_mlp_proj.weight" ), transpose = True
533- )
534- jax_sb ["linear2" ]["kernel" ] = convert_and_transpose_tensor (
535- pt_state_dict .pop (s_prefix + "attn.to_out.weight" ), transpose = True
536- )
521+ jax_sb ["linear1" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop (s_prefix + "attn.to_qkv_mlp_proj.weight" ), transpose = True )
522+ jax_sb ["linear2" ]["kernel" ] = convert_and_transpose_tensor (pt_state_dict .pop (s_prefix + "attn.to_out.weight" ), transpose = True )
537523
538524 # Norm scales
539525 jax_sb ["attn" ]["query_norm" ]["scale" ] = convert_and_transpose_tensor (pt_state_dict .pop (s_prefix + "attn.norm_q.weight" ))
@@ -567,15 +553,11 @@ def get_pytorch_weight_tensor(key):
567553 max_logging .log ("Mapping VAE decoder weights to JAX parameters..." )
568554
569555 # post_quant_conv
570- jax_params ["post_quant_conv" ]["kernel" ] = jnp .array (
571- get_pytorch_weight_tensor ("post_quant_conv.weight" ).transpose (2 , 3 , 1 , 0 )
572- )
556+ jax_params ["post_quant_conv" ]["kernel" ] = jnp .array (get_pytorch_weight_tensor ("post_quant_conv.weight" ).transpose (2 , 3 , 1 , 0 ))
573557 jax_params ["post_quant_conv" ]["bias" ] = jnp .array (get_pytorch_weight_tensor ("post_quant_conv.bias" ))
574558
575559 # decoder.conv_in
576- jax_params ["decoder" ]["conv_in" ]["kernel" ] = jnp .array (
577- get_pytorch_weight_tensor ("decoder.conv_in.weight" ).transpose (2 , 3 , 1 , 0 )
578- )
560+ jax_params ["decoder" ]["conv_in" ]["kernel" ] = jnp .array (get_pytorch_weight_tensor ("decoder.conv_in.weight" ).transpose (2 , 3 , 1 , 0 ))
579561 jax_params ["decoder" ]["conv_in" ]["bias" ] = jnp .array (get_pytorch_weight_tensor ("decoder.conv_in.bias" ))
580562
581563 # decoder.mid_block
@@ -639,17 +621,13 @@ def get_pytorch_weight_tensor(key):
639621 upsampler_jax = up_block_jax ["upsamplers_0" ]
640622 upsampler_pt = f"{ up_block_pt } .upsamplers.0"
641623
642- upsampler_jax ["conv" ]["kernel" ] = jnp .array (
643- get_pytorch_weight_tensor (f"{ upsampler_pt } .conv.weight" ).transpose (2 , 3 , 1 , 0 )
644- )
624+ upsampler_jax ["conv" ]["kernel" ] = jnp .array (get_pytorch_weight_tensor (f"{ upsampler_pt } .conv.weight" ).transpose (2 , 3 , 1 , 0 ))
645625 upsampler_jax ["conv" ]["bias" ] = jnp .array (get_pytorch_weight_tensor (f"{ upsampler_pt } .conv.bias" ))
646626
647627 # decoder.conv_norm_out & conv_out
648628 jax_params ["decoder" ]["conv_norm_out" ]["scale" ] = jnp .array (get_pytorch_weight_tensor ("decoder.conv_norm_out.weight" ))
649629 jax_params ["decoder" ]["conv_norm_out" ]["bias" ] = jnp .array (get_pytorch_weight_tensor ("decoder.conv_norm_out.bias" ))
650- jax_params ["decoder" ]["conv_out" ]["kernel" ] = jnp .array (
651- get_pytorch_weight_tensor ("decoder.conv_out.weight" ).transpose (2 , 3 , 1 , 0 )
652- )
630+ jax_params ["decoder" ]["conv_out" ]["kernel" ] = jnp .array (get_pytorch_weight_tensor ("decoder.conv_out.weight" ).transpose (2 , 3 , 1 , 0 ))
653631 jax_params ["decoder" ]["conv_out" ]["bias" ] = jnp .array (get_pytorch_weight_tensor ("decoder.conv_out.bias" ))
654632
655633 jax_params = jax .tree_util .tree_map (
0 commit comments