Skip to content
class

OpScope

OpScope

RAII scope that records one OpEvent into the thread's active Profiler on destruction.

Constructing the scope captures the wall-clock start time and the memory baseline for device; the destructor computes time_ns and memory_delta_bytes and calls Profiler::record. When no profiler is active, sink_ is set to nullptr at construction and the destructor short-circuits — the hot path costs one TLS load and one null check.

See Also

OpScopeFull — composite that also pushes an error-context frame.

Attributes

sink_Profiler*
Non-owning pointer to the profiler captured at construction time. nullptr indicates "profiling off for this scope"; once set to nullptr the destructor performs no work.
event_OpEvent
Partially-filled event accumulated through the scope's lifetime and forwarded to Profiler::record on destruction.
start_time_std::chrono::steady_clock::time_point
Wall-clock instant captured at construction.
start_memory_bytes_std::size_t
MemoryTracker::current_bytes snapshot taken at construction; subtracted on destruction to compute memory_delta_bytes.

Notes

Ownership: OpScope never owns the Profiler. Callers of set_current_profiler are responsible for keeping the pointed-to object alive for the entire scope of any OpScope that could observe it.

The scope is non-copyable — copying would double-record the same event into the sink.

Examples

Wrap a kernel implementation::
    OpScope scope("matmul", a->device(), a->dtype(), out_shape);
    scope.set_flops(2LL * M * N * K);
    // ... kernel work ...

Constructors

1
ctor

OpScope

void OpScope(int name, Device device, Dtype dtype, int shape)

Captures the start wall-clock time and memory baseline.

Parameters

namestd::string_view
Op name to store in the resulting OpEvent.
deviceDevice
Device on which the op is executing.
dtypeDtype
Element dtype of the op's output.
shapeShape
Output shape; moved into event_.

Notes

If no Profiler is active at this moment, sink_ is set to nullptr immediately and the destructor will skip the MemoryTracker query — keeping un-profiled ops cheap.

Destructor

1
dtor

~OpScope

void ~OpScope()

Finalises the event with elapsed time and memory delta, then forwards it to Profiler::record.

Notes

Short-circuits when sink_ == nullptr (the no-profiler fast path). Otherwise performs a steady-clock subtraction, a memory-tracker query, and one mutex-guarded vector append on the sink.

Operators

1
op

operator=

OpScope & operator=(const OpScope &)

Methods

2
fn

set_backward

void set_backward()

Mark the event as backward work (OpEvent::backward).

fn

set_flops

void set_flops(int f)

Records the estimated floating-point operation count for this scope.

Parameters

fint64_t
FLOPs estimate (e.g. 2 * M * N * K for an M x N matmul with K inner dim).

Notes

Must be called before the scope exits; otherwise the recorded event reports flops == 0. Ops free to omit this if they do not have a meaningful FLOPs count.