prediction_profiler
anomaly_match.utils.prediction_profiler
¶
Prediction pipeline profiler for structured performance metrics.
Provides context-manager-based stage timing, per-process partial JSON reports,
and a merge step that produces an aggregate performance_report.json.
Uses time.perf_counter() for all timing measurements — the highest-resolution
monotonic clock available on the platform. This is the same clock used internally
by timeit. Unlike timeit (designed for micro-benchmarking small snippets
via repeated execution), perf_counter is the standard choice for production
profiling of real workloads.
PredictionProfiler(output_dir, process_idx=None, script=None)
¶
Profiles prediction pipeline stages with per-batch timing and memory tracking.
Tracked metrics¶
Per-batch (recorded via stage() context managers + end_batch()):
stages: wall-clock time (seconds) for each named stage within the batch, measured withtime.perf_counter().wall_clock: total elapsed time from first stage entry toend_batch().overhead:wall_clock - sum(stages)— time not attributed to any stage.batch_size: number of images in the batch.memory(every MEMORY_SNAPSHOT_EVERY_N_BATCHES batches):resident_set_size_mb(via psutil if installed),gpu_allocated_mbandgpu_reserved_mb(viatorch.cuda).
Per-process (partial report via save_partial_report()):
total_images: sum of batch sizes.total_wall_clock_s: elapsed time from profiler creation to report save.throughput_images_per_sec:total_images / total_wall_clock_s.stages: per-stage totals, percentages, and individual batch times.peak_memory: maximum observed values across all snapshots.finalization_stages: one-off stage timings (e.g.result_save) recorded viarecord_finalization()— not part of batch metrics.script: name of the prediction script that produced this partial.timestamp: ISO-8601 UTC time when the report was written.
Aggregate (merged report via Coordinator.finalize()):
total_wall_clock_s: sum of per-process wall clocks plus spawn overhead, giving the true end-to-end sequential time.throughput_images_per_sec:total_images / total_wall_clock_s.stages: merged totals, percentages, and percentiles (p50/p95/p99) computed from all per-batch times across processes.peak_memory: element-wise max across all processes.per_process: per-process throughput summaries.process_spawn: inter-process spawn gap times and total overhead.batch_timeseries: sampled subset of per-batch stage times (max 200 entries, sampled uniformly per process x stage combination to avoid skipping groups).
Usage::
profiler = PredictionProfiler(output_dir="/tmp/out", script="prediction_zarr")
for batch in batches:
with profiler.stage("io_load"):
data = load(batch)
with profiler.stage("inference"):
result = model(data)
profiler.end_batch(batch_size=len(data))
with profiler.stage("result_save"):
save(result)
profiler.record_finalization()
profiler.save_partial_report()
Source code in anomaly_match/utils/prediction_profiler.py
Coordinator(output_dir)
¶
Coordinates profiler reports across sequential subprocess runs.
Tracks inter-process spawn timing and provides a single finalize()
call that merges partial reports, cleans up intermediates, and logs
the aggregate report path.
Usage in session.py::
coordinator = PredictionProfiler.Coordinator(output_dir)
for file_idx, input_file in enumerate(input_files):
coordinator.record_subprocess_gap()
run_pipeline(...)
coordinator.mark_subprocess_complete()
coordinator.finalize()
Source code in anomaly_match/utils/prediction_profiler.py
stage(name)
¶
end_batch(batch_size)
¶
Finalize the current batch record.
Source code in anomaly_match/utils/prediction_profiler.py
record_finalization()
¶
Record current stages as one-off finalization metrics.
Call this instead of end_batch() for post-loop operations like
result_save that should not inflate batch counts or throughput.
Source code in anomaly_match/utils/prediction_profiler.py
save_partial_report()
¶
Write a per-process partial JSON report to the profiling subdirectory.
Source code in anomaly_match/utils/prediction_profiler.py
merge_reports(profiling_dir, spawn_times=None)
staticmethod
¶
Merge per-process partial reports into a single aggregate report.
Source code in anomaly_match/utils/prediction_profiler.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
cleanup_partial_reports(profiling_dir)
staticmethod
¶
Remove partial report files, preserving the merged report.