forked from puzzlelib/PuzzleLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
470 lines (317 loc) · 12 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import os, stat, shutil, subprocess
from enum import Enum
from setuptools import setup, find_packages
from setuptools.command.install import install
from setuptools.command.sdist import sdist
libname = "PuzzleLib"
version = "1.0.2"
class Options(str, Enum):
cuda = "cuda"
hip = "hip"
intel = "intel"
tensorrt = "tensorrt"
openvino = "openvino"
def removeReadOnly(_, name, __):
os.chmod(name, stat.S_IWRITE)
os.remove(name)
def markPackages(path):
for dirpath, dirnames, filenames in os.walk(path):
if "__init__.py" not in filenames:
init(dirpath)
def init(dirpath):
initfile = os.path.join(dirpath, "__init__.py")
with open(initfile, "w", encoding="utf-8"):
pass
return initfile
def pathToPackageName(path, withLibname=True):
i = 1 if withLibname else 0
package = path.split(os.path.sep)
idx = list(reversed(package)).index(libname)
return ".".join(package[-idx - i:])
class InstallCommand(install):
description = "command for installation with the ability to select the desired backends and converters"
user_options = install.user_options + [
(
"backend=", None,
"desired backend. Possible entries: cuda, hip, intel; through comma if several"
),
(
"converter=", None,
"desired converter which will be included. Possible entries: tensorrt, openvino; through comma if both"
),
(
"no-runtime-check", None,
"omit runtime check for backends"
)
]
backend, converter = "", ""
no_runtime_check = 0
projectPath = os.path.dirname(os.path.abspath(__file__))
cachePath = os.path.join(projectPath, libname)
def run(self):
backends = self.backend.split(",") if len(self.backend) > 0 else []
converters = self.converter.split(",") if len(self.converter) > 0 else []
print("backends chosen: %s" % backends)
print("converters chosen: %s" % converters)
options = set()
for option in backends + converters:
try:
option = Options[option]
except KeyError:
raise ValueError("Invalid option: %s" % option)
options.add(option)
handlers = [
("Compiler", self.installCompilerPackage),
("CPU", self.installPythonPackage),
("Intel", self.installIntelPackage),
(("Cuda", "Hip"), self.installGpuPackages),
("Converter", self.installConverterPackage),
("Backend", self.installPythonPackage),
("Modules", self.installPythonPackage),
("Containers", self.installPythonPackage),
("Cost", self.installPythonPackage),
("Optimizers", self.installPythonPackage),
("Handlers", self.installPythonPackage),
("Passes", self.installPythonPackage),
("Models", self.installPythonPackage),
("Datasets", self.installPythonPackage),
("Transformers", self.installPythonPackage),
("TestData", self.installDataPackage),
("TestLib", self.installPythonPackage)
]
os.mkdir(self.cachePath)
try:
self.distribution.package_data = self.installPackages(self.projectPath, self.cachePath, handlers, options)
markPackages(self.cachePath)
self.distribution.packages = [libname] + [
"%s." % libname + pkg for pkg in find_packages(where=self.cachePath)
]
super().run()
finally:
shutil.rmtree(self.cachePath, onerror=removeReadOnly)
@staticmethod
def installPackages(src, dst, handlers, options):
packageData = {}
if not os.path.exists(dst):
os.mkdir(dst)
for file in os.listdir(src):
if file.endswith(".py") and os.path.abspath(os.path.join(src, file)) != __file__:
shutil.copy(os.path.join(src, file), os.path.join(dst, file))
for name, handler in handlers:
if isinstance(name, str):
pkgSrc, pkgDst = os.path.join(src, name), os.path.join(dst, name)
else:
pkgSrc, pkgDst = [os.path.join(src, nm) for nm in name], [os.path.join(dst, nm) for nm in name]
packageData.update(handler(pkgSrc, pkgDst, options))
return packageData
@staticmethod
def installPythonPackage(src, dst, _):
def ignore(s, names):
files = {name for name in names if not os.path.isdir(os.path.join(s, name)) and not name.endswith(".py")}
files.add("__pycache__")
return files
shutil.copytree(src, dst, ignore=ignore)
return {}
@staticmethod
def installCompilerPackage(src, dst, _):
shutil.copytree(src, dst, ignore=lambda s, names: {"__pycache__", "TestData"})
os.mkdir(os.path.join(dst, "TestData"))
data = {}
for dirpath, dirnames, filenames in os.walk(dst):
buildFiles = [file for file in filenames if any(file.endswith(ext) for ext in [".c", ".h"])]
if len(buildFiles) > 0:
data[pathToPackageName(dirpath)] = buildFiles
return data
def installGpuPackages(self, src, dst, options):
data = {}
cudaSrc, hipSrc = src
cudaDst, hipDst = dst
cuda, hip = Options.cuda in options, Options.hip in options
if cuda or hip:
shutil.copytree(cudaSrc, cudaDst, ignore=lambda s, names: {"__pycache__", ".gitignore"})
if cuda:
from PuzzleLib.Cuda.CheckInstall import checkCudaInstall
from PuzzleLib.Cuda.Source.Build import main as buildDriver
data.update(self.installGpuPackage("Cuda", checkCudaInstall, buildDriver, cudaDst))
if hip:
shutil.copytree(hipSrc, hipDst, ignore=lambda s, names: {"__pycache__", ".gitignore"})
from PuzzleLib.Hip.CheckInstall import main as checkHipInstall
from PuzzleLib.Hip.Source.Build import main as buildDriver
data.update(self.installGpuPackage("Hip", checkHipInstall, buildDriver, hipDst))
if cuda or hip:
shutil.rmtree(os.path.join(cudaDst, "Source"), onerror=removeReadOnly)
if hip:
shutil.rmtree(os.path.join(hipDst, "Source"), onerror=removeReadOnly)
return data
def installGpuPackage(self, name, checkInstall, buildDriver, dst):
print("\nChecking if all dependencies for %s are satisfied ..." % name)
checkInstall(withRuntime=not self.no_runtime_check, withPip=False)
cwd = os.getcwd()
try:
print("\nBuilding %s driver ..." % name)
os.chdir(os.path.join(dst, "Source"))
driver = os.path.abspath(buildDriver())
finally:
os.chdir(cwd)
return {pathToPackageName(dst): [os.path.basename(driver)]}
@staticmethod
def installIntelPackage(src, dst, options):
if Options.intel not in options:
return {}
shutil.copytree(src, dst, ignore=lambda s, names: {"__pycache__", ".gitignore"})
data = {}
from PuzzleLib.Intel.ThirdParty.finddnnl import findDNNL
print("\nChecking dnnl installation ...")
lib, _ = findDNNL();
if os.path.commonpath([dst, lib]) == dst:
data = {pathToPackageName(dst): [lib]}
return data
def installConverterPackage(self, src, dst, options):
handlers = [
("Caffe", self.installPythonPackage),
("MXNet", self.installPythonPackage),
("Examples", self.installPythonPackage),
("ONNX", self.installPythonPackage),
("TensorRT", self.installTensorRTPackage),
("OpenVINO", self.installOpenVINOPackage)
]
data = self.installPackages(src, dst, handlers, options)
os.mkdir(os.path.join(dst, "TestData"))
return data
def installTensorRTPackage(self, src, dst, options):
if Options.tensorrt not in options:
return {}
os.mkdir(dst)
shutil.copytree(os.path.join(src, "Source"), os.path.join(dst, "Source"))
from PuzzleLib.Converter.TensorRT.Source.Build import main as buildDriver
return self.installInferenceEnginePackage("TensorRT", buildDriver, src, dst)
def installOpenVINOPackage(self, src, dst, options):
if Options.openvino not in options:
return {}
os.mkdir(dst)
shutil.copytree(os.path.join(src, "Source"), os.path.join(dst, "Source"))
from PuzzleLib.Converter.OpenVINO.Source.Build import main as buildDriver
return self.installInferenceEnginePackage("OpenVINO", buildDriver, src, dst)
@staticmethod
def installInferenceEnginePackage(name, buildDriver, src, dst):
for file in os.listdir(src):
if file.endswith(".py"):
shutil.copy(os.path.join(src, file), os.path.join(dst, file))
shutil.copytree(os.path.join(src, "Tests"), os.path.join(dst, "Tests"))
os.mkdir(os.path.join(dst, "TestData"))
cwd = os.getcwd()
try:
print("\nBuilding %s driver ..." % name)
sourcePath = os.path.join(dst, "Source")
os.chdir(sourcePath)
driver = buildDriver()
finally:
os.chdir(cwd)
shutil.rmtree(sourcePath, onerror=removeReadOnly)
return {pathToPackageName(dst): [os.path.basename(driver)]}
@staticmethod
def installDataPackage(src, dst, _):
os.mkdir(dst)
data = ["test.tar", "test.zip"]
for file in data:
shutil.copy(os.path.join(src, file), os.path.join(dst, file))
return {pathToPackageName(dst): data}
class SdistCommand(sdist):
projectPath = os.path.dirname(os.path.abspath(__file__))
def run(self):
initfiles = []
handlers = [
("Compiler", self.distributeCompilerPackage),
("CPU", self.distributePythonPackage),
("Intel", self.distributePythonPackage),
("Hip", self.distributeGpuPackage),
("Cuda", self.distributeGpuPackage),
("Converter", self.distributeConverterPackage),
("Backend", self.distributePythonPackage),
("Modules", self.distributePythonPackage),
("Containers", self.distributePythonPackage),
("Cost", self.distributePythonPackage),
("Optimizers", self.distributePythonPackage),
("Handlers", self.distributePythonPackage),
("Passes", self.distributePythonPackage),
("Models", self.distributePythonPackage),
("Datasets", self.distributePythonPackage),
("Transformers", self.distributePythonPackage),
("TestData", self.distributeDataPackage),
("TestLib", self.distributePythonPackage)
]
try:
initfiles, data = self.distributePackages(self.projectPath, handlers)
self.distribution.package_data = data
self.distribution.packages = find_packages(where=self.projectPath)
super().run()
finally:
for initfile in initfiles:
os.unlink(initfile)
@staticmethod
def distributePackage(path, exclude, includeExts):
initfiles = []
data = {}
for dirpath, dirnames, filenames in os.walk(path):
for exdir in exclude:
if exdir in dirnames:
dirnames.remove(exdir)
includeFiles = [file for file in filenames if any(file.endswith(ext) for ext in includeExts)]
if len(includeFiles) > 0:
data[pathToPackageName(dirpath, withLibname=False)] = includeFiles
if "__init__.py" not in filenames:
initfiles.append(init(dirpath))
return initfiles, data
@staticmethod
def distributePackages(path, handlers, initPath=False):
initfiles = []
data = {}
for name, handler in handlers:
pkgInitfiles, pkgData = handler(os.path.join(path, name))
data.update(pkgData)
initfiles.extend(pkgInitfiles)
if initPath:
initfiles.append(init(path))
return initfiles, data
def distributePythonPackage(self, path):
return self.distributePackage(path=path, exclude=["__pycache__"], includeExts=[])
def distributeCompilerPackage(self, path):
return self.distributePackage(path=path, exclude=["__pycache__"], includeExts=[".c", ".h"])
def distributeGpuPackage(self, path):
return self.distributeCompilerPackage(path)
def distributeConverterPackage(self, path):
handlers = [
("Caffe", self.distributePythonPackage),
("MXNet", self.distributePythonPackage),
("Examples", self.distributePythonPackage),
("ONNX", self.distributePythonPackage),
("TensorRT", self.distributeTensorRTPackage),
("OpenVINO", self.distributeOpenVINOPackage)
]
return self.distributePackages(path, handlers, True)
def distributeTensorRTPackage(self, path):
return self.distributePackage(path=path, exclude=["__pycache__"], includeExts=[".cpp", ".h", ".cu"])
def distributeOpenVINOPackage(self, path):
return self.distributePackage(path=path, exclude=["__pycache__"], includeExts=[".cpp"])
def distributeDataPackage(self, path):
data = ["test.tar", "test.zip"]
initfiles, pkgData = self.distributePythonPackage(path)
pkgData.update({pathToPackageName(path, withLibname=False): data})
return initfiles, pkgData
def main():
setup(
name=libname,
version=version,
cmdclass={
"install": InstallCommand,
"sdist": SdistCommand
},
url="https://puzzlelib.org",
download_url="https://github.com/puzzlelib/PuzzleLib/tags",
author="Ashmanov Neural Networks",
python_requires=">=3.5",
license="Apache-2.0",
keywords=["puzzlelib", "deep learning", "neural nets"]
)
if __name__ == "__main__":
main()