From 778efd69dccf4abb8036d01ebe65ec8f2a212602 Mon Sep 17 00:00:00 2001 From: jenniew Date: Wed, 6 Nov 2024 22:48:34 +0000 Subject: [PATCH 1/7] add xpu --- install/install_requirements.sh | 37 +++++++++++++++++++++------------ torchchat/cli/cli.py | 2 +- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/install/install_requirements.sh b/install/install_requirements.sh index 6344509d8..5b13ba05f 100755 --- a/install/install_requirements.sh +++ b/install/install_requirements.sh @@ -35,9 +35,14 @@ fi # newer version of torch nightly installed later in this script. # +#( +# set -x +# $PIP_EXECUTABLE install -r install/requirements.txt --extra-index-url https://download.pytorch.org/whl/nightly/cu121 +#) + ( set -x - $PIP_EXECUTABLE install -r install/requirements.txt --extra-index-url https://download.pytorch.org/whl/nightly/cu121 + $PIP_EXECUTABLE install -r install/requirements.txt --extra-index-url https://download.pytorch.org/whl/nightly/xpu ) # Since torchchat often uses main-branch features of pytorch, only the nightly @@ -47,7 +52,7 @@ fi # NOTE: If a newly-fetched version of the executorch repo changes the value of # PYTORCH_NIGHTLY_VERSION, you should re-run this script to install the necessary # package versions. -PYTORCH_NIGHTLY_VERSION=dev20241002 +PYTORCH_NIGHTLY_VERSION=dev20241001 # Nightly version for torchvision VISION_NIGHTLY_VERSION=dev20241002 @@ -64,21 +69,21 @@ TUNE_NIGHTLY_VERSION=dev20241010 # The pip repository that hosts nightly torch packages. cpu by default. # If cuda is available, based on presence of nvidia-smi, install the pytorch nightly # with cuda for faster execution on cuda GPUs. -if [[ -x "$(command -v nvidia-smi)" ]]; -then - TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cu121" -elif [[ -x "$(command -v rocminfo)" ]]; -then - TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/rocm6.2" -else - TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cpu" -fi - +#if [[ -x "$(command -v nvidia-smi)" ]]; +#then +# TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cu121" +#elif [[ -x "$(command -v rocminfo)" ]]; +#then +# TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/rocm6.2" +#else +# TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cpu" +#fi +TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/xpu" # pip packages needed by exir. REQUIREMENTS_TO_INSTALL=( torch=="2.6.0.${PYTORCH_NIGHTLY_VERSION}" torchvision=="0.20.0.${VISION_NIGHTLY_VERSION}" - torchtune=="0.4.0.${TUNE_NIGHTLY_VERSION}" + #torchtune=="0.3.0.${TUNE_NIGHTLY_VERSION}" ) # Install the requirements. --extra-index-url tells pip to look for package @@ -89,6 +94,12 @@ REQUIREMENTS_TO_INSTALL=( "${REQUIREMENTS_TO_INSTALL[@]}" ) +( + set -x + $PIP_EXECUTABLE install --extra-index-url "${TORCH_NIGHTLY_URL}" \ + torchtune=="0.3.1" +) + ( set -x $PIP_EXECUTABLE install torchao=="0.5.0" diff --git a/torchchat/cli/cli.py b/torchchat/cli/cli.py index bc41d56ec..cc4681b11 100644 --- a/torchchat/cli/cli.py +++ b/torchchat/cli/cli.py @@ -167,7 +167,7 @@ def _add_model_config_args(parser, verb: str) -> None: "--device", type=str, default=default_device, - choices=["fast", "cpu", "cuda", "mps"], + choices=["fast", "cpu", "cuda", "mps", "xpu"], help="Hardware device to use. Options: cpu, cuda, mps", ) From 7c4e42b4f369c16dba9e02bf6b585fde0562aa26 Mon Sep 17 00:00:00 2001 From: jenniew Date: Sat, 9 Nov 2024 05:44:19 +0000 Subject: [PATCH 2/7] add xpu device --- install/install_requirements.sh | 62 ++++++++++++++++++--------------- torchchat/cli/builder.py | 8 ++++- torchchat/cli/cli.py | 2 +- torchchat/utils/build_utils.py | 8 +++-- torchchat/utils/device_info.py | 18 +++++++++- 5 files changed, 65 insertions(+), 33 deletions(-) diff --git a/install/install_requirements.sh b/install/install_requirements.sh index 5b13ba05f..c487e84fb 100755 --- a/install/install_requirements.sh +++ b/install/install_requirements.sh @@ -35,14 +35,9 @@ fi # newer version of torch nightly installed later in this script. # -#( -# set -x -# $PIP_EXECUTABLE install -r install/requirements.txt --extra-index-url https://download.pytorch.org/whl/nightly/cu121 -#) - ( set -x - $PIP_EXECUTABLE install -r install/requirements.txt --extra-index-url https://download.pytorch.org/whl/nightly/xpu + $PIP_EXECUTABLE install -r install/requirements.txt --extra-index-url https://download.pytorch.org/whl/nightly/cu121 ) # Since torchchat often uses main-branch features of pytorch, only the nightly @@ -52,7 +47,12 @@ fi # NOTE: If a newly-fetched version of the executorch repo changes the value of # PYTORCH_NIGHTLY_VERSION, you should re-run this script to install the necessary # package versions. -PYTORCH_NIGHTLY_VERSION=dev20241001 +if [[ -x "$(command -v xpu-smi)" ]]; +then + PYTORCH_NIGHTLY_VERSION=dev20241001 +else + PYTORCH_NIGHTLY_VERSION=dev20241002 +fi # Nightly version for torchvision VISION_NIGHTLY_VERSION=dev20241002 @@ -69,22 +69,34 @@ TUNE_NIGHTLY_VERSION=dev20241010 # The pip repository that hosts nightly torch packages. cpu by default. # If cuda is available, based on presence of nvidia-smi, install the pytorch nightly # with cuda for faster execution on cuda GPUs. -#if [[ -x "$(command -v nvidia-smi)" ]]; -#then -# TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cu121" -#elif [[ -x "$(command -v rocminfo)" ]]; -#then -# TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/rocm6.2" -#else -# TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cpu" -#fi -TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/xpu" +if [[ -x "$(command -v nvidia-smi)" ]]; +then + TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cu121" +elif [[ -x "$(command -v rocminfo)" ]]; +then + TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/rocm6.2" +elif [[ -x "$(command -v xpu-smi)" ]]; +then + TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/xpu" +else + TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cpu" +fi + # pip packages needed by exir. -REQUIREMENTS_TO_INSTALL=( - torch=="2.6.0.${PYTORCH_NIGHTLY_VERSION}" - torchvision=="0.20.0.${VISION_NIGHTLY_VERSION}" - #torchtune=="0.3.0.${TUNE_NIGHTLY_VERSION}" -) +if [[ -x "$(command -v xpu-smi)" ]]; +then + REQUIREMENTS_TO_INSTALL=( + torch=="2.6.0.${PYTORCH_NIGHTLY_VERSION}" + torchvision=="0.20.0.${VISION_NIGHTLY_VERSION}" + torchtune=="0.3.1" + ) +else + REQUIREMENTS_TO_INSTALL=( + torch=="2.6.0.${PYTORCH_NIGHTLY_VERSION}" + torchvision=="0.20.0.${VISION_NIGHTLY_VERSION}" + torchtune=="0.4.0.${TUNE_NIGHTLY_VERSION}" + ) +fi # Install the requirements. --extra-index-url tells pip to look for package # versions on the provided URL if they aren't available on the default URL. @@ -94,12 +106,6 @@ REQUIREMENTS_TO_INSTALL=( "${REQUIREMENTS_TO_INSTALL[@]}" ) -( - set -x - $PIP_EXECUTABLE install --extra-index-url "${TORCH_NIGHTLY_URL}" \ - torchtune=="0.3.1" -) - ( set -x $PIP_EXECUTABLE install torchao=="0.5.0" diff --git a/torchchat/cli/builder.py b/torchchat/cli/builder.py index 511cf1f35..767d0179e 100644 --- a/torchchat/cli/builder.py +++ b/torchchat/cli/builder.py @@ -68,7 +68,13 @@ class BuilderArgs: def __post_init__(self): if self.device is None: - self.device = "cuda" if torch.cuda.is_available() else "cpu" + # self.device = "cuda" if torch.cuda.is_available() else "cpu" + if torch.cuda.is_available(): + self.device = "cuda" + elif hasattr(torch, 'xpu') and torch.xpu.is_available(): + self.device = "xpu" + else: + self.device = "cpu" if not ( (self.checkpoint_path and self.checkpoint_path.is_file()) diff --git a/torchchat/cli/cli.py b/torchchat/cli/cli.py index cc4681b11..d7dc10f7d 100644 --- a/torchchat/cli/cli.py +++ b/torchchat/cli/cli.py @@ -168,7 +168,7 @@ def _add_model_config_args(parser, verb: str) -> None: type=str, default=default_device, choices=["fast", "cpu", "cuda", "mps", "xpu"], - help="Hardware device to use. Options: cpu, cuda, mps", + help="Hardware device to use. Options: cpu, cuda, mps, xpu", ) diff --git a/torchchat/utils/build_utils.py b/torchchat/utils/build_utils.py index fd30f87d5..abee25cd5 100644 --- a/torchchat/utils/build_utils.py +++ b/torchchat/utils/build_utils.py @@ -203,6 +203,8 @@ def find_multiple(n: int, k: int) -> int: def device_sync(device="cpu"): if "cuda" in device: torch.cuda.synchronize(device) + elif "xpu" in device: + torch.xpu.synchronize(device) elif ("cpu" in device) or ("mps" in device): pass else: @@ -251,7 +253,8 @@ def get_device_str(device) -> str: device = ( "cuda" if torch.cuda.is_available() - else "mps" if is_mps_available() else "cpu" + else "mps" if is_mps_available() + else "xpu" if torch.xpu.is_available() else "cpu" ) return device else: @@ -263,7 +266,8 @@ def get_device(device) -> str: device = ( "cuda" if torch.cuda.is_available() - else "mps" if is_mps_available() else "cpu" + else "mps" if is_mps_available() + else "xpu" if torch.xpu.is_available() else "cpu" ) return torch.device(device) diff --git a/torchchat/utils/device_info.py b/torchchat/utils/device_info.py index 9c5953944..1d4061b17 100644 --- a/torchchat/utils/device_info.py +++ b/torchchat/utils/device_info.py @@ -14,7 +14,7 @@ def get_device_info(device: str) -> str: """Returns a human-readable description of the hardware based on a torch.device.type Args: - device: A torch.device.type string: one of {"cpu", "cuda"}. + device: A torch.device.type string: one of {"cpu", "cuda", "xpu"}. Returns: str: A human-readable description of the hardware or an empty string if the device type is unhandled. @@ -37,4 +37,20 @@ def get_device_info(device: str) -> str: ) if device == "cuda": return torch.cuda.get_device_name(0) + if device == "xpu": + # return ( + # check_output( + # ["sycl-ls | grep gpu"], shell=True + # ) + # .decode("utf-8") + # .split("\n")[0] + # ) + return ( + check_output( + ["xpu-smi discovery |grep 'Device Name:'"], shell=True + ) + .decode("utf-8") + .split("\n")[0] + .split("Device Name:")[1] + ) return "" From 6ed3cda46e77dd9618365eb75efdd7bd1164dd9d Mon Sep 17 00:00:00 2001 From: jenniew Date: Sat, 9 Nov 2024 06:29:10 +0000 Subject: [PATCH 3/7] update --- torchchat/cli/builder.py | 3 +-- torchchat/utils/device_info.py | 7 ------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/torchchat/cli/builder.py b/torchchat/cli/builder.py index 767d0179e..95d0582cf 100644 --- a/torchchat/cli/builder.py +++ b/torchchat/cli/builder.py @@ -68,10 +68,9 @@ class BuilderArgs: def __post_init__(self): if self.device is None: - # self.device = "cuda" if torch.cuda.is_available() else "cpu" if torch.cuda.is_available(): self.device = "cuda" - elif hasattr(torch, 'xpu') and torch.xpu.is_available(): + elif torch.xpu.is_available(): self.device = "xpu" else: self.device = "cpu" diff --git a/torchchat/utils/device_info.py b/torchchat/utils/device_info.py index 1d4061b17..950c03002 100644 --- a/torchchat/utils/device_info.py +++ b/torchchat/utils/device_info.py @@ -38,13 +38,6 @@ def get_device_info(device: str) -> str: if device == "cuda": return torch.cuda.get_device_name(0) if device == "xpu": - # return ( - # check_output( - # ["sycl-ls | grep gpu"], shell=True - # ) - # .decode("utf-8") - # .split("\n")[0] - # ) return ( check_output( ["xpu-smi discovery |grep 'Device Name:'"], shell=True From 20e3eae2c3fd613280831080a3ab8c8f7a461e33 Mon Sep 17 00:00:00 2001 From: Guoqiong Date: Tue, 17 Dec 2024 00:19:15 +0000 Subject: [PATCH 4/7] profile --- torchchat/generate.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/torchchat/generate.py b/torchchat/generate.py index 9b4c6430a..6722192f4 100644 --- a/torchchat/generate.py +++ b/torchchat/generate.py @@ -1094,6 +1094,7 @@ def callback(x, *, done_generating=False): else: torch.profiler._utils._init_for_cuda_graphs() prof = torch.profiler.profile() + print("prof is: ", prof) t0 = time.perf_counter() num_tokens_generated = 0 with prof: @@ -1129,8 +1130,10 @@ def callback(x, *, done_generating=False): if hasattr(prof, "export_chrome_trace"): if self.builder_args.device == "cpu": print(prof.key_averages().table(sort_by="self_cpu_time_total")) - else: + elif self.builder_args.device == "cuda": print(prof.key_averages().table(sort_by="self_cuda_time_total")) + else: + print(prof.key_averages().table(sort_by="self_xpu_time_total")) prof.export_chrome_trace(f"{self.profile}.json") if start_pos >= max_seq_length: From ab61f0f80f1d1a3fba6b786377dbdee63ae625cb Mon Sep 17 00:00:00 2001 From: Guoqiong Date: Mon, 13 Jan 2025 22:38:24 +0000 Subject: [PATCH 5/7] update install --- install/install_requirements.sh | 37 ++++++++++++++++++++++++--------- torchchat/generate.py | 1 - torchchat/utils/quantize.py | 2 +- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/install/install_requirements.sh b/install/install_requirements.sh index 7251b972e..585a05dbd 100755 --- a/install/install_requirements.sh +++ b/install/install_requirements.sh @@ -53,16 +53,16 @@ echo "Using pip executable: $PIP_EXECUTABLE" # package versions. if [[ -x "$(command -v xpu-smi)" ]]; then - PYTORCH_NIGHTLY_VERSION=dev20241217 + PYTORCH_NIGHTLY_VERSION=dev20250110 else PYTORCH_NIGHTLY_VERSION=dev20241218 fi # Nightly version for torchvision -VISION_NIGHTLY_VERSION=dev20241218 +VISION_NIGHTLY_VERSION=dev20250111 # Nightly version for torchtune -TUNE_NIGHTLY_VERSION=dev20241218 +TUNE_NIGHTLY_VERSION=dev20250105 # Uninstall triton, as nightly will depend on pytorch-triton, which is one and the same ( @@ -90,9 +90,9 @@ fi if [[ -x "$(command -v xpu-smi)" ]]; then REQUIREMENTS_TO_INSTALL=( - torch=="2.6.0.${PYTORCH_NIGHTLY_VERSION}" + torch=="2.7.0.${PYTORCH_NIGHTLY_VERSION}" torchvision=="0.22.0.${VISION_NIGHTLY_VERSION}" - torchtune=="0.4.0" + # torchtune=="0.4.0" ) else REQUIREMENTS_TO_INSTALL=( @@ -122,10 +122,28 @@ fi # For torchao need to install from github since nightly build doesn't have macos build. # TODO: Remove this and install nightly build, once it supports macos -( - set -x - $PIP_EXECUTABLE install git+https://github.com/pytorch/ao.git@2f97b0955953fa1a46594a27f0df2bc48d93e79d -) +if [[ -x "$(command -v xpu-smi)" ]]; +then + # install torchao nightly for xpu + ( + set -x + $PIP_EXECUTABLE install --extra-index-url "${TORCH_NIGHTLY_URL}" torchao=="0.8.0.dev20250110" + ) +else + ( + set -x + $PIP_EXECUTABLE install git+https://github.com/pytorch/ao.git@2f97b0955953fa1a46594a27f0df2bc48d93e79d + ) +fi + +# install torchtune from source for xpu +if [[ -x "$(command -v xpu-smi)" ]]; +then + ( + set -x + $PIP_EXECUTABLE install git+https://github.com/pytorch/torchtune + ) +fi if [[ -x "$(command -v nvidia-smi)" ]]; then ( @@ -134,7 +152,6 @@ if [[ -x "$(command -v nvidia-smi)" ]]; then ) fi - ( set -x $PIP_EXECUTABLE install evaluate=="0.4.3" lm-eval=="0.4.2" psutil=="6.0.0" diff --git a/torchchat/generate.py b/torchchat/generate.py index 0c7757372..028d5712e 100644 --- a/torchchat/generate.py +++ b/torchchat/generate.py @@ -1168,7 +1168,6 @@ def callback(x, *, done_generating=False): else: torch.profiler._utils._init_for_cuda_graphs() prof = torch.profiler.profile() - print("prof is: ", prof) t0 = time.perf_counter() num_tokens_generated = 0 with prof: diff --git a/torchchat/utils/quantize.py b/torchchat/utils/quantize.py index ebb74f17f..2940b610c 100644 --- a/torchchat/utils/quantize.py +++ b/torchchat/utils/quantize.py @@ -111,7 +111,7 @@ def quantize_model( raise RuntimeError(f"unknown quantizer {quantizer} specified") else: # Use tensor subclass API for int4 weight only. - if device == "cuda" and quantizer == "linear:int4": + if (device == "cuda" or device == "xpu") and quantizer == "linear:int4": quantize_(model, int4_weight_only(q_kwargs["groupsize"])) if not support_tensor_subclass: unwrap_tensor_subclass(model) From 9f4cde30f560ab8771f85f6bb58c440118e438ac Mon Sep 17 00:00:00 2001 From: Guoqiong Date: Mon, 13 Jan 2025 23:09:18 +0000 Subject: [PATCH 6/7] update --- install/install_requirements.sh | 41 ++++++++------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/install/install_requirements.sh b/install/install_requirements.sh index 585a05dbd..2e5c3ef7a 100755 --- a/install/install_requirements.sh +++ b/install/install_requirements.sh @@ -51,18 +51,13 @@ echo "Using pip executable: $PIP_EXECUTABLE" # NOTE: If a newly-fetched version of the executorch repo changes the value of # PYTORCH_NIGHTLY_VERSION, you should re-run this script to install the necessary # package versions. -if [[ -x "$(command -v xpu-smi)" ]]; -then - PYTORCH_NIGHTLY_VERSION=dev20250110 -else - PYTORCH_NIGHTLY_VERSION=dev20241218 -fi +PYTORCH_NIGHTLY_VERSION=dev20241218 # Nightly version for torchvision -VISION_NIGHTLY_VERSION=dev20250111 +VISION_NIGHTLY_VERSION=dev20241218 # Nightly version for torchtune -TUNE_NIGHTLY_VERSION=dev20250105 +TUNE_NIGHTLY_VERSION=dev20241218 # Uninstall triton, as nightly will depend on pytorch-triton, which is one and the same ( @@ -90,9 +85,9 @@ fi if [[ -x "$(command -v xpu-smi)" ]]; then REQUIREMENTS_TO_INSTALL=( - torch=="2.7.0.${PYTORCH_NIGHTLY_VERSION}" + torch=="2.6.0.${PYTORCH_NIGHTLY_VERSION}" torchvision=="0.22.0.${VISION_NIGHTLY_VERSION}" - # torchtune=="0.4.0" + torchtune=="0.4.0" ) else REQUIREMENTS_TO_INSTALL=( @@ -122,28 +117,10 @@ fi # For torchao need to install from github since nightly build doesn't have macos build. # TODO: Remove this and install nightly build, once it supports macos -if [[ -x "$(command -v xpu-smi)" ]]; -then - # install torchao nightly for xpu - ( - set -x - $PIP_EXECUTABLE install --extra-index-url "${TORCH_NIGHTLY_URL}" torchao=="0.8.0.dev20250110" - ) -else - ( - set -x - $PIP_EXECUTABLE install git+https://github.com/pytorch/ao.git@2f97b0955953fa1a46594a27f0df2bc48d93e79d - ) -fi - -# install torchtune from source for xpu -if [[ -x "$(command -v xpu-smi)" ]]; -then - ( - set -x - $PIP_EXECUTABLE install git+https://github.com/pytorch/torchtune - ) -fi +( + set -x + $PIP_EXECUTABLE install git+https://github.com/pytorch/ao.git@2f97b0955953fa1a46594a27f0df2bc48d93e79d +) if [[ -x "$(command -v nvidia-smi)" ]]; then ( From 5c3306eccc9daca0fa04bed79e2bc1d3b0c4c016 Mon Sep 17 00:00:00 2001 From: Guoqiong Date: Mon, 13 Jan 2025 23:11:50 +0000 Subject: [PATCH 7/7] update --- install/install_requirements.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/install/install_requirements.sh b/install/install_requirements.sh index 2e5c3ef7a..8c69735fc 100755 --- a/install/install_requirements.sh +++ b/install/install_requirements.sh @@ -128,7 +128,6 @@ if [[ -x "$(command -v nvidia-smi)" ]]; then $PYTHON_EXECUTABLE torchchat/utils/scripts/patch_triton.py ) fi - ( set -x $PIP_EXECUTABLE install evaluate=="0.4.3" lm-eval=="0.4.2" psutil=="6.0.0"