Skip to content

Commit 5e3afa0

Browse files
authored
[build] fix aphrodite-kernels wheel installation for pypi compat (#1606)
* [build] fix aphrodite-kernels wheel installation for pypi compat Signed-off-by: AlpinDale <alpindale@gmail.com> * address comment Signed-off-by: AlpinDale <alpindale@gmail.com> * some cleanup Signed-off-by: AlpinDale <alpindale@gmail.com> --------- Signed-off-by: AlpinDale <alpindale@gmail.com>
1 parent 3cce659 commit 5e3afa0

5 files changed

Lines changed: 114 additions & 110 deletions

File tree

aphrodite/platforms/cuda.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,33 @@
33
"""
44

55
import os
6+
import sys
67
from collections.abc import Callable
78
from functools import cache, wraps
89
from typing import TYPE_CHECKING, TypeVar
910

11+
from aphrodite.utils.system_utils import get_kernels_install_command
12+
1013
# import custom ops, trigger op registration
11-
import aphrodite_kernels._C # noqa
14+
try:
15+
import aphrodite_kernels._C # noqa
16+
except ImportError:
17+
install_cmd = get_kernels_install_command()
18+
error_msg = f"Failed to import aphrodite_kernels. Please install it using:\n {install_cmd}"
19+
20+
# Override excepthook to suppress traceback for this specific error
21+
original_excepthook = sys.excepthook
22+
23+
def clean_excepthook(exc_type, exc_value, exc_traceback):
24+
# Check if this is our kernels import error by checking the message
25+
if exc_type is ImportError and exc_value and "Failed to import aphrodite_kernels" in str(exc_value):
26+
print(str(exc_value), file=sys.stderr)
27+
sys.exit(1)
28+
else:
29+
original_excepthook(exc_type, exc_value, exc_traceback)
30+
31+
sys.excepthook = clean_excepthook
32+
raise ImportError(error_msg) from None
1233
import torch
1334
from typing_extensions import ParamSpec
1435

aphrodite/utils/system_utils.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import sys
88
from collections.abc import Callable, Iterator
99
from pathlib import Path
10+
from shutil import which
1011
from typing import TextIO
1112

1213
import psutil
14+
import regex as re
1315

1416
import aphrodite.envs as envs
1517
from aphrodite.logger import init_logger
@@ -297,3 +299,85 @@ def set_ulimit(target_soft_limit: int = 65535):
297299
current_soft,
298300
e,
299301
)
302+
303+
304+
def get_kernels_install_command() -> str:
305+
"""Get the install command for aphrodite-kernels based on the current environment."""
306+
307+
base_url = "https://downloads.pygmalion.chat/whl"
308+
install_page = "https://aphrodite.pygmalion.chat/installation/installation/"
309+
310+
# Detect if uv is available
311+
pip_cmd = "uv pip" if which("uv") else "pip"
312+
313+
try:
314+
import torch
315+
316+
# Check for CPU build - no pre-built wheels available
317+
if torch.version.cuda is None and torch.version.hip is None:
318+
return (
319+
"Pre-built wheels for CPU are not available.\n"
320+
f"Please build aphrodite-kernels from source. See: {install_page}"
321+
)
322+
323+
# Check for ROCm - no pre-built wheels available
324+
if torch.version.hip is not None:
325+
return (
326+
"Pre-built wheels for ROCm are not available.\n"
327+
f"Please build aphrodite-kernels from source. See: {install_page}"
328+
)
329+
330+
# Check for CUDA
331+
if torch.version.cuda:
332+
cuda_major, cuda_minor = torch.version.cuda.split(".")
333+
cuda_version_str = f"{cuda_major}{cuda_minor}"
334+
335+
# Support CUDA 12.8 and 12.9
336+
if cuda_version_str == "129":
337+
extra_index_url = f"{base_url}/cu129"
338+
return f"{pip_cmd} install --extra-index-url {extra_index_url} aphrodite-kernels==0.0.1"
339+
elif cuda_version_str == "128":
340+
# Default (12.8) uses base URL without /cu128 suffix
341+
extra_index_url = base_url
342+
return f"{pip_cmd} install --extra-index-url {extra_index_url} aphrodite-kernels==0.0.1"
343+
344+
# Try to detect from nvcc if available
345+
try:
346+
import subprocess
347+
348+
from torch.utils.cpp_extension import CUDA_HOME
349+
350+
if CUDA_HOME:
351+
nvcc_output = subprocess.check_output(
352+
[f"{CUDA_HOME}/bin/nvcc", "-V"], universal_newlines=True, stderr=subprocess.DEVNULL
353+
)
354+
version_match = re.search(r"release (\d+\.\d+)", nvcc_output)
355+
if version_match:
356+
nvcc_version = version_match.group(1)
357+
nvcc_major, nvcc_minor = nvcc_version.split(".")
358+
nvcc_version_str = f"{nvcc_major}{nvcc_minor}"
359+
if nvcc_version_str == "129":
360+
extra_index_url = f"{base_url}/cu129"
361+
return f"{pip_cmd} install --extra-index-url {extra_index_url} aphrodite-kernels==0.0.1"
362+
elif nvcc_version_str == "128":
363+
extra_index_url = base_url
364+
return f"{pip_cmd} install --extra-index-url {extra_index_url} aphrodite-kernels==0.0.1"
365+
except (subprocess.CalledProcessError, FileNotFoundError, ImportError) as e:
366+
logger.warning(
367+
"nvcc-based CUDA version detection failed. This is expected if nvcc is not in your PATH. Error: %s",
368+
e,
369+
)
370+
pass
371+
372+
# Unsupported CUDA version - direct to build from source
373+
return (
374+
f"Your CUDA version ({torch.version.cuda}) is not supported by pre-built wheels.\n"
375+
f"Only CUDA 12.8 and 12.9 are supported. "
376+
f"Please build aphrodite-kernels from source. See: {install_page}"
377+
)
378+
except ImportError:
379+
pass
380+
381+
# Fallback - default to 12.8
382+
extra_index_url = base_url
383+
return f"{pip_cmd} install --extra-index-url {extra_index_url} aphrodite-kernels==0.0.1"

requirements/cu128.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
aphrodite-kernels @ https://downloads.pygmalion.chat/whl/aphrodite_kernels/aphrodite_kernels-0.0.1-cp38-abi3-manylinux1_x86_64.whl
1+
--extra-index-url https://downloads.pygmalion.chat/whl/
2+
aphrodite-kernels == 0.0.1

requirements/cu129.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
aphrodite-kernels @ https://downloads.pygmalion.chat/whl/cu129/aphrodite_kernels/aphrodite_kernels-0.0.1-cp38-abi3-manylinux1_x86_64.whl
2-
1+
--extra-index-url https://downloads.pygmalion.chat/whl/cu129/
2+
aphrodite-kernels == 0.0.1

setup.py

Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -512,54 +512,6 @@ def get_aphrodite_version() -> str:
512512
return version
513513

514514

515-
def get_kernels_extra_index_url() -> str | None:
516-
"""Get the extra index URL for aphrodite-kernels wheels based on target device."""
517-
base_url = "https://downloads.pygmalion.chat/whl"
518-
519-
if _is_cpu():
520-
return f"{base_url}/cpu"
521-
elif _is_hip():
522-
return f"{base_url}/rocm"
523-
elif _is_cuda():
524-
# Determine CUDA version for wheel URL
525-
cuda_version = None
526-
if torch.version.cuda:
527-
cuda_major, cuda_minor = torch.version.cuda.split(".")
528-
cuda_version_str = f"{cuda_major}{cuda_minor}"
529-
# Support CUDA 12.6, 12.8, and 12.9
530-
if cuda_version_str in ["126", "128", "129"]:
531-
cuda_version = cuda_version_str
532-
else:
533-
# Fallback to detected version or default
534-
try:
535-
nvcc_version = get_nvcc_cuda_version()
536-
if nvcc_version >= Version("12.9"):
537-
cuda_version = "129"
538-
elif nvcc_version >= Version("12.8"):
539-
cuda_version = "128"
540-
elif nvcc_version >= Version("12.6"):
541-
cuda_version = "126"
542-
except Exception:
543-
pass
544-
545-
# Use MAIN_CUDA_VERSION as fallback
546-
if cuda_version is None:
547-
main_cuda = envs.APHRODITE_MAIN_CUDA_VERSION
548-
if main_cuda == "12.9":
549-
cuda_version = "129"
550-
elif main_cuda == "12.8":
551-
cuda_version = "128"
552-
elif main_cuda == "12.6":
553-
cuda_version = "126"
554-
else:
555-
# Default to 12.8
556-
cuda_version = "128"
557-
558-
return f"{base_url}/cu{cuda_version}"
559-
560-
return None
561-
562-
563515
def get_requirements() -> list[str]:
564516
"""Get Python package dependencies from requirements.txt."""
565517
requirements_dir = ROOT_DIR / "requirements"
@@ -589,48 +541,6 @@ def _read_requirements(filename: str) -> list[str]:
589541
modified_requirements.append(req)
590542
requirements = modified_requirements
591543

592-
# Add CUDA-specific kernels requirements file based on detected CUDA version
593-
cuda_version = None
594-
if torch.version.cuda:
595-
cuda_major, cuda_minor = torch.version.cuda.split(".")
596-
cuda_version_str = f"{cuda_major}{cuda_minor}"
597-
if cuda_version_str == "128":
598-
cuda_version = "128"
599-
elif cuda_version_str == "129":
600-
cuda_version = "129"
601-
else:
602-
# Try to detect from nvcc
603-
try:
604-
nvcc_version = get_nvcc_cuda_version()
605-
if nvcc_version >= Version("12.9"):
606-
cuda_version = "129"
607-
elif nvcc_version >= Version("12.8"):
608-
cuda_version = "128"
609-
except Exception:
610-
pass
611-
612-
# Use MAIN_CUDA_VERSION as fallback
613-
if cuda_version is None:
614-
main_cuda = envs.APHRODITE_MAIN_CUDA_VERSION
615-
if main_cuda == "12.9":
616-
cuda_version = "129"
617-
elif main_cuda == "12.8":
618-
cuda_version = "128"
619-
620-
# Add kernels requirements file if CUDA version is supported
621-
if cuda_version == "128":
622-
requirements += _read_requirements("cu128.txt")
623-
elif cuda_version == "129":
624-
requirements += _read_requirements("cu129.txt")
625-
else:
626-
# If cuda_version is None or unsupported, don't add kernels
627-
# User will need to build from source
628-
logger.warning(
629-
"No pre-built aphrodite-kernels wheels available for CUDA version %s. "
630-
"Only CUDA 12.8 and 12.9 are supported. "
631-
"You will need to build aphrodite-kernels from source.",
632-
torch.version.cuda if torch.version.cuda else "unknown",
633-
)
634544
elif _is_hip():
635545
requirements = _read_requirements("rocm.txt")
636546
elif _is_hpu():
@@ -644,27 +554,15 @@ def _read_requirements(filename: str) -> list[str]:
644554
else:
645555
raise ValueError("Unsupported platform, please use CUDA, ROCm, or CPU.")
646556

557+
# Filter out aphrodite-kernels from install_requires
558+
# Users will get a helpful error message when they try to import it
559+
requirements = [req for req in requirements if "aphrodite-kernels" not in req.lower()]
560+
647561
return requirements
648562

649563

650564
ext_modules = []
651565

652-
# Determine extra index URL for aphrodite-kernels wheels
653-
kernels_extra_index_url = get_kernels_extra_index_url()
654-
if kernels_extra_index_url:
655-
current_extra_index = os.environ.get("PIP_EXTRA_INDEX_URL", "")
656-
if kernels_extra_index_url not in current_extra_index:
657-
if current_extra_index:
658-
os.environ["PIP_EXTRA_INDEX_URL"] = f"{current_extra_index} {kernels_extra_index_url}"
659-
else:
660-
os.environ["PIP_EXTRA_INDEX_URL"] = kernels_extra_index_url
661-
logger.info(
662-
"Added extra index URL for aphrodite-kernels: %s\n"
663-
"To install with this index URL, use: pip install --extra-index-url %s .",
664-
kernels_extra_index_url,
665-
kernels_extra_index_url,
666-
)
667-
668566
package_data = {
669567
"aphrodite": [
670568
"endpoints/kobold/klite.embd",

0 commit comments

Comments
 (0)