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*nullptr indicates "profiling off for this scope"; once set to nullptr the destructor performs no work.event_OpEventProfiler::record on destruction.start_time_std::chrono::steady_clock::time_pointstart_memory_bytes_std::size_tMemoryTracker::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
1Captures the start wall-clock time and memory baseline.
Parameters
namestd::string_viewOpEvent.deviceDevicedtypeDtypeshapeShapeevent_.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
1Finalises 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
1Methods
1Records the estimated floating-point operation count for this scope.
Parameters
fint64_t2 * 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.