class

Profiler

Profiler(with_memory: bool = True)
source

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= True
If True (default), also capture allocator stats via MemoryStats.

Notes

Total recorded time can be summarised per op as

Top=i:name(ei)=opti,T_{\text{op}} = \sum_{i : \text{name}(e_i) = \text{op}} t_i,

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)

dunder

__init__

None
__init__(with_memory: bool = True)
source

Initialise the instance. See the class docstring for parameter semantics.

fn

start

None
start()
source

Start recording operations.

fn

stop

None
stop()
source

Stop recording operations.

fn

events

list[OpEvent]
events()
source

Return all recorded OpEvents.

fn

key_averages

list[ProfileSummary]
key_averages()
source

Return per-operation-name summaries sorted by total time.

fn

memory_stats

MemoryStats | None
memory_stats()
source

Return memory stats if available.

fn

export_chrome_trace

None
export_chrome_trace(path: str)
source

Export events as a Chrome trace JSON file.

Open with chrome://tracing or https://ui.perfetto.dev.

fn

clear

None
clear()
source

Clear all recorded events.

dunder

__enter__

Profiler
__enter__()
source

Enter the context. Returns self so the value can be bound via with ... as.

dunder

__exit__

None
__exit__(args: object = ())
source

Exit the context, restoring any state that was modified on entry.