From f38d5a91bfadccc38b70163f798340abb45d7494 Mon Sep 17 00:00:00 2001 From: Disty0 Date: Fri, 28 Jul 2023 10:58:45 +0300 Subject: [PATCH] Move ipex fixes into it's own folder --- modules/devices.py | 87 +----------------------------- modules/ipex_specific/__init__.py | 89 +++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 85 deletions(-) create mode 100644 modules/ipex_specific/__init__.py diff --git a/modules/devices.py b/modules/devices.py index addb0b73e..8c57b54af 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -168,6 +168,8 @@ def set_cuda_params(): args = cmd_args.parser.parse_args() if args.use_ipex or (hasattr(torch, 'xpu') and torch.xpu.is_available()): backend = 'ipex' + from modules.ipex_specific import ipex_init + ipex_init() elif args.use_directml: backend = 'directml' elif torch.cuda.is_available() and torch.version.cuda: @@ -179,91 +181,6 @@ elif sys.platform == 'darwin': else: backend = 'cpu' -if backend == 'ipex': - import os - def ipex_no_cuda(orig_func, *args, **kwargs): # pylint: disable=redefined-outer-name - torch.cuda.is_available = lambda: False - orig_func(*args, **kwargs) - torch.cuda.is_available = torch.xpu.is_available - - #Fix functions with ipex - torch.cuda.is_available = torch.xpu.is_available - torch.cuda.device = torch.xpu.device - torch.cuda.device_count = torch.xpu.device_count - torch.cuda.current_device = torch.xpu.current_device - torch.cuda.get_device_name = torch.xpu.get_device_name - torch.cuda.get_device_properties = torch.xpu.get_device_properties - torch._utils._get_available_device_type = lambda: "xpu" # pylint: disable=protected-access - torch.cuda.set_device = torch.xpu.set_device - torch.Tensor.cuda = torch.Tensor.xpu - - torch.cuda.empty_cache = torch.xpu.empty_cache if "WSL2" not in os.popen("uname -a").read() else lambda: None - torch.cuda.ipc_collect = lambda: None - torch.cuda.memory_stats = torch.xpu.memory_stats - torch.cuda.mem_get_info = lambda device=None: [(torch.xpu.get_device_properties(device).total_memory - torch.xpu.memory_allocated(device)), torch.xpu.get_device_properties(device).total_memory] - torch.cuda.memory_allocated = torch.xpu.memory_allocated - torch.cuda.max_memory_allocated = torch.xpu.max_memory_allocated - torch.cuda.reset_peak_memory_stats = torch.xpu.reset_peak_memory_stats - torch.cuda.utilization = lambda: 0 - - torch.cuda.get_rng_state_all = torch.xpu.get_rng_state_all - torch.cuda.set_rng_state_all = torch.xpu.set_rng_state_all - try: - torch.cuda.amp.GradScaler = torch.xpu.amp.GradScaler - except Exception: - pass - - from modules.sd_hijack_utils import CondFunc - #Broken functions when torch.cuda.is_available is True: - CondFunc('torch.utils.data.dataloader._BaseDataLoaderIter.__init__', - lambda orig_func, *args, **kwargs: ipex_no_cuda(orig_func, *args, **kwargs), - lambda orig_func, *args, **kwargs: True) - - #Functions with dtype errors: - CondFunc('torch.nn.modules.GroupNorm.forward', - lambda orig_func, self, input: orig_func(self, input.to(self.weight.data.dtype)), - lambda orig_func, self, input: input.dtype != self.weight.data.dtype) - CondFunc('torch.nn.modules.Linear.forward', - lambda orig_func, self, input: orig_func(self, input.to(self.weight.data.dtype)), - lambda orig_func, self, input: input.dtype != self.weight.data.dtype) - #Diffusers bfloat16: - CondFunc('torch.nn.modules.Conv2d._conv_forward', - lambda orig_func, self, input, weight, bias=None: orig_func(self, input.to(weight.data.dtype), weight, bias=bias), - lambda orig_func, self, input, weight, bias=None: input.dtype != weight.data.dtype) - - #Functions that does not work with the XPU: - #UniPC: - CondFunc('torch.linalg.solve', - lambda orig_func, A, B, *args, left=True, out=None: orig_func(A.to("cpu"), B.to("cpu"), *args, left=left, out=out).to(get_cuda_device_string()), - lambda orig_func, A, B, *args, left=True, out=None: A.device != torch.device("cpu") or B.device != torch.device("cpu")) - #SDE Samplers: - CondFunc('torch.Generator', - lambda orig_func, device: torch.xpu.Generator(device), - lambda orig_func, device: device != torch.device("cpu") and device != "cpu") - #Latent antialias: - CondFunc('torch.nn.functional.interpolate', - lambda orig_func, input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False: orig_func(input.to("cpu"), size=size, scale_factor=scale_factor, mode=mode, align_corners=align_corners, recompute_scale_factor=recompute_scale_factor, antialias=antialias).to(get_cuda_device_string()), - lambda orig_func, input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False: antialias) - #Diffusers Float64 (ARC GPUs doesn't support double or Float64): - if not torch.xpu.has_fp64_dtype(): - CondFunc('torch.from_numpy', - lambda orig_func, ndarray: orig_func(ndarray.astype('float32')), - lambda orig_func, ndarray: ndarray.dtype == float) - #ControlNet and TiledVAE: - CondFunc('torch.batch_norm', - lambda orig_func, input, running_mean=None, running_var=None, weight=None, bias=None, training=False, momentum=0.1, eps=1e-5, cudnn_enabled=True: orig_func(input, - running_mean if running_mean is not None else torch.ones(input.size()[1], device=get_cuda_device_string()), - running_var if running_var is not None else torch.zeros(input.size()[1], device=get_cuda_device_string()), - weight, bias, training, momentum, eps, cudnn_enabled), - lambda orig_func, input, running_mean=None, running_var=None, weight=None, bias=None, training=False, momentum=0.1, eps=1e-5, cudnn_enabled=True: input.device != torch.device("cpu")) - #ControlNet - CondFunc('torch.instance_norm', - lambda orig_func, input, running_mean=None, running_var=None, weight=None, bias=None, use_input_stats=True, momentum=0.1, eps=1e-5, cudnn_enabled=True: orig_func(input, - running_mean if running_mean is not None else torch.ones(input.size()[1], device=get_cuda_device_string()), - running_var if running_var is not None else torch.zeros(input.size()[1], device=get_cuda_device_string()), - weight, bias, use_input_stats, momentum, eps, cudnn_enabled), - lambda orig_func, input, running_mean=None, running_var=None, weight=None, bias=None, use_input_stats=True, momentum=0.1, eps=1e-5, cudnn_enabled=True: input.device != torch.device("cpu")) - if backend == "directml": directml_init() diff --git a/modules/ipex_specific/__init__.py b/modules/ipex_specific/__init__.py new file mode 100644 index 000000000..c998de77e --- /dev/null +++ b/modules/ipex_specific/__init__.py @@ -0,0 +1,89 @@ +import os +import torch +import intel_extension_for_pytorch as ipex +from modules import shared +from modules.sd_hijack_utils import CondFunc + +def ipex_no_cuda(orig_func, *args, **kwargs): # pylint: disable=redefined-outer-name + torch.cuda.is_available = lambda: False + orig_func(*args, **kwargs) + torch.cuda.is_available = torch.xpu.is_available + +def ipex_init(): + #Fix functions with ipex + torch.cuda.is_available = torch.xpu.is_available + torch.cuda.device = torch.xpu.device + torch.cuda.device_count = torch.xpu.device_count + torch.cuda.current_device = torch.xpu.current_device + torch.cuda.get_device_name = torch.xpu.get_device_name + torch.cuda.get_device_properties = torch.xpu.get_device_properties + torch._utils._get_available_device_type = lambda: "xpu" # pylint: disable=protected-access + torch.cuda.set_device = torch.xpu.set_device + torch.Tensor.cuda = torch.Tensor.xpu + + torch.xpu.empty_cache = torch.xpu.empty_cache if "WSL2" not in os.popen("uname -a").read() else lambda: None + torch.cuda.empty_cache = torch.xpu.empty_cache + torch.cuda.ipc_collect = lambda: None + torch.cuda.memory_stats = torch.xpu.memory_stats + torch.cuda.mem_get_info = lambda device=None: [(torch.xpu.get_device_properties(device).total_memory - torch.xpu.memory_allocated(device)), torch.xpu.get_device_properties(device).total_memory] + torch.cuda.memory_allocated = torch.xpu.memory_allocated + torch.cuda.max_memory_allocated = torch.xpu.max_memory_allocated + torch.cuda.reset_peak_memory_stats = torch.xpu.reset_peak_memory_stats + torch.cuda.utilization = lambda: 0 + + torch.cuda.get_rng_state_all = torch.xpu.get_rng_state_all + torch.cuda.set_rng_state_all = torch.xpu.set_rng_state_all + try: + torch.cuda.amp.GradScaler = torch.xpu.amp.GradScaler + except Exception: + torch.cuda.amp.GradScaler = ipex.cpu.autocast._grad_scaler.GradScaler + + #Broken functions when torch.cuda.is_available is True: + CondFunc('torch.utils.data.dataloader._BaseDataLoaderIter.__init__', + lambda orig_func, *args, **kwargs: ipex_no_cuda(orig_func, *args, **kwargs), + lambda orig_func, *args, **kwargs: True) + + #Functions with dtype errors: + CondFunc('torch.nn.modules.GroupNorm.forward', + lambda orig_func, self, input: orig_func(self, input.to(self.weight.data.dtype)), + lambda orig_func, self, input: input.dtype != self.weight.data.dtype) + CondFunc('torch.nn.modules.Linear.forward', + lambda orig_func, self, input: orig_func(self, input.to(self.weight.data.dtype)), + lambda orig_func, self, input: input.dtype != self.weight.data.dtype) + #Diffusers bfloat16: + CondFunc('torch.nn.modules.Conv2d._conv_forward', + lambda orig_func, self, input, weight, bias=None: orig_func(self, input.to(weight.data.dtype), weight, bias=bias), + lambda orig_func, self, input, weight, bias=None: input.dtype != weight.data.dtype) + + #Functions that does not work with the XPU: + #UniPC: + CondFunc('torch.linalg.solve', + lambda orig_func, A, B, *args, left=True, out=None: orig_func(A.to("cpu"), B.to("cpu"), *args, left=left, out=out).to(shared.device), + lambda orig_func, A, B, *args, left=True, out=None: A.device != torch.device("cpu") or B.device != torch.device("cpu")) + #SDE Samplers: + CondFunc('torch.Generator', + lambda orig_func, device: torch.xpu.Generator(device), + lambda orig_func, device: device != torch.device("cpu") and device != "cpu") + #Latent antialias: + CondFunc('torch.nn.functional.interpolate', + lambda orig_func, input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False: orig_func(input.to("cpu"), size=size, scale_factor=scale_factor, mode=mode, align_corners=align_corners, recompute_scale_factor=recompute_scale_factor, antialias=antialias).to(shared.device), + lambda orig_func, input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False: antialias) + #Diffusers Float64 (ARC GPUs doesn't support double or Float64): + if not torch.xpu.has_fp64_dtype(): + CondFunc('torch.from_numpy', + lambda orig_func, ndarray: orig_func(ndarray.astype('float32')), + lambda orig_func, ndarray: ndarray.dtype == float) + #ControlNet and TiledVAE: + CondFunc('torch.batch_norm', + lambda orig_func, input, weight=None, bias=None, running_mean=None, running_var=None, training=False, momentum=0.1, eps=1e-5, cudnn_enabled=True: orig_func(input, + weight if weight is not None else torch.ones(input.size()[1], device=shared.device), + bias if bias is not None else torch.zeros(input.size()[1], device=shared.device), + running_mean, running_var, training, momentum, eps, cudnn_enabled), + lambda orig_func, input, weight=None, bias=None, running_mean=None, running_var=None, training=False, momentum=0.1, eps=1e-5, cudnn_enabled=True: input.device != torch.device("cpu")) + #ControlNet + CondFunc('torch.instance_norm', + lambda orig_func, input, weight=None, bias=None, running_mean=None, running_var=None, use_input_stats=True, momentum=0.1, eps=1e-5, cudnn_enabled=True: orig_func(input, + weight if weight is not None else torch.ones(input.size()[1], device=shared.device), + bias if bias is not None else torch.zeros(input.size()[1], device=shared.device), + running_mean, running_var, use_input_stats, momentum, eps, cudnn_enabled), + lambda orig_func, input, weight=None, bias=None, running_mean=None, running_var=None, use_input_stats=True, momentum=0.1, eps=1e-5, cudnn_enabled=True: input.device != torch.device("cpu"))