|
| 1 | +import argparse |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +import torch |
| 5 | +from safetensors.torch import load_file |
| 6 | +from transformers import T5EncoderModel, T5Tokenizer |
| 7 | + |
| 8 | +from diffusers import AutoencoderKLLTXVideo, FlowMatchEulerDiscreteScheduler, LTXPipeline, LTXVideoTransformer3DModel |
| 9 | + |
| 10 | + |
| 11 | +def remove_keys_(key: str, state_dict: Dict[str, Any]): |
| 12 | + state_dict.pop(key) |
| 13 | + |
| 14 | + |
| 15 | +TOKENIZER_MAX_LENGTH = 128 |
| 16 | + |
| 17 | +TRANSFORMER_KEYS_RENAME_DICT = { |
| 18 | + "patchify_proj": "proj_in", |
| 19 | + "adaln_single": "time_embed", |
| 20 | + "q_norm": "norm_q", |
| 21 | + "k_norm": "norm_k", |
| 22 | +} |
| 23 | + |
| 24 | +TRANSFORMER_SPECIAL_KEYS_REMAP = {} |
| 25 | + |
| 26 | +VAE_KEYS_RENAME_DICT = { |
| 27 | + # decoder |
| 28 | + "up_blocks.0": "mid_block", |
| 29 | + "up_blocks.1": "up_blocks.0", |
| 30 | + "up_blocks.2": "up_blocks.1.upsamplers.0", |
| 31 | + "up_blocks.3": "up_blocks.1", |
| 32 | + "up_blocks.4": "up_blocks.2.conv_in", |
| 33 | + "up_blocks.5": "up_blocks.2.upsamplers.0", |
| 34 | + "up_blocks.6": "up_blocks.2", |
| 35 | + "up_blocks.7": "up_blocks.3.conv_in", |
| 36 | + "up_blocks.8": "up_blocks.3.upsamplers.0", |
| 37 | + "up_blocks.9": "up_blocks.3", |
| 38 | + # encoder |
| 39 | + "down_blocks.0": "down_blocks.0", |
| 40 | + "down_blocks.1": "down_blocks.0.downsamplers.0", |
| 41 | + "down_blocks.2": "down_blocks.0.conv_out", |
| 42 | + "down_blocks.3": "down_blocks.1", |
| 43 | + "down_blocks.4": "down_blocks.1.downsamplers.0", |
| 44 | + "down_blocks.5": "down_blocks.1.conv_out", |
| 45 | + "down_blocks.6": "down_blocks.2", |
| 46 | + "down_blocks.7": "down_blocks.2.downsamplers.0", |
| 47 | + "down_blocks.8": "down_blocks.3", |
| 48 | + "down_blocks.9": "mid_block", |
| 49 | + # common |
| 50 | + "conv_shortcut": "conv_shortcut.conv", |
| 51 | + "res_blocks": "resnets", |
| 52 | + "norm3.norm": "norm3", |
| 53 | + "per_channel_statistics.mean-of-means": "latents_mean", |
| 54 | + "per_channel_statistics.std-of-means": "latents_std", |
| 55 | +} |
| 56 | + |
| 57 | +VAE_SPECIAL_KEYS_REMAP = { |
| 58 | + "per_channel_statistics.channel": remove_keys_, |
| 59 | + "per_channel_statistics.mean-of-means": remove_keys_, |
| 60 | + "per_channel_statistics.mean-of-stds": remove_keys_, |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +def get_state_dict(saved_dict: Dict[str, Any]) -> Dict[str, Any]: |
| 65 | + state_dict = saved_dict |
| 66 | + if "model" in saved_dict.keys(): |
| 67 | + state_dict = state_dict["model"] |
| 68 | + if "module" in saved_dict.keys(): |
| 69 | + state_dict = state_dict["module"] |
| 70 | + if "state_dict" in saved_dict.keys(): |
| 71 | + state_dict = state_dict["state_dict"] |
| 72 | + return state_dict |
| 73 | + |
| 74 | + |
| 75 | +def update_state_dict_inplace(state_dict: Dict[str, Any], old_key: str, new_key: str) -> Dict[str, Any]: |
| 76 | + state_dict[new_key] = state_dict.pop(old_key) |
| 77 | + |
| 78 | + |
| 79 | +def convert_transformer( |
| 80 | + ckpt_path: str, |
| 81 | + dtype: torch.dtype, |
| 82 | +): |
| 83 | + PREFIX_KEY = "" |
| 84 | + |
| 85 | + original_state_dict = get_state_dict(load_file(ckpt_path)) |
| 86 | + transformer = LTXVideoTransformer3DModel().to(dtype=dtype) |
| 87 | + |
| 88 | + for key in list(original_state_dict.keys()): |
| 89 | + new_key = key[len(PREFIX_KEY) :] |
| 90 | + for replace_key, rename_key in TRANSFORMER_KEYS_RENAME_DICT.items(): |
| 91 | + new_key = new_key.replace(replace_key, rename_key) |
| 92 | + update_state_dict_inplace(original_state_dict, key, new_key) |
| 93 | + |
| 94 | + for key in list(original_state_dict.keys()): |
| 95 | + for special_key, handler_fn_inplace in TRANSFORMER_SPECIAL_KEYS_REMAP.items(): |
| 96 | + if special_key not in key: |
| 97 | + continue |
| 98 | + handler_fn_inplace(key, original_state_dict) |
| 99 | + |
| 100 | + transformer.load_state_dict(original_state_dict, strict=True) |
| 101 | + return transformer |
| 102 | + |
| 103 | + |
| 104 | +def convert_vae(ckpt_path: str, dtype: torch.dtype): |
| 105 | + original_state_dict = get_state_dict(load_file(ckpt_path)) |
| 106 | + vae = AutoencoderKLLTXVideo().to(dtype=dtype) |
| 107 | + |
| 108 | + for key in list(original_state_dict.keys()): |
| 109 | + new_key = key[:] |
| 110 | + for replace_key, rename_key in VAE_KEYS_RENAME_DICT.items(): |
| 111 | + new_key = new_key.replace(replace_key, rename_key) |
| 112 | + update_state_dict_inplace(original_state_dict, key, new_key) |
| 113 | + |
| 114 | + for key in list(original_state_dict.keys()): |
| 115 | + for special_key, handler_fn_inplace in VAE_SPECIAL_KEYS_REMAP.items(): |
| 116 | + if special_key not in key: |
| 117 | + continue |
| 118 | + handler_fn_inplace(key, original_state_dict) |
| 119 | + |
| 120 | + vae.load_state_dict(original_state_dict, strict=True) |
| 121 | + return vae |
| 122 | + |
| 123 | + |
| 124 | +def get_args(): |
| 125 | + parser = argparse.ArgumentParser() |
| 126 | + parser.add_argument( |
| 127 | + "--transformer_ckpt_path", type=str, default=None, help="Path to original transformer checkpoint" |
| 128 | + ) |
| 129 | + parser.add_argument("--vae_ckpt_path", type=str, default=None, help="Path to original vae checkpoint") |
| 130 | + parser.add_argument( |
| 131 | + "--text_encoder_cache_dir", type=str, default=None, help="Path to text encoder cache directory" |
| 132 | + ) |
| 133 | + parser.add_argument( |
| 134 | + "--typecast_text_encoder", |
| 135 | + action="store_true", |
| 136 | + default=False, |
| 137 | + help="Whether or not to apply fp16/bf16 precision to text_encoder", |
| 138 | + ) |
| 139 | + parser.add_argument("--save_pipeline", action="store_true") |
| 140 | + parser.add_argument("--output_path", type=str, required=True, help="Path where converted model should be saved") |
| 141 | + parser.add_argument("--dtype", default="fp32", help="Torch dtype to save the model in.") |
| 142 | + return parser.parse_args() |
| 143 | + |
| 144 | + |
| 145 | +DTYPE_MAPPING = { |
| 146 | + "fp32": torch.float32, |
| 147 | + "fp16": torch.float16, |
| 148 | + "bf16": torch.bfloat16, |
| 149 | +} |
| 150 | + |
| 151 | +VARIANT_MAPPING = { |
| 152 | + "fp32": None, |
| 153 | + "fp16": "fp16", |
| 154 | + "bf16": "bf16", |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + args = get_args() |
| 160 | + |
| 161 | + transformer = None |
| 162 | + dtype = DTYPE_MAPPING[args.dtype] |
| 163 | + variant = VARIANT_MAPPING[args.dtype] |
| 164 | + |
| 165 | + if args.save_pipeline: |
| 166 | + assert args.transformer_ckpt_path is not None and args.vae_ckpt_path is not None |
| 167 | + |
| 168 | + if args.transformer_ckpt_path is not None: |
| 169 | + transformer: LTXVideoTransformer3DModel = convert_transformer(args.transformer_ckpt_path, dtype) |
| 170 | + if not args.save_pipeline: |
| 171 | + transformer.save_pretrained( |
| 172 | + args.output_path, safe_serialization=True, max_shard_size="5GB", variant=variant |
| 173 | + ) |
| 174 | + |
| 175 | + if args.vae_ckpt_path is not None: |
| 176 | + vae: AutoencoderKLLTXVideo = convert_vae(args.vae_ckpt_path, dtype) |
| 177 | + if not args.save_pipeline: |
| 178 | + vae.save_pretrained(args.output_path, safe_serialization=True, max_shard_size="5GB", variant=variant) |
| 179 | + |
| 180 | + if args.save_pipeline: |
| 181 | + text_encoder_id = "google/t5-v1_1-xxl" |
| 182 | + tokenizer = T5Tokenizer.from_pretrained(text_encoder_id, model_max_length=TOKENIZER_MAX_LENGTH) |
| 183 | + text_encoder = T5EncoderModel.from_pretrained(text_encoder_id, cache_dir=args.text_encoder_cache_dir) |
| 184 | + |
| 185 | + if args.typecast_text_encoder: |
| 186 | + text_encoder = text_encoder.to(dtype=dtype) |
| 187 | + |
| 188 | + # Apparently, the conversion does not work anymore without this :shrug: |
| 189 | + for param in text_encoder.parameters(): |
| 190 | + param.data = param.data.contiguous() |
| 191 | + |
| 192 | + scheduler = FlowMatchEulerDiscreteScheduler( |
| 193 | + use_dynamic_shifting=True, |
| 194 | + base_shift=0.95, |
| 195 | + max_shift=2.05, |
| 196 | + base_image_seq_len=1024, |
| 197 | + max_image_seq_len=4096, |
| 198 | + shift_terminal=0.1, |
| 199 | + ) |
| 200 | + |
| 201 | + pipe = LTXPipeline( |
| 202 | + scheduler=scheduler, |
| 203 | + vae=vae, |
| 204 | + text_encoder=text_encoder, |
| 205 | + tokenizer=tokenizer, |
| 206 | + transformer=transformer, |
| 207 | + ) |
| 208 | + |
| 209 | + pipe.save_pretrained(args.output_path, safe_serialization=True, variant=variant, max_shard_size="5GB") |
0 commit comments