forked from PufferAI/PufferLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
281 lines (250 loc) · 8.29 KB
/
setup.py
File metadata and controls
281 lines (250 loc) · 8.29 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Debug command:
# DEBUG=1 python setup.py build_ext --inplace --force
# CUDA_VISIBLE_DEVICES=None LD_PRELOAD=$(gcc -print-file-name=libasan.so) python3.12 -m pufferlib.clean_pufferl eval --train.device cpu
from setuptools import find_packages, find_namespace_packages, setup, Extension
import numpy
import os
import glob
import urllib.request
import zipfile
import tarfile
import platform
import shutil
from setuptools.command.build_ext import build_ext
from torch.utils import cpp_extension
from torch.utils.cpp_extension import (
CppExtension,
CUDAExtension,
BuildExtension,
CUDA_HOME,
)
# Build with DEBUG=1 to enable debug symbols
DEBUG = os.getenv("DEBUG", "0") == "1"
NO_OCEAN = os.getenv("NO_OCEAN", "0") == "1"
NO_TRAIN = os.getenv("NO_TRAIN", "0") == "1"
# Build raylib for your platform
RAYLIB_URL = 'https://github.com/raysan5/raylib/releases/download/5.5/'
RAYLIB_NAME = 'raylib-5.5_macos' if platform.system() == "Darwin" else 'raylib-5.5_linux_amd64'
RLIGHTS_URL = 'https://raw.githubusercontent.com/raysan5/raylib/refs/heads/master/examples/shaders/rlights.h'
def download_raylib(platform, ext):
if not os.path.exists(platform):
print(f'Downloading Raylib {platform}')
urllib.request.urlretrieve(RAYLIB_URL + platform + ext, platform + ext)
if ext == '.zip':
with zipfile.ZipFile(platform + ext, 'r') as zip_ref:
zip_ref.extractall()
else:
with tarfile.open(platform + ext, 'r') as tar_ref:
tar_ref.extractall()
os.remove(platform + ext)
urllib.request.urlretrieve(RLIGHTS_URL, platform + '/include/rlights.h')
if not NO_OCEAN:
download_raylib('raylib-5.5_webassembly', '.zip')
download_raylib(RAYLIB_NAME, '.tar.gz')
BOX2D_URL = 'https://github.com/capnspacehook/box2d/releases/latest/download/'
BOX2D_NAME = 'box2d-macos-arm64' if platform.system() == "Darwin" else 'box2d-linux-amd64'
def download_box2d(platform):
if not os.path.exists(platform):
ext = ".tar.gz"
print(f'Downloading Box2D {platform}')
urllib.request.urlretrieve(BOX2D_URL + platform + ext, platform + ext)
with tarfile.open(platform + ext, 'r') as tar_ref:
tar_ref.extractall()
os.remove(platform + ext)
if not NO_OCEAN:
download_box2d('box2d-web')
download_box2d(BOX2D_NAME)
# Shared compile args for all platforms
extra_compile_args = [
'-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION',
'-DPLATFORM_DESKTOP',
]
extra_link_args = [
'-fwrapv'
]
cxx_args = [
'-fdiagnostics-color=always',
]
nvcc_args = []
if DEBUG:
extra_compile_args += [
'-O0',
'-g',
'-fsanitize=address,undefined,bounds,pointer-overflow,leak',
'-fno-omit-frame-pointer',
]
extra_link_args += [
'-g',
'-fsanitize=address,undefined,bounds,pointer-overflow,leak',
]
cxx_args += [
'-O0',
'-g',
]
nvcc_args += [
'-O0',
'-g',
]
else:
extra_compile_args += [
'-O2',
'-flto',
]
extra_link_args += [
'-O2',
]
cxx_args += [
'-O3',
]
nvcc_args += [
'-O3',
]
system = platform.system()
if system == 'Linux':
extra_compile_args += [
'-Wno-alloc-size-larger-than',
'-Wno-implicit-function-declaration',
'-fmax-errors=3',
]
extra_link_args += [
'-Bsymbolic-functions',
]
elif system == 'Darwin':
extra_compile_args += [
'-Wno-error=int-conversion',
'-Wno-error=incompatible-function-pointer-types',
'-Wno-error=implicit-function-declaration',
]
extra_link_args += [
'-framework', 'Cocoa',
'-framework', 'OpenGL',
'-framework', 'IOKit',
]
else:
raise ValueError(f'Unsupported system: {system}')
# Default Gym/Gymnasium/PettingZoo versions
# Gym:
# - 0.26 still has deprecation warnings and is the last version of the package
# - 0.25 adds a breaking API change to reset, step, and render_modes
# - 0.24 is broken
# - 0.22-0.23 triggers deprecation warnings by calling its own functions
# - 0.21 is the most stable version
# - <= 0.20 is missing dict methods for gym.spaces.Dict
# - 0.18-0.21 require setuptools<=65.5.0
# Extensions
class BuildExt(build_ext):
def run(self):
# Propagate any build_ext options (e.g., --inplace, --force) to subcommands
build_ext_opts = self.distribution.command_options.get('build_ext', {})
if build_ext_opts:
# Copy flags so build_torch and build_c respect inplace/force
self.distribution.command_options['build_torch'] = build_ext_opts.copy()
self.distribution.command_options['build_c'] = build_ext_opts.copy()
# Run the torch and C builds (which will handle copying when inplace is set)
self.run_command('build_torch')
self.run_command('build_c')
class CBuildExt(build_ext):
def run(self, *args, **kwargs):
self.extensions = [e for e in self.extensions if e.name != "pufferlib._C"]
super().run(*args, **kwargs)
class TorchBuildExt(cpp_extension.BuildExtension):
def run(self):
self.extensions = [e for e in self.extensions if e.name == "pufferlib._C"]
super().run()
INCLUDE = [f'{BOX2D_NAME}/include', f'{BOX2D_NAME}/src']
RAYLIB_A = f'{RAYLIB_NAME}/lib/libraylib.a'
extension_kwargs = dict(
include_dirs=INCLUDE,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
extra_objects=[RAYLIB_A],
)
# Find C extensions
c_extensions = []
if not NO_OCEAN:
c_extension_paths = glob.glob('pufferlib/ocean/**/binding.c', recursive=True)
c_extensions = [
Extension(
path.rstrip('.c').replace('/', '.'),
sources=[path],
**extension_kwargs,
)
for path in c_extension_paths if 'matsci' not in path
]
c_extension_paths = [os.path.join(*path.split('/')[:-1]) for path in c_extension_paths]
for c_ext in c_extensions:
if "impulse_wars" in c_ext.name:
print(f"Adding {c_ext.name} to extra objects")
c_ext.extra_objects.append(f'{BOX2D_NAME}/libbox2d.a')
if 'matsci' in c_ext.name:
c_ext.include_dirs.append('/usr/local/include')
c_ext.extra_link_args.extend(['-L/usr/local/lib', '-llammps'])
# Check if CUDA compiler is available. You need cuda dev, not just runtime.
torch_extensions = []
if not NO_TRAIN:
torch_sources = [
"pufferlib/extensions/pufferlib.cpp",
]
if shutil.which("nvcc"):
extension = CUDAExtension
torch_sources.append("pufferlib/extensions/cuda/pufferlib.cu")
else:
extension = CppExtension
torch_extensions = [
extension(
"pufferlib._C",
torch_sources,
extra_compile_args = {
"cxx": cxx_args,
"nvcc": nvcc_args,
}
),
]
# Prevent Conda from injecting garbage compile flags
from distutils.sysconfig import get_config_vars
cfg_vars = get_config_vars()
for key in ('CC', 'CXX', 'LDSHARED'):
if cfg_vars[key]:
cfg_vars[key] = cfg_vars[key].replace('-B /root/anaconda3/compiler_compat', '')
cfg_vars[key] = cfg_vars[key].replace('-pthread', '')
cfg_vars[key] = cfg_vars[key].replace('-fno-strict-overflow', '')
for key, value in cfg_vars.items():
if value and '-fno-strict-overflow' in str(value):
cfg_vars[key] = value.replace('-fno-strict-overflow', '')
install_requires = [
'setuptools',
'numpy<2.0',
'shimmy[gym-v21]',
'gym==0.23',
'gymnasium==0.29.1',
'pettingzoo==1.24.1',
]
if not NO_TRAIN:
install_requires += [
'torch',
'psutil',
'nvidia-ml-py',
'rich',
'rich_argparse',
'imageio',
'pyro-ppl',
'heavyball',
'neptune',
'wandb',
]
setup(
version="3.0.0",
packages=find_namespace_packages() + find_packages() + c_extension_paths + ['pufferlib/extensions'],
package_data={
"pufferlib": [RAYLIB_NAME + '/lib/libraylib.a']
},
include_package_data=True,
install_requires=install_requires,
ext_modules = c_extensions + torch_extensions,
cmdclass={
"build_ext": BuildExt,
"build_torch": TorchBuildExt,
"build_c": CBuildExt,
},
include_dirs=[numpy.get_include(), RAYLIB_NAME + '/include'],
)