File size: 3,020 Bytes
42cd39c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | """Reusable actual FP32 VAE encoding entry; Root runs only on its leased GPU."""
import argparse
import importlib.util
import json
from pathlib import Path
import sys
import numpy as np
from float_fields import load_video,normalize_for_vae
def load_official_vae(package,weight_file,device='cuda'):
import torch
torch.backends.cuda.matmul.allow_tf32=False
torch.backends.cudnn.allow_tf32=False
torch.backends.cudnn.benchmark=False
torch.backends.cudnn.deterministic=True
torch.set_float32_matmul_precision('highest')
path=Path(package)/'code/LumiLens/diffsynth/models/wan_video_vae.py'
spec=importlib.util.spec_from_file_location('official_vae38_float_entry',path)
m=importlib.util.module_from_spec(spec);sys.modules[spec.name]=m;spec.loader.exec_module(m)
vae=m.WanVideoVAE38()
state=torch.load(weight_file,map_location='cpu',weights_only=True,mmap=True)
vae.load_state_dict(vae.state_dict_converter().from_civitai(state),strict=True)
del state
vae=vae.to(device=device,dtype=torch.float32).eval().requires_grad_(False)
assert all(x.dtype==torch.float32 for x in vae.parameters())
return vae
def encode_array_fp32(vae,array,device='cuda'):
import torch
assert all(x.dtype==torch.float32 for x in vae.parameters()),'Reject premature reduced-precision VAE'
assert array.dtype==np.float32 and array.ndim==4 and array.shape[-1]==3
assert (len(array)-1)%4==0
torch.backends.cuda.matmul.allow_tf32=False
torch.backends.cudnn.allow_tf32=False
v=torch.from_numpy(normalize_for_vae(array)).permute(3,0,1,2)[None].to(device,dtype=torch.float32)
with torch.inference_mode(),torch.autocast(device_type='cuda',enabled=False):
z=vae.encode(v,device=device,tiled=False).cpu()
assert z.dtype==torch.float32 and torch.isfinite(z).all()
assert z.shape[1:]==(48,(len(array)-1)//4+1,array.shape[1]//16,array.shape[2]//16)
return z
def main():
p=argparse.ArgumentParser();p.add_argument('--task',required=True);p.add_argument('--vae-file',required=True);p.add_argument('--out',required=True);p.add_argument('--package',default='/mnt/atlas/fangrui/LUMILENS_QINGHAI_TRAINING_20261010');a=p.parse_args()
import torch
assert torch.cuda.is_available(),'Root-owned GPU stage required'
out=Path(a.out);assert not out.exists();out.mkdir(parents=True)
task=json.loads(Path(a.task).read_text());vae=load_official_vae(a.package,a.vae_file)
for field in ('color','intensity'):
array=load_video(task['float_fields'],field)
z=encode_array_fp32(vae,array)
torch.save(z,out/(field+'_fp32_full49.pt'))
(out/'receipt.json').write_text(json.dumps({'schema':'lampedit.fp32_condition_encoding_entry.v001','sample_id':task['sample_id'],'frame_count':49,'latent_dtype':'float32','no_uint8_or_BF16_light_path':True,'ready_for_training':False,'scope':'two physical light conditions only; source/target/joint/text/adapter acceptance separate'},indent=2)+'\n')
if __name__=='__main__':main()
|