pub struct Tensor { /* private fields */ }Expand description
A tensor handle.
In the lazy graph model a Tensor is a lightweight reference to a node in
the computation graph. Operations build up the graph; actual computation
happens when eval() is called (or implicitly via to_vec_f32()).
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn zeros(shape: &Shape, dtype: DType, device: &Device) -> Result<Self>
pub fn zeros(shape: &Shape, dtype: DType, device: &Device) -> Result<Self>
Create a tensor filled with zeros.
Sourcepub fn ones(shape: &Shape, dtype: DType, device: &Device) -> Result<Self>
pub fn ones(shape: &Shape, dtype: DType, device: &Device) -> Result<Self>
Create a tensor filled with ones.
Sourcepub fn from_f32(data: &[f32], shape: &Shape, device: &Device) -> Result<Self>
pub fn from_f32(data: &[f32], shape: &Shape, device: &Device) -> Result<Self>
Create a tensor from f32 data.
Sourcepub fn from_f32_on_stream(
data: &[f32],
shape: &Shape,
stream: &Arc<Stream>,
) -> Result<Self>
pub fn from_f32_on_stream( data: &[f32], shape: &Shape, stream: &Arc<Stream>, ) -> Result<Self>
Create a tensor from f32 data on a specific stream.
Sourcepub fn from_data_with_dtype(
data: Vec<f32>,
shape: &Shape,
dtype: DType,
device: &Device,
) -> Result<Self>
pub fn from_data_with_dtype( data: Vec<f32>, shape: &Shape, dtype: DType, device: &Device, ) -> Result<Self>
Create a tensor from f32 data with a specified dtype.
The data is stored as f32 internally; dtype records the logical type
(e.g. F16 weights that were converted to f32 on load).
Sourcepub fn transpose(&self, axes: Option<&[usize]>) -> Result<Tensor>
pub fn transpose(&self, axes: Option<&[usize]>) -> Result<Tensor>
Transpose (reverses axes by default, or use specified permutation).
Sourcepub fn layer_norm(&self, eps: f32) -> Tensor
pub fn layer_norm(&self, eps: f32) -> Tensor
Layer normalization over the last dimension.
Sourcepub fn rope(&self, rotary_dim: usize, pos_offset: usize, theta: f32) -> Tensor
pub fn rope(&self, rotary_dim: usize, pos_offset: usize, theta: f32) -> Tensor
Apply Rotary Positional Embeddings.
Sourcepub fn layer_norm_vjp(&self, input: &Tensor, eps: f32) -> Result<Tensor>
pub fn layer_norm_vjp(&self, input: &Tensor, eps: f32) -> Result<Tensor>
LayerNorm VJP: compute grad_input given grad_output and original input.
Sourcepub fn rms_norm_vjp(&self, input: &Tensor, eps: f32) -> Result<Tensor>
pub fn rms_norm_vjp(&self, input: &Tensor, eps: f32) -> Result<Tensor>
RmsNorm VJP: compute grad_input given grad_output and original input.
Sourcepub fn softmax_vjp(&self, softmax_output: &Tensor, axis: i32) -> Result<Tensor>
pub fn softmax_vjp(&self, softmax_output: &Tensor, axis: i32) -> Result<Tensor>
Softmax VJP: compute grad_input given grad_output (self) and softmax output.
Sourcepub fn silu_vjp(&self, input: &Tensor) -> Result<Tensor>
pub fn silu_vjp(&self, input: &Tensor) -> Result<Tensor>
SiLU VJP: compute grad_input given grad_output (self) and original input.
Sourcepub fn gelu_vjp(&self, input: &Tensor) -> Result<Tensor>
pub fn gelu_vjp(&self, input: &Tensor) -> Result<Tensor>
GELU VJP: compute grad_input given grad_output (self) and original input.
Sourcepub fn embedding_lookup(&self, indices: &Tensor) -> Result<Tensor>
pub fn embedding_lookup(&self, indices: &Tensor) -> Result<Tensor>
Embedding lookup: gather rows from this weight matrix [vocab, dim]
using indices [seq_len]. Returns [seq_len, dim].
Sourcepub fn narrow(&self, axis: i32, start: i64, length: i64) -> Result<Tensor>
pub fn narrow(&self, axis: i32, start: i64, length: i64) -> Result<Tensor>
Narrow (slice) along an axis: extract length elements starting at start.
Sourcepub fn cat(tensors: &[&Tensor], axis: i32) -> Result<Tensor>
pub fn cat(tensors: &[&Tensor], axis: i32) -> Result<Tensor>
Concatenate tensors along an axis.
Sourcepub fn attention(
&self,
k: &Tensor,
v: &Tensor,
scale: f32,
causal: bool,
) -> Result<Tensor>
pub fn attention( &self, k: &Tensor, v: &Tensor, scale: f32, causal: bool, ) -> Result<Tensor>
Single-head attention: Q @ K^T * scale → causal mask → softmax → @ V. Q: [Tq, Dh], K: [Tk, Dh], V: [Tk, Dh] → Output: [Tq, Dh]
Sourcepub fn eval(&self) -> Result<()>
pub fn eval(&self) -> Result<()>
Materialize the tensor — triggers evaluation of the computation graph.
Sourcepub fn to_vec_f32(&self) -> Result<Vec<f32>>
pub fn to_vec_f32(&self) -> Result<Vec<f32>>
Copy data out as Vec
Sourcepub fn from_node_id(
node_id: NodeId,
shape: Shape,
dtype: DType,
device: Device,
stream: Arc<Stream>,
) -> Self
pub fn from_node_id( node_id: NodeId, shape: Shape, dtype: DType, device: Device, stream: Arc<Stream>, ) -> Self
Reconstruct a tensor handle from a node ID and metadata.
Used by autograd to create handles for graph introspection.
Sourcepub fn broadcast_to(&self, target: &Shape) -> Result<Tensor>
pub fn broadcast_to(&self, target: &Shape) -> Result<Tensor>
Broadcast this tensor to the target shape (numpy-style rules).