From 432b3c9d9e7d44fc05dd0aa0a01a52bdfd74237c Mon Sep 17 00:00:00 2001 From: Nuullll Date: Tue, 8 Aug 2023 13:16:48 +0800 Subject: [PATCH] [IPEX] Fix native windows setup --- installer.py | 25 +++++++++++++++++++++++++ launch.py | 1 + modules/ipex_specific/__init__.py | 4 +++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/installer.py b/installer.py index 644b6b4b8..f41e7ca89 100644 --- a/installer.py +++ b/installer.py @@ -287,6 +287,30 @@ def check_python(): log.debug(f'Git {git_version.replace("git version", "").strip()}') +# Intel hasn't released a corresponding torchvision wheel along with torch and +# ipex wheels, so we have to install official pytorch torchvision as a W/A. +# However, the latest torchvision explicitly requires torch version == 2.0.1, +# which is incompatible with the Intel torch version 2.0.0a0. This will cause +# intel torch to be uninstalled when pip scans the dependencies of torchvision. +# This function will check the torch version and force installing Intel torch +# 2.0.0a0 to avoid the underlying dll version error. +# TODO remove this W/A when Intel releases torchvision wheel for windows. +def fix_ipex_win_torch(): + if not args.use_ipex or not 'win' in sys.platform: + return + try: + ipex_torch_ver = '2.0.0a0' + installed_torch_ver = pkg_resources.get_distribution('torch').version + if not installed_torch_ver.startswith(ipex_torch_ver): + log.warning(f'Incompatible torch version {installed_torch_ver} for ipex windows, reinstalling to {ipex_torch_ver}') + torch_command = os.environ.get('TORCH_COMMAND', 'torch==2.0.0a0 intel_extension_for_pytorch==2.0.110+gitba7f6c1 -f https://developer.intel.com/ipex-whl-stable-xpu') + install(torch_command) + import torch + import intel_extension_for_pytorch as ipex + except Exception as e: + log.warning(e) + + # check torch version def check_torch(): if args.quick: @@ -348,6 +372,7 @@ def check_torch(): import torch log.info(f'Torch {torch.__version__}') if args.use_ipex and allow_ipex: + fix_ipex_win_torch() import intel_extension_for_pytorch as ipex # pylint: disable=import-error, unused-import log.info(f'Torch backend: Intel IPEX {ipex.__version__}') if shutil.which('icpx') is not None: diff --git a/launch.py b/launch.py index a645faf9a..9b495b7a7 100644 --- a/launch.py +++ b/launch.py @@ -184,6 +184,7 @@ if __name__ == "__main__": installer.log.warning(f'Setup complete with errors: {installer.errors}') installer.log.warning(f'See log file for more details: {installer.log_file}') installer.extensions_preload(parser) # adds additional args from extensions + installer.fix_ipex_win_torch() # redo ipex win torch fix since extensions may scan the deps of torchvision args = installer.parse_args(parser) # installer.run_setup() # installer.log.debug(f"Args: {vars(args)}") diff --git a/modules/ipex_specific/__init__.py b/modules/ipex_specific/__init__.py index f7af296c1..aa99f5bab 100644 --- a/modules/ipex_specific/__init__.py +++ b/modules/ipex_specific/__init__.py @@ -1,4 +1,5 @@ import os +import sys import contextlib import torch import intel_extension_for_pytorch as ipex @@ -81,7 +82,8 @@ def ipex_init(): #Fix functions with ipex: 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._utils._get_available_device_type = lambda: "xpu" # pylint: disable=protected-access - torch.xpu.empty_cache = torch.xpu.empty_cache if "WSL2" not in os.popen("uname -a").read() else lambda: None + if 'linux' in sys.platform: + torch.xpu.empty_cache = torch.xpu.empty_cache if "WSL2" not in os.popen("uname -a").read() else lambda: None torch.cuda.get_device_properties.major = 2023 torch.cuda.get_device_properties.minor = 2 torch.backends.cuda.sdp_kernel = return_null_context