halib 0.1.77__tar.gz → 0.1.81__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. {halib-0.1.77 → halib-0.1.81}/PKG-INFO +5 -1
  2. {halib-0.1.77 → halib-0.1.81}/README.md +4 -0
  3. halib-0.1.81/halib/research/profiler.py +299 -0
  4. {halib-0.1.77 → halib-0.1.81}/halib.egg-info/PKG-INFO +5 -1
  5. {halib-0.1.77 → halib-0.1.81}/halib.egg-info/SOURCES.txt +1 -0
  6. {halib-0.1.77 → halib-0.1.81}/setup.py +1 -1
  7. {halib-0.1.77 → halib-0.1.81}/.gitignore +0 -0
  8. {halib-0.1.77 → halib-0.1.81}/GDriveFolder.txt +0 -0
  9. {halib-0.1.77 → halib-0.1.81}/LICENSE.txt +0 -0
  10. {halib-0.1.77 → halib-0.1.81}/MANIFEST.in +0 -0
  11. {halib-0.1.77 → halib-0.1.81}/guide_publish_pip.pdf +0 -0
  12. {halib-0.1.77 → halib-0.1.81}/halib/__init__.py +0 -0
  13. {halib-0.1.77 → halib-0.1.81}/halib/common.py +0 -0
  14. {halib-0.1.77 → halib-0.1.81}/halib/cuda.py +0 -0
  15. {halib-0.1.77 → halib-0.1.81}/halib/filetype/__init__.py +0 -0
  16. {halib-0.1.77 → halib-0.1.81}/halib/filetype/csvfile.py +0 -0
  17. {halib-0.1.77 → halib-0.1.81}/halib/filetype/jsonfile.py +0 -0
  18. {halib-0.1.77 → halib-0.1.81}/halib/filetype/textfile.py +0 -0
  19. {halib-0.1.77 → halib-0.1.81}/halib/filetype/videofile.py +0 -0
  20. {halib-0.1.77 → halib-0.1.81}/halib/filetype/yamlfile.py +0 -0
  21. {halib-0.1.77 → halib-0.1.81}/halib/online/__init__.py +0 -0
  22. {halib-0.1.77 → halib-0.1.81}/halib/online/gdrive.py +0 -0
  23. {halib-0.1.77 → halib-0.1.81}/halib/online/gdrive_mkdir.py +0 -0
  24. {halib-0.1.77 → halib-0.1.81}/halib/online/gdrive_test.py +0 -0
  25. {halib-0.1.77 → halib-0.1.81}/halib/online/projectmake.py +0 -0
  26. {halib-0.1.77 → halib-0.1.81}/halib/research/__init__.py +0 -0
  27. {halib-0.1.77 → halib-0.1.81}/halib/research/base_config.py +0 -0
  28. {halib-0.1.77 → halib-0.1.81}/halib/research/base_exp.py +0 -0
  29. {halib-0.1.77 → halib-0.1.81}/halib/research/dataset.py +0 -0
  30. {halib-0.1.77 → halib-0.1.81}/halib/research/metrics.py +0 -0
  31. {halib-0.1.77 → halib-0.1.81}/halib/research/perfcalc.py +0 -0
  32. {halib-0.1.77 → halib-0.1.81}/halib/research/perftb.py +0 -0
  33. {halib-0.1.77 → halib-0.1.81}/halib/research/plot.py +0 -0
  34. {halib-0.1.77 → halib-0.1.81}/halib/research/torchloader.py +0 -0
  35. {halib-0.1.77 → halib-0.1.81}/halib/research/wandb_op.py +0 -0
  36. {halib-0.1.77 → halib-0.1.81}/halib/rich_color.py +0 -0
  37. {halib-0.1.77 → halib-0.1.81}/halib/system/__init__.py +0 -0
  38. {halib-0.1.77 → halib-0.1.81}/halib/system/cmd.py +0 -0
  39. {halib-0.1.77 → halib-0.1.81}/halib/system/filesys.py +0 -0
  40. {halib-0.1.77 → halib-0.1.81}/halib/utils/__init__.py +0 -0
  41. {halib-0.1.77 → halib-0.1.81}/halib/utils/dataclass_util.py +0 -0
  42. {halib-0.1.77 → halib-0.1.81}/halib/utils/dict_op.py +0 -0
  43. {halib-0.1.77 → halib-0.1.81}/halib/utils/gpu_mon.py +0 -0
  44. {halib-0.1.77 → halib-0.1.81}/halib/utils/listop.py +0 -0
  45. {halib-0.1.77 → halib-0.1.81}/halib/utils/tele_noti.py +0 -0
  46. {halib-0.1.77 → halib-0.1.81}/halib/utils/video.py +0 -0
  47. {halib-0.1.77 → halib-0.1.81}/halib.egg-info/dependency_links.txt +0 -0
  48. {halib-0.1.77 → halib-0.1.81}/halib.egg-info/requires.txt +0 -0
  49. {halib-0.1.77 → halib-0.1.81}/halib.egg-info/top_level.txt +0 -0
  50. {halib-0.1.77 → halib-0.1.81}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: halib
3
- Version: 0.1.77
3
+ Version: 0.1.81
4
4
  Summary: Small library for common tasks
5
5
  Author: Hoang Van Ha
6
6
  Author-email: hoangvanhauit@gmail.com
@@ -52,6 +52,10 @@ Dynamic: summary
52
52
 
53
53
  Helper package for coding and automation
54
54
 
55
+ **Version 0.1.81**
56
+
57
+ + `research/profiler`: add `zProfiler` class to measure execution time of contexts and steps, with support for dynamic color scales in plots.
58
+
55
59
  **Version 0.1.77**
56
60
 
57
61
  + `research/base_exp`: add base experiment class to handle common experiment tasks, including performance calculation and saving results.
@@ -1,5 +1,9 @@
1
1
  Helper package for coding and automation
2
2
 
3
+ **Version 0.1.81**
4
+
5
+ + `research/profiler`: add `zProfiler` class to measure execution time of contexts and steps, with support for dynamic color scales in plots.
6
+
3
7
  **Version 0.1.77**
4
8
 
5
9
  + `research/base_exp`: add base experiment class to handle common experiment tasks, including performance calculation and saving results.
@@ -0,0 +1,299 @@
1
+ import os
2
+ import time
3
+ import json
4
+
5
+ from pathlib import Path
6
+ from pprint import pprint
7
+ from threading import Lock
8
+
9
+ from plotly.subplots import make_subplots
10
+ import plotly.graph_objects as go
11
+ import plotly.express as px # for dynamic color scales
12
+ from ..common import ConsoleLog
13
+
14
+ from loguru import logger
15
+
16
+ class zProfiler:
17
+ """A singleton profiler to measure execution time of contexts and steps.
18
+
19
+ Args:
20
+ interval_report (int): Frequency of periodic reports (0 to disable).
21
+ stop_to_view (bool): Pause execution to view reports if True (only in debug mode).
22
+ output_file (str): Path to save the profiling report.
23
+ report_format (str): Output format for reports ("json" or "csv").
24
+
25
+ Example:
26
+ prof = zProfiler()
27
+ prof.ctx_start("my_context")
28
+ prof.step_start("my_context", "step1")
29
+ time.sleep(0.1)
30
+ prof.step_end("my_context", "step1")
31
+ prof.ctx_end("my_context")
32
+ """
33
+
34
+ _instance = None
35
+ _lock = Lock()
36
+
37
+ def __new__(cls, *args, **kwargs):
38
+ with cls._lock:
39
+ if cls._instance is None:
40
+ cls._instance = super().__new__(cls)
41
+ return cls._instance
42
+
43
+ def __init__(
44
+ self,
45
+ ):
46
+ if not hasattr(self, "_initialized"):
47
+ self.time_dict = {}
48
+ self._initialized = True
49
+
50
+ def ctx_start(self, ctx_name="ctx_default"):
51
+ if not isinstance(ctx_name, str) or not ctx_name:
52
+ raise ValueError("ctx_name must be a non-empty string")
53
+ if ctx_name not in self.time_dict:
54
+ self.time_dict[ctx_name] = {
55
+ "start": time.perf_counter(),
56
+ "step_dict": {},
57
+ "report_count": 0,
58
+ }
59
+ self.time_dict[ctx_name]["report_count"] += 1
60
+
61
+ def ctx_end(self, ctx_name="ctx_default", report_func=None):
62
+ if ctx_name not in self.time_dict:
63
+ return
64
+ self.time_dict[ctx_name]["end"] = time.perf_counter()
65
+ self.time_dict[ctx_name]["duration"] = (
66
+ self.time_dict[ctx_name]["end"] - self.time_dict[ctx_name]["start"]
67
+ )
68
+
69
+ def step_start(self, ctx_name, step_name):
70
+ if not isinstance(step_name, str) or not step_name:
71
+ raise ValueError("step_name must be a non-empty string")
72
+ if ctx_name not in self.time_dict:
73
+ return
74
+ if step_name not in self.time_dict[ctx_name]["step_dict"]:
75
+ self.time_dict[ctx_name]["step_dict"][step_name] = []
76
+ self.time_dict[ctx_name]["step_dict"][step_name].append([time.perf_counter()])
77
+
78
+ def step_end(self, ctx_name, step_name):
79
+ if (
80
+ ctx_name not in self.time_dict
81
+ or step_name not in self.time_dict[ctx_name]["step_dict"]
82
+ ):
83
+ return
84
+ self.time_dict[ctx_name]["step_dict"][step_name][-1].append(time.perf_counter())
85
+
86
+ def _step_dict_to_detail(self, ctx_step_dict):
87
+ """
88
+ 'ctx_step_dict': {
89
+ │ │ 'preprocess': [
90
+ │ │ │ [278090.947465806, 278090.960484853],
91
+ │ │ │ [278091.178424035, 278091.230944486],
92
+ │ │ 'infer': [
93
+ │ │ │ [278090.960490534, 278091.178424035],
94
+ │ │ │ [278091.230944486, 278091.251378469],
95
+ │ }
96
+ """
97
+ assert (
98
+ len(ctx_step_dict.keys()) > 1
99
+ ), "step_dict must have only one key (step_name) for detail."
100
+
101
+ for step_name, time_list in ctx_step_dict.items():
102
+ normed_ctx_step_dict = {}
103
+ if not isinstance(ctx_step_dict[step_name], list):
104
+ raise ValueError(f"Step data for {step_name} must be a list")
105
+ step_name = list(ctx_step_dict.keys())[0]
106
+ normed_time_ls = []
107
+ for idx, time_data in enumerate(time_list):
108
+ elapsed_time = -1
109
+ if len(time_data) == 2:
110
+ start, end = time_data[0], time_data[1]
111
+ elapsed_time = end - start
112
+ normed_time_ls.append((idx, elapsed_time)) # including step
113
+ normed_ctx_step_dict[step_name] = normed_time_ls
114
+ return normed_ctx_step_dict
115
+
116
+ def get_report_dict(self, with_detail=False):
117
+ report_dict = {}
118
+ for ctx_name, ctx_dict in self.time_dict.items():
119
+ report_dict[ctx_name] = {
120
+ "duration": ctx_dict.get("duration", 0.0),
121
+ "step_dict": {
122
+ "summary": {"avg_time": {}, "percent_time": {}},
123
+ "detail": {},
124
+ },
125
+ }
126
+
127
+ if with_detail:
128
+ report_dict[ctx_name]["step_dict"]["detail"] = (
129
+ self._step_dict_to_detail(ctx_dict["step_dict"])
130
+ )
131
+ avg_time_list = []
132
+ epsilon = 1e-5
133
+ for step_name, step_list in ctx_dict["step_dict"].items():
134
+ durations = []
135
+ try:
136
+ for time_data in step_list:
137
+ if len(time_data) != 2:
138
+ continue
139
+ start, end = time_data
140
+ durations.append(end - start)
141
+ except Exception as e:
142
+ logger.error(
143
+ f"Error processing step {step_name} in context {ctx_name}: {e}"
144
+ )
145
+ continue
146
+ if not durations:
147
+ continue
148
+ avg_time = sum(durations) / len(durations)
149
+ if avg_time < epsilon:
150
+ continue
151
+ avg_time_list.append((step_name, avg_time))
152
+ total_avg_time = (
153
+ sum(time for _, time in avg_time_list) or 1e-10
154
+ ) # Avoid division by zero
155
+ for step_name, avg_time in avg_time_list:
156
+ report_dict[ctx_name]["step_dict"]["summary"]["percent_time"][
157
+ f"per_{step_name}"
158
+ ] = (avg_time / total_avg_time) * 100.0
159
+ report_dict[ctx_name]["step_dict"]["summary"]["avg_time"][
160
+ f"avg_{step_name}"
161
+ ] = avg_time
162
+ report_dict[ctx_name]["step_dict"]["summary"][
163
+ "total_avg_time"
164
+ ] = total_avg_time
165
+ report_dict[ctx_name]["step_dict"]["summary"] = dict(
166
+ sorted(report_dict[ctx_name]["step_dict"]["summary"].items())
167
+ )
168
+ return report_dict
169
+
170
+ @classmethod
171
+ @classmethod
172
+ def plot_formatted_data(
173
+ cls, profiler_data, outdir=None, file_format="png", do_show=False
174
+ ):
175
+ """
176
+ Plot each context in a separate figure with bar + pie charts.
177
+ Save each figure in the specified format (png or svg).
178
+ """
179
+
180
+ if outdir is not None:
181
+ os.makedirs(outdir, exist_ok=True)
182
+
183
+ if file_format.lower() not in ["png", "svg"]:
184
+ raise ValueError("file_format must be 'png' or 'svg'")
185
+
186
+ results = {} # {context: fig}
187
+
188
+ for ctx, ctx_data in profiler_data.items():
189
+ summary = ctx_data["step_dict"]["summary"]
190
+ avg_times = summary["avg_time"]
191
+ percent_times = summary["percent_time"]
192
+
193
+ step_names = [s.replace("avg_", "") for s in avg_times.keys()]
194
+ # pprint(f'{step_names=}')
195
+ n_steps = len(step_names)
196
+
197
+ # Generate dynamic colors
198
+ colors = px.colors.sample_colorscale(
199
+ "Viridis", [i / (n_steps - 1) for i in range(n_steps)]
200
+ )
201
+ # pprint(f'{len(colors)} colors generated for {n_steps} steps')
202
+ color_map = dict(zip(step_names, colors))
203
+
204
+ # Create figure
205
+ fig = make_subplots(
206
+ rows=1,
207
+ cols=2,
208
+ subplot_titles=[f"Avg Time", f"% Time"],
209
+ specs=[[{"type": "bar"}, {"type": "pie"}]],
210
+ )
211
+
212
+ # Bar chart
213
+ fig.add_trace(
214
+ go.Bar(
215
+ x=step_names,
216
+ y=list(avg_times.values()),
217
+ text=[f"{v*1000:.2f} ms" for v in avg_times.values()],
218
+ textposition="outside",
219
+ marker=dict(color=[color_map[s] for s in step_names]),
220
+ name="", # unified legend
221
+ showlegend=False,
222
+ ),
223
+ row=1,
224
+ col=1,
225
+ )
226
+
227
+ # Pie chart (colors match bar)
228
+ fig.add_trace(
229
+ go.Pie(
230
+ labels=step_names,
231
+ values=list(percent_times.values()),
232
+ marker=dict(colors=[color_map[s] for s in step_names]),
233
+ hole=0.4,
234
+ name="",
235
+ showlegend=True,
236
+ ),
237
+ row=1,
238
+ col=2,
239
+ )
240
+
241
+ # Layout
242
+ fig.update_layout(
243
+ title_text=f"Context Profiler: {ctx}",
244
+ width=1000,
245
+ height=400,
246
+ showlegend=True,
247
+ legend=dict(title="Steps", x=1.05, y=0.5, traceorder="normal"),
248
+ hovermode="x unified",
249
+ )
250
+
251
+ fig.update_xaxes(title_text="Steps", row=1, col=1)
252
+ fig.update_yaxes(title_text="Avg Time (ms)", row=1, col=1)
253
+
254
+ # Show figure
255
+ if do_show:
256
+ fig.show()
257
+
258
+ # Save figure
259
+ if outdir is not None:
260
+ file_path = os.path.join(outdir, f"{ctx}_summary.{file_format.lower()}")
261
+ fig.write_image(file_path)
262
+ print(f"Saved figure: {file_path}")
263
+
264
+ results[ctx] = fig
265
+
266
+ return results
267
+
268
+ def report_and_plot(self, outdir=None, file_format="png", do_show=False):
269
+ """
270
+ Generate the profiling report and plot the formatted data.
271
+
272
+ Args:
273
+ outdir (str): Directory to save figures. If None, figures are only shown.
274
+ file_format (str): Target file format, "png" or "svg". Default is "png".
275
+ do_show (bool): Whether to display the plots. Default is False.
276
+ """
277
+ report = self.get_report_dict()
278
+ self.get_report_dict(with_detail=False)
279
+ return self.plot_formatted_data(
280
+ report, outdir=outdir, file_format=file_format, do_show=do_show
281
+ )
282
+ def meta_info(self):
283
+ """
284
+ Print the structure of the profiler's time dictionary.
285
+ Useful for debugging and understanding the profiler's internal state.
286
+ """
287
+ for ctx_name, ctx_dict in self.time_dict.items():
288
+ with ConsoleLog(f"Context: {ctx_name}"):
289
+ step_names = list(ctx_dict['step_dict'].keys())
290
+ for step_name in step_names:
291
+ pprint(f"Step: {step_name}")
292
+
293
+ def save_report_dict(self, output_file, with_detail=False):
294
+ try:
295
+ report = self.get_report_dict(with_detail=with_detail)
296
+ with open(output_file, "w") as f:
297
+ json.dump(report, f, indent=4)
298
+ except Exception as e:
299
+ logger.error(f"Failed to save report to {output_file}: {e}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: halib
3
- Version: 0.1.77
3
+ Version: 0.1.81
4
4
  Summary: Small library for common tasks
5
5
  Author: Hoang Van Ha
6
6
  Author-email: hoangvanhauit@gmail.com
@@ -52,6 +52,10 @@ Dynamic: summary
52
52
 
53
53
  Helper package for coding and automation
54
54
 
55
+ **Version 0.1.81**
56
+
57
+ + `research/profiler`: add `zProfiler` class to measure execution time of contexts and steps, with support for dynamic color scales in plots.
58
+
55
59
  **Version 0.1.77**
56
60
 
57
61
  + `research/base_exp`: add base experiment class to handle common experiment tasks, including performance calculation and saving results.
@@ -33,6 +33,7 @@ halib/research/metrics.py
33
33
  halib/research/perfcalc.py
34
34
  halib/research/perftb.py
35
35
  halib/research/plot.py
36
+ halib/research/profiler.py
36
37
  halib/research/torchloader.py
37
38
  halib/research/wandb_op.py
38
39
  halib/system/__init__.py
@@ -8,7 +8,7 @@ with open("requirements.txt") as f:
8
8
 
9
9
  setuptools.setup(
10
10
  name="halib",
11
- version="0.1.77",
11
+ version="0.1.81",
12
12
  author="Hoang Van Ha",
13
13
  author_email="hoangvanhauit@gmail.com",
14
14
  description="Small library for common tasks",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes