From 836324cd2c5ff362d5d15b09c395ae39134d07d4 Mon Sep 17 00:00:00 2001 From: Seunghoon Lee Date: Tue, 25 Apr 2023 01:55:27 +0900 Subject: [PATCH] Fix PLMS & DPM & DDIM. Unstable and not tested. --- modules/realesrgan_model.py | 2 +- modules/sd_hijack.py | 2 +- modules/sd_hijack_inpainting.py | 2 +- setup.py | 21 +++++++++++++++------ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/modules/realesrgan_model.py b/modules/realesrgan_model.py index ac1102e38..1d48ac42c 100644 --- a/modules/realesrgan_model.py +++ b/modules/realesrgan_model.py @@ -10,7 +10,7 @@ from modules.shared import cmd_opts, opts, device import modules.errors as errors -# DML ISSUE: Some tensors turn 0 after Extended Slices. +# DML Solution: Some tensors turn 0 after Extended Slices. Move output to cpu and get it back. def realesrgan_tile_process_dml_fix(self): import math import torch diff --git a/modules/sd_hijack.py b/modules/sd_hijack.py index cd859a6c3..4a68216df 100644 --- a/modules/sd_hijack.py +++ b/modules/sd_hijack.py @@ -257,7 +257,7 @@ class EmbeddingsWithFixes(torch.nn.Module): for offset, embedding in fixes: emb = devices.cond_cast_unet(embedding.vec) emb_len = min(tensor.shape[0] - offset - 1, emb.shape[0]) - # DML ISSUE: type mismatch on half mode + # DML Solution: type mismatch on half mode if tensor.dtype == torch.float16 and emb.dtype == torch.float32 and not shared.cmd_opts.no_half: emb = emb.half() tensor = torch.cat([tensor[0:offset + 1], emb[0:emb_len], tensor[offset + 1 + emb_len:]]) diff --git a/modules/sd_hijack_inpainting.py b/modules/sd_hijack_inpainting.py index 0c37fef89..3405e8a40 100644 --- a/modules/sd_hijack_inpainting.py +++ b/modules/sd_hijack_inpainting.py @@ -53,7 +53,7 @@ def p_sample_plms(self, x, c, t, index, repeat_noise=False, use_original_steps=F def get_x_prev_and_pred_x0(e_t, index): # select parameters corresponding to the currently considered timestep - print(alphas[index]) # DML ISSUE: PLMS Sampling does not work without this print. + print(alphas[index]) # DML Solution: PLMS Sampling does not work without this print. a_t = torch.full((b, 1, 1, 1), alphas[index], device=device) a_prev = torch.full((b, 1, 1, 1), alphas_prev[index], device=device) sigma_t = torch.full((b, 1, 1, 1), sigmas[index], device=device) diff --git a/setup.py b/setup.py index aa01a3de5..237fd22d7 100644 --- a/setup.py +++ b/setup.py @@ -254,15 +254,24 @@ def install_repositories(): return os.path.join(os.path.dirname(__file__), 'repositories', name) log.info('Installing repositories') os.makedirs(os.path.join(os.path.dirname(__file__), 'repositories'), exist_ok=True) - stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/Stability-AI/stablediffusion.git") # DML TODO: check samplers work well - stable_diffusion_commit = os.environ.get('STABLE_DIFFUSION_COMMIT_HASH', "d4c168b2ad29d82e5fdfea4d598075f40a3b0341") - clone(stable_diffusion_repo, d('stable-diffusion-stability-ai'), stable_diffusion_commit) + try: + import torch_directml + stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/lshqqytiger/stablediffusion-directml.git") # DML Solution: DDIM sampler fix + stable_diffusion_commit = os.environ.get('STABLE_DIFFUSION_COMMIT_HASH', "d4c168b2ad29d82e5fdfea4d598075f40a3b0341") + clone(stable_diffusion_repo, d('stable-diffusion-stability-ai'), stable_diffusion_commit) + k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/lshqqytiger/k-diffusion-directml.git') # DML Solution: DPM fix + k_diffusion_commit = os.environ.get('K_DIFFUSION_COMMIT_HASH', "47b6ef08bca986ff5e72815e74a419ef6616bdbb") + clone(k_diffusion_repo, d('k-diffusion'), k_diffusion_commit) + except: + stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/Stability-AI/stablediffusion.git") + stable_diffusion_commit = os.environ.get('STABLE_DIFFUSION_COMMIT_HASH', "cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf") + clone(stable_diffusion_repo, d('stable-diffusion-stability-ai'), stable_diffusion_commit) + k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/crowsonkb/k-diffusion.git') + k_diffusion_commit = os.environ.get('K_DIFFUSION_COMMIT_HASH', "b43db16749d51055f813255eea2fdf1def801919") + clone(k_diffusion_repo, d('k-diffusion'), k_diffusion_commit) taming_transformers_repo = os.environ.get('TAMING_TRANSFORMERS_REPO', "https://github.com/CompVis/taming-transformers.git") taming_transformers_commit = os.environ.get('TAMING_TRANSFORMERS_COMMIT_HASH', "3ba01b241669f5ade541ce990f7650a3b8f65318") clone(taming_transformers_repo, d('taming-transformers'), taming_transformers_commit) - k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/crowsonkb/k-diffusion.git') # DML TODO: check samplers work well - k_diffusion_commit = os.environ.get('K_DIFFUSION_COMMIT_HASH', "47b6ef08bca986ff5e72815e74a419ef6616bdbb") - clone(k_diffusion_repo, d('k-diffusion'), k_diffusion_commit) codeformer_repo = os.environ.get('CODEFORMER_REPO', 'https://github.com/sczhou/CodeFormer.git') codeformer_commit = os.environ.get('CODEFORMER_COMMIT_HASH', "c5b4593074ba6214284d6acd5f1719b6c5d739af") clone(codeformer_repo, d('CodeFormer'), codeformer_commit)