You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""Wrappers for diffusion model integration"""
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
from packaging import version
|
|
|
|
OPENAIUNETWRAPPER = "sgm.modules.diffusionmodules.wrappers.OpenAIWrapper"
|
|
|
|
|
|
class IdentityWrapper(nn.Module):
|
|
def __init__(self, diffusion_model, compile_model: bool = False):
|
|
super().__init__()
|
|
torch_compile = (
|
|
torch.compile
|
|
if (version.parse(torch.__version__) >= version.parse("2.0.0"))
|
|
and compile_model
|
|
else lambda x: x
|
|
)
|
|
self.diffusion_model = torch_compile(diffusion_model)
|
|
|
|
def forward(self, *args, **kwargs):
|
|
return self.diffusion_model(*args, **kwargs)
|
|
|
|
|
|
class OpenAIWrapper(IdentityWrapper):
|
|
def forward(
|
|
self, x: torch.Tensor, t: torch.Tensor, c: dict, **kwargs
|
|
) -> torch.Tensor:
|
|
x = torch.cat((x, c.get("concat", torch.Tensor([]).type_as(x))), dim=1)
|
|
return self.diffusion_model(
|
|
x,
|
|
timesteps=t,
|
|
context=c.get("crossattn"),
|
|
y=c.get("vector"),
|
|
**kwargs,
|
|
)
|