| """ |
| Debug script: print shapes through VAE encoder for different temporal inputs. |
| |
| Usage: |
| python debug_vae2.py --vae_path /path/to/Wan2.2_VAE.pth --height 384 --width 640 |
| """ |
|
|
| import sys |
| import os |
| import argparse |
| import torch |
|
|
| print("[0] Script started", flush=True) |
|
|
| _REPO_ROOT = os.environ.get("DIFFSYNTH_ROOT", ".") |
| if _REPO_ROOT not in sys.path: |
| sys.path.insert(0, _REPO_ROOT) |
|
|
| print("[1] Importing diffsynth modules...", flush=True) |
| from diffsynth.models.wan_video_vae import ( |
| WanVideoVAE, WanVideoVAE38, |
| Down_ResidualBlock, AvgDown3D, Resample, Resample38, |
| CausalConv3d, ResidualBlock, Encoder3d, Encoder3d_38, |
| ) |
| print("[2] Import done", flush=True) |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--vae_path", type=str, required=True) |
| parser.add_argument("--height", type=int, default=384) |
| parser.add_argument("--width", type=int, default=640) |
| parser.add_argument("--device", type=str, default="cuda") |
| parser.add_argument("--test_frames", type=str, default="1,4,5,8,9,13,17,21") |
| args = parser.parse_args() |
|
|
| temporal_lengths = [int(x) for x in args.test_frames.split(",")] |
| print(f"[3] Will test frames: {temporal_lengths}", flush=True) |
|
|
| print(f"[4] Loading checkpoint from {args.vae_path} ...", flush=True) |
| state_dict = torch.load(args.vae_path, map_location="cpu", weights_only=False) |
| print(f"[5] Checkpoint loaded, type={type(state_dict)}", flush=True) |
|
|
| if "model_state" in state_dict: |
| state_dict = state_dict["model_state"] |
| print("[5.1] Extracted model_state", flush=True) |
|
|
| keys = list(state_dict.keys()) |
| print(f"[6] Total keys: {len(keys)}", flush=True) |
| print(f" First 5 keys: {keys[:5]}", flush=True) |
|
|
| has_model_prefix = any(k.startswith("model.") for k in keys) |
| is_vae38 = any("avg_shortcut" in k for k in keys) |
| print(f" has_model_prefix={has_model_prefix}, is_vae38={is_vae38}", flush=True) |
|
|
| if is_vae38 or any("avg_shortcut" in k for k in keys): |
| print("[7] Creating WanVideoVAE38...", flush=True) |
| vae = WanVideoVAE38() |
| else: |
| print("[7] Creating WanVideoVAE...", flush=True) |
| vae = WanVideoVAE() |
|
|
| if not has_model_prefix: |
| print("[8] Adding 'model.' prefix to keys...", flush=True) |
| state_dict = {f"model.{k}": v for k, v in state_dict.items()} |
|
|
| print("[9] Loading state dict...", flush=True) |
| missing, unexpected = vae.load_state_dict(state_dict, strict=False) |
| print(f" Missing: {len(missing)}, Unexpected: {len(unexpected)}", flush=True) |
| if missing: |
| for k in missing[:10]: |
| print(f" missing: {k}", flush=True) |
| if unexpected: |
| for k in unexpected[:10]: |
| print(f" unexpected: {k}", flush=True) |
|
|
| print(f"[10] Moving VAE to {args.device}...", flush=True) |
| vae = vae.to(args.device).eval() |
| print("[11] VAE ready", flush=True) |
|
|
| encoder = vae.model.encoder |
| print(f"[12] Encoder type: {type(encoder).__name__}", flush=True) |
|
|
| if hasattr(encoder, 'downsamples'): |
| for i, layer in enumerate(encoder.downsamples): |
| print(f" downsamples[{i}]: {type(layer).__name__}", flush=True) |
| if isinstance(layer, Down_ResidualBlock): |
| avg = layer.avg_shortcut |
| print(f" avg_shortcut: factor_t={avg.factor_t}, factor_s={avg.factor_s}", flush=True) |
| |
| for j, sub in enumerate(layer.downsamples): |
| print(f" downsamples[{j}]: {type(sub).__name__}", flush=True) |
|
|
| for t in temporal_lengths: |
| print(f"\n{'='*50}", flush=True) |
| print(f"Testing t={t}", flush=True) |
| print(f"{'='*50}", flush=True) |
|
|
| video = torch.randn(3, t, args.height, args.width, dtype=torch.float32) |
| print(f" Input video shape: {video.shape}", flush=True) |
|
|
| try: |
| with torch.no_grad(): |
| latent = vae.encode([video], device=args.device) |
| print(f" Output latent shape: {latent.shape}", flush=True) |
| except RuntimeError as e: |
| print(f" FAILED: {e}", flush=True) |
|
|
| print("\n[DONE]", flush=True) |
|
|
|
|
| if __name__ == "__main__": |
| main() |