Main function to convert CogView4 checkpoints to Diffusers format. Args: args (argparse.Namespace): Parsed command-line arguments.
(args)
| 265 | |
| 266 | |
| 267 | def main(args): |
| 268 | """ |
| 269 | Main function to convert CogView4 checkpoints to Diffusers format. |
| 270 | |
| 271 | Args: |
| 272 | args (argparse.Namespace): Parsed command-line arguments. |
| 273 | """ |
| 274 | # Determine the desired data type |
| 275 | if args.dtype == "fp16": |
| 276 | dtype = torch.float16 |
| 277 | elif args.dtype == "bf16": |
| 278 | dtype = torch.bfloat16 |
| 279 | elif args.dtype == "fp32": |
| 280 | dtype = torch.float32 |
| 281 | else: |
| 282 | raise ValueError(f"Unsupported dtype: {args.dtype}") |
| 283 | |
| 284 | transformer = None |
| 285 | vae = None |
| 286 | |
| 287 | # Convert Transformer checkpoint if provided |
| 288 | if args.transformer_checkpoint_path is not None: |
| 289 | converted_transformer_state_dict = convert_megatron_transformer_checkpoint_to_diffusers( |
| 290 | ckpt_path=args.transformer_checkpoint_path, |
| 291 | num_layers=args.num_layers, |
| 292 | num_heads=args.num_heads, |
| 293 | hidden_size=args.hidden_size, |
| 294 | ) |
| 295 | transformer = CogView4Transformer2DModel( |
| 296 | patch_size=2, |
| 297 | in_channels=32 if args.control else 16, |
| 298 | num_layers=args.num_layers, |
| 299 | attention_head_dim=args.attention_head_dim, |
| 300 | num_attention_heads=args.num_heads, |
| 301 | out_channels=16, |
| 302 | text_embed_dim=args.hidden_size, |
| 303 | time_embed_dim=args.time_embed_dim, |
| 304 | condition_dim=args.condition_dim, |
| 305 | pos_embed_max_size=args.pos_embed_max_size, |
| 306 | ) |
| 307 | |
| 308 | transformer.load_state_dict(converted_transformer_state_dict, strict=True) |
| 309 | |
| 310 | # Convert to the specified dtype |
| 311 | if dtype is not None: |
| 312 | transformer = transformer.to(dtype=dtype) |
| 313 | |
| 314 | # Convert VAE checkpoint if provided |
| 315 | if args.vae_checkpoint_path is not None: |
| 316 | vae_config = { |
| 317 | "in_channels": 3, |
| 318 | "out_channels": 3, |
| 319 | "down_block_types": ("DownEncoderBlock2D",) * 4, |
| 320 | "up_block_types": ("UpDecoderBlock2D",) * 4, |
| 321 | "block_out_channels": (128, 512, 1024, 1024), |
| 322 | "layers_per_block": 3, |
| 323 | "act_fn": "silu", |
| 324 | "latent_channels": 16, |
no test coverage detected
searching dependent graphs…