|
| 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 AutoencoderDC |
| 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 | + |
| 19 | +TRANSFORMER_SPECIAL_KEYS_REMAP = {} |
| 20 | + |
| 21 | +VAE_KEYS_RENAME_DICT = { |
| 22 | + # common |
| 23 | + "norm.": "norm.norm.", |
| 24 | + # encoder |
| 25 | + "encoder.project_in": "encoder.conv_in", |
| 26 | + "encoder.project_out.main.op_list.0": "encoder.conv_out", |
| 27 | + # decoder |
| 28 | + "decoder.project_in.main": "decoder.conv_in", |
| 29 | + "decoder.project_out.op_list.0": "decoder.norm_out.norm", |
| 30 | + "decoder.project_out.op_list.2": "decoder.conv_out", |
| 31 | +} |
| 32 | + |
| 33 | +VAE_SPECIAL_KEYS_REMAP = {} |
| 34 | + |
| 35 | + |
| 36 | +def get_state_dict(saved_dict: Dict[str, Any]) -> Dict[str, Any]: |
| 37 | + state_dict = saved_dict |
| 38 | + if "model" in saved_dict.keys(): |
| 39 | + state_dict = state_dict["model"] |
| 40 | + if "module" in saved_dict.keys(): |
| 41 | + state_dict = state_dict["module"] |
| 42 | + if "state_dict" in saved_dict.keys(): |
| 43 | + state_dict = state_dict["state_dict"] |
| 44 | + return state_dict |
| 45 | + |
| 46 | + |
| 47 | +def update_state_dict_inplace(state_dict: Dict[str, Any], old_key: str, new_key: str) -> Dict[str, Any]: |
| 48 | + state_dict[new_key] = state_dict.pop(old_key) |
| 49 | + |
| 50 | + |
| 51 | +# def convert_transformer( |
| 52 | +# ckpt_path: str, |
| 53 | +# dtype: torch.dtype, |
| 54 | +# ): |
| 55 | +# PREFIX_KEY = "" |
| 56 | + |
| 57 | +# original_state_dict = get_state_dict(load_file(ckpt_path)) |
| 58 | +# transformer = LTXTransformer3DModel().to(dtype=dtype) |
| 59 | + |
| 60 | +# for key in list(original_state_dict.keys()): |
| 61 | +# new_key = key[len(PREFIX_KEY) :] |
| 62 | +# for replace_key, rename_key in TRANSFORMER_KEYS_RENAME_DICT.items(): |
| 63 | +# new_key = new_key.replace(replace_key, rename_key) |
| 64 | +# update_state_dict_inplace(original_state_dict, key, new_key) |
| 65 | + |
| 66 | +# for key in list(original_state_dict.keys()): |
| 67 | +# for special_key, handler_fn_inplace in TRANSFORMER_SPECIAL_KEYS_REMAP.items(): |
| 68 | +# if special_key not in key: |
| 69 | +# continue |
| 70 | +# handler_fn_inplace(key, original_state_dict) |
| 71 | + |
| 72 | +# transformer.load_state_dict(original_state_dict, strict=True) |
| 73 | +# return transformer |
| 74 | + |
| 75 | + |
| 76 | +def convert_vae(ckpt_path: str, dtype: torch.dtype): |
| 77 | + original_state_dict = get_state_dict(load_file(ckpt_path)) |
| 78 | + vae = AutoencoderDC( |
| 79 | + in_channels=3, |
| 80 | + latent_channels=32, |
| 81 | + encoder_width_list=[128, 256, 512, 512, 1024, 1024], |
| 82 | + encoder_depth_list=[2, 2, 2, 3, 3, 3], |
| 83 | + encoder_block_type=["ResBlock", "ResBlock", "ResBlock", "EViTS5_GLU", "EViTS5_GLU", "EViTS5_GLU"], |
| 84 | + encoder_norm="rms2d", |
| 85 | + encoder_act="silu", |
| 86 | + downsample_block_type="Conv", |
| 87 | + decoder_width_list=[128, 256, 512, 512, 1024, 1024], |
| 88 | + decoder_depth_list=[3, 3, 3, 3, 3, 3], |
| 89 | + decoder_block_type=["ResBlock", "ResBlock", "ResBlock", "EViTS5_GLU", "EViTS5_GLU", "EViTS5_GLU"], |
| 90 | + decoder_norm="rms2d", |
| 91 | + decoder_act="silu", |
| 92 | + upsample_block_type="InterpolateConv", |
| 93 | + scaling_factor=0.41407, |
| 94 | + ).to(dtype=dtype) |
| 95 | + |
| 96 | + for key in list(original_state_dict.keys()): |
| 97 | + new_key = key[:] |
| 98 | + for replace_key, rename_key in VAE_KEYS_RENAME_DICT.items(): |
| 99 | + new_key = new_key.replace(replace_key, rename_key) |
| 100 | + update_state_dict_inplace(original_state_dict, key, new_key) |
| 101 | + |
| 102 | + for key in list(original_state_dict.keys()): |
| 103 | + for special_key, handler_fn_inplace in VAE_SPECIAL_KEYS_REMAP.items(): |
| 104 | + if special_key not in key: |
| 105 | + continue |
| 106 | + handler_fn_inplace(key, original_state_dict) |
| 107 | + |
| 108 | + vae.load_state_dict(original_state_dict, strict=True) |
| 109 | + return vae |
| 110 | + |
| 111 | + |
| 112 | +def get_args(): |
| 113 | + parser = argparse.ArgumentParser() |
| 114 | + parser.add_argument( |
| 115 | + "--transformer_ckpt_path", type=str, default=None, help="Path to original transformer checkpoint" |
| 116 | + ) |
| 117 | + parser.add_argument("--vae_ckpt_path", type=str, default=None, help="Path to original vae checkpoint") |
| 118 | + parser.add_argument( |
| 119 | + "--text_encoder_cache_dir", type=str, default=None, help="Path to text encoder cache directory" |
| 120 | + ) |
| 121 | + parser.add_argument( |
| 122 | + "--typecast_text_encoder", |
| 123 | + action="store_true", |
| 124 | + default=False, |
| 125 | + help="Whether or not to apply fp16/bf16 precision to text_encoder", |
| 126 | + ) |
| 127 | + parser.add_argument("--save_pipeline", action="store_true") |
| 128 | + parser.add_argument("--output_path", type=str, required=True, help="Path where converted model should be saved") |
| 129 | + parser.add_argument("--dtype", default="fp32", help="Torch dtype to save the model in.") |
| 130 | + return parser.parse_args() |
| 131 | + |
| 132 | + |
| 133 | +DTYPE_MAPPING = { |
| 134 | + "fp32": torch.float32, |
| 135 | + "fp16": torch.float16, |
| 136 | + "bf16": torch.bfloat16, |
| 137 | +} |
| 138 | + |
| 139 | +VARIANT_MAPPING = { |
| 140 | + "fp32": None, |
| 141 | + "fp16": "fp16", |
| 142 | + "bf16": "bf16", |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + args = get_args() |
| 148 | + |
| 149 | + transformer = None |
| 150 | + dtype = DTYPE_MAPPING[args.dtype] |
| 151 | + variant = VARIANT_MAPPING[args.dtype] |
| 152 | + |
| 153 | + if args.save_pipeline: |
| 154 | + assert args.transformer_ckpt_path is not None and args.vae_ckpt_path is not None |
| 155 | + |
| 156 | + # if args.transformer_ckpt_path is not None: |
| 157 | + # transformer = convert_transformer(args.transformer_ckpt_path, dtype) |
| 158 | + # if not args.save_pipeline: |
| 159 | + # transformer.save_pretrained( |
| 160 | + # args.output_path, safe_serialization=True, max_shard_size="5GB", variant=variant |
| 161 | + # ) |
| 162 | + |
| 163 | + if args.vae_ckpt_path is not None: |
| 164 | + vae = convert_vae(args.vae_ckpt_path, dtype) |
| 165 | + if not args.save_pipeline: |
| 166 | + vae.save_pretrained(args.output_path, safe_serialization=True, max_shard_size="5GB", variant=variant) |
0 commit comments