Improve clarity of comments regarded when 'noise' and 'latents' are expected to be set.

This commit is contained in:
Ryan Dick 2024-06-13 10:19:43 -04:00 committed by Kent Keirsey
parent 1e41949a02
commit 60ac937698
2 changed files with 13 additions and 2 deletions

View File

@ -669,6 +669,18 @@ class DenoiseLatentsInvocation(BaseInvocation):
def prepare_noise_and_latents(
context: InvocationContext, noise_field: LatentsField | None, latents_field: LatentsField | None
) -> Tuple[int, torch.Tensor | None, torch.Tensor]:
"""Depending on the workflow, we expect different combinations of noise and latents to be provided. This
function handles preparing these values accordingly.
Expected workflows:
- Text-to-Image Denoising: `noise` is provided, `latents` is not. `latents` is initialized to zeros.
- Image-to-Image Denoising: `noise` and `latents` are both provided.
- Text-to-Image SDXL Refiner Denoising: `latents` is provided, `noise` is not.
- Image-to-Image SDXL Refiner Denoising: `latents` is provided, `noise` is not.
NOTE(ryand): I wrote this docstring, but I am not the original author of this code. There may be other workflows
I haven't considered.
"""
noise = None
if noise_field is not None:
noise = context.tensors.load(noise_field.latents_name)

View File

@ -298,8 +298,7 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline):
batch_size = latents.shape[0]
batched_t = init_timestep.expand(batch_size)
# NOTE(ryand): noise will be None if we are running the SDXL refiner. I can't think of any other reason that
# we'd want noise to be None, but I didn't write this logic, so there might be a reason I haven't thought of.
# noise can be None if the latents have already been noised (e.g. when running the SDXL refiner).
if noise is not None:
# latents = noise * self.scheduler.init_noise_sigma # it's like in t2l according to diffusers
latents = self.scheduler.add_noise(latents, noise, batched_t)