The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Remove some trailing white space.
comfyanonymousd17029259477
diff --git a/comfy/controlnet.py b/comfy/controlnet.pyindex 7f5988377..ee29251b9 100644--- a/comfy/controlnet.py+++ b/comfy/controlnet.py@@ -120,7 +120,7 @@ class ControlBase:if self.previous_controlnet is not None:out += self.previous_controlnet.get_models()return out-+def get_extra_hooks(self):out = []if self.extra_hooks is not None:diff --git a/comfy/extra_samplers/uni_pc.py b/comfy/extra_samplers/uni_pc.pyindex b61baaa8e..77d20bbf5 100644--- a/comfy/extra_samplers/uni_pc.py+++ b/comfy/extra_samplers/uni_pc.py@@ -80,7 +80,7 @@ class NoiseScheduleVP:'linear' or 'cosine' for continuous-time DPMs.Returns:A wrapper object of the forward SDE (VP type).-+===============================================================Example:@@ -208,7 +208,7 @@ def model_wrapper(arXiv preprint arXiv:2202.00512 (2022).[2] Ho, Jonathan, et al. "Imagen Video: High Definition Video Generation with Diffusion Models."arXiv preprint arXiv:2210.02303 (2022).-+4. "score": marginal score function. (Trained by denoising score matching).Note that the score function and the noise prediction model follows a simple relationship:```@@ -245,7 +245,7 @@ def model_wrapper([4] Ho, Jonathan, and Tim Salimans. "Classifier-free diffusion guidance."arXiv preprint arXiv:2207.12598 (2022).-+The `t_input` is the time label of the model, which may be discrete-time labels (i.e. 0 to 999)or continuous-time labels (i.e. epsilon to T).@@ -621,7 +621,7 @@ class UniPC:B_h = torch.expm1(hh)else:raise NotImplementedError()-+for i in range(1, order + 1):R.append(torch.pow(rks, i - 1))b.append(h_phi_k * factorial_i / B_h)@@ -870,4 +870,4 @@ def sample_unipc(model, noise, sigmas, extra_args=None, callback=None, disable=Freturn xdef sample_unipc_bh2(model, noise, sigmas, extra_args=None, callback=None, disable=False):- return sample_unipc(model, noise, sigmas, extra_args, callback, disable, variant='bh2')\ No newline at end of file+ return sample_unipc(model, noise, sigmas, extra_args, callback, disable, variant='bh2')diff --git a/comfy/hooks.py b/comfy/hooks.pyindex b6f0ac213..7f9eee301 100644--- a/comfy/hooks.py+++ b/comfy/hooks.py@@ -101,7 +101,7 @@ class WeightHook(Hook):self.need_weight_init = Trueself._strength_model = strength_modelself._strength_clip = strength_clip-+@propertydef strength_model(self):return self._strength_model * self.strengthdiff --git a/comfy/k_diffusion/sampling.py b/comfy/k_diffusion/sampling.pyindex f08370f83..0f7cc4ca9 100644--- a/comfy/k_diffusion/sampling.py+++ b/comfy/k_diffusion/sampling.py@@ -1230,7 +1230,7 @@ def sample_dpmpp_2m_cfg_pp(model, x, sigmas, extra_args=None, callback=None, disnonlocal uncond_denoiseduncond_denoised = args["uncond_denoised"]return args["denoised"]-+model_options = extra_args.get("model_options", {}).copy()extra_args["model_options"] = comfy.model_patcher.set_model_options_post_cfg_function(model_options, post_cfg_function, disable_cfg1_optimization=True)diff --git a/comfy/ldm/modules/sub_quadratic_attention.py b/comfy/ldm/modules/sub_quadratic_attention.pyindex 7c5f1d9f9..fca8d1178 100644--- a/comfy/ldm/modules/sub_quadratic_attention.py+++ b/comfy/ldm/modules/sub_quadratic_attention.py@@ -261,7 +261,7 @@ def efficient_dot_product_attention(value=value,mask=mask,)-+# TODO: maybe we should use torch.empty_like(query) to allocate storage in-advance,# and pass slices to be mutated, instead of torch.cat()ing the returned slicesres = torch.cat([diff --git a/comfy/ldm/pixart/pixartms.py b/comfy/ldm/pixart/pixartms.pyindex 50dc58c23..7d4eebdce 100644--- a/comfy/ldm/pixart/pixartms.py+++ b/comfy/ldm/pixart/pixartms.py@@ -223,7 +223,7 @@ class PixArtMS(nn.Module):if self.micro_conditioning:if c_size is None:c_size = torch.tensor([H*8, W*8], dtype=x.dtype, device=x.device).repeat(B, 1)-+if c_ar is None:c_ar = torch.tensor([H/W], dtype=x.dtype, device=x.device).repeat(B, 1)diff --git a/comfy/model_detection.py b/comfy/model_detection.pyindex c53bef5bb..de00f773e 100644--- a/comfy/model_detection.py+++ b/comfy/model_detection.py@@ -229,7 +229,7 @@ def detect_unet_config(state_dict, key_prefix):if pe_key in state_dict_keys:dit_config["input_size"] = int(math.sqrt(state_dict[pe_key].shape[1])) * patch_sizedit_config["pe_interpolation"] = dit_config["input_size"] // (512//8) # guess-+ar_key = "{}ar_embedder.mlp.0.weight".format(key_prefix)if ar_key in state_dict_keys:dit_config["image_model"] = "pixart_alpha"@@ -571,7 +571,7 @@ def unet_config_from_diffusers_unet(state_dict, dtype=None):'transformer_depth': [0, 1, 1], 'channel_mult': [1, 2, 4], 'transformer_depth_middle': -2, 'use_linear_in_transformer': False,'context_dim': 768, 'num_head_channels': 64, 'transformer_depth_output': [0, 0, 1, 1, 1, 1],'use_temporal_attention': False, 'use_temporal_resblock': False}-+SD15_diffusers_inpaint = {'use_checkpoint': False, 'image_size': 32, 'out_channels': 4, 'use_spatial_transformer': True, 'legacy': False, 'adm_in_channels': None,'dtype': dtype, 'in_channels': 9, 'model_channels': 320, 'num_res_blocks': [2, 2, 2, 2], 'transformer_depth': [1, 1, 1, 1, 1, 1, 0, 0],'channel_mult': [1, 2, 4, 4], 'transformer_depth_middle': 1, 'use_linear_in_transformer': False, 'context_dim': 768, 'num_heads': 8,diff --git a/comfy/sample.py b/comfy/sample.pyindex 98dcaca7f..9974e0657 100644--- a/comfy/sample.py+++ b/comfy/sample.py@@ -13,7 +13,7 @@ def prepare_noise(latent_image, seed, noise_inds=None):generator = torch.manual_seed(seed)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):diff --git a/comfy/sampler_helpers.py b/comfy/sampler_helpers.pyindex 0691de63f..ac9735369 100644--- a/comfy/sampler_helpers.py+++ b/comfy/sampler_helpers.py@@ -42,7 +42,7 @@ def get_hooks_from_cond(cond, hooks_dict: dict[comfy.hooks.EnumHookType, dict[coif cnet.previous_controlnet is None:return _listreturn get_extra_hooks_from_cnet(cnet.previous_controlnet, _list)-+hooks_list = []cnets = set(cnets)for base_cnet in cnets:diff --git a/comfy/utils.py b/comfy/utils.pyindex 7de659337..de64b91df 100644--- a/comfy/utils.py+++ b/comfy/utils.py@@ -455,7 +455,7 @@ def pixart_to_diffusers(mmdit_config, output_prefix=""):for k in PIXART_MAP_BASIC:key_map[k[1]] = "{}{}".format(output_prefix, k[0])-+return key_mapdef auraflow_to_diffusers(mmdit_config, output_prefix=""):@@ -702,7 +702,7 @@ def get_attr(obj, attr):def bislerp(samples, width, height):def slerp(b1, b2, r):'''slerps batches b1, b2 according to ratio r, batches should be flat e.g. NxC'''-+c = b1.shape[-1]#norms@@ -730,13 +730,13 @@ def bislerp(samples, width, height):res[dot > 1 - 1e-5] = b1[dot > 1 - 1e-5]res[dot < 1e-5 - 1] = (b1 * (1.0-r) + b2 * r)[dot < 1e-5 - 1]return res-+def generate_bilinear_data(length_old, length_new, device):coords_1 = torch.arange(length_old, dtype=torch.float32, device=device).reshape((1,1,1,-1))coords_1 = torch.nn.functional.interpolate(coords_1, size=(1, length_new), mode="bilinear")ratios = coords_1 - coords_1.floor()coords_1 = coords_1.to(torch.int64)-+coords_2 = torch.arange(length_old, dtype=torch.float32, device=device).reshape((1,1,1,-1)) + 1coords_2[:,:,:,-1] -= 1coords_2 = torch.nn.functional.interpolate(coords_2, size=(1, length_new), mode="bilinear")@@ -747,7 +747,7 @@ def bislerp(samples, width, height):samples = samples.float()n,c,h,w = samples.shapeh_new, w_new = (height, width)-+#linear wratios, coords_1, coords_2 = generate_bilinear_data(w, w_new, samples.device)coords_1 = coords_1.expand((n, c, h, -1))diff --git a/comfy_extras/nodes_mask.py b/comfy_extras/nodes_mask.pyindex 29589b4ab..63fd13b9a 100644--- a/comfy_extras/nodes_mask.py+++ b/comfy_extras/nodes_mask.py@@ -305,7 +305,7 @@ class FeatherMask:output[:, -y, :] *= feather_ratereturn (output,)-+class GrowMask:@classmethoddef INPUT_TYPES(cls):@@ -316,7 +316,7 @@ class GrowMask:"tapered_corners": ("BOOLEAN", {"default": True}),},}-+CATEGORY = "mask"RETURN_TYPES = ("MASK",)diff --git a/comfy_extras/nodes_perpneg.py b/comfy_extras/nodes_perpneg.pyindex 762c40220..290bc4a51 100644--- a/comfy_extras/nodes_perpneg.py+++ b/comfy_extras/nodes_perpneg.py@@ -64,7 +64,7 @@ class Guider_PerpNeg(comfy.samplers.CFGGuider):def predict_noise(self, x, timestep, model_options={}, seed=None):# in CFGGuider.predict_noise, we call sampling_function(), which uses cfg_function() to compute pos & neg# but we'd rather do a single batch of sampling pos, neg, and empty, so we call calc_cond_batch([pos,neg,empty]) directly-+positive_cond = self.conds.get("positive", None)negative_cond = self.conds.get("negative", None)empty_cond = self.conds.get("empty_negative_prompt", None)diff --git a/comfy_extras/nodes_rebatch.py b/comfy_extras/nodes_rebatch.pyindex 3010fbd4b..e29cb9ed1 100644--- a/comfy_extras/nodes_rebatch.py+++ b/comfy_extras/nodes_rebatch.py@@ -40,7 +40,7 @@ class LatentRebatch:return slices, indexable[num * batch_size:]else:return slices, None-+@staticmethoddef slice_batch(batch, num, batch_size):result = [LatentRebatch.get_slices(x, num, batch_size) for x in batch]@@ -81,7 +81,7 @@ class LatentRebatch:if current_batch[0].shape[0] > batch_size:num = current_batch[0].shape[0] // batch_sizesliced, remainder = self.slice_batch(current_batch, num, batch_size)-+for i in range(num):output_list.append({'samples': sliced[0][i], 'noise_mask': sliced[1][i], 'batch_index': sliced[2][i]})diff --git a/comfy_extras/nodes_tomesd.py b/comfy_extras/nodes_tomesd.pyindex ce7b32c77..9f77c06fc 100644--- a/comfy_extras/nodes_tomesd.py+++ b/comfy_extras/nodes_tomesd.py@@ -40,9 +40,8 @@ def bipartite_soft_matching_random2d(metric: torch.Tensor,return do_nothing, do_nothinggather = mps_gather_workaround if metric.device.type == "mps" else torch.gather-+with torch.no_grad():-hsy, wsx = h // sy, w // sx# For each sy by sx kernel, randomly assign one token to be dst and the rest src@@ -50,7 +49,7 @@ def bipartite_soft_matching_random2d(metric: torch.Tensor,rand_idx = torch.zeros(hsy, wsx, 1, device=metric.device, dtype=torch.int64)else:rand_idx = torch.randint(sy*sx, size=(hsy, wsx, 1), device=metric.device)-+# The image might not divide sx and sy, so we need to work on a view of the top left if the idx buffer insteadidx_buffer_view = torch.zeros(hsy, wsx, sy*sx, device=metric.device, dtype=torch.int64)idx_buffer_view.scatter_(dim=2, index=rand_idx, src=-torch.ones_like(rand_idx, dtype=rand_idx.dtype))@@ -99,7 +98,7 @@ def bipartite_soft_matching_random2d(metric: torch.Tensor,def merge(x: torch.Tensor, mode="mean") -> torch.Tensor:src, dst = split(x)n, t1, c = src.shape-+unm = gather(src, dim=-2, index=unm_idx.expand(n, t1 - r, c))src = gather(src, dim=-2, index=src_idx.expand(n, r, c))dst = dst.scatter_reduce(-2, dst_idx.expand(n, r, c), src, reduce=mode)diff --git a/nodes.py b/nodes.pyindex a135a6120..89cecc480 100644--- a/nodes.py+++ b/nodes.py@@ -65,7 +65,7 @@ class CLIPTextEncode(ComfyNodeABC):def encode(self, clip, text):tokens = clip.tokenize(text)return (clip.encode_from_tokens_scheduled(tokens), )-+class ConditioningCombine:@classmethod@@ -641,7 +641,7 @@ class LoraLoader:"strength_clip": ("FLOAT", {"default": 1.0, "min": -100.0, "max": 100.0, "step": 0.01, "tooltip": "How strongly to modify the CLIP model. This value can be negative."}),}}-+RETURN_TYPES = ("MODEL", "CLIP")OUTPUT_TOOLTIPS = ("The modified diffusion model.", "The modified CLIP model.")FUNCTION = "load_lora"@@ -1211,7 +1211,7 @@ class LatentFromBatch:else:s["batch_index"] = samples["batch_index"][batch_index:batch_index + length]return (s,)-+class RepeatLatentBatch:@classmethoddef INPUT_TYPES(s):@@ -1226,7 +1226,7 @@ class RepeatLatentBatch:def repeat(self, samples, amount):s = samples.copy()s_in = samples["samples"]-+s["samples"] = s_in.repeat((amount, 1,1,1))if "noise_mask" in samples and samples["noise_mask"].shape[0] > 1:masks = samples["noise_mask"]@@ -1636,15 +1636,15 @@ class LoadImage:FUNCTION = "load_image"def load_image(self, image):image_path = folder_paths.get_annotated_filepath(image)-+img = node_helpers.pillow(Image.open, image_path)-+output_images = []output_masks = []w, h = None, Noneexcluded_formats = ['MPO']-+for i in ImageSequence.Iterator(img):i = node_helpers.pillow(ImageOps.exif_transpose, i)@@ -1655,10 +1655,10 @@ class LoadImage:if len(output_images) == 0:w = image.size[0]h = image.size[1]-+if image.size[0] != w or image.size[1] != h:continue-+image = np.array(image).astype(np.float32) / 255.0image = torch.from_numpy(image)[None,]if 'A' in i.getbands():@@ -2234,5 +2234,5 @@ def init_extra_nodes(init_custom_nodes=True):else:logging.warning("Please do a: pip install -r requirements.txt")logging.warning("")-+return import_failed