MCPcopy Index your code
hub / github.com/huggingface/diffusers / convert_transformer

Function convert_transformer

scripts/convert_wan_to_diffusers.py:605–643  ·  view source on GitHub ↗
(model_type: str, stage: str = None)

Source from the content-addressed store, hash-verified

603
604
605def convert_transformer(model_type: str, stage: str = None):
606 config, RENAME_DICT, SPECIAL_KEYS_REMAP = get_transformer_config(model_type)
607
608 diffusers_config = config["diffusers_config"]
609 model_id = config["model_id"]
610 model_dir = pathlib.Path(snapshot_download(model_id, repo_type="model"))
611
612 if stage is not None:
613 model_dir = model_dir / stage
614
615 original_state_dict = load_sharded_safetensors(model_dir)
616
617 with init_empty_weights():
618 if "Animate" in model_type:
619 transformer = WanAnimateTransformer3DModel.from_config(diffusers_config)
620 elif "VACE" in model_type:
621 transformer = WanVACETransformer3DModel.from_config(diffusers_config)
622 else:
623 transformer = WanTransformer3DModel.from_config(diffusers_config)
624
625 for key in list(original_state_dict.keys()):
626 new_key = key[:]
627 for replace_key, rename_key in RENAME_DICT.items():
628 new_key = new_key.replace(replace_key, rename_key)
629 update_state_dict_(original_state_dict, key, new_key)
630
631 for key in list(original_state_dict.keys()):
632 for special_key, handler_fn_inplace in SPECIAL_KEYS_REMAP.items():
633 if special_key not in key:
634 continue
635 handler_fn_inplace(key, original_state_dict)
636
637 # Load state dict into the meta model, which will materialize the tensors
638 transformer.load_state_dict(original_state_dict, strict=True, assign=True)
639
640 # Move to CPU to ensure all tensors are materialized
641 transformer = transformer.to("cpu")
642
643 return transformer
644
645
646def convert_vae():

Callers 1

Calls 6

get_transformer_configFunction · 0.70
load_sharded_safetensorsFunction · 0.70
update_state_dict_Function · 0.70
from_configMethod · 0.45
load_state_dictMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…