pub struct ActivationCache { /* private fields */ }Expand description
Stores per-layer last-token activations from a forward pass.
Each tensor has shape [d_model] — the residual stream activation
at the final sequence position for a given layer.
§Example
use candle_mi::ActivationCache;
use candle_core::{Device, Tensor};
let mut cache = ActivationCache::with_capacity(32);
cache.push(Tensor::zeros(128, candle_core::DType::F32, &Device::Cpu).unwrap());
cache.push(Tensor::zeros(128, candle_core::DType::F32, &Device::Cpu).unwrap());
assert_eq!(cache.n_layers(), 2);Implementations§
Source§impl ActivationCache
impl ActivationCache
Sourcepub const fn new(activations: Vec<Tensor>) -> Result<Self>
pub const fn new(activations: Vec<Tensor>) -> Result<Self>
Create a new cache from collected activations.
§Errors
Currently infallible but returns Result for forward compatibility.
Sourcepub fn with_capacity(n_layers: usize) -> Self
pub fn with_capacity(n_layers: usize) -> Self
Create an empty cache with capacity for n_layers layers.
Sourcepub fn get_layer(&self, layer: usize) -> Option<&Tensor>
pub fn get_layer(&self, layer: usize) -> Option<&Tensor>
Get the activation for a specific layer.
Sourcepub fn activations(&self) -> &[Tensor]
pub fn activations(&self) -> &[Tensor]
All cached activations as a slice.
Sourcepub fn to_f32_vecs(&self) -> Result<Vec<Vec<f32>>>
pub fn to_f32_vecs(&self) -> Result<Vec<Vec<f32>>>
Extract activations as f32 vectors.
Returns one Vec<f32> of shape [d_model] per layer.
§Errors
Returns MIError::Model if dtype conversion or flattening fails.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ActivationCache
impl !RefUnwindSafe for ActivationCache
impl Send for ActivationCache
impl Sync for ActivationCache
impl Unpin for ActivationCache
impl UnsafeUnpin for ActivationCache
impl !UnwindSafe for ActivationCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more