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

Add cond and uncond hidden states to CFGDenoiserParams #8064

Merged
merged 3 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion modules/script_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, image, p, filename, pnginfo):


class CFGDenoiserParams:
def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps):
def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps, tensor, uncond):
self.x = x
"""Latent image representation in the process of being denoised"""

Expand All @@ -44,6 +44,12 @@ def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps):

self.total_sampling_steps = total_sampling_steps
"""Total number of sampling steps planned"""

self.tensor = tensor
""" Encoder hidden states of conditioning"""

self.uncond = uncond
""" Encoder hidden states of unconditioning"""


class CFGDenoisedParams:
Expand Down
4 changes: 3 additions & 1 deletion modules/sd_samplers_kdiffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ def forward(self, x, sigma, uncond, cond, cond_scale, image_cond):
sigma_in = torch.cat([torch.stack([sigma[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [sigma] + [sigma])
image_cond_in = torch.cat([torch.stack([image_cond[i] for _ in range(n)]) for i, n in enumerate(repeats)] + [image_cond] + [torch.zeros_like(self.init_latent)])

denoiser_params = CFGDenoiserParams(x_in, image_cond_in, sigma_in, state.sampling_step, state.sampling_steps)
denoiser_params = CFGDenoiserParams(x_in, image_cond_in, sigma_in, state.sampling_step, state.sampling_steps, tensor, uncond)
cfg_denoiser_callback(denoiser_params)
x_in = denoiser_params.x
image_cond_in = denoiser_params.image_cond
sigma_in = denoiser_params.sigma
tensor = denoiser_params.tensor
uncond = denoiser_params.uncond

if tensor.shape[1] == uncond.shape[1]:
if not is_edit_model:
Expand Down