(args)
| 164 | |
| 165 | |
| 166 | def main(args): |
| 167 | if args.dtype == "fp16": |
| 168 | dtype = torch.float16 |
| 169 | elif args.dtype == "bf16": |
| 170 | dtype = torch.bfloat16 |
| 171 | elif args.dtype == "fp32": |
| 172 | dtype = torch.float32 |
| 173 | else: |
| 174 | raise ValueError(f"Unsupported dtype: {args.dtype}") |
| 175 | |
| 176 | transformer = None |
| 177 | vae = None |
| 178 | |
| 179 | if args.transformer_checkpoint_path is not None: |
| 180 | converted_transformer_state_dict = convert_cogview4_transformer_checkpoint_to_diffusers( |
| 181 | args.transformer_checkpoint_path |
| 182 | ) |
| 183 | transformer = CogView4Transformer2DModel( |
| 184 | patch_size=2, |
| 185 | in_channels=16, |
| 186 | num_layers=28, |
| 187 | attention_head_dim=128, |
| 188 | num_attention_heads=32, |
| 189 | out_channels=16, |
| 190 | text_embed_dim=4096, |
| 191 | time_embed_dim=512, |
| 192 | condition_dim=256, |
| 193 | pos_embed_max_size=128, |
| 194 | ) |
| 195 | transformer.load_state_dict(converted_transformer_state_dict, strict=True) |
| 196 | if dtype is not None: |
| 197 | # Original checkpoint data type will be preserved |
| 198 | transformer = transformer.to(dtype=dtype) |
| 199 | |
| 200 | if args.vae_checkpoint_path is not None: |
| 201 | vae_config = { |
| 202 | "in_channels": 3, |
| 203 | "out_channels": 3, |
| 204 | "down_block_types": ("DownEncoderBlock2D",) * 4, |
| 205 | "up_block_types": ("UpDecoderBlock2D",) * 4, |
| 206 | "block_out_channels": (128, 512, 1024, 1024), |
| 207 | "layers_per_block": 3, |
| 208 | "act_fn": "silu", |
| 209 | "latent_channels": 16, |
| 210 | "norm_num_groups": 32, |
| 211 | "sample_size": 1024, |
| 212 | "scaling_factor": 1.0, |
| 213 | "shift_factor": 0.0, |
| 214 | "force_upcast": True, |
| 215 | "use_quant_conv": False, |
| 216 | "use_post_quant_conv": False, |
| 217 | "mid_block_add_attention": False, |
| 218 | } |
| 219 | converted_vae_state_dict = convert_cogview4_vae_checkpoint_to_diffusers(args.vae_checkpoint_path, vae_config) |
| 220 | vae = AutoencoderKL(**vae_config) |
| 221 | vae.load_state_dict(converted_vae_state_dict, strict=True) |
| 222 | if dtype is not None: |
| 223 | vae = vae.to(dtype=dtype) |
no test coverage detected
searching dependent graphs…