Merge branch 'dev' into RUF013

This commit is contained in:
awsr
2026-03-24 07:12:51 -07:00
committed by GitHub
66 changed files with 3643 additions and 3019 deletions
+51 -18
View File
@@ -16,6 +16,28 @@ torch_version[0], torch_version[1] = int(torch_version[0]), int(torch_version[1]
# pylint: disable=protected-access, missing-function-docstring, line-too-long
def return_true(*args, **kwargs):
return True
def return_false(*args, **kwargs):
return False
def return_none(*args, **kwargs):
return None
def return_zero(*args, **kwargs):
return 0
def return_cuda_version(*args, **kwargs):
return (12,1)
def return_xpu_string(*args, **kwargs):
return "xpu"
def return_arch_list(*args, **kwargs):
return ["pvc", "dg2", "ats-m150"]
def ipex_init(): # pylint: disable=too-many-statements
try:
if hasattr(torch, "cuda") and hasattr(torch.cuda, "is_xpu_hijacked") and torch.cuda.is_xpu_hijacked:
@@ -26,9 +48,9 @@ def ipex_init(): # pylint: disable=too-many-statements
# import inductor utils to get around lazy import
from torch._inductor import utils as torch_inductor_utils # pylint: disable=import-error, unused-import # noqa: F401,RUF100
torch._inductor.utils.GPU_TYPES = ["xpu"]
torch._inductor.utils.get_gpu_type = lambda *args, **kwargs: "xpu"
torch._inductor.utils.get_gpu_type = return_xpu_string
from triton import backends as triton_backends # pylint: disable=import-error
triton_backends.backends["nvidia"].driver.is_active = lambda *args, **kwargs: False
triton_backends.backends["nvidia"].driver.is_active = return_false
except Exception:
pass
# Replace cuda with xpu:
@@ -51,15 +73,12 @@ def ipex_init(): # pylint: disable=too-many-statements
torch.cuda.default_generators = torch.xpu.default_generators
torch.cuda.set_stream = torch.xpu.set_stream
torch.cuda.torch = torch.xpu.torch
torch.cuda.Union = torch.xpu.Union
torch.cuda.StreamContext = torch.xpu.StreamContext
torch.cuda.random = torch.xpu.random
torch.cuda._get_device_index = torch.xpu._get_device_index
torch.cuda._lazy_init = torch.xpu._lazy_init
torch.cuda._lazy_call = torch.xpu._lazy_call
torch.cuda._device = torch.xpu._device
torch.cuda._device_t = torch.xpu._device_t
torch.cuda.is_current_stream_capturing = lambda: False
torch.cuda.is_current_stream_capturing = return_false
torch.cuda.__annotations__ = torch.xpu.__annotations__
torch.cuda.__builtins__ = torch.xpu.__builtins__
@@ -141,12 +160,23 @@ def ipex_init(): # pylint: disable=too-many-statements
torch.cuda.memory_summary = torch.xpu.memory_summary
torch.cuda.memory_snapshot = torch.xpu.memory_snapshot
if torch_version[0] < 2 or (torch_version[0] == 2 and torch_version[1] < 11):
torch.cuda.Union = torch.xpu.Union
torch.cuda._device = torch.xpu._device
torch.cuda._device_t = torch.xpu._device_t
# Memory:
if "linux" in sys.platform and "WSL2" in os.popen("uname -a").read():
torch.xpu.empty_cache = lambda: None
torch.xpu.empty_cache = return_none
torch.cuda.empty_cache = torch.xpu.empty_cache
torch.cuda.memory = torch.xpu.memory
if torch_version[0] >= 2 and torch_version[1] >= 8:
old_cpa = torch.cuda.memory.CUDAPluggableAllocator
torch.cuda.memory = torch.xpu.memory
torch.xpu.memory.CUDAPluggableAllocator = old_cpa
else:
torch.cuda.memory = torch.xpu.memory
torch.cuda.memory_stats = torch.xpu.memory_stats
torch.cuda.memory_allocated = torch.xpu.memory_allocated
torch.cuda.max_memory_allocated = torch.xpu.max_memory_allocated
@@ -172,21 +202,24 @@ def ipex_init(): # pylint: disable=too-many-statements
torch.cuda.initial_seed = torch.xpu.initial_seed
# Fix functions with ipex:
# torch.xpu.mem_get_info always returns the total memory as free memory
torch.has_cuda = True
torch.version.cuda = "12.1"
torch.backends.cuda.is_built = lambda *args, **kwargs: True
torch._utils._get_available_device_type = lambda: "xpu"
torch.backends.cuda.is_built = return_true
torch._utils._get_available_device_type = return_xpu_string
torch.xpu.mem_get_info = lambda device=None: [(torch.xpu.get_device_properties(device).total_memory - torch.xpu.memory_reserved(device)), torch.xpu.get_device_properties(device).total_memory]
# torch.xpu.mem_get_info always returns the total memory as free memory
def mem_get_info(device=None):
return [(torch.xpu.get_device_properties(device).total_memory - torch.xpu.memory_reserved(device)), torch.xpu.get_device_properties(device).total_memory]
torch.xpu.mem_get_info = mem_get_info
torch.cuda.mem_get_info = torch.xpu.mem_get_info
torch.cuda.has_half = True
torch.cuda.is_bf16_supported = getattr(torch.xpu, "is_bf16_supported", lambda *args, **kwargs: True)
torch.cuda.is_fp16_supported = lambda *args, **kwargs: True
torch.cuda.get_arch_list = getattr(torch.xpu, "get_arch_list", lambda: ["pvc", "dg2", "ats-m150"])
torch.cuda.get_device_capability = lambda *args, **kwargs: (12,1)
torch.cuda.ipc_collect = lambda *args, **kwargs: None
torch.cuda.utilization = lambda *args, **kwargs: 0
torch.cuda.is_bf16_supported = getattr(torch.xpu, "is_bf16_supported", return_true)
torch.cuda.is_fp16_supported = getattr(torch.xpu, "is_fp16_supported", return_true)
torch.cuda.get_arch_list = getattr(torch.xpu, "get_arch_list", return_arch_list)
torch.cuda.get_device_capability = return_cuda_version
torch.cuda.ipc_collect = return_none
torch.cuda.utilization = return_zero
device_supports_fp64 = ipex_hijacks()
try:
+65 -53
View File
@@ -15,8 +15,10 @@ torch_version[0], torch_version[1] = int(torch_version[0]), int(torch_version[1]
device_supports_fp64 = torch.xpu.has_fp64_dtype() if hasattr(torch.xpu, "has_fp64_dtype") else torch.xpu.get_device_properties(devices.device).has_fp64
# pylint: disable=protected-access, missing-function-docstring, line-too-long, unnecessary-lambda, no-else-return
# pylint: disable=protected-access, missing-function-docstring, line-too-long, no-else-return
def return_false(*args, **kwargs):
return False
@property
def is_cuda(self):
@@ -24,7 +26,7 @@ def is_cuda(self):
def check_device_type(device, device_type: str) -> bool:
if device is None or type(device) not in {str, int, torch.device}:
if device is None or not isinstance(device, (str, int, torch.device)):
return False
else:
return bool(torch.device(device).type == device_type)
@@ -137,24 +139,9 @@ def as_tensor(data, dtype=None, device=None):
return original_as_tensor(data, dtype=dtype, device=device)
original_torch_tensor = torch.tensor
@wraps(torch.tensor)
def torch_tensor(data, *args, dtype=None, device=None, **kwargs):
global device_supports_fp64
if check_cuda(device):
device = return_xpu(device)
if not device_supports_fp64 and check_device_type(device, "xpu"):
if dtype == torch.float64:
dtype = torch.float32
elif dtype is None and (hasattr(data, "dtype") and (data.dtype == torch.float64 or data.dtype == float)):
dtype = torch.float32
return original_torch_tensor(data, *args, dtype=dtype, device=device, **kwargs)
torch.Tensor.original_Tensor_to = torch.Tensor.to
@wraps(torch.Tensor.to)
def Tensor_to(self, device=None, *args, **kwargs):
global device_supports_fp64
if check_cuda(device):
device = return_xpu(device)
if not device_supports_fp64:
@@ -210,6 +197,24 @@ if torch_version[0] > 2 or (torch_version[0] == 2 and torch_version[1] >= 4):
return original_UntypedStorage_cuda(self, device=device, non_blocking=non_blocking, **kwargs)
original_torch_tensor = torch.tensor
@wraps(torch.tensor)
def torch_tensor(data, *args, dtype=None, device=None, **kwargs):
if check_cuda(device):
if not device_supports_fp64 and (dtype == torch.float64 or (dtype is None and getattr(data, "dtype", None) in {torch.float64, float})):
return original_torch_tensor(data, *args, dtype=torch.float32, device=return_xpu(device), **kwargs)
else:
return original_torch_tensor(data, *args, dtype=dtype, device=return_xpu(device), **kwargs)
else:
if (
not device_supports_fp64 and check_device_type(device, "xpu")
and (dtype == torch.float64 or (dtype is None and getattr(data, "dtype", None) in {torch.float64, float}))
):
return original_torch_tensor(data, *args, dtype=torch.float32, device=device, **kwargs)
else:
return original_torch_tensor(data, *args, dtype=dtype, device=device, **kwargs)
original_torch_empty = torch.empty
@wraps(torch.empty)
def torch_empty(*args, device=None, **kwargs):
@@ -221,11 +226,11 @@ def torch_empty(*args, device=None, **kwargs):
original_torch_randn = torch.randn
@wraps(torch.randn)
def torch_randn(*args, device=None, dtype=None, **kwargs):
def torch_randn(*args, device=None, **kwargs):
if check_cuda(device):
return original_torch_randn(*args, device=return_xpu(device), dtype=dtype, **kwargs)
return original_torch_randn(*args, device=return_xpu(device), **kwargs)
else:
return original_torch_randn(*args, device=device, dtype=dtype, **kwargs)
return original_torch_randn(*args, device=device, **kwargs)
original_torch_ones = torch.ones
@@ -255,34 +260,6 @@ def torch_full(*args, device=None, **kwargs):
return original_torch_full(*args, device=device, **kwargs)
original_torch_arange = torch.arange
@wraps(torch.arange)
def torch_arange(*args, device=None, dtype=None, **kwargs):
global device_supports_fp64
if check_cuda(device):
if not device_supports_fp64 and dtype == torch.float64:
dtype = torch.float32
return original_torch_arange(*args, device=return_xpu(device), dtype=dtype, **kwargs)
else:
if not device_supports_fp64 and check_device_type(device, "xpu") and dtype == torch.float64:
dtype = torch.float32
return original_torch_arange(*args, device=device, dtype=dtype, **kwargs)
original_torch_linspace = torch.linspace
@wraps(torch.linspace)
def torch_linspace(*args, device=None, dtype=None, **kwargs):
global device_supports_fp64
if check_cuda(device):
if not device_supports_fp64 and dtype == torch.float64:
dtype = torch.float32
return original_torch_linspace(*args, device=return_xpu(device), dtype=dtype, **kwargs)
else:
if not device_supports_fp64 and check_device_type(device, "xpu") and dtype == torch.float64:
dtype = torch.float32
return original_torch_linspace(*args, device=device, dtype=dtype, **kwargs)
original_torch_eye = torch.eye
@wraps(torch.eye)
def torch_eye(*args, device=None, **kwargs):
@@ -292,6 +269,36 @@ def torch_eye(*args, device=None, **kwargs):
return original_torch_eye(*args, device=device, **kwargs)
original_torch_arange = torch.arange
@wraps(torch.arange)
def torch_arange(*args, dtype=None, device=None, **kwargs):
if check_cuda(device):
if not device_supports_fp64 and dtype == torch.float64:
return original_torch_arange(*args, dtype=torch.float32, device=return_xpu(device), **kwargs)
else:
return original_torch_arange(*args, dtype=dtype, device=return_xpu(device), **kwargs)
else:
if not device_supports_fp64 and check_device_type(device, "xpu") and dtype == torch.float64:
return original_torch_arange(*args, dtype=torch.float32, device=device, **kwargs)
else:
return original_torch_arange(*args, dtype=dtype, device=device, **kwargs)
original_torch_linspace = torch.linspace
@wraps(torch.linspace)
def torch_linspace(*args, dtype=None, device=None, **kwargs):
if check_cuda(device):
if not device_supports_fp64 and dtype == torch.float64:
return original_torch_linspace(*args, dtype=torch.float32, device=return_xpu(device), **kwargs)
else:
return original_torch_linspace(*args, dtype=dtype, device=return_xpu(device), **kwargs)
else:
if not device_supports_fp64 and check_device_type(device, "xpu") and dtype == torch.float64:
return original_torch_linspace(*args, dtype=torch.float32, device=device, **kwargs)
else:
return original_torch_linspace(*args, dtype=dtype, device=device, **kwargs)
original_torch_load = torch.load
@wraps(torch.load)
def torch_load(f, map_location=None, *args, **kwargs):
@@ -360,24 +367,29 @@ class torch_Generator(original_torch_Generator):
# Hijack Functions:
def ipex_hijacks():
global device_supports_fp64
torch.UntypedStorage.__init__ = UntypedStorage_init
if torch_version[0] > 2 or (torch_version[0] == 2 and torch_version[1] >= 4):
torch.UntypedStorage.cuda = UntypedStorage_cuda
torch.UntypedStorage.to = UntypedStorage_to
torch.tensor = torch_tensor
torch.Tensor.to = Tensor_to
torch.Tensor.cuda = Tensor_cuda
torch.Tensor.pin_memory = Tensor_pin_memory
torch.UntypedStorage.__init__ = UntypedStorage_init
# transformers completely breaks when anything is done to torch.tensor
# even straight passthroughs breaks transformers for some reason
#torch.tensor = torch_tensor
torch.empty = torch_empty
torch.randn = torch_randn
torch.ones = torch_ones
torch.zeros = torch_zeros
torch.full = torch_full
torch.eye = torch_eye
torch.arange = torch_arange
torch.linspace = torch_linspace
torch.eye = torch_eye
torch.load = torch_load
torch.cuda.synchronize = torch_cuda_synchronize
torch.cuda.device = torch_cuda_device
torch.cuda.set_device = torch_cuda_set_device
@@ -437,6 +449,6 @@ def ipex_hijacks():
if not hasattr(torch.cuda.amp, "common"):
torch.cuda.amp.common = nullcontext()
torch.cuda.amp.common.amp_definitely_not_available = lambda: False
torch.cuda.amp.common.amp_definitely_not_available = return_false
return device_supports_fp64
+14 -95
View File
@@ -1,13 +1,11 @@
import os
import sys
import torch
import nncf
from openvino.frontend.pytorch.torchdynamo.partition import Partitioner
from openvino.frontend.pytorch.fx_decoder import TorchFXPythonDecoder
from openvino.frontend import FrontEndManager
from openvino import Core, Type, PartialShape, serialize
from openvino.properties import hint as ov_hints
from openvino.frontend import FrontEndManager # pylint: disable=no-name-in-module
from openvino import Core, Type, PartialShape, serialize # pylint: disable=no-name-in-module
from openvino.properties import hint as ov_hints # pylint: disable=no-name-in-module
from torch._dynamo.backends.common import fake_tensor_unsupported
from torch._dynamo.backends.registry import register_backend
@@ -23,25 +21,6 @@ from modules import shared, devices, sd_models_utils
from modules.logger import log
# importing openvino.runtime forces DeprecationWarning to "always"
# And Intel's own libs (NNCF) imports the deprecated module
# Don't allow openvino to override warning filters:
try:
import warnings
filterwarnings = warnings.filterwarnings
warnings.filterwarnings = lambda *args, **kwargs: None
import openvino.runtime # pylint: disable=unused-import
installer.torch_info.set(openvino=openvino.runtime.get_version())
warnings.filterwarnings = filterwarnings
except Exception:
pass
try:
# silence the pytorch version warning
nncf.common.logging.logger.warn_bkc_version_mismatch = lambda *args, **kwargs: None
except Exception:
pass
# Set default params
torch._dynamo.config.cache_size_limit = max(64, torch._dynamo.config.cache_size_limit) # pylint: disable=protected-access
torch._dynamo.eval_frame.check_if_dynamo_supported = lambda: True # pylint: disable=protected-access
@@ -213,11 +192,7 @@ def execute_cached(compiled_model, *args):
def openvino_compile(gm: GraphModule, *example_inputs, model_hash_str: str | None = None, file_name=""):
core = Core()
device = get_device()
global dont_use_4bit_nncf
global dont_use_nncf
global dont_use_quant
if file_name is not None and os.path.isfile(file_name + ".xml") and os.path.isfile(file_name + ".bin"):
om = core.read_model(file_name + ".xml")
@@ -259,26 +234,6 @@ def openvino_compile(gm: GraphModule, *example_inputs, model_hash_str: str | Non
om.inputs[idx-idx_minus].get_node().set_partial_shape(PartialShape(list(input_data.shape)))
om.validate_nodes_and_infer_types()
if shared.opts.nncf_quantize and not dont_use_quant:
new_inputs = []
for idx, _ in enumerate(example_inputs):
new_inputs.append(example_inputs[idx].detach().cpu().numpy())
new_inputs = [new_inputs]
if shared.opts.nncf_quantize_mode == "INT8":
om = nncf.quantize(om, nncf.Dataset(new_inputs))
else:
om = nncf.quantize(om, nncf.Dataset(new_inputs), mode=getattr(nncf.QuantizationMode, shared.opts.nncf_quantize_mode),
advanced_parameters=nncf.quantization.advanced_parameters.AdvancedQuantizationParameters(
overflow_fix=nncf.quantization.advanced_parameters.OverflowFix.DISABLE, backend_params=None))
if shared.opts.nncf_compress_weights and not dont_use_nncf:
if dont_use_4bit_nncf or shared.opts.nncf_compress_weights_mode == "INT8":
om = nncf.compress_weights(om)
else:
compress_group_size = shared.opts.nncf_compress_weights_group_size if shared.opts.nncf_compress_weights_group_size != 0 else None
compress_ratio = shared.opts.nncf_compress_weights_raito if shared.opts.nncf_compress_weights_raito != 0 else None
om = nncf.compress_weights(om, mode=getattr(nncf.CompressWeightsMode, shared.opts.nncf_compress_weights_mode), group_size=compress_group_size, ratio=compress_ratio)
hints = {}
if shared.opts.openvino_accuracy == "performance":
hints[ov_hints.execution_mode] = ov_hints.ExecutionMode.PERFORMANCE
@@ -287,9 +242,6 @@ def openvino_compile(gm: GraphModule, *example_inputs, model_hash_str: str | Non
if model_hash_str is not None:
hints['CACHE_DIR'] = shared.opts.openvino_cache_path + '/blob'
core.set_property(hints)
dont_use_nncf = False
dont_use_quant = False
dont_use_4bit_nncf = False
compiled_model = core.compile_model(om, device)
return compiled_model
@@ -299,44 +251,17 @@ def openvino_compile_cached_model(cached_model_path, *example_inputs):
core = Core()
om = core.read_model(cached_model_path + ".xml")
global dont_use_4bit_nncf
global dont_use_nncf
global dont_use_quant
for idx, input_data in enumerate(example_inputs):
om.inputs[idx].get_node().set_element_type(dtype_mapping[input_data.dtype])
om.inputs[idx].get_node().set_partial_shape(PartialShape(list(input_data.shape)))
om.validate_nodes_and_infer_types()
if shared.opts.nncf_quantize and not dont_use_quant:
new_inputs = []
for idx, _ in enumerate(example_inputs):
new_inputs.append(example_inputs[idx].detach().cpu().numpy())
new_inputs = [new_inputs]
if shared.opts.nncf_quantize_mode == "INT8":
om = nncf.quantize(om, nncf.Dataset(new_inputs))
else:
om = nncf.quantize(om, nncf.Dataset(new_inputs), mode=getattr(nncf.QuantizationMode, shared.opts.nncf_quantize_mode),
advanced_parameters=nncf.quantization.advanced_parameters.AdvancedQuantizationParameters(
overflow_fix=nncf.quantization.advanced_parameters.OverflowFix.DISABLE, backend_params=None))
if shared.opts.nncf_compress_weights and not dont_use_nncf:
if dont_use_4bit_nncf or shared.opts.nncf_compress_weights_mode == "INT8":
om = nncf.compress_weights(om)
else:
compress_group_size = shared.opts.nncf_compress_weights_group_size if shared.opts.nncf_compress_weights_group_size != 0 else None
compress_ratio = shared.opts.nncf_compress_weights_raito if shared.opts.nncf_compress_weights_raito != 0 else None
om = nncf.compress_weights(om, mode=getattr(nncf.CompressWeightsMode, shared.opts.nncf_compress_weights_mode), group_size=compress_group_size, ratio=compress_ratio)
hints = {'CACHE_DIR': shared.opts.openvino_cache_path + '/blob'}
if shared.opts.openvino_accuracy == "performance":
hints[ov_hints.execution_mode] = ov_hints.ExecutionMode.PERFORMANCE
elif shared.opts.openvino_accuracy == "accuracy":
hints[ov_hints.execution_mode] = ov_hints.ExecutionMode.ACCURACY
core.set_property(hints)
dont_use_nncf = False
dont_use_quant = False
dont_use_4bit_nncf = False
compiled_model = core.compile_model(om, get_device())
return compiled_model
@@ -462,14 +387,8 @@ def get_subgraph_type(tensor):
@fake_tensor_unsupported
def openvino_fx(subgraph, example_inputs, options=None):
global dont_use_4bit_nncf
global dont_use_nncf
global dont_use_quant
global subgraph_type
dont_use_4bit_nncf = False
dont_use_nncf = False
dont_use_quant = False
dont_use_faketensors = False
executor_parameters = None
inputs_reversed = False
@@ -478,25 +397,25 @@ def openvino_fx(subgraph, example_inputs, options=None):
subgraph_type = []
subgraph.apply(get_subgraph_type)
"""
# SD 1.5 / SDXL VAE
if (subgraph_type[0] is torch.nn.modules.conv.Conv2d and
if (
subgraph_type[0] is torch.nn.modules.conv.Conv2d and
subgraph_type[1] is torch.nn.modules.conv.Conv2d and
subgraph_type[2] is torch.nn.modules.normalization.GroupNorm and
subgraph_type[3] is torch.nn.modules.activation.SiLU):
dont_use_4bit_nncf = True
dont_use_nncf = bool("VAE" not in shared.opts.nncf_compress_weights)
dont_use_quant = bool("VAE" not in shared.opts.nncf_quantize)
subgraph_type[3] is torch.nn.modules.activation.SiLU
):
pass
"""
# SD 1.5 / SDXL Text Encoder
elif (subgraph_type[0] is torch.nn.modules.sparse.Embedding and
if (
subgraph_type[0] is torch.nn.modules.sparse.Embedding and
subgraph_type[1] is torch.nn.modules.sparse.Embedding and
subgraph_type[2] is torch.nn.modules.normalization.LayerNorm and
subgraph_type[3] is torch.nn.modules.linear.Linear):
subgraph_type[3] is torch.nn.modules.linear.Linear
):
dont_use_faketensors = True
dont_use_nncf = bool("TE" not in shared.opts.nncf_compress_weights)
dont_use_quant = bool("TE" not in shared.opts.nncf_quantize)
# Create a hash to be used for caching
shared.compiled_model_state.model_hash_str = ""