Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unbatch conditioned and unconditioned model calls #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions optimizedSD/ddpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,8 @@ def get_model_output(x, t):
if unconditional_conditioning is None or unconditional_guidance_scale == 1.:
e_t = self.apply_model(x, t, c)
else:
x_in = torch.cat([x] * 2)
t_in = torch.cat([t] * 2)
c_in = torch.cat([unconditional_conditioning, c])
e_t_uncond, e_t = self.apply_model(x_in, t_in, c_in).chunk(2)
e_t_uncond = self.apply_model(x, t, unconditional_conditioning)
e_t = self.apply_model(x, t, c)
e_t = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond)

if score_corrector is not None:
Expand Down Expand Up @@ -726,10 +724,8 @@ def p_sample_ddim(self, x, c, t, index, repeat_noise=False, use_original_steps=F
if unconditional_conditioning is None or unconditional_guidance_scale == 1.:
e_t = self.apply_model(x, t, c)
else:
x_in = torch.cat([x] * 2)
t_in = torch.cat([t] * 2)
c_in = torch.cat([unconditional_conditioning, c])
e_t_uncond, e_t = self.apply_model(x_in, t_in, c_in).chunk(2)
e_t_uncond = self.apply_model(x, t, unconditional_conditioning)
e_t = self.apply_model(x, t, c)
e_t = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond)

if score_corrector is not None:
Expand Down Expand Up @@ -839,12 +835,12 @@ def heun_sampling(self, x,sigmas, cond,unconditional_conditioning = None,uncondi
x = x + eps * (sigma_hat ** 2 - sigmas[i] ** 2) ** 0.5

# print(sigma_hat * s_in)
# x, sigma_hat * s_in, cond
x_in = torch.cat([x] * 2)
t_in = torch.cat([sigma_hat * s_in] * 2)
c_in = torch.cat([unconditional_conditioning, cond])
e_t_uncond, e_t = self.apply_model(x_in, t_in, c_in).chunk(2)
t = sigma_hat * s_in
e_t_uncond = self.apply_model(x, t, unconditional_conditioning)
e_t = self.apply_model(x, t, cond)
denoised = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond)
del e_t
del e_t_uncond
# denoised = self.apply_model(x, sigma_hat * s_in, cond)
d = self.to_d(x, sigma_hat, denoised)
if callback is not None:
Expand Down