Skip to content
class

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= 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]

Used by 1

Constructors

1
dunder

__init__

→None
__init__(with_memory: bool = True)

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

Instance methods

7
fn

clear

→None
clear()

Clear all recorded events.

fn

start

→None
start()

Start recording operations.

fn

stop

→None
stop()

Stop recording operations.

Dunder methods

2
dunder

__enter__

→Profiler
__enter__()

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

dunder

__exit__

→None
__exit__(args: object = ())

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