-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_avx2.py
More file actions
executable file
·266 lines (218 loc) · 6.9 KB
/
build_avx2.py
File metadata and controls
executable file
·266 lines (218 loc) · 6.9 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
"""
AVX2 Build Script
=================
Automated build script for compiling AUDIOANALYSISX1 with AVX2 optimizations.
Usage:
python build_avx2.py # Build with AVX2
python build_avx2.py --no-avx2 # Build without AVX2
python build_avx2.py --clean # Clean build artifacts
python build_avx2.py --test # Build and run tests
"""
import os
import sys
import subprocess
import argparse
import shutil
from pathlib import Path
class AVX2Builder:
"""Handles building with AVX2 optimizations."""
def __init__(self, enable_avx2=True, verbose=False):
"""
Initialize builder.
Args:
enable_avx2: Enable AVX2 optimizations
verbose: Verbose output
"""
self.enable_avx2 = enable_avx2
self.verbose = verbose
self.root_dir = Path(__file__).parent
def clean(self):
"""Clean build artifacts."""
print("Cleaning build artifacts...")
dirs_to_remove = [
'build',
'dist',
'*.egg-info',
'__pycache__',
'.pytest_cache',
'audioanalysisx1/**/__pycache__',
'audioanalysisx1/**/*.so',
'audioanalysisx1/**/*.pyd',
]
for pattern in dirs_to_remove:
for path in self.root_dir.glob(pattern):
if path.is_dir():
print(f" Removing directory: {path}")
shutil.rmtree(path)
elif path.is_file():
print(f" Removing file: {path}")
path.unlink()
print("Clean complete!")
def check_dependencies(self):
"""Check if required dependencies are installed."""
print("Checking dependencies...")
required = ['numpy', 'setuptools']
missing = []
for package in required:
try:
__import__(package)
print(f" ✓ {package}")
except ImportError:
print(f" ✗ {package} - MISSING")
missing.append(package)
if missing:
print(f"\nMissing dependencies: {', '.join(missing)}")
print("Install with: pip install " + " ".join(missing))
return False
return True
def build(self):
"""Build the package."""
if not self.check_dependencies():
return False
print("\n" + "=" * 70)
print("BUILDING AUDIOANALYSISX1")
print("=" * 70)
print(f"AVX2 Optimization: {'ENABLED' if self.enable_avx2 else 'DISABLED'}")
print("=" * 70 + "\n")
# Set environment variable
env = os.environ.copy()
env['ENABLE_AVX2'] = '1' if self.enable_avx2 else '0'
# Build command
cmd = [sys.executable, 'setup.py', 'build_ext', '--inplace']
if self.verbose:
cmd.append('--verbose')
print(f"Running: {' '.join(cmd)}\n")
try:
result = subprocess.run(
cmd,
env=env,
cwd=self.root_dir,
check=True,
capture_output=not self.verbose
)
if not self.verbose and result.stdout:
print(result.stdout.decode())
print("\n" + "=" * 70)
print("BUILD SUCCESSFUL!")
print("=" * 70)
return True
except subprocess.CalledProcessError as e:
print("\n" + "=" * 70)
print("BUILD FAILED!")
print("=" * 70)
if e.stdout:
print(e.stdout.decode())
if e.stderr:
print(e.stderr.decode())
return False
def install(self, develop=False):
"""Install the package."""
print("\nInstalling package...")
env = os.environ.copy()
env['ENABLE_AVX2'] = '1' if self.enable_avx2 else '0'
mode = 'develop' if develop else 'install'
cmd = [sys.executable, 'setup.py', mode]
try:
subprocess.run(cmd, env=env, cwd=self.root_dir, check=True)
print("Installation successful!")
return True
except subprocess.CalledProcessError:
print("Installation failed!")
return False
def test(self):
"""Run tests."""
print("\nRunning tests...")
try:
# Test CPU detection
print("\n1. Testing CPU detection...")
subprocess.run(
[sys.executable, '-m', 'audioanalysisx1.cpu_features'],
cwd=self.root_dir,
check=True
)
# Test extension import
print("\n2. Testing AVX2 extension import...")
test_code = """
import sys
from audioanalysisx1.extensions import has_avx2_support
if has_avx2_support():
print("✓ AVX2 extensions loaded successfully")
sys.exit(0)
else:
print("✗ AVX2 extensions not available")
sys.exit(1)
"""
result = subprocess.run(
[sys.executable, '-c', test_code],
cwd=self.root_dir,
capture_output=True,
text=True
)
print(result.stdout)
if result.returncode != 0 and self.enable_avx2:
print("Warning: AVX2 extensions not loaded")
print("\nTests completed!")
return True
except subprocess.CalledProcessError as e:
print(f"Tests failed: {e}")
return False
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description='Build AUDIOANALYSISX1 with AVX2 optimizations'
)
parser.add_argument(
'--no-avx2',
action='store_true',
help='Disable AVX2 optimizations'
)
parser.add_argument(
'--clean',
action='store_true',
help='Clean build artifacts'
)
parser.add_argument(
'--test',
action='store_true',
help='Run tests after building'
)
parser.add_argument(
'--install',
action='store_true',
help='Install after building'
)
parser.add_argument(
'--develop',
action='store_true',
help='Install in development mode'
)
parser.add_argument(
'--verbose',
action='store_true',
help='Verbose output'
)
args = parser.parse_args()
builder = AVX2Builder(
enable_avx2=not args.no_avx2,
verbose=args.verbose
)
# Clean if requested
if args.clean:
builder.clean()
if len(sys.argv) == 2: # Only clean requested
return 0
# Build
if not builder.build():
return 1
# Install if requested
if args.install or args.develop:
if not builder.install(develop=args.develop):
return 1
# Test if requested
if args.test:
if not builder.test():
return 1
return 0
if __name__ == '__main__':
sys.exit(main())