forked from google-ai-edge/LiteRT-LM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
269 lines (237 loc) · 8.16 KB
/
Copy pathsetup.py
File metadata and controls
269 lines (237 loc) · 8.16 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
"""Python packaging file for ODML LiteRT LM library."""
import atexit
import contextlib
import glob
import os
import shutil
import subprocess
import sys
import tempfile
import urllib.request
import zipfile
import setuptools
import setuptools.command.build_py
import setuptools.command.develop
import setuptools.command.egg_info
import setuptools.command.sdist
_build_py = setuptools.command.build_py.build_py
_develop = setuptools.command.develop.develop
_egg_info = setuptools.command.egg_info.egg_info
_sdist = setuptools.command.sdist.sdist
def compile_protos() -> None:
"""Compiles .proto files using grpc_tools.protoc."""
proto_dir = "runtime/proto"
proto_files = glob.glob(os.path.join(proto_dir, "*.proto"))
if not proto_files:
return
os.makedirs("schema/py/runtime/proto", exist_ok=True)
with open("schema/py/runtime/__init__.py", "a"):
pass
with open("schema/py/runtime/proto/__init__.py", "a"):
pass
for proto_file in proto_files:
print(f"Compiling {proto_file}...")
subprocess.check_call([
sys.executable,
"-m",
"grpc_tools.protoc",
"-I.",
"--python_out=schema/py",
proto_file,
])
# Modify the import paths in the generated files to match the package
# structure. This makes the generated code importable as part of the
# litert_lm package.
pb2_files = glob.glob("schema/py/runtime/proto/*_pb2.py")
for pb2_file in pb2_files:
with open(pb2_file, "r") as f:
content = f.read().replace(
"from runtime.proto import", "from litert_lm.runtime.proto import"
).replace("runtime.proto.", "litert_lm.runtime.proto.")
with open(pb2_file, "w") as f:
f.write(content)
def _compile_with_flatc(executable_flatc: str) -> None:
"""Compiles .fbs files using the provided flatc executable."""
fbs_dir = "schema/core"
fbs_files = glob.glob(os.path.join(fbs_dir, "*.fbs"))
if not fbs_files:
return
os.makedirs("schema/py/schema/core", exist_ok=True)
with open("schema/py/schema/__init__.py", "a"):
pass
with open("schema/py/schema/core/__init__.py", "a"):
pass
for fbs_file in fbs_files:
print(f"Compiling {fbs_file}...")
subprocess.check_call([
executable_flatc,
"--python",
"-o",
"schema/py/schema/core",
fbs_file,
])
generated_dir = "schema/py/schema/core/litert/lm/schema"
if os.path.exists(generated_dir):
generated_modules = (
f[:-3]
for f in os.listdir(generated_dir)
if f.endswith(".py") and f != "__init__.py"
)
with open(
"schema/py/schema/core/litertlm_header_schema_py_generated.py", "w"
) as outfile:
for mod in generated_modules:
outfile.write(f"from litert.lm.schema.{mod} import *\n")
@contextlib.contextmanager
def _ensure_flatc_executable():
"""Finds or downloads the flatc compiler, yielding its path."""
try_paths = ["./flatc", "../../flatc"]
for path in try_paths:
if os.path.exists(path):
yield path
return
print("flatc not found. Downloading flatc...")
with tempfile.TemporaryDirectory() as temp_dir:
zip_path = os.path.join(temp_dir, "flatc.zip")
url = "https://github.com/google/flatbuffers/releases/download/v23.5.26/Linux.flatc.binary.clang++-15.zip"
try:
urllib.request.urlretrieve(url, zip_path)
except urllib.error.HTTPError:
url = "https://github.com/google/flatbuffers/releases/download/v24.3.25/Linux.flatc.binary.clang++-15.zip"
urllib.request.urlretrieve(url, zip_path)
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(temp_dir)
downloaded_flatc = os.path.join(temp_dir, "flatc")
os.chmod(downloaded_flatc, 0o755)
yield downloaded_flatc
def compile_flatbuffers() -> None:
"""Compiles .fbs files using the flatc compiler."""
with _ensure_flatc_executable() as executable_flatc:
_compile_with_flatc(executable_flatc)
def _create_package_stubs() -> None:
"""Creates package directories before setup() accesses them."""
os.makedirs("schema/py/runtime/proto", exist_ok=True)
with open("schema/py/runtime/__init__.py", "a"):
pass
with open("schema/py/runtime/proto/__init__.py", "a"):
pass
os.makedirs("schema/py/schema/core/litert/lm/schema", exist_ok=True)
with open("schema/py/schema/__init__.py", "a"):
pass
with open("schema/py/schema/core/__init__.py", "a"):
pass
with open("schema/py/schema/core/litert/__init__.py", "a"):
pass
with open("schema/py/schema/core/litert/lm/__init__.py", "a"):
pass
class CustomBuildPy(_build_py):
# Custom build_py command to compile protos and flatbuffers before building.
def run(self) -> None:
_ = self # unused
_create_package_stubs()
compile_protos()
compile_flatbuffers()
super().run()
class CustomDevelop(_develop):
# Custom develop command to compile protos and flatbuffers before building.
def run(self) -> None:
_ = self # unused
_create_package_stubs()
compile_protos()
compile_flatbuffers()
super().run()
class CustomSdist(_sdist):
# Custom sdist command to compile protos and flatbuffers before building.
def run(self) -> None:
_ = self # unused
_create_package_stubs()
compile_protos()
compile_flatbuffers()
super().run()
class CustomEggInfo(_egg_info):
# Custom egg_info command to create package
# stubs so setuptools package detection won't fail.
def run(self) -> None:
_ = self # unused
_create_package_stubs()
super().run()
def cleanup_generated() -> None:
# Keep generated files for editable installs so they can be imported.
is_editable = any("develop" in arg or "editable" in arg for arg in sys.argv)
if not is_editable:
shutil.rmtree("schema/py/runtime", ignore_errors=True)
# Be careful to only remove schema/py/schema/core and related generated
# files, actually schema/py/schema contains generated __init__ and core/
shutil.rmtree("schema/py/schema", ignore_errors=True)
atexit.register(cleanup_generated)
setuptools.setup(
name="litertlm-builder",
version="0.0.1",
author="Litert-lm Authors",
description="LiteRT LM Builder library",
long_description=(
"Python tools for building, inspecting,"
"and converting LiteRT-LM (.litertlm) files"
),
long_description_content_type="text/markdown",
url="https://github.com/google-ai-edge/LiteRT-LM.git",
package_dir={
"litert_lm": "schema/py",
"litert_lm.runtime.proto": "schema/py/runtime/proto",
"litert_lm.schema": "schema",
"litert_lm.schema.py": "schema/py",
"litert_lm.schema.core": "schema/py/schema/core",
"litert": "schema/py/schema/core/litert",
"litert.lm": "schema/py/schema/core/litert/lm",
"litert.lm.schema": "schema/py/schema/core/litert/lm/schema",
},
packages=[
"litert_lm",
"litert_lm.runtime",
"litert_lm.runtime.proto",
"litert_lm.schema",
"litert_lm.schema.py",
"litert_lm.schema.core",
"litert",
"litert.lm",
"litert.lm.schema",
],
include_package_data=True,
package_data={
"litert_lm.runtime.proto": ["*.py"],
"litert.lm.schema": ["*.py"],
"litert_lm.schema.core": ["*.py"],
},
data_files=[
("runtime/proto", glob.glob("runtime/proto/*.proto")),
("schema/core", glob.glob("schema/core/*.fbs")),
],
cmdclass={
"build_py": CustomBuildPy,
"develop": CustomDevelop,
"sdist": CustomSdist,
"egg_info": CustomEggInfo,
},
install_requires=[
"protobuf",
"flatbuffers",
"absl-py",
"tomli",
],
entry_points={
"console_scripts": [
"litertlm-builder=litert_lm.litertlm_builder_cli:main",
"litertlm-peek=litert_lm.litertlm_peek_main:main",
],
},
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
python_requires=">=3.8",
)