This commit is contained in:
lllyasviel
2023-10-12 04:23:10 -07:00
committed by GitHub
parent 4c867c1b8b
commit e61aac34ca
147 changed files with 523 additions and 642 deletions
+3 -3
View File
@@ -20,7 +20,7 @@ def worker():
import modules.flags as flags
import modules.path
import modules.patch
import comfy.model_management
import fcbh.model_management
import fooocus_extras.preprocessors as preprocessors
import modules.inpaint_worker as inpaint_worker
import modules.advanced_parameters as advanced_parameters
@@ -483,7 +483,7 @@ def worker():
outputs.append(['preview', (13, 'Moving model to GPU ...', None)])
execution_start_time = time.perf_counter()
comfy.model_management.load_models_gpu([pipeline.final_unet])
fcbh.model_management.load_models_gpu([pipeline.final_unet])
moving_time = time.perf_counter() - execution_start_time
print(f'Moving model to GPU: {moving_time:.2f} seconds')
@@ -558,7 +558,7 @@ def worker():
log(x, d, single_line_number=3)
results += imgs
except comfy.model_management.InterruptProcessingException as e:
except fcbh.model_management.InterruptProcessingException as e:
if shared.last_stop == 'skip':
print('User skipped')
continue
+21 -21
View File
@@ -8,22 +8,22 @@ import einops
import torch
import numpy as np
import comfy.model_management
import comfy.model_detection
import comfy.model_patcher
import comfy.utils
import comfy.controlnet
import fcbh.model_management
import fcbh.model_detection
import fcbh.model_patcher
import fcbh.utils
import fcbh.controlnet
import modules.sample_hijack
import comfy.samplers
import comfy.latent_formats
import fcbh.samplers
import fcbh.latent_formats
from comfy.sd import load_checkpoint_guess_config
from fcbh.sd import load_checkpoint_guess_config
from nodes import VAEDecode, EmptyLatentImage, VAEEncode, VAEEncodeTiled, VAEDecodeTiled, VAEEncodeForInpaint, \
ControlNetApplyAdvanced
from comfy_extras.nodes_freelunch import FreeU
from comfy.sample import prepare_mask
from fcbh_extras.nodes_freelunch import FreeU
from fcbh.sample import prepare_mask
from modules.patch import patched_sampler_cfg_function, patched_model_function_wrapper
from comfy.lora import model_lora_keys_unet, model_lora_keys_clip, load_lora
from fcbh.lora import model_lora_keys_unet, model_lora_keys_clip, load_lora
opEmptyLatentImage = EmptyLatentImage()
@@ -53,7 +53,7 @@ def apply_freeu(model, b1, b2, s1, s2):
@torch.no_grad()
@torch.inference_mode()
def load_controlnet(ckpt_filename):
return comfy.controlnet.load_controlnet(ckpt_filename)
return fcbh.controlnet.load_controlnet(ckpt_filename)
@torch.no_grad()
@@ -78,7 +78,7 @@ def load_sd_lora(model, lora_filename, strength_model=1.0, strength_clip=1.0):
if strength_model == 0 and strength_clip == 0:
return model
lora = comfy.utils.load_torch_file(lora_filename, safe_load=False)
lora = fcbh.utils.load_torch_file(lora_filename, safe_load=False)
if lora_filename.lower().endswith('.fooocus.patch'):
loaded = lora
@@ -164,7 +164,7 @@ def get_previewer(model):
global VAE_approx_models
from modules.path import vae_approx_path
is_sdxl = isinstance(model.model.latent_format, comfy.latent_formats.SDXL)
is_sdxl = isinstance(model.model.latent_format, fcbh.latent_formats.SDXL)
vae_approx_filename = os.path.join(vae_approx_path, 'xlvaeapp.pth' if is_sdxl else 'vaeapp_sd15.pth')
if vae_approx_filename in VAE_approx_models:
@@ -176,14 +176,14 @@ def get_previewer(model):
del sd
VAE_approx_model.eval()
if comfy.model_management.should_use_fp16():
if fcbh.model_management.should_use_fp16():
VAE_approx_model.half()
VAE_approx_model.current_type = torch.float16
else:
VAE_approx_model.float()
VAE_approx_model.current_type = torch.float32
VAE_approx_model.to(comfy.model_management.get_torch_device())
VAE_approx_model.to(fcbh.model_management.get_torch_device())
VAE_approx_models[vae_approx_filename] = VAE_approx_model
@torch.no_grad()
@@ -207,14 +207,14 @@ def ksampler(model, positive, negative, latent, seed=None, steps=30, cfg=7.0, sa
previewer_start=None, previewer_end=None, sigmas=None):
if sigmas is not None:
sigmas = sigmas.clone().to(comfy.model_management.get_torch_device())
sigmas = sigmas.clone().to(fcbh.model_management.get_torch_device())
latent_image = latent["samples"]
if disable_noise:
noise = torch.zeros(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, device="cpu")
else:
batch_inds = latent["batch_index"] if "batch_index" in latent else None
noise = comfy.sample.prepare_noise(latent_image, seed, batch_inds)
noise = fcbh.sample.prepare_noise(latent_image, seed, batch_inds)
noise_mask = None
if "noise_mask" in latent:
@@ -229,7 +229,7 @@ def ksampler(model, positive, negative, latent, seed=None, steps=30, cfg=7.0, sa
previewer_end = steps
def callback(step, x0, x, total_steps):
comfy.model_management.throw_exception_if_processing_interrupted()
fcbh.model_management.throw_exception_if_processing_interrupted()
y = None
if previewer is not None:
y = previewer(x0, previewer_start + step, previewer_end)
@@ -239,10 +239,10 @@ def ksampler(model, positive, negative, latent, seed=None, steps=30, cfg=7.0, sa
disable_pbar = False
modules.sample_hijack.current_refiner = refiner
modules.sample_hijack.refiner_switch_step = refiner_switch
comfy.samplers.sample = modules.sample_hijack.sample_hacked
fcbh.samplers.sample = modules.sample_hijack.sample_hacked
try:
samples = comfy.sample.sample(model, noise, steps, cfg, sampler_name, scheduler, positive, negative, latent_image,
samples = fcbh.sample.sample(model, noise, steps, cfg, sampler_name, scheduler, positive, negative, latent_image,
denoise=denoise, disable_noise=disable_noise, start_step=start_step,
last_step=last_step,
force_full_denoise=force_full_denoise, noise_mask=noise_mask, callback=callback,
+6 -6
View File
@@ -2,11 +2,11 @@ import modules.core as core
import os
import torch
import modules.path
import comfy.model_management
import comfy.latent_formats
import fcbh.model_management
import fcbh.latent_formats
import modules.inpaint_worker
from comfy.model_base import SDXL, SDXLRefiner
from fcbh.model_base import SDXL, SDXLRefiner
from modules.expansion import FooocusExpansion
from modules.sample_hijack import clip_separate
@@ -211,7 +211,7 @@ def prepare_text_encoder(async_call=True):
# TODO: make sure that this is always called in an async way so that users cannot feel it.
pass
assert_model_integrity()
comfy.model_management.load_models_gpu([final_clip.patcher, final_expansion.patcher])
fcbh.model_management.load_models_gpu([final_clip.patcher, final_expansion.patcher])
return
@@ -284,7 +284,7 @@ def vae_parse(x, tiled=False, use_interpose=True):
@torch.no_grad()
@torch.inference_mode()
def calculate_sigmas_all(sampler, model, scheduler, steps):
from comfy.samplers import calculate_sigmas_scheduler
from fcbh.samplers import calculate_sigmas_scheduler
discard_penultimate_sigma = False
if sampler in ['dpm_2', 'dpm_2_ancestral']:
@@ -316,7 +316,7 @@ def process_diffusion(positive_cond, negative_cond, steps, switch, width, height
assert refiner_swap_method in ['joint', 'separate', 'vae', 'upscale']
if final_refiner_unet is not None:
if isinstance(final_refiner_unet.model.latent_format, comfy.latent_formats.SD15) \
if isinstance(final_refiner_unet.model.latent_format, fcbh.latent_formats.SD15) \
and refiner_swap_method != 'upscale':
refiner_swap_method = 'vae'
+2 -2
View File
@@ -1,10 +1,10 @@
import torch
import comfy.model_management as model_management
import fcbh.model_management as model_management
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
from modules.path import fooocus_expansion_path
from comfy.model_patcher import ModelPatcher
from fcbh.model_patcher import ModelPatcher
fooocus_magic_split = [
-43
View File
@@ -22,49 +22,6 @@ index_url = os.environ.get('INDEX_URL', "")
modules_path = os.path.dirname(os.path.realpath(__file__))
script_path = os.path.dirname(modules_path)
dir_repos = "repositories"
def onerror(func, path, exc_info):
import stat
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise 'Failed to invoke "shutil.rmtree", git management failed.'
def git_clone(url, dir, name, hash=None):
try:
try:
repo = pygit2.Repository(dir)
remote_url = repo.remotes['origin'].url
if remote_url != url:
print(f'{name} exists but remote URL will be updated.')
del repo
raise url
else:
print(f'{name} exists and URL is correct.')
except:
if os.path.isdir(dir) or os.path.exists(dir):
shutil.rmtree(dir, onerror=onerror)
os.makedirs(dir, exist_ok=True)
repo = pygit2.clone_repository(url, dir)
print(f'{name} cloned from {url}.')
if hash is not None:
remote = repo.remotes['origin']
remote.fetch()
commit = repo.get(hash)
repo.checkout_tree(commit, strategy=pygit2.GIT_CHECKOUT_FORCE)
repo.set_head(commit.id)
print(f'{name} checkout finished for {hash}.')
except Exception as e:
print(f'Git clone failed for {name}: {str(e)}')
def repo_dir(name):
return os.path.join(script_path, dir_repos, name)
def is_installed(package):
+63 -63
View File
@@ -1,27 +1,27 @@
import torch
import comfy.model_base
import comfy.ldm.modules.diffusionmodules.openaimodel
import comfy.samplers
import comfy.k_diffusion.external
import comfy.model_management
import fcbh.model_base
import fcbh.ldm.modules.diffusionmodules.openaimodel
import fcbh.samplers
import fcbh.k_diffusion.external
import fcbh.model_management
import modules.anisotropic as anisotropic
import comfy.ldm.modules.attention
import comfy.k_diffusion.sampling
import comfy.sd1_clip
import fcbh.ldm.modules.attention
import fcbh.k_diffusion.sampling
import fcbh.sd1_clip
import modules.inpaint_worker as inpaint_worker
import comfy.ldm.modules.diffusionmodules.openaimodel
import comfy.ldm.modules.diffusionmodules.model
import comfy.sd
import comfy.cldm.cldm
import comfy.model_patcher
import comfy.samplers
import comfy.cli_args
import fcbh.ldm.modules.diffusionmodules.openaimodel
import fcbh.ldm.modules.diffusionmodules.model
import fcbh.sd
import fcbh.cldm.cldm
import fcbh.model_patcher
import fcbh.samplers
import fcbh.cli_args
import args_manager
import modules.advanced_parameters as advanced_parameters
from comfy.k_diffusion import utils
from comfy.k_diffusion.sampling import BrownianTreeNoiseSampler, trange
from comfy.ldm.modules.diffusionmodules.openaimodel import timestep_embedding, forward_timestep_embed
from fcbh.k_diffusion import utils
from fcbh.k_diffusion.sampling import BrownianTreeNoiseSampler, trange
from fcbh.ldm.modules.diffusionmodules.openaimodel import timestep_embedding, forward_timestep_embed
sharpness = 2.0
@@ -54,26 +54,26 @@ def calculate_weight_patched(self, patches, weight, key):
if w1.shape != weight.shape:
print("WARNING SHAPE MISMATCH {} WEIGHT NOT MERGED {} != {}".format(key, w1.shape, weight.shape))
else:
weight += alpha * comfy.model_management.cast_to_device(w1, weight.device, weight.dtype)
weight += alpha * fcbh.model_management.cast_to_device(w1, weight.device, weight.dtype)
elif len(v) == 3:
# fooocus
w1 = comfy.model_management.cast_to_device(v[0], weight.device, torch.float32)
w_min = comfy.model_management.cast_to_device(v[1], weight.device, torch.float32)
w_max = comfy.model_management.cast_to_device(v[2], weight.device, torch.float32)
w1 = fcbh.model_management.cast_to_device(v[0], weight.device, torch.float32)
w_min = fcbh.model_management.cast_to_device(v[1], weight.device, torch.float32)
w_max = fcbh.model_management.cast_to_device(v[2], weight.device, torch.float32)
w1 = (w1 / 255.0) * (w_max - w_min) + w_min
if alpha != 0.0:
if w1.shape != weight.shape:
print("WARNING SHAPE MISMATCH {} FOOOCUS WEIGHT NOT MERGED {} != {}".format(key, w1.shape, weight.shape))
else:
weight += alpha * comfy.model_management.cast_to_device(w1, weight.device, weight.dtype)
weight += alpha * fcbh.model_management.cast_to_device(w1, weight.device, weight.dtype)
elif len(v) == 4: # lora/locon
mat1 = comfy.model_management.cast_to_device(v[0], weight.device, torch.float32)
mat2 = comfy.model_management.cast_to_device(v[1], weight.device, torch.float32)
mat1 = fcbh.model_management.cast_to_device(v[0], weight.device, torch.float32)
mat2 = fcbh.model_management.cast_to_device(v[1], weight.device, torch.float32)
if v[2] is not None:
alpha *= v[2] / mat2.shape[0]
if v[3] is not None:
# locon mid weights, hopefully the math is fine because I didn't properly test it
mat3 = comfy.model_management.cast_to_device(v[3], weight.device, torch.float32)
mat3 = fcbh.model_management.cast_to_device(v[3], weight.device, torch.float32)
final_shape = [mat2.shape[1], mat2.shape[0], mat3.shape[2], mat3.shape[3]]
mat2 = torch.mm(mat2.transpose(0, 1).flatten(start_dim=1),
mat3.transpose(0, 1).flatten(start_dim=1)).reshape(final_shape).transpose(0, 1)
@@ -94,23 +94,23 @@ def calculate_weight_patched(self, patches, weight, key):
if w1 is None:
dim = w1_b.shape[0]
w1 = torch.mm(comfy.model_management.cast_to_device(w1_a, weight.device, torch.float32),
comfy.model_management.cast_to_device(w1_b, weight.device, torch.float32))
w1 = torch.mm(fcbh.model_management.cast_to_device(w1_a, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w1_b, weight.device, torch.float32))
else:
w1 = comfy.model_management.cast_to_device(w1, weight.device, torch.float32)
w1 = fcbh.model_management.cast_to_device(w1, weight.device, torch.float32)
if w2 is None:
dim = w2_b.shape[0]
if t2 is None:
w2 = torch.mm(comfy.model_management.cast_to_device(w2_a, weight.device, torch.float32),
comfy.model_management.cast_to_device(w2_b, weight.device, torch.float32))
w2 = torch.mm(fcbh.model_management.cast_to_device(w2_a, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w2_b, weight.device, torch.float32))
else:
w2 = torch.einsum('i j k l, j r, i p -> p r k l',
comfy.model_management.cast_to_device(t2, weight.device, torch.float32),
comfy.model_management.cast_to_device(w2_b, weight.device, torch.float32),
comfy.model_management.cast_to_device(w2_a, weight.device, torch.float32))
fcbh.model_management.cast_to_device(t2, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w2_b, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w2_a, weight.device, torch.float32))
else:
w2 = comfy.model_management.cast_to_device(w2, weight.device, torch.float32)
w2 = fcbh.model_management.cast_to_device(w2, weight.device, torch.float32)
if len(w2.shape) == 4:
w1 = w1.unsqueeze(2).unsqueeze(2)
@@ -132,19 +132,19 @@ def calculate_weight_patched(self, patches, weight, key):
t1 = v[5]
t2 = v[6]
m1 = torch.einsum('i j k l, j r, i p -> p r k l',
comfy.model_management.cast_to_device(t1, weight.device, torch.float32),
comfy.model_management.cast_to_device(w1b, weight.device, torch.float32),
comfy.model_management.cast_to_device(w1a, weight.device, torch.float32))
fcbh.model_management.cast_to_device(t1, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w1b, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w1a, weight.device, torch.float32))
m2 = torch.einsum('i j k l, j r, i p -> p r k l',
comfy.model_management.cast_to_device(t2, weight.device, torch.float32),
comfy.model_management.cast_to_device(w2b, weight.device, torch.float32),
comfy.model_management.cast_to_device(w2a, weight.device, torch.float32))
fcbh.model_management.cast_to_device(t2, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w2b, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w2a, weight.device, torch.float32))
else:
m1 = torch.mm(comfy.model_management.cast_to_device(w1a, weight.device, torch.float32),
comfy.model_management.cast_to_device(w1b, weight.device, torch.float32))
m2 = torch.mm(comfy.model_management.cast_to_device(w2a, weight.device, torch.float32),
comfy.model_management.cast_to_device(w2b, weight.device, torch.float32))
m1 = torch.mm(fcbh.model_management.cast_to_device(w1a, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w1b, weight.device, torch.float32))
m2 = torch.mm(fcbh.model_management.cast_to_device(w2a, weight.device, torch.float32),
fcbh.model_management.cast_to_device(w2b, weight.device, torch.float32))
try:
weight += (alpha * m1 * m2).reshape(weight.shape).type(weight.dtype)
@@ -205,7 +205,7 @@ def patched_model_function_wrapper(func, args):
def sdxl_encode_adm_patched(self, **kwargs):
global positive_adm_scale, negative_adm_scale
clip_pooled = comfy.model_base.sdxl_pooled(kwargs, self.noise_augmentor)
clip_pooled = fcbh.model_base.sdxl_pooled(kwargs, self.noise_augmentor)
width = kwargs.get("width", 768)
height = kwargs.get("height", 768)
target_width = width
@@ -453,8 +453,8 @@ def patched_unet_forward(self, x, timesteps=None, context=None, y=None, control=
def text_encoder_device_patched():
# Fooocus's style system uses text encoder much more times than comfy so this makes things much faster.
return comfy.model_management.get_torch_device()
# Fooocus's style system uses text encoder much more times than fcbh so this makes things much faster.
return fcbh.model_management.get_torch_device()
def patched_get_autocast_device(dev):
@@ -470,25 +470,25 @@ def patched_get_autocast_device(dev):
def patch_all():
if not comfy.model_management.DISABLE_SMART_MEMORY:
vram_inadequate = comfy.model_management.total_vram < 20 * 1024
is_old_gpu_arch = not comfy.model_management.should_use_fp16()
if not fcbh.model_management.DISABLE_SMART_MEMORY:
vram_inadequate = fcbh.model_management.total_vram < 20 * 1024
is_old_gpu_arch = not fcbh.model_management.should_use_fp16()
if vram_inadequate or is_old_gpu_arch:
# https://github.com/lllyasviel/Fooocus/issues/602
print(f'[Fooocus Smart Memory] Disabling smart memory, '
f'vram_inadequate = {vram_inadequate}, is_old_gpu_arch = {is_old_gpu_arch}.')
comfy.model_management.DISABLE_SMART_MEMORY = True
fcbh.model_management.DISABLE_SMART_MEMORY = True
args_manager.args.disable_smart_memory = True
comfy.cli_args.args.disable_smart_memory = True
fcbh.cli_args.args.disable_smart_memory = True
comfy.model_management.get_autocast_device = patched_get_autocast_device
comfy.samplers.SAMPLER_NAMES += ['dpmpp_fooocus_2m_sde_inpaint_seamless']
comfy.model_management.text_encoder_device = text_encoder_device_patched
comfy.model_patcher.ModelPatcher.calculate_weight = calculate_weight_patched
comfy.cldm.cldm.ControlNet.forward = patched_cldm_forward
comfy.ldm.modules.diffusionmodules.openaimodel.UNetModel.forward = patched_unet_forward
comfy.k_diffusion.sampling.sample_dpmpp_fooocus_2m_sde_inpaint_seamless = sample_dpmpp_fooocus_2m_sde_inpaint_seamless
comfy.k_diffusion.external.DiscreteEpsDDPMDenoiser.forward = patched_discrete_eps_ddpm_denoiser_forward
comfy.model_base.SDXL.encode_adm = sdxl_encode_adm_patched
comfy.sd1_clip.ClipTokenWeightEncoder.encode_token_weights = encode_token_weights_patched_with_a1111_method
fcbh.model_management.get_autocast_device = patched_get_autocast_device
fcbh.samplers.SAMPLER_NAMES += ['dpmpp_fooocus_2m_sde_inpaint_seamless']
fcbh.model_management.text_encoder_device = text_encoder_device_patched
fcbh.model_patcher.ModelPatcher.calculate_weight = calculate_weight_patched
fcbh.cldm.cldm.ControlNet.forward = patched_cldm_forward
fcbh.ldm.modules.diffusionmodules.openaimodel.UNetModel.forward = patched_unet_forward
fcbh.k_diffusion.sampling.sample_dpmpp_fooocus_2m_sde_inpaint_seamless = sample_dpmpp_fooocus_2m_sde_inpaint_seamless
fcbh.k_diffusion.external.DiscreteEpsDDPMDenoiser.forward = patched_discrete_eps_ddpm_denoiser_forward
fcbh.model_base.SDXL.encode_adm = sdxl_encode_adm_patched
fcbh.sd1_clip.ClipTokenWeightEncoder.encode_token_weights = encode_token_weights_patched_with_a1111_method
return
+7 -7
View File
@@ -1,10 +1,10 @@
import torch
import comfy.samplers
import comfy.model_management
import fcbh.samplers
import fcbh.model_management
from comfy.model_base import SDXLRefiner, SDXL
from comfy.sample import get_additional_models
from comfy.samplers import resolve_areas_and_cond_masks, wrap_model, calculate_start_end_timesteps, \
from fcbh.model_base import SDXLRefiner, SDXL
from fcbh.sample import get_additional_models
from fcbh.samplers import resolve_areas_and_cond_masks, wrap_model, calculate_start_end_timesteps, \
create_cond_with_same_area_if_none, pre_run_control, apply_empty_x_to_equal_area, encode_adm, \
blank_inpaint_image_like
@@ -119,7 +119,7 @@ def sample_hacked(model, noise, positive, negative, cfg, device, sampler, sigmas
extra_args['model_options'] = {k: {} if k == 'transformer_options' else v for k, v in extra_args['model_options'].items()}
models, inference_memory = get_additional_models(positive_refiner, negative_refiner, current_refiner.model_dtype())
comfy.model_management.load_models_gpu([current_refiner] + models, comfy.model_management.batch_area_memory(
fcbh.model_management.load_models_gpu([current_refiner] + models, fcbh.model_management.batch_area_memory(
noise.shape[0] * noise.shape[2] * noise.shape[3]) + inference_memory)
model_wrap.inner_model.inner_model = current_refiner.model
@@ -136,4 +136,4 @@ def sample_hacked(model, noise, positive, negative, cfg, device, sampler, sigmas
return model.process_latent_out(samples.to(torch.float32))
comfy.samplers.sample = sample_hacked
fcbh.samplers.sample = sample_hacked
+2 -2
View File
@@ -1,8 +1,8 @@
import os
import torch
from comfy_extras.chainner_models.architecture.RRDB import RRDBNet as ESRGAN
from comfy_extras.nodes_upscale_model import ImageUpscaleWithModel
from fcbh_extras.chainner_models.architecture.RRDB import RRDBNet as ESRGAN
from fcbh_extras.nodes_upscale_model import ImageUpscaleWithModel
from collections import OrderedDict
from modules.path import upscale_models_path