mirror of
https://github.com/vladmandic/automatic
synced 2026-09-19 17:24:32 +02:00
overal quality fixes
This commit is contained in:
@@ -8,6 +8,8 @@ if sys.platform == "darwin":
|
||||
from modules import mac_specific # pylint: disable=ungrouped-imports
|
||||
|
||||
cuda_ok = torch.cuda.is_available()
|
||||
previous_oom = 0
|
||||
|
||||
|
||||
def has_mps() -> bool:
|
||||
if sys.platform != "darwin":
|
||||
@@ -15,6 +17,7 @@ def has_mps() -> bool:
|
||||
else:
|
||||
return mac_specific.has_mps
|
||||
|
||||
|
||||
def extract_device_id(args, name): # pylint: disable=redefined-outer-name
|
||||
for x in range(len(args)):
|
||||
if name in args[x]:
|
||||
@@ -61,6 +64,18 @@ def get_device_for(task):
|
||||
|
||||
|
||||
def torch_gc(force=False):
|
||||
mem = memstats.memory_stats()
|
||||
gpu = mem.get('gpu', {})
|
||||
oom = gpu.get('oom', 0)
|
||||
used = round(100 * gpu.get('used', 0) / gpu.get('total', 1))
|
||||
global previous_oom # pylint: disable=global-statement
|
||||
if oom > previous_oom:
|
||||
previous_oom = oom
|
||||
shared.log.warning(f'GPU out-of-memory error: {mem}')
|
||||
if used > 90:
|
||||
shared.log.warning(f'GPU high memory utilization: {used}% {mem}')
|
||||
force = True
|
||||
|
||||
if shared.opts.disable_gc and not force:
|
||||
return
|
||||
collected = gc.collect()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import diffusers
|
||||
from modules import shared
|
||||
|
||||
lora_state = { # TODO this is ugly but diffusers
|
||||
lora_state = { # TODO Lora state for Diffusers
|
||||
'multiplier': 1.0,
|
||||
'active': False,
|
||||
'loaded': 0,
|
||||
|
||||
@@ -30,7 +30,10 @@ def download_diffusers_model(hub_id: str, cache_dir: str = None, download_config
|
||||
shared.log.debug(f"Diffusers authentication: {token}")
|
||||
hf.login(token)
|
||||
pipeline_dir = DiffusionPipeline.download(hub_id, **download_config)
|
||||
model_info_dict = hf.model_info(hub_id).cardData # TODO hfhub card-data?
|
||||
try:
|
||||
model_info_dict = hf.model_info(hub_id).cardData # TODO HF-Hub cardData invalid property
|
||||
except Exception:
|
||||
model_info_dict = None
|
||||
# some checkpoints need to be downloaded as "hidden" as they just serve as pre- or post-pipelines of other pipelines
|
||||
if model_info_dict is not None and "prior" in model_info_dict:
|
||||
download_dir = DiffusionPipeline.download(model_info_dict["prior"], **download_config)
|
||||
|
||||
@@ -174,7 +174,6 @@ class DDPM(pl.LightningModule):
|
||||
lvlb_weights = 0.5 * np.sqrt(torch.Tensor(alphas_cumprod)) / (2. * 1 - torch.Tensor(alphas_cumprod))
|
||||
else:
|
||||
raise NotImplementedError("mu not supported")
|
||||
# TODO how to choose this term
|
||||
lvlb_weights[0] = lvlb_weights[1]
|
||||
self.register_buffer('lvlb_weights', lvlb_weights, persistent=False)
|
||||
assert not torch.isnan(self.lvlb_weights).all()
|
||||
@@ -887,7 +886,7 @@ class LatentDiffusion(DDPM):
|
||||
assert c is not None
|
||||
if self.cond_stage_trainable:
|
||||
c = self.get_learned_conditioning(c)
|
||||
if self.shorten_cond_schedule: # TODO: drop this option
|
||||
if self.shorten_cond_schedule:
|
||||
tc = self.cond_ids[t].to(self.device)
|
||||
c = self.q_sample(x_start=c, t=tc, noise=torch.randn_like(c.float()))
|
||||
return self.p_losses(x, c, t, *args, **kwargs)
|
||||
@@ -1430,7 +1429,6 @@ class DiffusionWrapper(pl.LightningModule):
|
||||
|
||||
|
||||
class Layout2ImgDiffusion(LatentDiffusion):
|
||||
# TODO: move all layout-specific hacks to this class
|
||||
def __init__(self, cond_stage_key, *args, **kwargs):
|
||||
assert cond_stage_key == 'coordinates_bbox', 'Layout2ImgDiffusion only for cond_stage_key="coordinates_bbox"'
|
||||
super().__init__(*args, cond_stage_key=cond_stage_key, **kwargs)
|
||||
|
||||
+11
-10
@@ -447,24 +447,23 @@ def create_infotext(p: StableDiffusionProcessing, all_prompts, all_seeds, all_su
|
||||
if uses_ensd:
|
||||
uses_ensd = sd_samplers_common.is_sampler_using_eta_noise_seed_delta(p)
|
||||
|
||||
|
||||
generation_params = {
|
||||
"Steps": p.steps,
|
||||
"Sampler": p.sampler_name,
|
||||
"CFG scale": p.cfg_scale,
|
||||
"Image CFG scale": getattr(p, 'image_cfg_scale', None),
|
||||
"Seed": all_seeds[index],
|
||||
"Face restoration": (opts.face_restoration_model if p.restore_faces else None),
|
||||
"Face restoration": opts.face_restoration_model if p.restore_faces else None,
|
||||
"Size": f"{p.width}x{p.height}",
|
||||
"Model hash": getattr(p, 'sd_model_hash', None if not opts.add_model_hash_to_info or not shared.sd_model.sd_model_hash else shared.sd_model.sd_model_hash),
|
||||
"Model": (None if not opts.add_model_name_to_info or not shared.sd_model.sd_checkpoint_info.model_name else shared.sd_model.sd_checkpoint_info.model_name.replace(',', '').replace(':', '')),
|
||||
"VAE": (None if not opts.add_model_name_to_info or sd_vae.loaded_vae_file is None else os.path.splitext(os.path.basename(sd_vae.loaded_vae_file))[0]),
|
||||
"Variation seed": (None if p.subseed_strength == 0 else all_subseeds[index]),
|
||||
"Variation seed strength": (None if p.subseed_strength == 0 else p.subseed_strength),
|
||||
"Seed resize from": (None if p.seed_resize_from_w == 0 or p.seed_resize_from_h == 0 else f"{p.seed_resize_from_w}x{p.seed_resize_from_h}"),
|
||||
"Model": None if not opts.add_model_name_to_info or not shared.sd_model.sd_checkpoint_info.model_name else shared.sd_model.sd_checkpoint_info.model_name.replace(',', '').replace(':', ''),
|
||||
"VAE": None if not opts.add_model_name_to_info or sd_vae.loaded_vae_file is None else os.path.splitext(os.path.basename(sd_vae.loaded_vae_file))[0],
|
||||
"Variation seed": None if p.subseed_strength == 0 else all_subseeds[index],
|
||||
"Variation seed strength": None if p.subseed_strength == 0 else p.subseed_strength,
|
||||
"Seed resize from": None if p.seed_resize_from_w == 0 or p.seed_resize_from_h == 0 else f"{p.seed_resize_from_w}x{p.seed_resize_from_h}",
|
||||
"Denoising strength": getattr(p, 'denoising_strength', None),
|
||||
"Conditional mask weight": getattr(p, "inpainting_mask_weight", shared.opts.inpainting_mask_weight) if p.is_using_inpainting_conditioning else None,
|
||||
"Clip skip": p.clip_skip,
|
||||
"Clip skip": p.clip_skip if p.clip_skip > 1 else None,
|
||||
"ENSD": opts.eta_noise_seed_delta if uses_ensd else None,
|
||||
"Init image hash": getattr(p, 'init_img_hash', None),
|
||||
"Version": git_commit,
|
||||
@@ -705,7 +704,9 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed:
|
||||
# TODO(PVP): change out to latents once possible with `diffusers`
|
||||
task_specific_kwargs = {"image": p.init_images[0], "mask_image": p.image_mask, "strength": p.denoising_strength}
|
||||
|
||||
def diffusers_callback(step: int, _timestep: int, latents: torch.FloatTensor): # TODO simplified callback for now
|
||||
# TODO Diffusers limited callbacks
|
||||
# TODO Diffusers processing is not using p.sample so second pass is ignored
|
||||
def diffusers_callback(step: int, _timestep: int, latents: torch.FloatTensor):
|
||||
shared.state.sampling_step = step
|
||||
shared.state.sampling_steps = p.steps
|
||||
shared.state.current_latent = latents
|
||||
@@ -728,9 +729,9 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed:
|
||||
# shared.sd_model.to('cpu')
|
||||
# devices.torch_gc(force=True)
|
||||
|
||||
|
||||
if shared.sd_refiner is not None:
|
||||
# shared.sd_refiner.to(devices.device)
|
||||
devices.torch_gc()
|
||||
init_image = output.images[0]
|
||||
output = shared.sd_refiner( # pylint: disable=not-callable
|
||||
prompt=prompts,
|
||||
|
||||
@@ -59,6 +59,8 @@ class CheckpointInfo:
|
||||
self.sha256 = hashes.sha256_from_cache(self.filename, f"checkpoint/{name}")
|
||||
self.path = abspath
|
||||
self.type = abspath.split('.')[-1].lower()
|
||||
self.name_for_extra = os.path.splitext(os.path.basename(filename))[0]
|
||||
self.model_name = os.path.splitext(name.replace("/", "_").replace("\\", "_"))[0]
|
||||
else: # maybe a diffuser
|
||||
repo = [r for r in modelloader.diffuser_repos if filename == r['filename']]
|
||||
if len(repo) == 0:
|
||||
@@ -70,16 +72,13 @@ class CheckpointInfo:
|
||||
self.sha256 = repo[0]['hash']
|
||||
self.path = repo[0]['path']
|
||||
self.type = 'diffusers'
|
||||
|
||||
self.name_for_extra = repo[0]['name']
|
||||
self.model_name = repo[0]['name']
|
||||
if os.path.isfile(repo[0]['model_info']):
|
||||
file_path = repo[0]['model_info']
|
||||
with open(file_path, "r", encoding="utf-8") as json_file:
|
||||
self.model_info = json.load(json_file)
|
||||
else:
|
||||
self.model_info = None
|
||||
|
||||
self.name_for_extra = os.path.splitext(os.path.basename(filename))[0]
|
||||
self.model_name = os.path.splitext(name.replace("/", "_").replace("\\", "_"))[0]
|
||||
self.shorthash = self.sha256[0:10] if self.sha256 else None
|
||||
self.title = self.name if self.shorthash is None else f'{self.name} [{self.shorthash}]'
|
||||
self.ids = [self.hash, self.model_name, self.title, self.name, f'{self.name} [{self.hash}]'] + ([self.shorthash, self.sha256, f'{self.name} [{self.shorthash}]'] if self.shorthash else [])
|
||||
@@ -161,7 +160,7 @@ def list_models():
|
||||
model_list = modelloader.load_models(model_path=model_path, model_url=model_url, command_path=shared.opts.ckpt_dir, ext_filter=[".ckpt", ".safetensors"], download_name="v1-5-pruned-emaonly.safetensors", ext_blacklist=[".vae.ckpt", ".vae.safetensors"])
|
||||
else:
|
||||
default_model_id = "runwayml/stable-diffusion-v1-5"
|
||||
modelloader.download_diffusers_model(default_model_id, os.path.join(models_path, 'Diffusers'))
|
||||
modelloader.download_diffusers_model(default_model_id, shared.opts.diffusers_dir)
|
||||
model_list = modelloader.load_diffusers_models(model_path=os.path.join(models_path, 'Diffusers'), command_path=shared.opts.diffusers_dir)
|
||||
|
||||
for filename in sorted(model_list, key=str.lower):
|
||||
@@ -665,6 +664,8 @@ def load_diffuser(checkpoint_info=None, already_loaded_state_dict=None, timer=No
|
||||
else:
|
||||
shared.log.error(f'Diffusers cannot load safetensor model: {checkpoint_info.path} {shared.opts.diffusers_pipeline}')
|
||||
return
|
||||
if sd_model is not None:
|
||||
shared.log.debug(f'Diffusers pipeline: {type(sd_model)}') # pylint: disable=protected-access
|
||||
except Exception as e:
|
||||
shared.log.error(f'Diffusers failed loading model using pipeline: {checkpoint_info.path} {shared.opts.diffusers_pipeline} {e}')
|
||||
return
|
||||
|
||||
@@ -219,5 +219,5 @@ def create_ui():
|
||||
hf_results.select(fn=hf_select, inputs=[hf_results], outputs=[hf_selected])
|
||||
hf_download_model_btn.click(fn=hf_download_model, inputs=[hf_selected, hf_token], outputs=[models_outcome])
|
||||
|
||||
with gr.Tab(label="CivitAI"):
|
||||
pass
|
||||
# with gr.Tab(label="CivitAI"):
|
||||
# pass
|
||||
|
||||
Reference in New Issue
Block a user