-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
171 lines (150 loc) · 5.17 KB
/
Copy pathsetup.py
File metadata and controls
171 lines (150 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Setup script for BitLinear PyTorch extension.
This script builds the C++/CUDA extension using PyTorch's built-in
cpp_extension utilities. It handles:
- CPU-only builds (development)
- CUDA builds (production)
- Conditional compilation based on CUDA availability
"""
import os
import torch
from setuptools import setup, find_packages
from torch.utils.cpp_extension import (
BuildExtension,
CppExtension,
CUDAExtension,
CUDA_HOME,
)
# Package metadata
VERSION = "0.1.0"
DESCRIPTION = "BitLinear: Ultra-Low-Precision Linear Layers for PyTorch"
LONG_DESCRIPTION = """
A research-grade PyTorch extension for ultra-low-precision (1.58-bit) ternary
linear layers inspired by BitNet and recent JMLR work on ternary representations
of neural networks.
Features:
- Drop-in replacement for nn.Linear with ternary weights
- 20x memory compression
- Optimized CUDA kernels for GPU acceleration
- Greedy ternary decomposition for improved expressiveness
"""
# Determine if CUDA is available
def cuda_is_available():
"""Check if CUDA is available for compilation."""
return torch.cuda.is_available() and CUDA_HOME is not None
def get_extensions():
"""
Build extension modules based on CUDA availability.
Returns:
List of extension modules to compile
"""
# Source files
source_dir = os.path.join("bitlinear", "cpp")
sources = [os.path.join(source_dir, "bitlinear.cpp")]
# Compiler flags
extra_compile_args = {
"cxx": ["-O3", "-std=c++17"],
}
# Define macros
define_macros = []
if cuda_is_available():
print("CUDA detected, building with GPU support")
# Add CUDA source
sources.append(os.path.join(source_dir, "bitlinear_kernel.cu"))
# CUDA compiler flags
extra_compile_args["nvcc"] = [
"-O3",
"-std=c++17",
"--use_fast_math",
"-gencode=arch=compute_70,code=sm_70", # V100
"-gencode=arch=compute_75,code=sm_75", # T4, RTX 20xx
"-gencode=arch=compute_80,code=sm_80", # A100
"-gencode=arch=compute_86,code=sm_86", # RTX 30xx
"-gencode=arch=compute_89,code=sm_89", # RTX 40xx
"-gencode=arch=compute_90,code=sm_90", # H100
]
# Define CUDA macro
define_macros.append(("WITH_CUDA", None))
# Create CUDA extension
extension = CUDAExtension(
name="bitlinear_cpp",
sources=sources,
extra_compile_args=extra_compile_args,
define_macros=define_macros,
)
else:
print("CUDA not detected, building CPU-only version")
# Create CPU-only extension
extension = CppExtension(
name="bitlinear_cpp",
sources=sources,
extra_compile_args=extra_compile_args["cxx"],
define_macros=define_macros,
)
return [extension]
# Read requirements
def read_requirements():
"""Read requirements from requirements.txt if it exists."""
req_file = "requirements.txt"
if os.path.exists(req_file):
with open(req_file, "r") as f:
return [line.strip() for line in f if line.strip() and not line.startswith("#")]
return []
# Main setup
setup(
name="bitlinear",
version=VERSION,
author="BitLinear Contributors",
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
url="https://github.com/yourusername/bitlinear", # TODO: Update with actual repo
packages=find_packages(),
ext_modules=get_extensions(),
cmdclass={
"build_ext": BuildExtension.with_options(no_python_abi_suffix=True)
},
install_requires=[
"torch>=2.0.0",
"numpy>=1.20.0",
],
extras_require={
"dev": [
"pytest>=7.0.0",
"pytest-cov>=4.0.0",
"black>=22.0.0",
"flake8>=5.0.0",
"mypy>=0.990",
],
"test": [
"pytest>=7.0.0",
"pytest-cov>=4.0.0",
],
"hf": [
"transformers>=4.30.0",
"huggingface-hub>=0.16.0",
"safetensors>=0.3.0",
"accelerate>=0.20.0",
],
},
python_requires=">=3.8",
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: C++",
"Programming Language :: Python :: Implementation :: CPython",
],
keywords="pytorch deep-learning quantization ternary bitnet transformer",
project_urls={
"Bug Reports": "https://github.com/yourusername/bitlinear/issues",
"Source": "https://github.com/yourusername/bitlinear",
"Documentation": "https://github.com/yourusername/bitlinear/blob/main/README.md",
},
)