forked from open-edge-platform/edge-ai-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize.py
More file actions
248 lines (199 loc) · 9.65 KB
/
optimize.py
File metadata and controls
248 lines (199 loc) · 9.65 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
import logging
import pprint
import re
import subprocess
import time
from dataclasses import dataclass
from itertools import product
from subprocess import PIPE, Popen
from typing import Dict, List
import psutil as ps
from pipeline import GstPipeline
logging.basicConfig(level=logging.INFO)
@dataclass
class OptimizationResult:
params: Dict[str, str]
exit_code: int
total_fps: float
per_stream_fps: float
class PipelineOptimizer:
def __init__(
self,
pipeline: GstPipeline,
constants: Dict[str, str],
param_grid: Dict[str, List[str]],
poll_interval: int = 1,
channels: int | tuple[int, int] = 1,
):
# Initialize class variables
self.pipeline = pipeline
self.constants = constants
self.param_grid = param_grid
self.poll_interval = poll_interval
# Set the number of channels
self.channels = (
channels if isinstance(channels, int) else channels[0] + channels[1]
)
# Set the number if regular channels
# If no tuple is provided, the number of regular channels is 0
self.regular_channels = 0 if isinstance(channels, int) else channels[0]
# Set the number of inference channels
# If no tuple is provided, the number of inference channels is equal to the number of channels
self.inference_channels = channels if isinstance(channels, int) else channels[1]
# Initialize results
self.results: List[OptimizationResult] = []
# Configure logging
self.logger = logging.getLogger("PipelineOptimizer")
def _iterate_param_grid(self, param_grid: Dict[str, List[str]]):
keys, values = zip(*param_grid.items())
for combination in product(*values):
yield dict(zip(keys, combination))
def optimize(self):
# Run gst-inspect-1.0 to get the list of elements
process = Popen(["gst-inspect-1.0", "va"], stdout=PIPE, stderr=PIPE)
elements = process.communicate()[0].decode("utf-8").split("\n")
# Log the elements
self.logger.info("Elements:")
self.logger.info(pprint.pformat(elements))
# Find the available encoder
# Note that the selected encoder is the last one on the list.
# This is usually vah264lpenc if the encoder is available.
# Otherwise, fallback to the only available encoder, usually vah264enc.
encoder = [element for element in elements if "vah264enc" in element or "vah264lpenc" in element][-1]
encoder = encoder.split(":")[0].strip()
# Log the encoder
self.logger.info(f"Encoder: {encoder}")
for params in self._iterate_param_grid(self.param_grid):
# Evaluate the pipeline with the given parameters, constants, and channels
_pipeline = self.pipeline.evaluate(
self.constants, params, self.regular_channels, self.inference_channels, encoder
)
# Log the command
self.logger.info(f"Running pipeline: {_pipeline}")
try:
# Spawn command in a subprocess
process = Popen(_pipeline.split(" "), stdout=PIPE, stderr=PIPE)
exit_code = None
total_fps = None
per_stream_fps = None
num_streams = None
last_fps = None
avg_fps_dict = {}
# Capture Memory and CPU metrics
while process.poll() is None:
time.sleep(self.poll_interval)
if ps.Process(process.pid).status() == "zombie":
exit_code = process.wait()
break
# Define pattern to capture FPSCounter metrics
overall_pattern = r"FpsCounter\(overall ([\d.]+)sec\): total=([\d.]+) fps, number-streams=(\d+), per-stream=([\d.]+) fps"
avg_pattern = r"FpsCounter\(average ([\d.]+)sec\): total=([\d.]+) fps, number-streams=(\d+), per-stream=([\d.]+) fps"
last_pattern = r"FpsCounter\(last ([\d.]+)sec\): total=([\d.]+) fps, number-streams=(\d+), per-stream=([\d.]+) fps"
# Capture FPSCounter metrics
for line in iter(process.stdout.readline, b""):
line_str = line.decode("utf-8")
match = re.search(overall_pattern, line_str)
if match:
result = {
"total_fps": float(match.group(2)),
"number_streams": int(match.group(3)),
"per_stream_fps": float(match.group(4)),
}
if result["number_streams"] == self.channels:
total_fps = result["total_fps"]
num_streams = result["number_streams"]
per_stream_fps = result["per_stream_fps"]
break
match = re.search(avg_pattern, line_str)
if match:
result = {
"total_fps": float(match.group(2)),
"number_streams": int(match.group(3)),
"per_stream_fps": float(match.group(4)),
}
avg_fps_dict[result["number_streams"]] = result
match = re.search(last_pattern, line_str)
if match:
result = {
"total_fps": float(match.group(2)),
"number_streams": int(match.group(3)),
"per_stream_fps": float(match.group(4)),
}
last_fps = result
found_fps = False
if total_fps is None and avg_fps_dict.keys():
if self.channels in avg_fps_dict.keys():
total_fps = avg_fps_dict[self.channels]["total_fps"]
num_streams = avg_fps_dict[self.channels]["number_streams"]
per_stream_fps = avg_fps_dict[self.channels]["per_stream_fps"]
found_fps = True
else:
closest_match = min(avg_fps_dict.keys(), key=lambda x: abs(x -self.channels), default=None)
total_fps = avg_fps_dict[closest_match]["total_fps"]
num_streams = avg_fps_dict[closest_match]["number_streams"]
per_stream_fps = avg_fps_dict[closest_match]["per_stream_fps"]
found_fps = True
if not found_fps and total_fps is None and last_fps:
total_fps = last_fps["total_fps"]
num_streams = last_fps["number_streams"]
per_stream_fps = last_fps["per_stream_fps"]
if total_fps is None:
total_fps = "N/A"
num_streams = "N/A"
per_stream_fps = "N/A"
# Log the metrics
self.logger.info("Exit code: {}".format(exit_code))
self.logger.info("Total FPS is {}".format(total_fps))
self.logger.info("Per Stream FPS is {}".format(per_stream_fps))
# Save results
self.results.append(
OptimizationResult(
params=params,
exit_code=exit_code,
total_fps=total_fps,
per_stream_fps=per_stream_fps,
)
)
except subprocess.CalledProcessError as e:
self.logger.error(f"Error: {e}")
continue
def evaluate(self) -> OptimizationResult:
if not self.results:
raise ValueError("No results to evaluate")
# ignore results where the exit code is not 0
_results = [result for result in self.results if result.exit_code == 0]
# Find the best result
best_result = max(_results, key=lambda x: x.per_stream_fps, default=None)
# Log the best result
self.logger.info("Best result:")
self.logger.info(best_result)
return best_result
if __name__ == "__main__":
# Define constants grid
constants = {"NUM_BUFFERS": "1000"}
# Define parameter grid
# param_grid = {"pattern": ["snow", "ball", "gradient"]}
param_grid = {"pattern": ["snow"]}
class TestPipeline(GstPipeline):
def __init__(self):
super().__init__()
self._pipeline = (
"videotestsrc "
" num-buffers={NUM_BUFFERS} "
" pattern={pattern} ! "
"videoconvert ! "
"gvafpscounter ! "
"fakesink"
)
def evaluate(self, constants, parameters, inference_channels, regular_channels):
return "gst-launch-1.0 -q " + " ".join(
[self._pipeline.format(**parameters, **constants)]
* (inference_channels + regular_channels)
)
# Define parametrized pipeline
pipeline = TestPipeline()
optimizer = PipelineOptimizer(
pipeline=pipeline, constants=constants, param_grid=param_grid, channels=20
)
optimizer.optimize()
_ = optimizer.evaluate()