Repository navigation
Expand file tree
/
Copy pathpyml_engines.py
More file actions
167 lines (144 loc) · 5.11 KB
/
Copy pathpyml_engines.py
File metadata and controls
167 lines (144 loc) · 5.11 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
# pyml-engines
# Copyright (C) 2024-2026 Collabora Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
import argparse
import json
import sys
from engine.benchmark import benchmark_rows, status_text
from engine.model_engines import (
available_devices,
engine_options,
with_available_devices,
unprobed_devices,
)
REACHABLE_MARK = "*"
UNPROBED_MARK = "?"
LEGEND = f"{REACHABLE_MARK} reachable on this machine, {UNPROBED_MARK} not probed"
PACKAGE_NAME = "gst-python-ml"
NO_PIP_EXTRA = "not on PyPI"
DEFAULT_BENCHMARK_FRAMES = 20
BENCHMARK_HEADER = ("task", "engine", "device", "load s", "ms/frame", "fps", "note")
def marked_devices(engine, devices, reachable):
unprobed = unprobed_devices(engine)
marks = []
for device in devices:
mark = REACHABLE_MARK if device in reachable else ""
if device in unprobed:
mark = UNPROBED_MARK
marks.append(device + mark)
return " ".join(marks)
def install_hint(entry):
if entry["installed"]:
return ""
if not entry["extra"]:
return NO_PIP_EXTRA
first_extra, *other_extras = entry["extra"]
alternatives = "".join(f" or [{extra}]" for extra in other_extras)
return f"pip install {PACKAGE_NAME}[{first_extra}]{alternatives}"
def task_lines(task, reachable_devices):
heading = f"{task['task']}: {task['note']}" if task["note"] else task["task"]
rows = [
(
entry["engine"],
status_text(entry),
marked_devices(
entry["engine"],
entry["devices"],
reachable_devices.get(entry["engine"], ()),
),
install_hint(entry),
)
for entry in task["engines"]
]
return [heading, *(f" {line}" for line in aligned_lines(rows))]
def aligned_lines(rows):
aligned_columns = len(rows[0]) - 1
widths = [
max(len(row[column]) for row in rows) for column in range(aligned_columns)
]
return [
" ".join(
[*(cell.ljust(width) for cell, width in zip(row, widths)), row[-1]]
).rstrip()
for row in rows
]
def one_decimal(value):
return "" if value is None else f"{value:.1f}"
def benchmark_lines(rows):
cells = [
(
row["task"],
row["engine"],
row["device"],
one_decimal(row["load_seconds"]),
one_decimal(row["latency_ms"]),
one_decimal(row["fps"]),
row["note"],
)
for row in rows
]
return aligned_lines([BENCHMARK_HEADER, *cells])
def print_progress(task, engine, device):
print(f"{task} {engine} {device}", file=sys.stderr, flush=True)
def print_benchmark(options, frames, as_json):
rows = benchmark_rows(options, frames, print_progress)
if as_json:
report = {"model": options["model"], "frames": frames, "results": rows}
print(json.dumps(report, indent=2))
return
print("\n".join(benchmark_lines(rows)))
def main(argv=None):
parser = argparse.ArgumentParser(
prog="pyml-engines",
description="List the engines and devices that run a model name or file.",
)
parser.add_argument("model", help="a Hugging Face id, a model name or a file")
parser.add_argument("--json", action="store_true", help="print JSON")
parser.add_argument(
"--benchmark",
action="store_true",
help="time each engine and device that runs the model here",
)
parser.add_argument(
"--frames",
type=int,
default=DEFAULT_BENCHMARK_FRAMES,
help="frames to time per engine and device",
)
arguments = parser.parse_args(argv)
if arguments.frames < 1:
parser.error("--frames must be at least 1")
try:
options = engine_options(arguments.model)
except OSError as error:
first_line = str(error).splitlines()[0]
sys.exit(f"pyml-engines: cannot read {arguments.model}: {first_line}")
if not options["tasks"]:
sys.exit(f"pyml-engines: no task or engine matches {arguments.model}")
if arguments.benchmark:
print_benchmark(options, arguments.frames, arguments.json)
return
if arguments.json:
print(json.dumps(with_available_devices(options), indent=2))
return
reachable_devices = available_devices()
blocks = [
"\n".join(task_lines(task, reachable_devices)) for task in options["tasks"]
]
print("\n\n".join([*blocks, LEGEND]))
if __name__ == "__main__":
main()