121 lines
4.8 KiB
Python
121 lines
4.8 KiB
Python
# Copyright 2024 MIT Han Lab
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
from typing import Callable, Optional
|
|
|
|
import diffusers
|
|
import torch
|
|
from huggingface_hub import PyTorchModelHubMixin
|
|
from torch import nn
|
|
|
|
from ..efficientvit.models.efficientvit.dc_ae import (
|
|
DCAE,
|
|
DCAEConfig,
|
|
dc_ae_f32c32,
|
|
dc_ae_f64c128,
|
|
dc_ae_f128c512,
|
|
dc_vae_f32,
|
|
)
|
|
from ..efficientvit.models.efficientvit.dc_ae_with_temporal import (
|
|
DCAEWithTemporal,
|
|
DCAEWithTemporalConfig,
|
|
st_dc_ae_f32t4c32_chunked_causal,
|
|
)
|
|
|
|
__all__ = ["create_dc_ae_model_cfg", "DCAE_HF", "AutoencoderKL"]
|
|
|
|
|
|
REGISTERED_DCAE_MODEL: dict[str, tuple[Callable, Optional[str]]] = {
|
|
"dc-ae-f32c32-in-1.0": (dc_ae_f32c32, None),
|
|
"dc-ae-f64c128-in-1.0": (dc_ae_f64c128, None),
|
|
"dc-ae-f128c512-in-1.0": (dc_ae_f128c512, None),
|
|
#################################################################################################
|
|
"dc-ae-f32c32-mix-1.0": (dc_ae_f32c32, None),
|
|
"dc-ae-f64c128-mix-1.0": (dc_ae_f64c128, None),
|
|
"dc-ae-f128c512-mix-1.0": (dc_ae_f128c512, None),
|
|
#################################################################################################
|
|
"dc-ae-f32c32-sana-1.0": (dc_ae_f32c32, None),
|
|
"dc-ae-f32c32-sana-1.1": (dc_ae_f32c32, None),
|
|
"dc-ae-lite-f32c32-sana-1.1": (dc_ae_f32c32, None),
|
|
"dc-vae-f32t4c128": (dc_vae_f32, None),
|
|
"dc-vae-f32t1c128": (dc_vae_f32, None),
|
|
"dc-vae-f32t4c128-nospatialtiling": (dc_vae_f32, None),
|
|
## st-dc-ae
|
|
"st-dc-ae-f32t4c32": (st_dc_ae_f32t4c32_chunked_causal, None),
|
|
"st-dc-ae-f32t4c32-chunk40": (st_dc_ae_f32t4c32_chunked_causal, None),
|
|
"st-dc-ae-f32t4c32-chunk40-ivj": (st_dc_ae_f32t4c32_chunked_causal, None),
|
|
}
|
|
|
|
|
|
def create_dc_ae_model_cfg(name: str, pretrained_path: Optional[str] = None) -> DCAEConfig:
|
|
assert name in REGISTERED_DCAE_MODEL, f"{name} is not supported"
|
|
dc_ae_cls, default_pt_path = REGISTERED_DCAE_MODEL[name]
|
|
pretrained_path = default_pt_path if pretrained_path is None else pretrained_path
|
|
model_cfg = dc_ae_cls(name, pretrained_path)
|
|
return model_cfg
|
|
|
|
|
|
class DCAE_HF(DCAE, PyTorchModelHubMixin):
|
|
def __init__(self, model_name: str):
|
|
cfg = create_dc_ae_model_cfg(model_name)
|
|
DCAE.__init__(self, cfg)
|
|
|
|
|
|
class DCAEWithTemporal_HF(DCAEWithTemporal, PyTorchModelHubMixin):
|
|
def __init__(self, model_name: str):
|
|
cfg = create_dc_ae_model_cfg(model_name)
|
|
DCAEWithTemporal.__init__(self, cfg)
|
|
|
|
@classmethod
|
|
def from_pretrained(cls, pretrained_model_name_or_path: str, **kwargs):
|
|
if pretrained_model_name_or_path.endswith(".pt"):
|
|
model_name = kwargs.get("model_name", "st-dc-ae-f32t4c32")
|
|
model = cls(model_name)
|
|
state_dict = torch.load(pretrained_model_name_or_path, map_location="cpu")["model_state_dict"]
|
|
model.load_state_dict(state_dict, strict=True)
|
|
return model
|
|
else:
|
|
super().from_pretrained(pretrained_model_name_or_path, **kwargs)
|
|
|
|
|
|
class AutoencoderKL(nn.Module):
|
|
def __init__(self, model_name: str):
|
|
super().__init__()
|
|
self.model_name = model_name
|
|
if self.model_name in ["stabilityai/sd-vae-ft-ema"]:
|
|
self.model = diffusers.models.AutoencoderKL.from_pretrained(self.model_name)
|
|
self.spatial_compression_ratio = 8
|
|
elif self.model_name == "flux-vae":
|
|
from diffusers import FluxPipeline
|
|
|
|
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16)
|
|
self.model = diffusers.models.AutoencoderKL.from_pretrained(pipe.vae.config._name_or_path)
|
|
self.spatial_compression_ratio = 8
|
|
else:
|
|
raise ValueError(f"{self.model_name} is not supported for AutoencoderKL")
|
|
|
|
def encode(self, x: torch.Tensor) -> torch.Tensor:
|
|
if self.model_name in ["stabilityai/sd-vae-ft-ema", "flux-vae"]:
|
|
return self.model.encode(x).latent_dist.sample()
|
|
else:
|
|
raise ValueError(f"{self.model_name} is not supported for AutoencoderKL")
|
|
|
|
def decode(self, latent: torch.Tensor) -> torch.Tensor:
|
|
if self.model_name in ["stabilityai/sd-vae-ft-ema", "flux-vae"]:
|
|
return self.model.decode(latent).sample
|
|
else:
|
|
raise ValueError(f"{self.model_name} is not supported for AutoencoderKL")
|