Download docs/understand/data_v10_tools/vae_bench.py from teawhite/maliang: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/teawhite/maliang/resolve/main/docs/understand/data_v10_tools/vae_bench.py
- Command line
-
hf download hf://teawhite/maliang/docs/understand/data_v10_tools/vae_bench.py
-
curl -L -o vae_bench.py https://huggingface.co/teawhite/maliang/resolve/main/docs/understand/data_v10_tools/vae_bench.py
2.64 kB
| """Wan2.2 VAE (WanVideoVAE38) encode benchmark on a real 81x480x832 clip, fp32 (as gc25d infer.encode_video) and bf16.""" | |
| import sys, os, io, json, time, numpy as np, torch, cv2 | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| sys.path.insert(0, '/opt/dlami/nvme/zhiyangdeng/maliang/ref/gc25d-live-demo/code') | |
| from tario import RunTars | |
| from gc25d.geometry import resample_rgb | |
| from diffsynth.pipelines.wan_video import ModelConfig, WanVideoPipeline | |
| run = sys.argv[1]; ci = int(sys.argv[2]); dev = 'cuda:0' | |
| R = RunTars(run); s = json.load(open(os.path.join(run, 'index', 'clips_81.json')))[ci]['start'] | |
| frames = np.stack([resample_rgb(cv2.cvtColor(cv2.imdecode(np.frombuffer(R.get(s + i, 'jpg'), np.uint8), cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)) for i in range(81)]) | |
| t0 = time.time() | |
| pipe = WanVideoPipeline.from_pretrained(torch_dtype=torch.bfloat16, device=dev, model_configs=[ModelConfig(path='/opt/dlami/nvme/zhiyangdeng/maliang/models/Wan2.2-TI2V-5B/Wan2.2_VAE.pth')], tokenizer_config=None, redirect_common_files=False) | |
| print('vae load %.1fs' % (time.time() - t0), type(pipe.vae).__name__, 'tf32 conv', torch.backends.cudnn.allow_tf32, 'tf32 matmul', torch.backends.cuda.matmul.allow_tf32, flush=True) | |
| def enc(fp32, x_u8): | |
| dtype = torch.float32 if fp32 else torch.bfloat16 | |
| x = torch.from_numpy(x_u8).permute(3, 0, 1, 2).to(dtype).div_(127.5).sub_(1.0) | |
| with torch.no_grad(): | |
| z = pipe.vae.encode([x], device=dev, tiled=False) | |
| return z.to(torch.bfloat16) | |
| for mode in ('fp32', 'bf16'): | |
| if mode == 'fp32': pipe.vae.model.float() | |
| else: pipe.vae.model.to(torch.bfloat16) | |
| torch.cuda.synchronize(); torch.cuda.reset_peak_memory_stats() | |
| z = enc(mode == 'fp32', frames); torch.cuda.synchronize() # warmup | |
| ts = [] | |
| for _ in range(3): | |
| t = time.time(); z = enc(mode == 'fp32', frames); torch.cuda.synchronize(); ts.append(time.time() - t) | |
| print(f'{mode}: z {tuple(z.shape)} {z.dtype}; encode s/clip {np.round(ts, 3).tolist()} -> {1/np.median(ts):.3f} clips/s/GPU; peak mem {torch.cuda.max_memory_allocated()/2**30:.1f} GiB', flush=True) | |
| if mode == 'fp32': z32 = z.float().cpu() | |
| else: print('bf16 vs fp32 latent: rel L2 err %.4f, max abs %.3f' % (((z.float().cpu() - z32).norm() / z32.norm()).item(), (z.float().cpu() - z32).abs().max().item())) | |
| print('latent stats fp32: mean %.3f std %.3f' % (z32.mean().item(), z32.std().item())) | |
| buf = io.BytesIO(); np.save(buf, z32.to(torch.bfloat16).view(torch.int16).numpy()); raw = buf.tell() | |
| import zlib; comp = len(zlib.compress(buf.getvalue(), 6)); print(f'latent bf16 bytes {raw/1e6:.3f} MB raw, zlib6 {comp/1e6:.3f} MB') | |