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( attn_layer_idx: usize, window_tokens: usize, headroom: f32, gpu: &dyn GpuBackend, ) -> Result<Self>

Create a new calibration tracker.

attn_layer_idx: this layer’s index, for the freeze log line. window_tokens: --fp8-kv-calibration-tokens. The amax accumulates over this many observed tokens, across requests, before the scale freezes (#919). Clamped to MAX_STAGED_TOKENS so the BF16 staging cannot reserve gigabytes per layer; 1 reproduces the pre-#919 freeze-on-first-observe behaviour, and 0 never gets here (the attention initializer does not build a calibrator at all). headroom: multiplier on the accumulated amax 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).

Inside the window: PROVISIONAL_SCALE (private to this module), which every read during the window also uses — consistent, just coarse. After the freeze: the data-derived scale the whole window was requantized to (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, target: &Fp8KvWriteTarget, ) -> Result<()>

Observe K/V projection outputs and update the running max.

Launches absmax reductions on the K and V buffers, reads them back after a sync, then either stages this batch (still inside the window) or freezes and requantizes the staged window. Call this AFTER the K/V projections and BEFORE writing to the KV cache — the caller’s write then uses Self::scales, which is exactly what this call just decided.

k_data/v_data: device BF16 K/V projection outputs. num_tokens/num_kv_heads/head_dim: this batch’s shape. target: where the caller is about to write, so the freeze can replay the window into the same pools.

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