From 2cb1b65309256763814a006a07b683c0f1013a30 Mon Sep 17 00:00:00 2001 From: Kohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com> Date: Sun, 28 Jan 2024 22:18:46 +0800 Subject: [PATCH 01/10] Bump safetensors' version to 0.4.2 --- requirements_versions.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_versions.txt b/requirements_versions.txt index 2a922f28..5e30b5ea 100644 --- a/requirements_versions.txt +++ b/requirements_versions.txt @@ -19,7 +19,7 @@ piexif==1.1.3 psutil==5.9.5 pytorch_lightning==1.9.4 resize-right==0.0.2 -safetensors==0.3.1 +safetensors==0.4.2 scikit-image==0.21.0 spandrel==0.1.6 tomesd==0.1.3 From baaf39b6f92f24275a1b264a634514bac571dfae Mon Sep 17 00:00:00 2001 From: AUTOMATIC1111 <16777216c@gmail.com> Date: Mon, 29 Jan 2024 10:20:27 +0300 Subject: [PATCH 02/10] fix the typo -- thanks Cyberbeing --- modules/sd_samplers_cfg_denoiser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/sd_samplers_cfg_denoiser.py b/modules/sd_samplers_cfg_denoiser.py index ef237396..941dff4b 100644 --- a/modules/sd_samplers_cfg_denoiser.py +++ b/modules/sd_samplers_cfg_denoiser.py @@ -94,7 +94,7 @@ class CFGDenoiser(torch.nn.Module): def pad_cond_uncond(self, cond, uncond): empty = shared.sd_model.cond_stage_model_empty_prompt - num_repeats = (cond.shape[1] - cond.shape[1]) // empty.shape[1] + num_repeats = (cond.shape[1] - uncond.shape[1]) // empty.shape[1] if num_repeats < 0: cond = pad_cond(cond, -num_repeats, empty) From ec124607f47371a6cfd61a795f86a7f1cbd44651 Mon Sep 17 00:00:00 2001 From: wangshuai09 <391746016@qq.com> Date: Sat, 27 Jan 2024 17:21:32 +0800 Subject: [PATCH 03/10] Add NPU Support --- modules/devices.py | 9 +++-- modules/initialize.py | 6 +++- modules/npu_specific.py | 34 +++++++++++++++++++ .../textual_inversion/textual_inversion.py | 4 +++ requirements.txt | 4 +++ requirements_versions.txt | 4 +++ webui.sh | 4 +++ 7 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 modules/npu_specific.py diff --git a/modules/devices.py b/modules/devices.py index ea1f712f..f1e56501 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -3,7 +3,7 @@ import contextlib from functools import lru_cache import torch -from modules import errors, shared +from modules import errors, shared, npu_specific if sys.platform == "darwin": from modules import mac_specific @@ -40,6 +40,9 @@ def get_optimal_device_name(): if has_xpu(): return xpu_specific.get_xpu_device_string() + if npu_specific.has_npu: + return npu_specific.get_npu_device_string() + return "cpu" @@ -67,6 +70,9 @@ def torch_gc(): if has_xpu(): xpu_specific.torch_xpu_gc() + if npu_specific.has_npu: + npu_specific.torch_npu_gc() + def enable_tf32(): if torch.cuda.is_available(): @@ -164,4 +170,3 @@ def first_time_calculation(): x = torch.zeros((1, 1, 3, 3)).to(device, dtype) conv2d = torch.nn.Conv2d(1, 1, (3, 3)).to(device, dtype) conv2d(x) - diff --git a/modules/initialize.py b/modules/initialize.py index ac95fc6f..3285cc3c 100644 --- a/modules/initialize.py +++ b/modules/initialize.py @@ -143,13 +143,17 @@ def initialize_rest(*, reload_script_modules=False): its optimization may be None because the list of optimizaers has neet been filled by that time, so we apply optimization again. """ + from modules import devices + # Work around due to bug in torch_npu, revert me after fixed, @see https://gitee.com/ascend/pytorch/issues/I8KECW?from=project-issue + if devices.npu_specific.has_npu: + import torch + torch.npu.set_device(0) shared.sd_model # noqa: B018 if sd_hijack.current_optimizer is None: sd_hijack.apply_optimizations() - from modules import devices devices.first_time_calculation() if not shared.cmd_opts.skip_load_model_at_start: Thread(target=load_model).start() diff --git a/modules/npu_specific.py b/modules/npu_specific.py new file mode 100644 index 00000000..d8aebf9c --- /dev/null +++ b/modules/npu_specific.py @@ -0,0 +1,34 @@ +import importlib +import torch + +from modules import shared + + +def check_for_npu(): + if importlib.util.find_spec("torch_npu") is None: + return False + import torch_npu + torch_npu.npu.set_device(0) + + try: + # Will raise a RuntimeError if no NPU is found + _ = torch.npu.device_count() + return torch.npu.is_available() + except RuntimeError: + return False + + +def get_npu_device_string(): + if shared.cmd_opts.device_id is not None: + return f"npu:{shared.cmd_opts.device_id}" + return "npu:0" + + +def torch_npu_gc(): + # Work around due to bug in torch_npu, revert me after fixed, @see https://gitee.com/ascend/pytorch/issues/I8KECW?from=project-issue + torch.npu.set_device(0) + with torch.npu.device(get_npu_device_string()): + torch.npu.empty_cache() + + +has_npu = check_for_npu() diff --git a/modules/textual_inversion/textual_inversion.py b/modules/textual_inversion/textual_inversion.py index 04dda585..9c062503 100644 --- a/modules/textual_inversion/textual_inversion.py +++ b/modules/textual_inversion/textual_inversion.py @@ -151,6 +151,10 @@ class EmbeddingDatabase: return embedding def get_expected_shape(self): + # workaround + if devices.npu_specific.has_npu: + import torch + torch.npu.set_device(0) vec = shared.sd_model.cond_stage_model.encode_embedding_init_text(",", 1) return vec.shape[1] diff --git a/requirements.txt b/requirements.txt index 80b43845..4537402b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,8 @@ accelerate basicsr blendmodes clean-fid +cloudpickle +decorator einops fastapi>=0.90.1 gfpgan @@ -26,9 +28,11 @@ resize-right safetensors scikit-image>=0.19 +synr==0.5.0 timm tomesd torch torchdiffeq torchsde +tornado transformers==4.30.2 diff --git a/requirements_versions.txt b/requirements_versions.txt index cb7403a9..95515b55 100644 --- a/requirements_versions.txt +++ b/requirements_versions.txt @@ -4,6 +4,8 @@ accelerate==0.21.0 basicsr==1.4.2 blendmodes==2022 clean-fid==0.1.35 +cloudpickle==3.0.0 +decorator==5.1.1 einops==0.4.1 fastapi==0.94.0 gfpgan==1.3.8 @@ -23,10 +25,12 @@ realesrgan==0.3.0 resize-right==0.0.2 safetensors==0.3.1 scikit-image==0.21.0 +synr==0.5.0 timm==0.9.2 tomesd==0.1.3 torch torchdiffeq==0.2.3 torchsde==0.2.6 +tornado==6.4 transformers==4.30.2 httpx==0.24.1 diff --git a/webui.sh b/webui.sh index cff43327..3f6e87fd 100755 --- a/webui.sh +++ b/webui.sh @@ -159,6 +159,10 @@ then if echo "$gpu_info" | grep -q "AMD" && [[ -z "${TORCH_COMMAND}" ]] then export TORCH_COMMAND="pip install torch==2.0.1+rocm5.4.2 torchvision==0.15.2+rocm5.4.2 --index-url https://download.pytorch.org/whl/rocm5.4.2" + elif echo "$gpu_info" | grep -q "Huawei" && [[ -z "${TORCH_COMMAND}" ]] + then + export TORCH_COMMAND="pip install torch==2.1.0 torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu; pip install torch_npu" + fi fi From 750dd6014a45397979cad42a74634451d0861581 Mon Sep 17 00:00:00 2001 From: Kohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com> Date: Mon, 29 Jan 2024 22:27:53 +0800 Subject: [PATCH 04/10] Fix potential bugs --- modules/devices.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/devices.py b/modules/devices.py index dfffaf24..60f7d6d7 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -141,7 +141,12 @@ def manual_cast_forward(target_dtype): args = [arg.to(target_dtype) if isinstance(arg, torch.Tensor) else arg for arg in args] kwargs = {k: v.to(target_dtype) if isinstance(v, torch.Tensor) else v for k, v in kwargs.items()} - org_dtype = torch_utils.get_param(self).dtype + org_dtype = target_dtype + for param in self.parameters(): + if param.dtype != target_dtype: + org_dtype = param.dtype + break + if org_dtype != target_dtype: self.to(target_dtype) result = self.org_forward(*args, **kwargs) @@ -170,7 +175,7 @@ def manual_cast(target_dtype): continue applied = True org_forward = module_type.forward - if module_type == torch.nn.MultiheadAttention and has_xpu(): + if module_type == torch.nn.MultiheadAttention: module_type.forward = manual_cast_forward(torch.float32) else: module_type.forward = manual_cast_forward(target_dtype) From 6e7f0860f7ae4a0ce59f9416fb9b2f3bcab44f1d Mon Sep 17 00:00:00 2001 From: Kohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com> Date: Mon, 29 Jan 2024 22:46:43 +0800 Subject: [PATCH 05/10] linting --- modules/devices.py | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/devices.py b/modules/devices.py index 60f7d6d7..8f49f7a4 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -4,7 +4,6 @@ from functools import lru_cache import torch from modules import errors, shared -from modules import torch_utils if sys.platform == "darwin": from modules import mac_specific From d243e24f539d717b221992e894a5db5a321bf3cd Mon Sep 17 00:00:00 2001 From: Kohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com> Date: Mon, 29 Jan 2024 22:49:45 +0800 Subject: [PATCH 06/10] Try to reverse the dtype checking mechanism --- modules/devices.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/modules/devices.py b/modules/devices.py index 8f49f7a4..f9648e9a 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -4,6 +4,7 @@ from functools import lru_cache import torch from modules import errors, shared +from modules import torch_utils if sys.platform == "darwin": from modules import mac_specific @@ -140,11 +141,7 @@ def manual_cast_forward(target_dtype): args = [arg.to(target_dtype) if isinstance(arg, torch.Tensor) else arg for arg in args] kwargs = {k: v.to(target_dtype) if isinstance(v, torch.Tensor) else v for k, v in kwargs.items()} - org_dtype = target_dtype - for param in self.parameters(): - if param.dtype != target_dtype: - org_dtype = param.dtype - break + org_dtype = torch_utils.get_param(self).dtype if org_dtype != target_dtype: self.to(target_dtype) From f9ba7e648ad5bf7dbdf2b95fa207936179bf784e Mon Sep 17 00:00:00 2001 From: Kohaku-Blueleaf <59680068+KohakuBlueleaf@users.noreply.github.com> Date: Mon, 29 Jan 2024 22:54:12 +0800 Subject: [PATCH 07/10] Revert "Try to reverse the dtype checking mechanism" This reverts commit d243e24f539d717b221992e894a5db5a321bf3cd. --- modules/devices.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/devices.py b/modules/devices.py index f9648e9a..8f49f7a4 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -4,7 +4,6 @@ from functools import lru_cache import torch from modules import errors, shared -from modules import torch_utils if sys.platform == "darwin": from modules import mac_specific @@ -141,7 +140,11 @@ def manual_cast_forward(target_dtype): args = [arg.to(target_dtype) if isinstance(arg, torch.Tensor) else arg for arg in args] kwargs = {k: v.to(target_dtype) if isinstance(v, torch.Tensor) else v for k, v in kwargs.items()} - org_dtype = torch_utils.get_param(self).dtype + org_dtype = target_dtype + for param in self.parameters(): + if param.dtype != target_dtype: + org_dtype = param.dtype + break if org_dtype != target_dtype: self.to(target_dtype) From c4255d12f7531725e591160e1cfe47d7a2fc0f02 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 31 Jan 2024 04:36:11 +0900 Subject: [PATCH 08/10] add tooltip create_submit_box --- modules/ui_toprow.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ui_toprow.py b/modules/ui_toprow.py index fbe705be..457fbf52 100644 --- a/modules/ui_toprow.py +++ b/modules/ui_toprow.py @@ -96,9 +96,9 @@ class Toprow: with gr.Row(elem_id=f"{self.id_part}_generate_box", elem_classes=["generate-box"] + (["generate-box-compact"] if self.is_compact else []), render=not self.is_compact) as submit_box: self.submit_box = submit_box - self.interrupt = gr.Button('Interrupt', elem_id=f"{self.id_part}_interrupt", elem_classes="generate-box-interrupt") - self.skip = gr.Button('Skip', elem_id=f"{self.id_part}_skip", elem_classes="generate-box-skip") - self.submit = gr.Button('Generate', elem_id=f"{self.id_part}_generate", variant='primary') + self.interrupt = gr.Button('Interrupt', elem_id=f"{self.id_part}_interrupt", elem_classes="generate-box-interrupt", tooltip="End generation immediately or after completing current batch") + self.skip = gr.Button('Skip', elem_id=f"{self.id_part}_skip", elem_classes="generate-box-skip", tooltip="Stop generation of current batch and continues onto next batch") + self.submit = gr.Button('Generate', elem_id=f"{self.id_part}_generate", variant='primary', tooltip="Right click generate forever menu") self.skip.click( fn=lambda: shared.state.skip(), From cc3f604310458eed7d26456c1b3934d582283ffe Mon Sep 17 00:00:00 2001 From: wangshuai09 <391746016@qq.com> Date: Wed, 31 Jan 2024 10:46:53 +0800 Subject: [PATCH 09/10] Update --- modules/devices.py | 7 +++++++ modules/initialize.py | 5 +---- modules/launch_utils.py | 8 ++++++++ modules/npu_specific.py | 5 +---- modules/textual_inversion/textual_inversion.py | 5 +---- requirements.txt | 4 ---- requirements_npu.txt | 4 ++++ requirements_versions.txt | 4 ---- 8 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 requirements_npu.txt diff --git a/modules/devices.py b/modules/devices.py index c737162a..28c0c54d 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -88,9 +88,16 @@ def torch_gc(): xpu_specific.torch_xpu_gc() if npu_specific.has_npu: + torch_npu_set_device() npu_specific.torch_npu_gc() +def torch_npu_set_device(): + # Work around due to bug in torch_npu, revert me after fixed, @see https://gitee.com/ascend/pytorch/issues/I8KECW?from=project-issue + if npu_specific.has_npu: + torch.npu.set_device(0) + + def enable_tf32(): if torch.cuda.is_available(): diff --git a/modules/initialize.py b/modules/initialize.py index cc34fd6f..f7313ff4 100644 --- a/modules/initialize.py +++ b/modules/initialize.py @@ -143,10 +143,7 @@ def initialize_rest(*, reload_script_modules=False): by that time, so we apply optimization again. """ from modules import devices - # Work around due to bug in torch_npu, revert me after fixed, @see https://gitee.com/ascend/pytorch/issues/I8KECW?from=project-issue - if devices.npu_specific.has_npu: - import torch - torch.npu.set_device(0) + devices.torch_npu_set_device() shared.sd_model # noqa: B018 diff --git a/modules/launch_utils.py b/modules/launch_utils.py index 3ff4576a..107c72b0 100644 --- a/modules/launch_utils.py +++ b/modules/launch_utils.py @@ -338,6 +338,7 @@ def prepare_environment(): torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://pytorch-extension.intel.com/release-whl/stable/xpu/us/") torch_command = os.environ.get('TORCH_COMMAND', f"pip install torch==2.0.0a0 intel-extension-for-pytorch==2.0.110+gitba7f6c1 --extra-index-url {torch_index_url}") requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt") + requirements_file_for_npu = os.environ.get('REQS_FILE_FOR_NPU', "requirements_npu.txt") xformers_package = os.environ.get('XFORMERS_PACKAGE', 'xformers==0.0.23.post1') clip_package = os.environ.get('CLIP_PACKAGE', "https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip") @@ -421,6 +422,13 @@ def prepare_environment(): run_pip(f"install -r \"{requirements_file}\"", "requirements") startup_timer.record("install requirements") + if not os.path.isfile(requirements_file_for_npu): + requirements_file_for_npu = os.path.join(script_path, requirements_file_for_npu) + + if "torch_npu" in torch_command and not requirements_met(requirements_file_for_npu): + run_pip(f"install -r \"{requirements_file_for_npu}\"", "requirements_for_npu") + startup_timer.record("install requirements_for_npu") + if not args.skip_install: run_extensions_installers(settings_file=args.ui_settings_file) diff --git a/modules/npu_specific.py b/modules/npu_specific.py index d8aebf9c..94100691 100644 --- a/modules/npu_specific.py +++ b/modules/npu_specific.py @@ -8,11 +8,10 @@ def check_for_npu(): if importlib.util.find_spec("torch_npu") is None: return False import torch_npu - torch_npu.npu.set_device(0) try: # Will raise a RuntimeError if no NPU is found - _ = torch.npu.device_count() + _ = torch_npu.npu.device_count() return torch.npu.is_available() except RuntimeError: return False @@ -25,8 +24,6 @@ def get_npu_device_string(): def torch_npu_gc(): - # Work around due to bug in torch_npu, revert me after fixed, @see https://gitee.com/ascend/pytorch/issues/I8KECW?from=project-issue - torch.npu.set_device(0) with torch.npu.device(get_npu_device_string()): torch.npu.empty_cache() diff --git a/modules/textual_inversion/textual_inversion.py b/modules/textual_inversion/textual_inversion.py index d16e3b9a..6d815c0b 100644 --- a/modules/textual_inversion/textual_inversion.py +++ b/modules/textual_inversion/textual_inversion.py @@ -150,10 +150,7 @@ class EmbeddingDatabase: return embedding def get_expected_shape(self): - # workaround - if devices.npu_specific.has_npu: - import torch - torch.npu.set_device(0) + devices.torch_npu_set_device() vec = shared.sd_model.cond_stage_model.encode_embedding_init_text(",", 1) return vec.shape[1] diff --git a/requirements.txt b/requirements.txt index d1e4ede9..731a1be7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,8 +4,6 @@ accelerate blendmodes clean-fid -cloudpickle -decorator einops facexlib fastapi>=0.90.1 @@ -26,10 +24,8 @@ resize-right safetensors scikit-image>=0.19 -synr==0.5.0 tomesd torch torchdiffeq torchsde -tornado transformers==4.30.2 diff --git a/requirements_npu.txt b/requirements_npu.txt new file mode 100644 index 00000000..5e6a4364 --- /dev/null +++ b/requirements_npu.txt @@ -0,0 +1,4 @@ +cloudpickle +decorator +synr==0.5.0 +tornado diff --git a/requirements_versions.txt b/requirements_versions.txt index 1c66cd8c..5e30b5ea 100644 --- a/requirements_versions.txt +++ b/requirements_versions.txt @@ -3,8 +3,6 @@ Pillow==9.5.0 accelerate==0.21.0 blendmodes==2022 clean-fid==0.1.35 -cloudpickle==3.0.0 -decorator==5.1.1 einops==0.4.1 facexlib==0.3.0 fastapi==0.94.0 @@ -23,12 +21,10 @@ pytorch_lightning==1.9.4 resize-right==0.0.2 safetensors==0.4.2 scikit-image==0.21.0 -synr==0.5.0 spandrel==0.1.6 tomesd==0.1.3 torch torchdiffeq==0.2.3 torchsde==0.2.6 -tornado==6.4 transformers==4.30.2 httpx==0.24.1 From 74b214a92a2959948dcd05a78b7380e046163871 Mon Sep 17 00:00:00 2001 From: Cyberbeing Date: Mon, 29 Jan 2024 02:06:50 -0800 Subject: [PATCH 10/10] Fix potential autocast NaNs in image upscale --- modules/upscaler_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/upscaler_utils.py b/modules/upscaler_utils.py index afed8b40..b5e5a80c 100644 --- a/modules/upscaler_utils.py +++ b/modules/upscaler_utils.py @@ -6,7 +6,7 @@ import torch import tqdm from PIL import Image -from modules import images, shared, torch_utils +from modules import devices, images, shared, torch_utils logger = logging.getLogger(__name__) @@ -44,7 +44,8 @@ def upscale_pil_patch(model, img: Image.Image) -> Image.Image: with torch.no_grad(): tensor = pil_image_to_torch_bgr(img).unsqueeze(0) # add batch dimension tensor = tensor.to(device=param.device, dtype=param.dtype) - return torch_bgr_to_pil_image(model(tensor)) + with devices.without_autocast(): + return torch_bgr_to_pil_image(model(tensor)) def upscale_with_model(