attempt at unipc latent upscaling

i should have taken linear algebra before i dropped out...
This commit is contained in:
cool-bigdogs-tshirt
2023-04-27 11:24:26 -05:00
parent 3b6effe4a9
commit 9a09b2eef3
2 changed files with 76 additions and 1 deletions
@@ -4,6 +4,7 @@ import torch
from .uni_pc import NoiseScheduleVP, model_wrapper, UniPC
from modules import shared, devices
from ldm.modules.diffusionmodules.util import extract_into_tensor
class UniPCSampler(object):
@@ -15,6 +16,79 @@ class UniPCSampler(object):
self.after_sample = None
self.register_buffer('alphas_cumprod', to_torch(model.alphas_cumprod))
def make_schedule(self, ddim_num_steps, ddim_discretize="uniform", ddim_eta=0., verbose=True):
# persist steps so we can eventually find denoising strength
self.inflated_steps = ddim_num_steps
@torch.no_grad()
def stochastic_encode(self, x0, t, use_original_steps=False, noise=None):
if noise is None:
noise = torch.randn_like(x0)
# first time we have all the info to get the real parameters from the ui
hires_steps = t[0] + 1
inflated_steps = self.inflated_steps
self.denoising_strength = hires_steps/inflated_steps
adjusted_steps = int(hires_steps * self.denoising_strength)
self.steps = max(adjusted_steps, shared.opts.uni_pc_order+1)
t = torch.full(t.shape, self.steps).to(t.device)
timesteps = torch.asarray(list(range(
t,
self.model.num_timesteps,
self.model.num_timesteps // hires_steps,
))) + 1
alphas = self.model.alphas_cumprod[timesteps]
sqrt_one_minus_alphas = torch.sqrt(1. - alphas)
a = extract_into_tensor(torch.sqrt(alphas), t, x0.shape) * x0
b = extract_into_tensor(sqrt_one_minus_alphas, t, x0.shape) * noise
return (a+b)
def decode(self, x_latent, conditioning, t_start, unconditional_guidance_scale=1.0, unconditional_conditioning=None,
use_original_steps=False, callback=None):
#print(f'steps {self.steps} denoising {self.denoising_strength}')
noise_schedule = NoiseScheduleVP("discrete", alphas_cumprod=self.alphas_cumprod)
# same as in .sample(), i guess
model_type = "v" if self.model.parameterization == "v" else "noise"
model_fn = model_wrapper(
lambda x, t, c: self.model.apply_model(x, t, c),
noise_schedule,
model_type=model_type,
guidance_type="classifier-free",
#condition=conditioning,
#unconditional_condition=unconditional_conditioning,
guidance_scale=unconditional_guidance_scale,
)
self.uni_pc = UniPC(
model_fn,
noise_schedule,
predict_x0=True,
thresholding=False,
variant=shared.opts.uni_pc_variant,
condition=conditioning,
unconditional_condition=unconditional_conditioning,
before_sample=self.before_sample,
after_sample=self.after_sample,
after_update=self.after_update,
)
return self.uni_pc.sample(
x_latent,
steps=self.steps,
skip_type=shared.opts.uni_pc_skip_type,
method="multistep",
order=shared.opts.uni_pc_order,
lower_order_final=shared.opts.uni_pc_lower_order_final,
t_start=self.denoising_strength,
)
def register_buffer(self, name, attr):
if type(attr) == torch.Tensor:
if attr.device != devices.device: