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
impl TensorCapture
Sourcepub fn for_layers(layer_indices: &[usize]) -> Self
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]);Sourcepub fn for_prefix(prefix: &str) -> Self
pub fn for_prefix(prefix: &str) -> Self
Sourcepub fn all() -> Self
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.
Sourcepub fn clear(&mut self)
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.
Sourcepub fn get(&self, name: &str) -> Option<&CapturedTensor>
pub fn get(&self, name: &str) -> Option<&CapturedTensor>
Get a captured tensor by its full name (e.g. "l_out-13").
Sourcepub fn get_layer(&self, layer_idx: usize) -> Option<&CapturedTensor>
pub fn get_layer(&self, layer_idx: usize) -> Option<&CapturedTensor>
Get a captured layer output by layer index.
Looks up "l_out-{layer_idx}".
Sourcepub fn has_layer(&self, layer_idx: usize) -> bool
pub fn has_layer(&self, layer_idx: usize) -> bool
Returns true if the specified layer was captured.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &CapturedTensor)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &CapturedTensor)>
Iterate over all captured tensors.
Sourcepub fn captured_layers(&self) -> Vec<usize>
pub fn captured_layers(&self) -> Vec<usize>
Get all captured layer indices (sorted).