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-10-25 03:31:12 +00:00
|
|
|
import comfy.conds
|
2023-09-02 07:42:49 +00:00
|
|
|
import comfy.utils
|
2023-04-25 05:12:40 +00:00
|
|
|
import math
|
2023-05-13 15:15:45 +00:00
|
|
|
import numpy as np
|
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
|
|
|
|
2023-04-25 03:25:51 +00:00
|
|
|
def prepare_mask(noise_mask, shape, device):
|
2023-04-24 10:53:10 +00:00
|
|
|
"""ensures noise mask is of proper dimensions"""
|
2023-04-25 05:12:40 +00:00
|
|
|
noise_mask = torch.nn.functional.interpolate(noise_mask.reshape((-1, 1, noise_mask.shape[-2], noise_mask.shape[-1])), size=(shape[2], shape[3]), mode="bilinear")
|
2023-04-24 10:53:10 +00:00
|
|
|
noise_mask = noise_mask.round()
|
2023-04-25 03:25:51 +00:00
|
|
|
noise_mask = torch.cat([noise_mask] * shape[1], dim=1)
|
2023-09-02 07:42:49 +00:00
|
|
|
noise_mask = comfy.utils.repeat_to_batch_size(noise_mask, shape[0])
|
2023-04-24 10:53:10 +00:00
|
|
|
noise_mask = noise_mask.to(device)
|
2023-04-23 18:02:08 +00:00
|
|
|
return noise_mask
|
|
|
|
|
2023-04-24 10:53:10 +00:00
|
|
|
def get_models_from_cond(cond, model_type):
|
|
|
|
models = []
|
|
|
|
for c in cond:
|
2023-10-25 03:31:12 +00:00
|
|
|
if model_type in c:
|
|
|
|
models += [c[model_type]]
|
2023-04-24 10:53:10 +00:00
|
|
|
return models
|
2023-04-23 18:02:08 +00:00
|
|
|
|
2023-10-25 03:31:12 +00:00
|
|
|
def convert_cond(cond):
|
|
|
|
out = []
|
|
|
|
for c in cond:
|
|
|
|
temp = c[1].copy()
|
|
|
|
model_conds = temp.get("model_conds", {})
|
|
|
|
if c[0] is not None:
|
|
|
|
model_conds["c_crossattn"] = comfy.conds.CONDCrossAttn(c[0])
|
|
|
|
temp["model_conds"] = model_conds
|
|
|
|
out.append(temp)
|
|
|
|
return out
|
|
|
|
|
2023-08-24 21:20:54 +00:00
|
|
|
def get_additional_models(positive, negative, dtype):
|
2023-04-23 18:09:09 +00:00
|
|
|
"""loads additional models in positive and negative conditioning"""
|
2023-08-17 17:38:51 +00:00
|
|
|
control_nets = set(get_models_from_cond(positive, "control") + get_models_from_cond(negative, "control"))
|
2023-08-17 05:06:34 +00:00
|
|
|
|
2023-08-24 21:20:54 +00:00
|
|
|
inference_memory = 0
|
2023-08-17 05:06:34 +00:00
|
|
|
control_models = []
|
|
|
|
for m in control_nets:
|
|
|
|
control_models += m.get_models()
|
2023-08-24 21:20:54 +00:00
|
|
|
inference_memory += m.inference_memory_requirements(dtype)
|
2023-08-17 05:06:34 +00:00
|
|
|
|
2023-04-24 19:47:57 +00:00
|
|
|
gligen = get_models_from_cond(positive, "gligen") + get_models_from_cond(negative, "gligen")
|
2023-08-17 05:06:34 +00:00
|
|
|
gligen = [x[1] for x in gligen]
|
|
|
|
models = control_models + gligen
|
2023-08-24 21:20:54 +00:00
|
|
|
return models, inference_memory
|
2023-04-23 18:02:08 +00:00
|
|
|
|
|
|
|
def cleanup_additional_models(models):
|
2023-04-23 18:09:09 +00:00
|
|
|
"""cleanup additional models that were loaded"""
|
2023-04-23 18:02:08 +00:00
|
|
|
for m in models:
|
2023-08-17 05:06:34 +00:00
|
|
|
if hasattr(m, 'cleanup'):
|
|
|
|
m.cleanup()
|
2023-04-25 03:25:51 +00:00
|
|
|
|
2023-09-27 20:45:22 +00:00
|
|
|
def prepare_sampling(model, noise_shape, positive, negative, noise_mask):
|
|
|
|
device = model.load_device
|
2023-10-25 03:31:12 +00:00
|
|
|
positive = convert_cond(positive)
|
|
|
|
negative = convert_cond(negative)
|
2023-04-25 03:25:51 +00:00
|
|
|
|
|
|
|
if noise_mask is not None:
|
2023-09-27 20:45:22 +00:00
|
|
|
noise_mask = prepare_mask(noise_mask, noise_shape, device)
|
2023-04-25 03:25:51 +00:00
|
|
|
|
|
|
|
real_model = None
|
2023-08-24 21:20:54 +00:00
|
|
|
models, inference_memory = get_additional_models(positive, negative, model.model_dtype())
|
2023-09-27 20:45:22 +00:00
|
|
|
comfy.model_management.load_models_gpu([model] + models, comfy.model_management.batch_area_memory(noise_shape[0] * noise_shape[2] * noise_shape[3]) + inference_memory)
|
2023-04-25 03:25:51 +00:00
|
|
|
real_model = model.model
|
|
|
|
|
2023-10-25 03:31:12 +00:00
|
|
|
return real_model, positive, negative, noise_mask, models
|
2023-09-27 20:45:22 +00:00
|
|
|
|
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):
|
|
|
|
real_model, positive_copy, negative_copy, noise_mask, models = prepare_sampling(model, noise.shape, positive, negative, noise_mask)
|
2023-04-25 03:25:51 +00:00
|
|
|
|
2023-09-27 20:45:22 +00:00
|
|
|
noise = noise.to(model.load_device)
|
|
|
|
latent_image = latent_image.to(model.load_device)
|
2023-04-25 03:25:51 +00:00
|
|
|
|
2023-09-27 20:45:22 +00:00
|
|
|
sampler = comfy.samplers.KSampler(real_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
|
|
|
|
2023-06-25 06:41:31 +00:00
|
|
|
samples = sampler.sample(noise, positive_copy, negative_copy, 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-04-25 03:25:51 +00:00
|
|
|
samples = samples.cpu()
|
|
|
|
|
|
|
|
cleanup_additional_models(models)
|
2023-10-18 06:43:01 +00:00
|
|
|
cleanup_additional_models(set(get_models_from_cond(positive, "control") + get_models_from_cond(negative, "control")))
|
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):
|
|
|
|
real_model, positive_copy, negative_copy, noise_mask, models = prepare_sampling(model, noise.shape, positive, negative, noise_mask)
|
|
|
|
noise = noise.to(model.load_device)
|
|
|
|
latent_image = latent_image.to(model.load_device)
|
|
|
|
sigmas = sigmas.to(model.load_device)
|
|
|
|
|
|
|
|
samples = comfy.samplers.sample(real_model, noise, positive_copy, negative_copy, 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)
|
|
|
|
samples = samples.cpu()
|
|
|
|
cleanup_additional_models(models)
|
2023-10-18 06:43:01 +00:00
|
|
|
cleanup_additional_models(set(get_models_from_cond(positive, "control") + get_models_from_cond(negative, "control")))
|
2023-09-28 02:21:18 +00:00
|
|
|
return samples
|
|
|
|
|