|
|
|
|
@@ -89,6 +89,7 @@ def ipex_autocast(*args, **kwargs):
|
|
|
|
|
else:
|
|
|
|
|
return original_autocast(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
# Embedding BF16
|
|
|
|
|
original_torch_cat = torch.cat
|
|
|
|
|
def torch_cat(tensor, *args, **kwargs):
|
|
|
|
|
if len(tensor) == 3 and (tensor[0].dtype != tensor[1].dtype or tensor[2].dtype != tensor[1].dtype):
|
|
|
|
|
@@ -96,6 +97,7 @@ def torch_cat(tensor, *args, **kwargs):
|
|
|
|
|
else:
|
|
|
|
|
return original_torch_cat(tensor, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
# Latent antialias:
|
|
|
|
|
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
|
|
|
|
|
if antialias or align_corners is not None:
|
|
|
|
|
@@ -115,19 +117,29 @@ def linalg_solve(A, B, *args, **kwargs): # pylint: disable=invalid-name
|
|
|
|
|
else:
|
|
|
|
|
return original_linalg_solve(A, B, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_cuda(self):
|
|
|
|
|
return self.device.type == 'xpu'
|
|
|
|
|
|
|
|
|
|
def ipex_hijacks():
|
|
|
|
|
CondFunc('torch.tensor',
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: orig_func(*args, device=return_xpu(device), **kwargs),
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.Tensor.to',
|
|
|
|
|
lambda orig_func, self, device=None, *args, **kwargs: orig_func(self, return_xpu(device), *args, **kwargs),
|
|
|
|
|
lambda orig_func, self, device=None, *args, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.Tensor.cuda',
|
|
|
|
|
lambda orig_func, self, device=None, *args, **kwargs: orig_func(self, return_xpu(device), *args, **kwargs),
|
|
|
|
|
lambda orig_func, self, device=None, *args, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.UntypedStorage.__init__',
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: orig_func(*args, device=return_xpu(device), **kwargs),
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.UntypedStorage.cuda',
|
|
|
|
|
lambda orig_func, self, device=None, *args, **kwargs: orig_func(self, return_xpu(device), *args, **kwargs),
|
|
|
|
|
lambda orig_func, self, device=None, *args, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.empty',
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: orig_func(*args, device=return_xpu(device), **kwargs),
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.load',
|
|
|
|
|
lambda orig_func, *args, map_location=None, **kwargs: orig_func(*args, return_xpu(map_location), **kwargs),
|
|
|
|
|
lambda orig_func, *args, map_location=None, **kwargs: map_location is None or check_device(map_location))
|
|
|
|
|
CondFunc('torch.randn',
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: orig_func(*args, device=return_xpu(device), **kwargs),
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: check_device(device))
|
|
|
|
|
@@ -137,17 +149,19 @@ def ipex_hijacks():
|
|
|
|
|
CondFunc('torch.zeros',
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: orig_func(*args, device=return_xpu(device), **kwargs),
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.tensor',
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: orig_func(*args, device=return_xpu(device), **kwargs),
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.linspace',
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: orig_func(*args, device=return_xpu(device), **kwargs),
|
|
|
|
|
lambda orig_func, *args, device=None, **kwargs: check_device(device))
|
|
|
|
|
CondFunc('torch.load',
|
|
|
|
|
lambda orig_func, f, map_location=None, pickle_module=None, *, weights_only=False, mmap=None, **kwargs:
|
|
|
|
|
orig_func(orig_func, f, map_location=return_xpu(map_location), pickle_module=pickle_module, weights_only=weights_only, mmap=mmap, **kwargs),
|
|
|
|
|
lambda orig_func, f, map_location=None, pickle_module=None, *, weights_only=False, mmap=None, **kwargs: check_device(map_location))
|
|
|
|
|
|
|
|
|
|
CondFunc('torch.Generator',
|
|
|
|
|
lambda orig_func, device=None: torch.xpu.Generator(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")
|
|
|
|
|
|
|
|
|
|
# TiledVAE and ControlNet:
|
|
|
|
|
CondFunc('torch.batch_norm',
|
|
|
|
|
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),
|
|
|
|
|
@@ -163,25 +177,32 @@ def ipex_hijacks():
|
|
|
|
|
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)
|
|
|
|
|
# Training:
|
|
|
|
|
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: input.dtype != self.weight.data.dtype)
|
|
|
|
|
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: input.dtype != self.weight.data.dtype)
|
|
|
|
|
# BF16:
|
|
|
|
|
CondFunc('torch.nn.functional.layer_norm',
|
|
|
|
|
lambda orig_func, input, normalized_shape=None, weight=None, *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:
|
|
|
|
|
weight is not None and input.dtype != weight.data.dtype)
|
|
|
|
|
# SwinIR BF16:
|
|
|
|
|
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: 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():
|
|
|
|
|
CondFunc('torch.from_numpy',
|
|
|
|
|
lambda orig_func, ndarray: orig_func(ndarray.astype('float32')),
|
|
|
|
|
lambda orig_func, ndarray: ndarray.dtype == float)
|
|
|
|
|
|
|
|
|
|
# Broken functions when torch.cuda.is_available is True:
|
|
|
|
|
# Pin Memory:
|
|
|
|
|
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)
|
|
|
|
|
@@ -192,5 +213,6 @@ def ipex_hijacks():
|
|
|
|
|
torch.autocast = ipex_autocast
|
|
|
|
|
torch.cat = torch_cat
|
|
|
|
|
torch.linalg.solve = linalg_solve
|
|
|
|
|
torch.UntypedStorage.is_cuda = is_cuda
|
|
|
|
|
torch.nn.functional.interpolate = interpolate
|
|
|
|
|
torch.backends.cuda.sdp_kernel = return_null_context
|
|
|
|
|
|