made sample functions more explicit

This commit is contained in:
BlenderNeko 2023-04-24 12:53:10 +02:00
parent 5818539743
commit d9b1595f85
2 changed files with 29 additions and 33 deletions

View File

@ -2,30 +2,25 @@ import torch
import comfy.model_management import comfy.model_management
def prepare_noise(latent, seed): def prepare_noise(latent_image, seed, skip=0):
"""creates random noise given a LATENT and a seed""" """
latent_image = latent["samples"] creates random noise given a latent image and a seed.
batch_index = 0 optional arg skip can be used to skip and discard x number of noise generations for a given seed
if "batch_index" in latent: """
batch_index = latent["batch_index"]
generator = torch.manual_seed(seed) generator = torch.manual_seed(seed)
for i in range(batch_index): for _ in range(skip):
noise = torch.randn([1] + list(latent_image.size())[1:], dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu") noise = torch.randn([1] + list(latent_image.size())[1:], dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu")
noise = torch.randn(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu") noise = torch.randn(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu")
return noise return noise
def create_mask(latent, noise): def prepare_mask(noise_mask, noise):
"""creates a mask for a given LATENT and noise""" """ensures noise mask is of proper dimensions"""
noise_mask = None
device = comfy.model_management.get_torch_device() device = comfy.model_management.get_torch_device()
if "noise_mask" in latent: noise_mask = torch.nn.functional.interpolate(noise_mask[None,None,], size=(noise.shape[2], noise.shape[3]), mode="bilinear")
noise_mask = latent['noise_mask'] noise_mask = noise_mask.round()
noise_mask = torch.nn.functional.interpolate(noise_mask[None,None,], size=(noise.shape[2], noise.shape[3]), mode="bilinear") noise_mask = torch.cat([noise_mask] * noise.shape[1], dim=1)
noise_mask = noise_mask.round() noise_mask = torch.cat([noise_mask] * noise.shape[0])
noise_mask = torch.cat([noise_mask] * noise.shape[1], dim=1) noise_mask = noise_mask.to(device)
noise_mask = torch.cat([noise_mask] * noise.shape[0])
noise_mask = noise_mask.to(device)
return noise_mask return noise_mask
def broadcast_cond(cond, noise): def broadcast_cond(cond, noise):
@ -40,22 +35,20 @@ def broadcast_cond(cond, noise):
copy += [[t] + p[1:]] copy += [[t] + p[1:]]
return copy return copy
def load_c_nets(positive, negative): def get_models_from_cond(cond, model_type):
"""loads control nets in positive and negative conditioning""" models = []
def get_models(cond): for c in cond:
models = [] if model_type in c[1]:
for c in cond: models += [c[1][model_type]]
if 'control' in c[1]: return models
models += [c[1]['control']]
if 'gligen' in c[1]:
models += [c[1]['gligen'][1]]
return models
return get_models(positive) + get_models(negative)
def load_additional_models(positive, negative): def load_additional_models(positive, negative):
"""loads additional models in positive and negative conditioning""" """loads additional models in positive and negative conditioning"""
models = load_c_nets(positive, negative) models = []
models += get_models_from_cond(positive, "control")
models += get_models_from_cond(negative, "control")
models += get_models_from_cond(positive, "gligen")
models += get_models_from_cond(negative, "gligen")
comfy.model_management.load_controlnet_gpu(models) comfy.model_management.load_controlnet_gpu(models)
return models return models

View File

@ -747,9 +747,12 @@ def common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, positive,
if disable_noise: if disable_noise:
noise = torch.zeros(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, device="cpu") noise = torch.zeros(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, device="cpu")
else: else:
noise = comfy.sample.prepare_noise(latent, seed) skip = latent["batch_index"] if "batch_index" in latent else 0
noise = comfy.sample.prepare_noise(latent_image, seed, skip)
noise_mask = comfy.sample.create_mask(latent, noise) noise_mask = None
if "noise_mask" in latent:
noise_mask = comfy.sample.prepare_mask(latent["noise_mask"], noise)
real_model = None real_model = None
comfy.model_management.load_model_gpu(model) comfy.model_management.load_model_gpu(model)