Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Intel XPU device support to generate and serve #1361

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions install/install_requirements.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ 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.
PYTORCH_NIGHTLY_VERSION=dev20241002
if [[ -x "$(command -v xpu-smi)" ]];
then
PYTORCH_NIGHTLY_VERSION=dev20241001
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does xpu need an older PYTORCH_NIGHTLY?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when install torch==2.6.0.dev20241002 and torchvision==0.20.0.dev20241002+xpu, it will get error:
ERROR: Cannot install torch==2.6.0.dev20241002 and torchvision==0.20.0.dev20241002+xpu because these package versions have conflicting dependencies.

The conflict is caused by:
The user requested torch==2.6.0.dev20241002
torchvision 0.20.0.dev20241002+xpu depends on torch==2.6.0.dev20241001

So for xpu, I changed the torch nightly version to dev20241001

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me see if I can get you a fresher version on XPU, the torch/vision discrepancy shouldn't be a normal thing

else
PYTORCH_NIGHTLY_VERSION=dev20241002
fi

# Nightly version for torchvision
VISION_NIGHTLY_VERSION=dev20241002
Expand All @@ -85,16 +90,28 @@ then
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.4.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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context on the varying tune version?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On xpu nightly URL, it does not have nightly version of torchtune, so just install 0.3.1 release for xpu environment.

Copy link
Contributor

@Jack-Khuu Jack-Khuu Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm we should add support for nightly, let me ping some torchtune folk

cc: @ebsmothers

)
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.
Expand Down
7 changes: 6 additions & 1 deletion torchchat/cli/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ 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 torch.xpu.is_available():
self.device = "xpu"
else:
self.device = "cpu"

if not (
(self.checkpoint_path and self.checkpoint_path.is_file())
Expand Down
4 changes: 2 additions & 2 deletions torchchat/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def _add_model_config_args(parser, verb: str) -> None:
"--device",
type=str,
default=None,
choices=["fast", "cpu", "cuda", "mps"],
help="Hardware device to use. Options: fast, cpu, cuda, mps",
choices=["fast", "cpu", "cuda", "mps", "xpu"],
help="Hardware device to use. Options: fast, cpu, cuda, mps, xpu",
)


Expand Down
8 changes: 6 additions & 2 deletions torchchat/utils/build_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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:
Expand Down Expand Up @@ -279,7 +281,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:
Expand All @@ -291,7 +294,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)

Expand Down
11 changes: 10 additions & 1 deletion torchchat/utils/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -37,4 +37,13 @@ def get_device_info(device: str) -> str:
)
if device == "cuda":
return torch.cuda.get_device_name(0)
if device == "xpu":
return (
check_output(
["xpu-smi discovery |grep 'Device Name:'"], shell=True
)
.decode("utf-8")
.split("\n")[0]
.split("Device Name:")[1]
)
return ""
Loading