Expand description
Per-kernel GPU timing for the native LLM forward/decode path (W2 / D17).
Wraps each LLM compute pass with TIMESTAMP_QUERY writes, resolves the query set,
and accumulates GPU-internal nanoseconds per Phase. Gated by a runtime flag
(QUALIA_LLM_GPU_PROFILE=1 or set_enabled) so production decode pays nothing:
when disabled, the pass_writes_* helpers return None and the pass is byte-identical
to before (timestamp_writes: None).
Requires the shared device to have negotiated TIMESTAMP_QUERY
(see crate::gpu_context::SharedGpuContext::timestamps_supported); degrades to a
no-op otherwise, so it is safe to call unconditionally from every dispatch site.
Honesty note: the per-phase nanoseconds are GPU-internal (begin→end of the pass) and individually accurate. A profiling run serialises per-op readback, so the headline tok/s of a profiled run is perturbed — report the per-phase split, not the profiled tok/s.
Not re-entrant: it uses one shared 2-slot query set, which the single LLM engine thread’s per-op blocking readback serialises. That matches how decode actually runs.
Structs§
- Phase
Timing - Accumulated GPU time for one phase across all dispatches since the last
reset.
Enums§
- Phase
- Distinct kernel families on the LLM forward/decode path.
Functions§
- accumulate
- Read the resolved timestamps and add
end - beginns tophase’s accumulator. Call after the kernel’s submit + device poll. No-op when profiling is off. - any_
recorded - True if any phase recorded GPU time since the last reset (test/diagnostic helper).
- enabled
- True when profiling is requested AND the shared device negotiated
TIMESTAMP_QUERY. - pass_
writes_ begin timestamp_writesfor the FIRST pass of a multi-pass kernel (begin only).- pass_
writes_ both timestamp_writesfor a single-pass kernel (writes both begin and end on this pass). ReturnsNone(zero overhead) when profiling is off — drop straight into the descriptor.- pass_
writes_ end timestamp_writesfor the LAST pass of a multi-pass kernel (end only).- reset
- Zero all phase accumulators (call before a measured run).
- resolve
- Encode the query-set resolve into
encoder(call after the pass, beforefinish()). No-op when profiling is off. - set_
enabled - Force GPU pass profiling on/off at runtime (bench-only; production leaves it off).
- snapshot
- Snapshot all phase accumulators (for the bench JSON).