Spaces:
Running
on
A100
Running
on
A100
File size: 1,805 Bytes
43c5292 15ad850 43c5292 879b56c 43c5292 |
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 60 61 62 |
import torch
from pathlib import Path
from hyimage.common.constants import PRECISION_TO_TYPE
from .hunyuanimage_vae import HunyuanVAE2D
from .refiner_vae import AutoencoderKLConv3D
def load_vae(device, vae_path: str = None, vae_precision: str = None):
config = HunyuanVAE2D.load_config(vae_path)
vae = HunyuanVAE2D.from_config(config)
ckpt_path = Path(vae_path) / "pytorch_model.ckpt"
if not ckpt_path.exists():
ckpt_path = Path(vae_path) / "pytorch_model.pt"
ckpt = torch.load(ckpt_path, map_location='cpu')
if "state_dict" in ckpt:
ckpt = ckpt["state_dict"]
vae_ckpt = {}
for k, v in ckpt.items():
if k.startswith("vae."):
vae_ckpt[k.replace("vae.", "")] = v
vae.load_state_dict(vae_ckpt)
if vae_precision is not None:
vae = vae.to(dtype=PRECISION_TO_TYPE[vae_precision])
vae.requires_grad_(False)
if device is not None:
vae = vae.to(device)
vae.eval()
return vae
def load_refiner_vae(device, vae_path: str = None, vae_precision: str = "fp16"):
config = AutoencoderKLConv3D.load_config(vae_path)
vae = AutoencoderKLConv3D.from_config(config)
ckpt_path = Path(vae_path) / "pytorch_model.ckpt"
if not ckpt_path.exists():
ckpt_path = Path(vae_path) / "pytorch_model.pt"
ckpt = torch.load(ckpt_path, map_location='cpu')
if "state_dict" in ckpt:
ckpt = ckpt["state_dict"]
vae_ckpt = {}
for k, v in ckpt.items():
if k.startswith("vae."):
vae_ckpt[k.replace("vae.", "")] = v
vae.load_state_dict(vae_ckpt)
if vae_precision is not None:
vae = vae.to(dtype=PRECISION_TO_TYPE[vae_precision])
vae.requires_grad_(False)
if device is not None:
vae = vae.to(device)
vae.eval()
return vae |