my-sd/modules_forge/forge_sampler.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
4.2 KiB
Python
Raw Permalink Normal View History

2024-01-27 21:21:25 +00:00
import torch
from ldm_patched.modules.conds import CONDRegular, CONDCrossAttn
from ldm_patched.modules.samplers import sampling_function
2024-01-28 06:11:14 +00:00
from ldm_patched.modules import model_management
from ldm_patched.modules.ops import cleanup_cache
2024-01-27 21:21:25 +00:00
def cond_from_a1111_to_patched_ldm(cond):
if isinstance(cond, torch.Tensor):
result = dict(
cross_attn=cond,
model_conds=dict(
c_crossattn=CONDCrossAttn(cond),
)
)
return [result, ]
cross_attn = cond['crossattn']
pooled_output = cond['vector']
result = dict(
cross_attn=cross_attn,
pooled_output=pooled_output,
model_conds=dict(
c_crossattn=CONDCrossAttn(cross_attn),
y=CONDRegular(pooled_output)
)
)
return [result, ]
2024-01-28 02:38:11 +00:00
def cond_from_a1111_to_patched_ldm_weighted(cond, weights):
transposed = list(map(list, zip(*weights)))
results = []
for cond_pre in transposed:
current_indices = []
current_weight = 0
for i, w in cond_pre:
current_indices.append(i)
current_weight = w
2024-01-28 02:53:24 +00:00
if hasattr(cond, 'advanced_indexing'):
feed = cond.advanced_indexing(current_indices)
else:
feed = cond[current_indices]
2024-01-28 02:38:11 +00:00
h = cond_from_a1111_to_patched_ldm(feed)
h[0]['strength'] = current_weight
results += h
return results
def forge_sample(self, denoiser_params, cond_scale, cond_composition):
2024-01-27 21:21:25 +00:00
model = self.inner_model.inner_model.forge_objects.unet.model
2024-01-28 06:44:49 +00:00
control = self.inner_model.inner_model.forge_objects.unet.controlnet_linked_list
extra_concat_condition = self.inner_model.inner_model.forge_objects.unet.extra_concat_condition
2024-01-27 21:21:25 +00:00
x = denoiser_params.x
timestep = denoiser_params.sigma
uncond = cond_from_a1111_to_patched_ldm(denoiser_params.text_uncond)
2024-01-28 02:38:11 +00:00
cond = cond_from_a1111_to_patched_ldm_weighted(denoiser_params.text_cond, cond_composition)
2024-01-27 21:21:25 +00:00
model_options = self.inner_model.inner_model.forge_objects.unet.model_options
seed = self.p.seeds[0]
if extra_concat_condition is not None:
image_cond_in = extra_concat_condition
else:
image_cond_in = denoiser_params.image_cond
2024-01-27 21:21:25 +00:00
if isinstance(image_cond_in, torch.Tensor):
if image_cond_in.shape[0] == x.shape[0] \
and image_cond_in.shape[2] == x.shape[2] \
and image_cond_in.shape[3] == x.shape[3]:
2024-01-28 17:15:48 +00:00
for i in range(len(uncond)):
uncond[i]['model_conds']['c_concat'] = CONDRegular(image_cond_in)
for i in range(len(cond)):
cond[i]['model_conds']['c_concat'] = CONDRegular(image_cond_in)
2024-01-27 21:21:25 +00:00
2024-01-28 06:44:49 +00:00
if control is not None:
for h in cond + uncond:
h['control'] = control
2024-01-30 18:32:54 +00:00
for modifier in model_options.get('conditioning_modifiers', []):
model, x, timestep, uncond, cond, cond_scale, model_options, seed = modifier(model, x, timestep, uncond, cond, cond_scale, model_options, seed)
2024-01-27 21:21:25 +00:00
denoised = sampling_function(model, x, timestep, uncond, cond, cond_scale, model_options, seed)
return denoised
2024-01-28 05:46:33 +00:00
2024-01-28 06:11:14 +00:00
def sampling_prepare(unet, x):
B, C, H, W = x.shape
2024-02-05 03:36:58 +00:00
memory_estimation_function = unet.model_options.get('memory_peak_estimation_modifier', unet.memory_required)
unet_inference_memory = memory_estimation_function([B * 2, C, H, W])
2024-02-01 06:10:15 +00:00
additional_inference_memory = unet.extra_preserved_memory_during_sampling
additional_model_patchers = unet.extra_model_patchers_during_sampling
2024-01-28 06:13:29 +00:00
if unet.controlnet_linked_list is not None:
additional_inference_memory += unet.controlnet_linked_list.inference_memory_requirements(unet.model_dtype())
additional_model_patchers += unet.controlnet_linked_list.get_models()
2024-01-28 06:11:14 +00:00
model_management.load_models_gpu(
models=[unet] + additional_model_patchers,
memory_required=unet_inference_memory + additional_inference_memory)
2024-01-28 06:24:40 +00:00
real_model = unet.model
percent_to_timestep_function = lambda p: real_model.model_sampling.percent_to_sigma(p)
for cnet in unet.list_controlnets():
cnet.pre_run(real_model, percent_to_timestep_function)
2024-01-28 06:11:14 +00:00
return
def sampling_cleanup(unet):
for cnet in unet.list_controlnets():
cnet.cleanup()
cleanup_cache()
2024-01-28 06:11:14 +00:00
return