Profiler
Profiler(with_memory: bool = True)Record op-level timing and memory traces from the C++ engine.
Drives the engine's profiling hook so that every dispatched op
becomes an OpEvent. Suitable for both interactive use
(with block) and programmatic control via explicit
start / stop. Recorded events can be exported as a
Chrome / Perfetto trace for visual inspection.
Parameters
with_memorybool= TrueTrue (default), also capture allocator stats via
MemoryStats.Notes
Total recorded time can be summarised per op as
via key_averages. Only one Profiler may be active
at a time; entering a nested instance overwrites the engine's
current profiler pointer until the outer context exits.
Examples
>>> import lucid
>>> with lucid.profiler.profile() as prof:
... y = lucid.randn(64, 64) @ lucid.randn(64, 64)
>>> top = prof.key_averages()[:5]Methods (10)
__init__
→None__init__(with_memory: bool = True)Initialise the instance. See the class docstring for parameter semantics.
start
→Nonestart()Start recording operations.
stop
→Nonestop()Stop recording operations.
events
→list[OpEvent]events()Return all recorded OpEvents.
key_averages
→list[ProfileSummary]key_averages()Return per-operation-name summaries sorted by total time.
memory_stats
→MemoryStats | Nonememory_stats()Return memory stats if available.
export_chrome_trace
→Noneexport_chrome_trace(path: str)Export events as a Chrome trace JSON file.
Open with chrome://tracing or https://ui.perfetto.dev.
clear
→Noneclear()Clear all recorded events.
__enter__
→Profiler__enter__()Enter the context. Returns self so the value can be bound via with ... as.
__exit__
→None__exit__(args: object = ())Exit the context, restoring any state that was modified on entry.