-
Notifications
You must be signed in to change notification settings - Fork 19
/
setup.py
178 lines (161 loc) · 5.9 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
#!/usr/bin/env python
# Copyright (c) Megvii, Inc. and its affiliates. All Rights Reserved
import re
import setuptools
import glob
import os
from os import path
import torch
import warnings
from torch.utils.cpp_extension import (CppExtension, CUDAExtension)
torch_ver = [int(x) for x in torch.__version__.split(".")[:2]]
assert torch_ver >= [1, 7], "Requires PyTorch >= 1.7"
def get_extensions():
this_dir = path.dirname(path.abspath(__file__))
extensions_dir = path.join(this_dir, "yolox", "layers", "csrc")
main_source = path.join(extensions_dir, "vision.cpp")
sources = glob.glob(path.join(extensions_dir, "**", "*.cpp"))
sources = [main_source] + sources
extension = CppExtension
extra_compile_args = {"cxx": ["-O3"]}
define_macros = []
include_dirs = [extensions_dir]
ext_modules = [
extension(
"yolox._C",
sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
),
make_cuda_ext(
name = 'box_iou_rotated_ext',
module = 'yolox.ops.pytorch.box_iou_rotated',
sources = [
'src/box_iou_rotated_cpu.cpp',
'src/box_iou_rotated_ext.cpp'
],
sources_cuda = ['src/box_iou_rotated_cuda.cu']
),
make_cuda_ext(
name = 'convex_ext',
module = 'yolox.ops.pytorch.convex',
sources = [
'src/convex_cpu.cpp',
'src/convex_ext.cpp'
],
sources_cuda = ['src/convex_cuda.cu']
),
make_cuda_ext(
name = 'nms_rotated_ext',
module = 'yolox.ops.pytorch.nms_rotated',
sources = [
'src/nms_rotated_cpu.cpp',
'src/nms_rotated_ext.cpp',
],
sources_cuda = [
'src/nms_rotated_cuda.cu',
'src/poly_nms_cuda.cu']
),
make_cuda_ext(
name = 'roi_align_ext',
module = 'yolox.ops.pytorch.roi_align',
sources=[
'src/roi_align_ext.cpp',
'src/cpu/roi_align_v2.cpp',
],
sources_cuda=[
'src/cuda/roi_align_kernel.cu',
'src/cuda/roi_align_kernel_v2.cu'
]),
make_cuda_ext(
name='roi_align_rotated_ext',
module='yolox.ops.pytorch.roi_align_rotated',
sources=[
'src/roi_align_rotated_cpu.cpp',
'src/roi_align_rotated_ext.cpp'
],
sources_cuda=['src/roi_align_rotated_cuda.cu']),
]
if os.getenv("ONNXRUNTIME_DIR", "0") != "0":
warnings.warn("This Part is incompleted!")
ext_modules.append(
make_onnxruntime_ext(
name="ort_ext",
module="yolox.ops.onnxruntime",
with_cuda=False
)
)
return ext_modules
def make_cuda_ext(name, module, sources, sources_cuda=[], with_cuda=True):
define_macros = []
extra_compile_args = {'cxx': ["-O2", "-std=c++14", "-Wall"]}
if torch.cuda.is_available() or os.getenv('FORCE_CUDA', '0') == '1' and with_cuda:
define_macros += [('WITH_CUDA', None)] # 宏定义的声明,#define WITH_CUDA None
extension = CUDAExtension
extra_compile_args['nvcc'] = [
'-O2',
'-D__CUDA_NO_HALF_OPERATORS__',
'-D__CUDA_NO_HALF_CONVERSIONS__',
'-D__CUDA_NO_HALF2_OPERATORS__',
]
sources += sources_cuda
else:
print(f"Compiling {name} without CUAD")
extension = CppExtension
return extension(
name=f"{module}.{name}",
sources=[os.path.join(*module.split('.'), p) for p in sources],
define_macros=define_macros,
extra_compilr_args=extra_compile_args
)
def make_onnxruntime_ext(name, module, with_cuda=True):
source_type = ("cpp", "c", "cc")
include_type = ("hpp", "h", "cuh")
source_files = []
include_dirs = []
define_macros = []
module_path = os.path.join(*module.split("."))
extra_compile_args = {'cxx': ["-O2", "-std=c++14", "-Wall"]}
for t in source_type:
for p in glob.glob(module_path + "/**/*." + t, recursive=True):
source_files.append(p)
for t in include_type:
for p in glob.glob(module_path + "/**/*." + t, recursive=True):
include_dirs.append(os.path.abspath(os.path.dirname(p)))
include_dirs = list(set(include_dirs))
if (torch.cuda.is_available() or os.getenv('FORCE_CUDA', '0') == '1') and with_cuda:
define_macros += [('WITH_CUDA', None)] # 宏定义的声明,#define WITH_CUDA None
raise NotImplementedError
else:
ort_path = os.path.abspath(os.getenv("ONNXRUNTIME_DIR", "0"))
libraries = ["onnxruntime"]
library_dirs = [os.path.join(ort_path, "lib")]
include_dirs.append(os.path.join(ort_path, "include"))
extension = CppExtension
return extension(
name=f"{module}.{name}",
sources=source_files,
define_macros=define_macros,
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
libraries=libraries,
library_dirs=library_dirs)
with open("yolox/__init__.py", "r") as f:
version = re.search(
r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
f.read(), re.MULTILINE
).group(1)
with open("README.md", "r", encoding="utf-8") as f:
long_description = f.read()
setuptools.setup(
name="yolox",
version=version,
author="basedet team",
python_requires=">=3.6",
long_description=long_description,
ext_modules=get_extensions(),
classifiers=["Programming Language :: Python :: 3", "Operating System :: OS Independent"],
cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension},
packages=setuptools.find_packages(),
)