fluxdev/stable-diffusion-webui-forge
1
1import torch2from ldm_patched.modules.conds import CONDRegular, CONDCrossAttn3from ldm_patched.modules.samplers import sampling_function4from ldm_patched.modules import model_management5from ldm_patched.modules.ops import cleanup_cache6 7 8def cond_from_a1111_to_patched_ldm(cond):9 if isinstance(cond, torch.Tensor):10 result = dict(11 cross_attn=cond,12 model_conds=dict(13 c_crossattn=CONDCrossAttn(cond),14 )15 )16 return [result, ]17 18 cross_attn = cond['crossattn']19 pooled_output = cond['vector']20 21 result = dict(22 cross_attn=cross_attn,23 pooled_output=pooled_output,24 model_conds=dict(25 c_crossattn=CONDCrossAttn(cross_attn),26 y=CONDRegular(pooled_output)27 )28 )29 30 return [result, ]31 32 33def cond_from_a1111_to_patched_ldm_weighted(cond, weights):34 transposed = list(map(list, zip(*weights)))35 results = []36 37 for cond_pre in transposed:38 current_indices = []39 current_weight = 040 for i, w in cond_pre:41 current_indices.append(i)42 current_weight = w43 44 if hasattr(cond, 'advanced_indexing'):45 feed = cond.advanced_indexing(current_indices)46 else:47 feed = cond[current_indices]48 49 h = cond_from_a1111_to_patched_ldm(feed)50 h[0]['strength'] = current_weight51 results += h52 53 return results54 55 56def forge_sample(self, denoiser_params, cond_scale, cond_composition):57 model = self.inner_model.inner_model.forge_objects.unet.model58 control = self.inner_model.inner_model.forge_objects.unet.controlnet_linked_list59 extra_concat_condition = self.inner_model.inner_model.forge_objects.unet.extra_concat_condition60 x = denoiser_params.x61 timestep = denoiser_params.sigma62 uncond = cond_from_a1111_to_patched_ldm(denoiser_params.text_uncond)63 cond = cond_from_a1111_to_patched_ldm_weighted(denoiser_params.text_cond, cond_composition)64 model_options = self.inner_model.inner_model.forge_objects.unet.model_options65 seed = self.p.seeds[0]66 67 if extra_concat_condition is not None:68 image_cond_in = extra_concat_condition69 else:70 image_cond_in = denoiser_params.image_cond71 72 if isinstance(image_cond_in, torch.Tensor):73 if image_cond_in.shape[0] == x.shape[0] \74 and image_cond_in.shape[2] == x.shape[2] \75 and image_cond_in.shape[3] == x.shape[3]:76 for i in range(len(uncond)):77 uncond[i]['model_conds']['c_concat'] = CONDRegular(image_cond_in)78 for i in range(len(cond)):79 cond[i]['model_conds']['c_concat'] = CONDRegular(image_cond_in)80 81 if control is not None:82 for h in cond + uncond:83 h['control'] = control84 85 for modifier in model_options.get('conditioning_modifiers', []):86 model, x, timestep, uncond, cond, cond_scale, model_options, seed = modifier(model, x, timestep, uncond, cond, cond_scale, model_options, seed)87 88 denoised = sampling_function(model, x, timestep, uncond, cond, cond_scale, model_options, seed)89 return denoised90 91 92def sampling_prepare(unet, x):93 B, C, H, W = x.shape94 95 memory_estimation_function = unet.model_options.get('memory_peak_estimation_modifier', unet.memory_required)96 97 unet_inference_memory = memory_estimation_function([B * 2, C, H, W])98 additional_inference_memory = unet.extra_preserved_memory_during_sampling99 additional_model_patchers = unet.extra_model_patchers_during_sampling100 101 if unet.controlnet_linked_list is not None:102 additional_inference_memory += unet.controlnet_linked_list.inference_memory_requirements(unet.model_dtype())103 additional_model_patchers += unet.controlnet_linked_list.get_models()104 105 model_management.load_models_gpu(106 models=[unet] + additional_model_patchers,107 memory_required=unet_inference_memory + additional_inference_memory)108 109 real_model = unet.model110 111 percent_to_timestep_function = lambda p: real_model.model_sampling.percent_to_sigma(p)112 113 for cnet in unet.list_controlnets():114 cnet.pre_run(real_model, percent_to_timestep_function)115 116 return117 118 119def sampling_cleanup(unet):120 for cnet in unet.list_controlnets():121 cnet.cleanup()122 cleanup_cache()123 return124 