Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

fix setup issue on cuda machine (#511)

Wenhua Cheng5801ee949b49
diff --git a/setup.py b/setup.py
index a68183aff..3b8ec0222 100644
--- a/setup.py
+++ b/setup.py
@@ -4,6 +4,7 @@ import os
from setuptools import find_packages, setup
import sys
from functools import lru_cache
+
os.environ["CC"] = "g++"
os.environ["CXX"] = "g++"
try:
@@ -15,29 +16,12 @@ except Exception as error:
version = __version__
-
# All BUILD_* flags are initially set to `False`` and
# will be updated to `True` if the corresponding environment check passes.
-BUILD_CUDA_EXT = int(os.environ.get("BUILD_CUDA_EXT", "0")) == 1
PYPI_RELEASE = os.environ.get("PYPI_RELEASE", None)
BUILD_HPU_ONLY = os.environ.get("BUILD_HPU_ONLY", "0") == "1"
-def is_cuda_available():
- try:
- import torch
-
- return torch.cuda.is_available()
- except Exception as e:
- print(f"Checking CUDA availability failed: {e}")
- return False
-
-
-if is_cuda_available():
- # When CUDA is available, we build CUDA extension by default
- BUILD_CUDA_EXT = True
-
-
@lru_cache(None)
def is_habana_framework_installed():
"""Check if Habana framework is installed.
@@ -58,10 +42,12 @@ def is_hpu_available():
except ImportError:
return False
+
if is_hpu_available() or is_habana_framework_installed():
# When HPU is available, we build HPU only by default
BUILD_HPU_ONLY = True
+
def is_cpu_env():
try:
import torch
@@ -72,7 +58,7 @@ def is_cpu_env():
if torch.cuda.is_available():
return False
try:
- import habana_frameworks.torch.core as htcore
+ import habana_frameworks.torch.core as htcore
return False
except:
return True
@@ -84,131 +70,6 @@ def fetch_requirements(path):
requirements = [r.strip() for r in fd.readlines()]
return requirements
-def detect_local_sm_architectures():
- """
- Detect compute capabilities of one machine's GPUs as PyTorch does.
-
- Copied from https://github.com/pytorch/pytorch/blob/v2.2.2/torch/utils/cpp_extension.py#L1962-L1976
- """
- arch_list = []
-
- for i in range(torch.cuda.device_count()):
- capability = torch.cuda.get_device_capability(i)
- supported_sm = [int(arch.split('_')[1])
- for arch in torch.cuda.get_arch_list() if 'sm_' in arch]
- max_supported_sm = max((sm // 10, sm % 10) for sm in supported_sm)
- # Capability of the device may be higher than what's supported by the user's
- # NVCC, causing compilation error. User's NVCC is expected to match the one
- # used to build pytorch, so we use the maximum supported capability of pytorch
- # to clamp the capability.
- capability = min(max_supported_sm, capability)
- arch = f'{capability[0]}.{capability[1]}'
- if arch not in arch_list:
- arch_list.append(arch)
-
- arch_list = sorted(arch_list)
- arch_list[-1] += '+PTX'
- return arch_list
-
-
-UNSUPPORTED_COMPUTE_CAPABILITIES = ['3.5', '3.7', '5.0', '5.2', '5.3']
-
-if BUILD_CUDA_EXT:
- try:
- import torch
- except Exception as e:
- print(
- f"Building PyTorch CUDA extension requires PyTorch being installed, please install PyTorch first: {e}.\n NOTE: This issue may be raised due to pip build isolation system (ignoring local packages). Please use `--no-build-isolation` when installing with pip, and refer to https://github.com/intel/auto-round for more details.")
- sys.exit(1)
- if not torch.cuda.is_available():
- print(
- f"set BUILD_CUDA_EXT to False as no cuda device is available")
- BUILD_CUDA_EXT = False
-
-
-if BUILD_CUDA_EXT:
- CUDA_VERSION = None
- ROCM_VERSION = os.environ.get('ROCM_VERSION', None)
- if ROCM_VERSION and not torch.version.hip:
- print(
- f"Trying to compile auto-round for ROCm, but PyTorch {torch.__version__} "
- "is installed without ROCm support."
- )
- sys.exit(1)
-
- if not ROCM_VERSION:
- default_cuda_version = torch.version.cuda
- CUDA_VERSION = "".join(os.environ.get("CUDA_VERSION", default_cuda_version).split("."))
-
- if ROCM_VERSION:
- version += f"+rocm{ROCM_VERSION}"
- else:
- if not CUDA_VERSION:
- print(
- f"Trying to compile auto-round for CUDA, but Pytorch {torch.__version__} "
- "is installed without CUDA support."
- )
- sys.exit(1)
-
- torch_cuda_arch_list = os.environ.get("TORCH_CUDA_ARCH_LIST", None)
- if torch_cuda_arch_list is not None:
- torch_cuda_arch_list = torch_cuda_arch_list.replace(' ', ';')
- archs = torch_cuda_arch_list.split(';')
-
- requested_but_unsupported_archs = {arch for arch in archs if arch in UNSUPPORTED_COMPUTE_CAPABILITIES}
- if len(requested_but_unsupported_archs) > 0:
- raise ValueError(
- f"Trying to compile AutoRound for CUDA compute capabilities {torch_cuda_arch_list}, but AutoRound does not support the compute capabilities {requested_but_unsupported_archs} (AutoRound requires Pascal or higher). Please fix your environment variable TORCH_CUDA_ARCH_LIST (Reference: https://github.com/pytorch/pytorch/blob/v2.2.2/setup.py#L135-L139).")
- else:
- local_arch_list = detect_local_sm_architectures()
- local_but_unsupported_archs = {arch for arch in local_arch_list if arch in UNSUPPORTED_COMPUTE_CAPABILITIES}
- if len(local_but_unsupported_archs) > 0:
- raise ValueError(
- f"PyTorch detected the compute capabilities {local_arch_list} for the NVIDIA GPUs on the current machine, but AutoRound can not be built for compute capabilities {local_but_unsupported_archs} (AutoRound requires Pascal or higher). Please set the environment variable TORCH_CUDA_ARCH_LIST (Reference: https://github.com/pytorch/pytorch/blob/v2.2.2/setup.py#L135-L139) with your necessary architectures.")
-
- # For the PyPI release, the version is simply x.x.x to comply with PEP 440.
- if not PYPI_RELEASE:
- version += f"+cu{CUDA_VERSION}"
-
-additional_setup_kwargs = {}
-include_dirs = ["autoround_cuda"]
-if BUILD_CUDA_EXT:
- from torch.utils import cpp_extension
-
- if not ROCM_VERSION:
- from distutils.sysconfig import get_python_lib
-
- conda_cuda_include_dir = os.path.join(get_python_lib(), "nvidia/cuda_runtime/include")
-
- print("conda_cuda_include_dir", conda_cuda_include_dir)
- if os.path.isdir(conda_cuda_include_dir):
- include_dirs.append(conda_cuda_include_dir)
- print(f"appending conda cuda include dir {conda_cuda_include_dir}")
- if os.name == "nt":
- # On Windows, fix an error LNK2001: unresolved external symbol cublasHgemm bug in the compilation
- cuda_path = os.environ.get("CUDA_PATH", None)
- if cuda_path is None:
- raise ValueError(
- "The environment variable CUDA_PATH must be set to the path to the CUDA install when installing from source on Windows systems.")
- extra_link_args = ["-L", f"{cuda_path}/lib/x64/cublas.lib"]
- else:
- extra_link_args = []
- extensions = []
- extensions.append(
- cpp_extension.CUDAExtension(
- "autoround_exllamav2_kernels",
- [
- "auto_round_extension/cuda/exllamav2/ext.cpp",
- "auto_round_extension/cuda/exllamav2/cuda/q_matrix.cu",
- "auto_round_extension/cuda/exllamav2/cuda/q_gemm.cu",
- ],
- extra_link_args=extra_link_args
- )
- )
- additional_setup_kwargs = {
- "ext_modules": extensions,
- "cmdclass": {'build_ext': cpp_extension.BuildExtension}
- }
PKG_INSTALL_CFG = {
"include_packages": find_packages(
@@ -280,7 +141,6 @@ if __name__ == "__main__":
license="Apache 2.0",
url="https://github.com/intel/auto-round",
packages=include_packages,
- include_dirs=include_dirs,
install_requires=install_requires,
extras_require=extras_require,
python_requires=">=3.7.0",
@@ -292,5 +152,4 @@ if __name__ == "__main__":
],
include_package_data=True,
package_data={"": ["mllm/templates/*.json"]},
- **additional_setup_kwargs,
)