Fp8KvCalibration

Struct Fp8KvCalibration 

Source
pub struct Fp8KvCalibration { /* private fields */ }
Expand description

Online FP8 KV cache scale calibration tracker for one attention layer.

Wraps calibration state in a Mutex so it can live inside a Send + Sync struct (required by TransformerLayer trait).

Implementations§

Source§

impl Fp8KvCalibration

Source

pub fn new( _warmup_tokens: usize, headroom: f32, gpu: &dyn GpuBackend, ) -> Result<Self>

Create a new calibration tracker.

_warmup_tokens: retained for API/CLI compat but no longer gates the freeze — the scale is now frozen on the FIRST observe (before any KV is persisted), so a warmup window would only reintroduce the write/read scale mismatch. Any value > 0 simply enables online calibration. headroom: multiplier on the first-observe absmax when freezing (--fp8-kv-headroom, CLI-validated ≥ 1.0; clamped here as defense in depth because a sub-1.0 value guarantees clipping). gpu: GPU backend for allocating the absmax reduction buffer.

Source

pub fn is_calibrating(&self) -> bool

Whether calibration is still in warmup phase (scales not yet frozen).

Source

pub fn scales(&self) -> (f32, f32)

Get current scales. Returns (k_scale, v_scale).

Before the first observe: the conservative construction default (2.0, covering ±896) — never used for a persisted write, since observe() runs before every write and freezes on its first call. After the first observe: the frozen, data-derived scale (constant thereafter).

Source

pub fn observe( &self, gpu: &dyn GpuBackend, k_data: DevicePtr, v_data: DevicePtr, num_tokens: u32, num_kv_heads: u32, head_dim: u32, stream: u64, ) -> Result<()>

Observe K/V projection outputs and update running max.

Launches absmax reduction kernels on the K and V buffers, then reads the results back to CPU after a sync. Call this AFTER K/V projections and BEFORE writing to the KV cache.

k_data: device BF16 buffer of K projection output v_data: device BF16 buffer of V projection output num_tokens: number of tokens in the batch num_kv_heads: number of KV heads head_dim: dimension per head

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more