Skip to main content

TensorCapture

Struct TensorCapture 

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

Captures intermediate tensor outputs during llama_decode.

Create a TensorCapture, attach it to LlamaContextParams via with_tensor_capture, then call decode(). After decode completes, read captured data via [get], [get_layer], or [iter].

§Lifetime & Safety

The TensorCapture must outlive the LlamaContext it is attached to. The borrow is enforced by with_tensor_capture taking &mut self.

Implementations§

Source§

impl TensorCapture

Source

pub fn for_layers(layer_indices: &[usize]) -> Self

Create a capture that intercepts layer outputs "l_out-{N}" for the specified layer indices.

This is the most common use case for extracting per-layer hidden states from a language model.

§Example
// Capture layers 13, 20, 27 (typical for LLaMA-3.2-3B with positions [0.5, 0.75, 1.0])
let mut capture = TensorCapture::for_layers(&[13, 20, 27]);
Source

pub fn for_names(names: &[&str]) -> Self

Create a capture that intercepts tensors with exact matching names.

§Example
let mut capture = TensorCapture::for_names(&["result_norm", "l_out-27"]);
Source

pub fn for_prefix(prefix: &str) -> Self

Create a capture that intercepts all tensors whose name starts with the given prefix.

§Example
// Capture all attention outputs
let mut capture = TensorCapture::for_prefix("attn_out");
Source

pub fn all() -> Self

Create a capture that intercepts all tensors.

⚠️ Warning: this can produce very large amounts of data. Use only for debugging or inspection.

Source

pub fn clear(&mut self)

Clear all previously captured data, keeping the filter configuration.

Call this before a new decode() if reusing the capture across multiple batches.

Source

pub fn get(&self, name: &str) -> Option<&CapturedTensor>

Get a captured tensor by its full name (e.g. "l_out-13").

Source

pub fn get_layer(&self, layer_idx: usize) -> Option<&CapturedTensor>

Get a captured layer output by layer index.

Looks up "l_out-{layer_idx}".

Source

pub fn has_layer(&self, layer_idx: usize) -> bool

Returns true if the specified layer was captured.

Source

pub fn len(&self) -> usize

Number of tensors captured so far.

Source

pub fn is_empty(&self) -> bool

Returns true if no tensors have been captured.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &CapturedTensor)>

Iterate over all captured tensors.

Source

pub fn captured_layers(&self) -> Vec<usize>

Get all captured layer indices (sorted).

Trait Implementations§

Source§

impl Debug for TensorCapture

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.

Source§

impl<T> Instrument for T

Source§

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

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

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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