mirror of
https://github.com/kohya-ss/sd-scripts.git
synced 2026-04-08 22:35:09 +00:00
Disable IPEX attention if the GPU supports 64 bit
This commit is contained in:
@@ -165,12 +165,13 @@ def ipex_init(): # pylint: disable=too-many-statements
|
|||||||
torch.cuda.get_device_id_list_per_card = torch.xpu.get_device_id_list_per_card
|
torch.cuda.get_device_id_list_per_card = torch.xpu.get_device_id_list_per_card
|
||||||
|
|
||||||
ipex_hijacks()
|
ipex_hijacks()
|
||||||
attention_init()
|
if not torch.xpu.has_fp64_dtype():
|
||||||
try:
|
attention_init()
|
||||||
from .diffusers import ipex_diffusers
|
try:
|
||||||
ipex_diffusers()
|
from .diffusers import ipex_diffusers
|
||||||
except Exception: # pylint: disable=broad-exception-caught
|
ipex_diffusers()
|
||||||
pass
|
except Exception: # pylint: disable=broad-exception-caught
|
||||||
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False, e
|
return False, e
|
||||||
return True, None
|
return True, None
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import torch
|
import torch
|
||||||
import intel_extension_for_pytorch as ipex # pylint: disable=import-error, unused-import
|
import intel_extension_for_pytorch as ipex # pylint: disable=import-error, unused-import
|
||||||
import diffusers #0.21.1 # pylint: disable=import-error
|
import diffusers #0.24.0 # pylint: disable=import-error
|
||||||
from diffusers.models.attention_processor import Attention
|
from diffusers.models.attention_processor import Attention
|
||||||
|
|
||||||
# pylint: disable=protected-access, missing-function-docstring, line-too-long
|
# pylint: disable=protected-access, missing-function-docstring, line-too-long
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import intel_extension_for_pytorch._C as core # pylint: disable=import-error, un
|
|||||||
|
|
||||||
# pylint: disable=protected-access, missing-function-docstring, line-too-long
|
# pylint: disable=protected-access, missing-function-docstring, line-too-long
|
||||||
|
|
||||||
|
device_supports_fp64 = torch.xpu.has_fp64_dtype()
|
||||||
OptState = ipex.cpu.autocast._grad_scaler.OptState
|
OptState = ipex.cpu.autocast._grad_scaler.OptState
|
||||||
_MultiDeviceReplicator = ipex.cpu.autocast._grad_scaler._MultiDeviceReplicator
|
_MultiDeviceReplicator = ipex.cpu.autocast._grad_scaler._MultiDeviceReplicator
|
||||||
_refresh_per_optimizer_state = ipex.cpu.autocast._grad_scaler._refresh_per_optimizer_state
|
_refresh_per_optimizer_state = ipex.cpu.autocast._grad_scaler._refresh_per_optimizer_state
|
||||||
@@ -96,7 +97,10 @@ def unscale_(self, optimizer):
|
|||||||
|
|
||||||
# FP32 division can be imprecise for certain compile options, so we carry out the reciprocal in FP64.
|
# FP32 division can be imprecise for certain compile options, so we carry out the reciprocal in FP64.
|
||||||
assert self._scale is not None
|
assert self._scale is not None
|
||||||
inv_scale = self._scale.to("cpu").double().reciprocal().float().to(self._scale.device)
|
if device_supports_fp64:
|
||||||
|
inv_scale = self._scale.double().reciprocal().float()
|
||||||
|
else:
|
||||||
|
inv_scale = self._scale.to("cpu").double().reciprocal().float().to(self._scale.device)
|
||||||
found_inf = torch.full(
|
found_inf = torch.full(
|
||||||
(1,), 0.0, dtype=torch.float32, device=self._scale.device
|
(1,), 0.0, dtype=torch.float32, device=self._scale.device
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ def ipex_autocast(*args, **kwargs):
|
|||||||
else:
|
else:
|
||||||
return original_autocast(*args, **kwargs)
|
return original_autocast(*args, **kwargs)
|
||||||
|
|
||||||
#Embedding BF16
|
# Embedding BF16
|
||||||
original_torch_cat = torch.cat
|
original_torch_cat = torch.cat
|
||||||
def torch_cat(tensor, *args, **kwargs):
|
def torch_cat(tensor, *args, **kwargs):
|
||||||
if len(tensor) == 3 and (tensor[0].dtype != tensor[1].dtype or tensor[2].dtype != tensor[1].dtype):
|
if len(tensor) == 3 and (tensor[0].dtype != tensor[1].dtype or tensor[2].dtype != tensor[1].dtype):
|
||||||
@@ -97,7 +97,7 @@ def torch_cat(tensor, *args, **kwargs):
|
|||||||
else:
|
else:
|
||||||
return original_torch_cat(tensor, *args, **kwargs)
|
return original_torch_cat(tensor, *args, **kwargs)
|
||||||
|
|
||||||
#Latent antialias:
|
# Latent antialias:
|
||||||
original_interpolate = torch.nn.functional.interpolate
|
original_interpolate = torch.nn.functional.interpolate
|
||||||
def interpolate(tensor, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False): # pylint: disable=too-many-arguments
|
def interpolate(tensor, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False): # pylint: disable=too-many-arguments
|
||||||
if antialias or align_corners is not None:
|
if antialias or align_corners is not None:
|
||||||
@@ -160,7 +160,7 @@ def ipex_hijacks():
|
|||||||
lambda orig_func, device=None: torch.xpu.Generator(return_xpu(device)),
|
lambda orig_func, device=None: torch.xpu.Generator(return_xpu(device)),
|
||||||
lambda orig_func, device=None: device is not None and device != torch.device("cpu") and device != "cpu")
|
lambda orig_func, device=None: device is not None and device != torch.device("cpu") and device != "cpu")
|
||||||
|
|
||||||
#TiledVAE and ControlNet:
|
# TiledVAE and ControlNet:
|
||||||
CondFunc('torch.batch_norm',
|
CondFunc('torch.batch_norm',
|
||||||
lambda orig_func, input, weight, bias, *args, **kwargs: orig_func(input,
|
lambda orig_func, input, weight, bias, *args, **kwargs: orig_func(input,
|
||||||
weight if weight is not None else torch.ones(input.size()[1], device=input.device),
|
weight if weight is not None else torch.ones(input.size()[1], device=input.device),
|
||||||
@@ -172,41 +172,41 @@ def ipex_hijacks():
|
|||||||
bias if bias is not None else torch.zeros(input.size()[1], device=input.device), *args, **kwargs),
|
bias if bias is not None else torch.zeros(input.size()[1], device=input.device), *args, **kwargs),
|
||||||
lambda orig_func, input, *args, **kwargs: input.device != torch.device("cpu"))
|
lambda orig_func, input, *args, **kwargs: input.device != torch.device("cpu"))
|
||||||
|
|
||||||
#Functions with dtype errors:
|
# Functions with dtype errors:
|
||||||
CondFunc('torch.nn.modules.GroupNorm.forward',
|
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: orig_func(self, input.to(self.weight.data.dtype)),
|
||||||
lambda orig_func, self, input: input.dtype != self.weight.data.dtype)
|
lambda orig_func, self, input: input.dtype != self.weight.data.dtype)
|
||||||
#Training:
|
# Training:
|
||||||
CondFunc('torch.nn.modules.linear.Linear.forward',
|
CondFunc('torch.nn.modules.linear.Linear.forward',
|
||||||
lambda orig_func, self, input: orig_func(self, input.to(self.weight.data.dtype)),
|
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)
|
lambda orig_func, self, input: input.dtype != self.weight.data.dtype)
|
||||||
CondFunc('torch.nn.modules.conv.Conv2d.forward',
|
CondFunc('torch.nn.modules.conv.Conv2d.forward',
|
||||||
lambda orig_func, self, input: orig_func(self, input.to(self.weight.data.dtype)),
|
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)
|
lambda orig_func, self, input: input.dtype != self.weight.data.dtype)
|
||||||
#BF16:
|
# BF16:
|
||||||
CondFunc('torch.nn.functional.layer_norm',
|
CondFunc('torch.nn.functional.layer_norm',
|
||||||
lambda orig_func, input, normalized_shape=None, weight=None, *args, **kwargs:
|
lambda orig_func, input, normalized_shape=None, weight=None, *args, **kwargs:
|
||||||
orig_func(input.to(weight.data.dtype), normalized_shape, weight, *args, **kwargs),
|
orig_func(input.to(weight.data.dtype), normalized_shape, weight, *args, **kwargs),
|
||||||
lambda orig_func, input, normalized_shape=None, weight=None, *args, **kwargs:
|
lambda orig_func, input, normalized_shape=None, weight=None, *args, **kwargs:
|
||||||
weight is not None and input.dtype != weight.data.dtype)
|
weight is not None and input.dtype != weight.data.dtype)
|
||||||
#SwinIR BF16:
|
# SwinIR BF16:
|
||||||
CondFunc('torch.nn.functional.pad',
|
CondFunc('torch.nn.functional.pad',
|
||||||
lambda orig_func, input, pad, mode='constant', value=None: orig_func(input.to(torch.float32), pad, mode=mode, value=value).to(dtype=torch.bfloat16),
|
lambda orig_func, input, pad, mode='constant', value=None: orig_func(input.to(torch.float32), pad, mode=mode, value=value).to(dtype=torch.bfloat16),
|
||||||
lambda orig_func, input, pad, mode='constant', value=None: mode == 'reflect' and input.dtype == torch.bfloat16)
|
lambda orig_func, input, pad, mode='constant', value=None: mode == 'reflect' and input.dtype == torch.bfloat16)
|
||||||
|
|
||||||
#Diffusers Float64 (ARC GPUs doesn't support double or Float64):
|
# Diffusers Float64 (Alchemist GPUs doesn't support 64 bit):
|
||||||
if not torch.xpu.has_fp64_dtype():
|
if not torch.xpu.has_fp64_dtype():
|
||||||
CondFunc('torch.from_numpy',
|
CondFunc('torch.from_numpy',
|
||||||
lambda orig_func, ndarray: orig_func(ndarray.astype('float32')),
|
lambda orig_func, ndarray: orig_func(ndarray.astype('float32')),
|
||||||
lambda orig_func, ndarray: ndarray.dtype == float)
|
lambda orig_func, ndarray: ndarray.dtype == float)
|
||||||
|
|
||||||
#Broken functions when torch.cuda.is_available is True:
|
# Broken functions when torch.cuda.is_available is True:
|
||||||
#Pin Memory:
|
# Pin Memory:
|
||||||
CondFunc('torch.utils.data.dataloader._BaseDataLoaderIter.__init__',
|
CondFunc('torch.utils.data.dataloader._BaseDataLoaderIter.__init__',
|
||||||
lambda orig_func, *args, **kwargs: ipex_no_cuda(orig_func, *args, **kwargs),
|
lambda orig_func, *args, **kwargs: ipex_no_cuda(orig_func, *args, **kwargs),
|
||||||
lambda orig_func, *args, **kwargs: True)
|
lambda orig_func, *args, **kwargs: True)
|
||||||
|
|
||||||
#Functions that make compile mad with CondFunc:
|
# Functions that make compile mad with CondFunc:
|
||||||
torch.utils.data.dataloader._MultiProcessingDataLoaderIter._shutdown_workers = _shutdown_workers
|
torch.utils.data.dataloader._MultiProcessingDataLoaderIter._shutdown_workers = _shutdown_workers
|
||||||
torch.nn.DataParallel = DummyDataParallel
|
torch.nn.DataParallel = DummyDataParallel
|
||||||
torch.autocast = ipex_autocast
|
torch.autocast = ipex_autocast
|
||||||
|
|||||||
Reference in New Issue
Block a user