pub struct HookCache { /* private fields */ }Expand description
Tensors captured during a forward pass, plus the output logits.
Returned by MIBackend::forward. Use
get to retrieve activations at specific hook points.
§Example
use candle_mi::{HookCache, HookPoint};
use candle_core::{Device, Tensor};
let logits = Tensor::zeros((1, 10, 32000), candle_core::DType::F32, &Device::Cpu).unwrap();
let mut cache = HookCache::new(logits);
// Store a captured activation
let pattern = Tensor::zeros((1, 8, 10, 10), candle_core::DType::F32, &Device::Cpu).unwrap();
cache.store(HookPoint::AttnPattern(5), pattern);
// Retrieve captured activations
let output = cache.output();
let attn = cache.get(&HookPoint::AttnPattern(5)).unwrap();Implementations§
Source§impl HookCache
impl HookCache
Sourcepub fn new(output: Tensor) -> Self
pub fn new(output: Tensor) -> Self
Create a new cache with the given output tensor and no captures.
Sourcepub fn into_output(self) -> Tensor
pub fn into_output(self) -> Tensor
Consume the cache and return the output tensor.
Sourcepub fn get(&self, hook: &HookPoint) -> Option<&Tensor>
pub fn get(&self, hook: &HookPoint) -> Option<&Tensor>
Retrieve a captured tensor by hook point.
Sourcepub fn require(&self, hook: &HookPoint) -> Result<&Tensor>
pub fn require(&self, hook: &HookPoint) -> Result<&Tensor>
Retrieve a captured tensor, returning an error if not found.
§Errors
Returns MIError::Hook if the hook point was not captured.
Sourcepub fn store(&mut self, hook: HookPoint, tensor: Tensor)
pub fn store(&mut self, hook: HookPoint, tensor: Tensor)
Store a captured activation. Called by backend implementations.
Sourcepub fn set_output(&mut self, output: Tensor)
pub fn set_output(&mut self, output: Tensor)
Replace the output tensor (e.g., after computing final logits).
This allows the forward pass to collect captures into a cache initialized with a placeholder, then set the real output at the end.
Sourcepub fn num_captures(&self) -> usize
pub fn num_captures(&self) -> usize
Number of captured tensors (excludes the output).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for HookCache
impl !RefUnwindSafe for HookCache
impl Send for HookCache
impl Sync for HookCache
impl Unpin for HookCache
impl UnsafeUnpin for HookCache
impl !UnwindSafe for HookCache
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