forked from jonahobw/gpu_model_extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_exe.py
More file actions
99 lines (87 loc) · 2.71 KB
/
Copy pathcreate_exe.py
File metadata and controls
99 lines (87 loc) · 2.71 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
"""
Creates an exe based on model_inference.py for your OS.
This is the exe file that can be used for profiling one inference.
"""
import sys
import shutil
import os
import subprocess
import shlex
from pathlib import Path
import site
def createHiddenImportStr():
HIDDEN_IMPORTS = [
"sklearn.utils._typedefs",
"sklearn.utils._heap",
"sklearn.utils._sorting",
"sklearn.utils._cython_blas",
"sklearn.neighbors.quad_tree",
"sklearn.tree._utils",
"sklearn.neighbors._typedefs",
"sklearn.utils._typedefs",
"sklearn.neighbors._partition_nodes",
"sklearn.utils._vector_sentinel",
"sklearn.metrics.pairwise",
"sklearn.metrics._pairwise_distances_reduction._datasets_pair",
"sklearn.metrics._pairwise_distances_reduction",
"scipy.special._cdflib",
"pkg_resources.extern",
# "torch",
# "torchvision",
# "torch.jit",
]
s = ""
for imprt in HIDDEN_IMPORTS:
s += f'--hidden-import="{imprt}" '
return s
def createAddDataStr():
site_packs_folder = Path(site.getsitepackages()[0])
pkgs = [
"torch"
]
s = ""
for pkg in pkgs:
folder = str(site_packs_folder / pkg)
s += f'--add-data="{folder}:." '
return s
def createExcludeModsStr():
exclude = [
"torch.distributions"
]
s = ""
for x in exclude:
s += f'--exclude-module="{x}" '
return s
def create_exe():
command = (f"pyinstaller {createHiddenImportStr()}"# {createAddDataStr()} {createExcludeModsStr()}"
f" --onefile --clean model_inference.py")
output = subprocess.run(shlex.split(command), stdout=sys.stdout)
exe_file = Path.cwd() / "dist" / "model_inference.exe"
if os.name != "nt":
# linux
destination_folder = Path.cwd() / "exe" / "linux"
if not destination_folder.exists():
destination_folder.mkdir(exist_ok=True, parents=True)
destination = destination_folder / "linux_inference.exe"
exe_file = Path.cwd() / "dist" / "model_inference"
else:
# windows
destination_folder = Path.cwd() / "exe" / "windows"
if not destination_folder.exists():
destination_folder.mkdir(exist_ok=True)
destination = destination_folder / "windows_inference.exe"
shutil.copy(exe_file, destination)
def cleanup():
dist_folder = Path.cwd() / "dist"
if dist_folder.exists():
shutil.rmtree(dist_folder)
build_folder = Path.cwd() / "build"
if build_folder.exists():
shutil.rmtree(build_folder)
spec_file = Path.cwd() / "model_inference.spec"
spec_file.unlink(missing_ok=True)
cleanup()
try:
create_exe()
finally:
cleanup()