2023-04-23 18:02:08 +00:00
|
|
|
import torch
|
|
|
|
import comfy.model_management
|
2023-04-25 03:25:51 +00:00
|
|
|
import comfy.samplers
|
2023-09-02 07:42:49 +00:00
|
|
|
import comfy.utils
|
2023-05-13 15:15:45 +00:00
|
|
|
import numpy as np
|
2024-04-04 04:48:42 +00:00
|
|
|
import logging
|
2023-04-23 18:02:08 +00:00
|
|
|
|
2023-05-13 15:15:45 +00:00
|
|
|
def prepare_noise(latent_image, seed, noise_inds=None):
|
2023-04-24 10:53:10 +00:00
|
|
|
"""
|
|
|
|
creates random noise given a latent image and a seed.
|
|
|
|
optional arg skip can be used to skip and discard x number of noise generations for a given seed
|
|
|
|
"""
|
2023-04-23 18:09:09 +00:00
|
|
|
generator = torch.manual_seed(seed)
|
2023-05-13 15:15:45 +00:00
|
|
|
if noise_inds is None:
|
|
|
|
return torch.randn(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu")
|
|
|
|
|
|
|
|
unique_inds, inverse = np.unique(noise_inds, return_inverse=True)
|
|
|
|
noises = []
|
|
|
|
for i in range(unique_inds[-1]+1):
|
2023-04-23 18:09:09 +00:00
|
|
|
noise = torch.randn([1] + list(latent_image.size())[1:], dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu")
|
2023-05-13 15:15:45 +00:00
|
|
|
if i in unique_inds:
|
|
|
|
noises.append(noise)
|
|
|
|
noises = [noises[i] for i in inverse]
|
|
|
|
noises = torch.cat(noises, axis=0)
|
|
|
|
return noises
|
2023-04-23 18:02:08 +00:00
|
|
|
|
2024-06-08 06:16:55 +00:00
|
|
|
def fix_empty_latent_channels(model, latent_image):
|
|
|
|
latent_channels = model.get_model_object("latent_format").latent_channels #Resize the empty latent image so it has the right number of channels
|
|
|
|
if latent_channels != latent_image.shape[1] and torch.count_nonzero(latent_image) == 0:
|
|
|
|
latent_image = comfy.utils.repeat_to_batch_size(latent_image, latent_channels, dim=1)
|
|
|
|
return latent_image
|
|
|
|
|
2024-04-04 04:48:42 +00:00
|
|
|
def prepare_sampling(model, noise_shape, positive, negative, noise_mask):
|
|
|
|
logging.warning("Warning: comfy.sample.prepare_sampling isn't used anymore and can be removed")
|
|
|
|
return model, positive, negative, noise_mask, []
|
2023-04-23 18:02:08 +00:00
|
|
|
|
|
|
|
def cleanup_additional_models(models):
|
2024-04-04 04:48:42 +00:00
|
|
|
logging.warning("Warning: comfy.sample.cleanup_additional_models isn't used anymore and can be removed")
|
2023-04-25 03:25:51 +00:00
|
|
|
|
2023-09-27 20:45:22 +00:00
|
|
|
def sample(model, noise, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=1.0, disable_noise=False, start_step=None, last_step=None, force_full_denoise=False, noise_mask=None, sigmas=None, callback=None, disable_pbar=False, seed=None):
|
2024-04-04 04:48:42 +00:00
|
|
|
sampler = comfy.samplers.KSampler(model, steps=steps, device=model.load_device, sampler=sampler_name, scheduler=scheduler, denoise=denoise, model_options=model.model_options)
|
2023-04-25 03:25:51 +00:00
|
|
|
|
2024-04-04 04:48:42 +00:00
|
|
|
samples = sampler.sample(noise, positive, negative, cfg=cfg, latent_image=latent_image, start_step=start_step, last_step=last_step, force_full_denoise=force_full_denoise, denoise_mask=noise_mask, sigmas=sigmas, callback=callback, disable_pbar=disable_pbar, seed=seed)
|
2023-12-08 07:35:45 +00:00
|
|
|
samples = samples.to(comfy.model_management.intermediate_device())
|
2023-04-25 03:25:51 +00:00
|
|
|
return samples
|
2023-09-28 02:21:18 +00:00
|
|
|
|
|
|
|
def sample_custom(model, noise, cfg, sampler, sigmas, positive, negative, latent_image, noise_mask=None, callback=None, disable_pbar=False, seed=None):
|
2024-04-04 04:48:42 +00:00
|
|
|
samples = comfy.samplers.sample(model, noise, positive, negative, cfg, model.load_device, sampler, sigmas, model_options=model.model_options, latent_image=latent_image, denoise_mask=noise_mask, callback=callback, disable_pbar=disable_pbar, seed=seed)
|
2023-12-08 07:35:45 +00:00
|
|
|
samples = samples.to(comfy.model_management.intermediate_device())
|
2023-09-28 02:21:18 +00:00
|
|
|
return samples
|