Files
hao-ai-lab-FastVideo/opensora/models/ae/imagebase/vae/vae.py
T
LinB203 5a80da7ec1 add vae and fix bug in dit
Former-commit-id: 2dd4254c22ce671d5cd5e7d22a2386b7660fbdc4
2024-03-06 19:32:20 +08:00

32 lines
999 B
Python

from einops import rearrange
from torch import nn
from diffusers.models import AutoencoderKL
class HFVAEWrapper(nn.Module):
def __init__(self, hfvae='mse'):
super(HFVAEWrapper, self).__init__()
self.vae = AutoencoderKL.from_pretrained(hfvae, cache_dir='cache_dir')
def encode(self, x): # b c h w
t = 0
if x.ndim == 5:
b, c, t, h, w = x.shape
x = rearrange(x, 'b c t h w -> (b t) c h w').contiguous()
x = self.vae.encode(x).latent_dist.sample().mul_(0.18215)
if t != 0:
x = rearrange(x, '(b t) c h w -> b t c h w', t=t).contiguous()
return x
def decode(self, x):
x = self.vae.decode(x / 0.18215).sample
return x
class SDVAEWrapper(nn.Module):
def __init__(self):
super(SDVAEWrapper, self).__init__()
raise NotImplementedError
def encode(self, x): # b c h w
raise NotImplementedError
def decode(self, x):
raise NotImplementedError