This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 730
/
setup.py
343 lines (312 loc) · 10.6 KB
/
setup.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import os
import sys
import torch
from setuptools import Extension, find_packages, setup
from torch.utils.cpp_extension import (
CppExtension,
CUDAExtension,
BuildExtension,
CUDA_HOME,
)
if sys.version_info < (3, 6):
sys.exit("Sorry, Python >= 3.6 is required for metaseq.")
def write_version_py():
with open(os.path.join("metaseq", "version.txt")) as f:
version = f.read().strip()
# append latest commit hash to version string
try:
sha = (
subprocess.check_output(["git", "rev-parse", "HEAD"])
.decode("ascii")
.strip()
)
version += "+" + sha[:7]
except Exception:
pass
# write version info to metaseq/version.py
with open(os.path.join("metaseq", "version.py"), "w") as f:
f.write('__version__ = "{}"\n'.format(version))
return version
version = write_version_py()
with open("README.md") as f:
readme = f.read()
if sys.platform == "darwin":
extra_compile_args = ["-stdlib=libc++", "-O3"]
else:
extra_compile_args = ["-std=c++11", "-O3"]
class NumpyExtension(Extension):
"""Source: https://stackoverflow.com/a/54128391"""
def __init__(self, *args, **kwargs):
self.__include_dirs = []
super().__init__(*args, **kwargs)
@property
def include_dirs(self):
import numpy
return self.__include_dirs + [numpy.get_include()]
@include_dirs.setter
def include_dirs(self, dirs):
self.__include_dirs = dirs
extension_modules = [
NumpyExtension(
"metaseq.data.data_utils_fast",
sources=["metaseq/data/data_utils_fast.pyx"],
language="c++",
extra_compile_args=extra_compile_args,
),
NumpyExtension(
"metaseq.data.token_block_utils_fast",
sources=["metaseq/data/token_block_utils_fast.pyx"],
language="c++",
extra_compile_args=extra_compile_args,
),
]
# TODO: Figure out how to actually gate this properly and still get CircleCI to work.
# By default, include megatron kernels unless --global-option="--no_megatron" is passed in to pip install.
if "--no_megatron" not in sys.argv:
# Reference:
# https://github.com/ngoyal2707/Megatron-LM/commit/9a16189ab1b5537205c708f8c8f952f2ae2ae72b
extension_modules.append(
CppExtension(
"metaseq.modules.megatron.fused_kernels.scaled_upper_triang_masked_softmax_cuda",
sources=[
"metaseq/modules/megatron/fused_kernels/scaled_upper_triang_masked_softmax.cpp",
"metaseq/modules/megatron/fused_kernels/scaled_upper_triang_masked_softmax_cuda.cu",
],
extra_compile_args={
"cxx": ["-O3"],
"nvcc": [
"-O3",
"--use_fast_math",
"-U__CUDA_NO_HALF_OPERATORS__",
"-U__CUDA_NO_HALF_CONVERSIONS__",
"--expt-relaxed-constexpr",
"--expt-extended-lambda",
],
},
)
)
extension_modules.append(
CppExtension(
"metaseq.modules.megatron.fused_kernels.scaled_masked_softmax_cuda",
sources=[
"metaseq/modules/megatron/fused_kernels/scaled_masked_softmax.cpp",
"metaseq/modules/megatron/fused_kernels/scaled_masked_softmax_cuda.cu",
],
extra_compile_args={
"cxx": ["-O3"],
"nvcc": [
"-O3",
"--use_fast_math",
"-U__CUDA_NO_HALF_OPERATORS__",
"-U__CUDA_NO_HALF_CONVERSIONS__",
"--expt-relaxed-constexpr",
"--expt-extended-lambda",
],
},
)
)
else:
print("*** Skipping megatron kernel installation... ***")
sys.argv.remove("--no_megatron")
# By default, include apex kernels unless --global-option="--no_apex" is passed in to pip install.
if "--no_apex" not in sys.argv:
# TODO[susanz]: not including --no-cache-dir anymore?
if CUDA_HOME is None:
raise RuntimeError(
f"Building apex kernels was requested, but nvcc was not found. "
"Are you sure your environment has nvcc available? "
"If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, "
"only images whose names contain 'devel' will provide nvcc."
)
print("\n\ntorch.__version__ = {}\n\n".format(torch.__version__))
TORCH_MAJOR = int(torch.__version__.split(".")[0])
TORCH_MINOR = int(torch.__version__.split(".")[1])
if TORCH_MAJOR == 0:
# TODO[susanz]: Gate this to Pytorch 1.6 or later?
raise RuntimeError(
"Apex kernels requires Pytorch 1.0 or later, "
"found torch.__version__ = {}".format(torch.__version__)
)
# --global-option="--cpp_ext"
extension_modules.append(
CppExtension(
name="apex_C", sources=["metaseq/modules/apex/flatten_unflatten.cpp"]
)
)
# --global-option="--cuda_ext"
extension_modules.append(
CUDAExtension(
name="amp_C",
sources=[
"metaseq/modules/apex/amp_C_frontend.cpp",
"metaseq/modules/apex/multi_tensor_l2norm_kernel.cu",
"metaseq/modules/apex/multi_tensor_l2norm_kernel_mp.cu",
"metaseq/modules/apex/multi_tensor_l2norm_scale_kernel.cu",
"metaseq/modules/apex/multi_tensor_adam.cu",
],
extra_compile_args={
"cxx": ["-O3"],
"nvcc": [
"-lineinfo",
"-O3",
"--use_fast_math",
],
},
)
)
extension_modules.append(
CUDAExtension(
name="fused_layer_norm_cuda",
sources=[
"metaseq/modules/apex/layer_norm_cuda.cpp",
"metaseq/modules/apex/layer_norm_cuda_kernel.cu",
],
extra_compile_args={
"cxx": ["-O3"],
"nvcc": ["-maxrregcount=50", "-O3", "--use_fast_math"],
},
)
)
extension_modules.append(
CUDAExtension(
name="fused_dense_cuda",
sources=[
"metaseq/modules/apex/fused_dense.cpp",
"metaseq/modules/apex/fused_dense_cuda.cu",
],
extra_compile_args={
"cxx": ["-O3"],
"nvcc": ["-O3"],
},
)
)
# --global-option="--deprecated_fused_adam"
extension_modules.append(
CUDAExtension(
name="fused_adam_cuda",
sources=[
"metaseq/modules/apex/fused_adam_cuda.cpp",
"metaseq/modules/apex/fused_adam_cuda_kernel.cu",
],
# include_dirs=[os.path.join(this_dir, "csrc")],
extra_compile_args={
"cxx": ["-O3"],
"nvcc": ["-O3", "--use_fast_math"],
},
)
)
else:
print("*** Skipping apex kernel installation... ***")
sys.argv.remove("--no_apex")
if "clean" in sys.argv[1:]:
# Source: https://bit.ly/2NLVsgE
print("deleting Cython files...")
import subprocess
subprocess.run(
["rm -f metaseq/*.so metaseq/**/*.so metaseq/*.pyd metaseq/**/*.pyd"],
shell=True,
)
def do_setup():
setup(
name="metaseq",
version=version,
description="MetaSeq, a framework for large language models, from Meta",
url="https://github.com/facebookresearch/metaseq",
long_description=readme,
long_description_content_type="text/markdown",
setup_requires=[
"cython",
'numpy; python_version>="3.7"',
"setuptools>=18.0",
],
install_requires=[
# protobuf version pinned due to tensorboard not pinning a version.
# https://github.com/protocolbuffers/protobuf/issues/10076
"protobuf==3.20.2",
# "click==8.0.4",
"cython",
'dataclasses; python_version<"3.7"',
# "editdistance",
"fire",
"flask>=2.2.5", # for api
"hydra-core>=1.1.0,<1.2",
"ipdb",
"ipython",
"Jinja2==3.1.1", # for evals
"markupsafe", # for evals
"more_itertools",
"ninja",
'numpy; python_version>="3.7"',
"omegaconf<=2.1.1",
"portalocker>=2.5",
"pre-commit",
"pytest",
"pytest-regressions",
"regex",
"scikit-learn", # for evals
"sacrebleu", # for evals
"tensorboard==2.8.0",
"timeout-decorator",
"tokenizers",
"torch",
"tqdm",
"typing_extensions",
],
packages=find_packages(
exclude=[
"scripts",
"scripts.*",
"tests",
"tests.*",
]
),
include_package_data=True,
zip_safe=False,
extras_require={
# install via: pip install -e ".[dev]"
"dev": [
"flake8",
"black==22.3.0",
"aim>=3.9.4",
"azure-storage-blob",
"mypy",
],
# install via: pip install -e ".[test]"
"test": [
"iopath",
"transformers",
"pyarrow",
"boto3",
"pandas",
"bitsandbytes",
],
# install via: pip install -e ".[multimodal]"
"multimodal": [
"albumentations",
"dalle_pytorch",
"einops",
"matplotlib==3.5.0",
"pytorchvideo==0.1.5",
"wandb",
"webdataset==0.1.103",
],
},
ext_modules=extension_modules,
test_suite="tests",
entry_points={
"console_scripts": [
"metaseq-train = metaseq.cli.train:cli_main",
"metaseq-validate = metaseq.cli.validate:cli_main",
"opt-baselines = metaseq.launcher.opt_baselines:cli_main",
"metaseq-api-local = metaseq.cli.interactive_hosted:cli_main",
],
},
cmdclass={"build_ext": BuildExtension},
)
if __name__ == "__main__":
do_setup()