pub struct TracedTensor {
pub id: TracedTensorId,
pub rank: usize,
pub dtype: DType,
pub val: LocalValueId,
/* private fields */
}Fields§
§id: TracedTensorId§rank: usize§dtype: DType§val: LocalValueIdImplementations§
Source§impl TracedTensor
impl TracedTensor
Sourcepub fn index_select(&self, axis: isize, positions: &[usize]) -> Result<Self>
pub fn index_select(&self, axis: isize, positions: &[usize]) -> Result<Self>
Select entries from one axis using host-known positions.
§Examples
use tenferro_cpu::CpuBackend;
use tenferro_runtime::{GraphCompiler, GraphExecutor, Tensor, TracedTensor};
let x = TracedTensor::from_tensor_concrete_shape(
Tensor::from_vec_col_major(vec![3], vec![10.0_f64, 20.0, 30.0]).unwrap(),
)
.unwrap();
let y = x.index_select(-1, &[2, 0]).unwrap();
let mut compiler = GraphCompiler::new();
let program = compiler.compile(&y).unwrap();
let out = GraphExecutor::new(CpuBackend::new()).run(&program).unwrap();
assert_eq!(
out.as_slice::<f64>().unwrap(),
&[30.0, 10.0],
);Sourcepub fn stack(tensors: &[&Self], dim: isize) -> Result<Self>
pub fn stack(tensors: &[&Self], dim: isize) -> Result<Self>
Stack tensors along a newly inserted axis.
§Examples
use tenferro_cpu::CpuBackend;
use tenferro_runtime::{GraphCompiler, GraphExecutor, Tensor, TracedTensor};
let a = TracedTensor::from_tensor_concrete_shape(Tensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap()).unwrap();
let b = TracedTensor::from_tensor_concrete_shape(Tensor::from_vec_col_major(vec![], vec![2.0_f64]).unwrap()).unwrap();
let stacked = TracedTensor::stack(&[&a, &b], -1).unwrap();
let mut compiler = GraphCompiler::new();
let program = compiler.compile(&stacked).unwrap();
let out = GraphExecutor::new(CpuBackend::new()).run(&program).unwrap();
assert_eq!(
out.as_slice::<f64>().unwrap(),
&[1.0, 2.0],
);Sourcepub fn concatenate(tensors: &[&Self], axis: usize) -> Result<Self>
pub fn concatenate(tensors: &[&Self], axis: usize) -> Result<Self>
Concatenate tensors along one existing axis.
Source§impl TracedTensor
impl TracedTensor
Sourcepub fn graph(&self) -> &Arc<Graph<StdTensorOp>>
pub fn graph(&self) -> &Arc<Graph<StdTensorOp>>
Return the graph that owns this traced tensor’s current value.
§Examples
use tenferro_runtime::TracedTensor;
let x = TracedTensor::from_vec_col_major(vec![1], vec![1.0_f64]).unwrap();
let _graph = x.graph();Sourcepub fn attached_data(&self) -> Option<&Arc<Tensor>>
pub fn attached_data(&self) -> Option<&Arc<Tensor>>
Return the concrete tensor data attached to this traced value, if any.
Placeholder tensors created with input_concrete_shape or
input_symbolic_shape have no attached data until execution bindings
provide it.
§Examples
use tenferro_runtime::{DType, TracedTensor};
let concrete = TracedTensor::from_vec_col_major(vec![1], vec![1.0_f64]).unwrap();
assert!(concrete.attached_data().is_some());
let placeholder = TracedTensor::input_symbolic_shape(DType::F64, 1).unwrap();
assert!(placeholder.attached_data().is_none());Sourcepub fn from_tensor_concrete_shape(tensor: Tensor) -> Result<Self>
pub fn from_tensor_concrete_shape(tensor: Tensor) -> Result<Self>
Build a TracedTensor leaf from a concrete Tensor, keeping its
shape as a concrete shape_hint.
This is the common constructor when you have concrete tensor data that you want to use both for graph building and for evaluation. The resulting tensor is treated as a concrete-shape leaf by downstream passes (binary einsum decomposition, build-time reshape folding, etc.).
§Examples
use tenferro_runtime::{Tensor, TracedTensor};
let a = TracedTensor::from_tensor_concrete_shape(
Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap(),
)
.unwrap();
assert_eq!(a.rank, 2);
assert!(a.is_concrete_shape());Sourcepub fn from_tensor_symbolic_shape(tensor: Tensor) -> Result<Self>
pub fn from_tensor_symbolic_shape(tensor: Tensor) -> Result<Self>
Build a TracedTensor leaf from a concrete Tensor but advertise
a symbolic shape during graph construction.
The tensor data is still attached (so plain eval works without
bindings), but graph passes see the leaf as shape-symbolic. This is
useful for building a single traced program that should not bake in
shape-specific optimizations.
§Examples
use tenferro_runtime::{Tensor, TracedTensor};
let t = TracedTensor::from_tensor_symbolic_shape(
Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap(),
)
.unwrap();
assert_eq!(t.rank, 2);
assert!(!t.is_concrete_shape());Sourcepub fn input_concrete_shape(dtype: DType, shape: &[usize]) -> Result<Self>
pub fn input_concrete_shape(dtype: DType, shape: &[usize]) -> Result<Self>
Build a data-less placeholder leaf with a fixed (concrete) shape.
Must be bound via crate::GraphExecutor::run_with_inputs before evaluation.
Use this when you know the exact shape of the input but want to build
the graph once and feed different concrete tensors at execution time.
§Examples
use tenferro_tensor::DType;
use tenferro_runtime::TracedTensor;
let x = TracedTensor::input_concrete_shape(DType::F64, &[2, 3]).unwrap();
assert_eq!(x.rank, 2);
assert!(x.is_concrete_shape());Sourcepub fn input_symbolic_shape(dtype: DType, rank: usize) -> Result<Self>
pub fn input_symbolic_shape(dtype: DType, rank: usize) -> Result<Self>
Build a data-less placeholder leaf with the given rank but fully
symbolic shape (every dim is a distinct SymDim::TensorAxis).
Must be bound via crate::GraphExecutor::run_with_inputs before
evaluation. Use this to build shape-agnostic graphs.
§Examples
use tenferro_tensor::DType;
use tenferro_runtime::TracedTensor;
let x = TracedTensor::input_symbolic_shape(DType::F64, 2).unwrap();
assert_eq!(x.rank, 2);
assert!(!x.is_concrete_shape());Sourcepub fn from_vec_col_major<T: TensorScalar>(
shape: Vec<usize>,
data: Vec<T>,
) -> Result<Self>
pub fn from_vec_col_major<T: TensorScalar>( shape: Vec<usize>, data: Vec<T>, ) -> Result<Self>
Build a concrete-shape TracedTensor leaf from column-major typed
Vec<T> data.
The data must already be in tenferro’s physical column-major order.
§Examples
use tenferro_runtime::TracedTensor;
let a = TracedTensor::from_vec_col_major(
vec![2, 3],
vec![1.0_f64, 4.0, 2.0, 5.0, 3.0, 6.0],
)?;
assert_eq!(a.rank, 2);Sourcepub fn is_concrete_shape(&self) -> bool
pub fn is_concrete_shape(&self) -> bool
Returns true iff every dim of this tensor’s shape_hint is a
constant SymDim (i.e. the shape is fully known at graph-build time).
§Examples
use tenferro_tensor::DType;
use tenferro_runtime::TracedTensor;
let a = TracedTensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let b = TracedTensor::input_symbolic_shape(DType::F64, 2).unwrap();
assert!(a.is_concrete_shape());
assert!(!b.is_concrete_shape());Sourcepub fn try_concrete_shape(&self) -> Option<Vec<usize>>
pub fn try_concrete_shape(&self) -> Option<Vec<usize>>
Return the fully-concrete shape of this tensor, if every dim of
its shape-hint is a constant SymDim. Returns None if any
dimension is symbolic.
This is the counterpart to Self::is_concrete_shape for callers
that need to use the concrete shape (e.g. external composition
wrappers building broadcast_in_dim payloads from known shapes).
§Examples
use tenferro_tensor::DType;
use tenferro_runtime::TracedTensor;
let a = TracedTensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
assert_eq!(a.try_concrete_shape(), Some(vec![2, 3]));
let b = TracedTensor::input_symbolic_shape(DType::F64, 2).unwrap();
assert!(b.try_concrete_shape().is_none());Sourcepub fn concrete_shape(&self) -> Result<Vec<usize>>
pub fn concrete_shape(&self) -> Result<Vec<usize>>
Return the concrete tensor shape.
Returns an error when a shape hint is missing or any dimension is symbolic. Composite traced ops that require concrete sizes should propagate this error instead of panicking.
Sourcepub fn input_key(&self) -> Option<TensorInputKey>
pub fn input_key(&self) -> Option<TensorInputKey>
If this TracedTensor is a leaf (single-node input graph),
return its input key. Computed tensors return None.
Sourcepub fn add(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn add(&self, other: &TracedTensor) -> Result<TracedTensor>
Elementwise addition with NumPy-style broadcasting.
Prefer using the + operator when it reads naturally.
§Examples
let y = x.add(&z);
let y2 = &x + &z;Sourcepub fn sub(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn sub(&self, other: &TracedTensor) -> Result<TracedTensor>
Elementwise subtraction with NumPy-style broadcasting.
Prefer using the - operator when it reads naturally.
Sourcepub fn mul(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn mul(&self, other: &TracedTensor) -> Result<TracedTensor>
Elementwise multiplication with NumPy-style broadcasting.
Prefer using the * operator when it reads naturally.
§Examples
let y = x.mul(&z);
let y2 = &x * &z;Sourcepub fn div(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn div(&self, other: &TracedTensor) -> Result<TracedTensor>
Elementwise division with NumPy-style broadcasting.
Prefer using the / operator when it reads naturally.
§Examples
let y = x.div(&z);
let y2 = &x / &z;Sourcepub fn compare(
&self,
other: &TracedTensor,
dir: CompareDir,
) -> Result<TracedTensor>
pub fn compare( &self, other: &TracedTensor, dir: CompareDir, ) -> Result<TracedTensor>
Elementwise comparison with NumPy-style broadcasting.
Sourcepub fn maximum(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn maximum(&self, other: &TracedTensor) -> Result<TracedTensor>
Elementwise maximum with NumPy-style broadcasting.
Sourcepub fn minimum(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn minimum(&self, other: &TracedTensor) -> Result<TracedTensor>
Elementwise minimum with NumPy-style broadcasting.
Sourcepub fn where_select(
condition: &TracedTensor,
on_true: &TracedTensor,
on_false: &TracedTensor,
) -> Result<TracedTensor>
pub fn where_select( condition: &TracedTensor, on_true: &TracedTensor, on_false: &TracedTensor, ) -> Result<TracedTensor>
Select values from on_true or on_false using condition.
Sourcepub fn select(
condition: &TracedTensor,
on_true: &TracedTensor,
on_false: &TracedTensor,
) -> Result<TracedTensor>
pub fn select( condition: &TracedTensor, on_true: &TracedTensor, on_false: &TracedTensor, ) -> Result<TracedTensor>
Alias for Self::where_select.
Sourcepub fn clamp(
&self,
lower: &TracedTensor,
upper: &TracedTensor,
) -> Result<TracedTensor>
pub fn clamp( &self, lower: &TracedTensor, upper: &TracedTensor, ) -> Result<TracedTensor>
Clamp values elementwise between lower and upper bounds.
Sourcepub fn neg(&self) -> TracedTensor
pub fn neg(&self) -> TracedTensor
Elementwise negation.
Prefer using the unary - operator when it reads naturally.
§Examples
let y = x.neg();
let y2 = -&x;Sourcepub fn conj(&self) -> TracedTensor
pub fn conj(&self) -> TracedTensor
Sourcepub fn abs(&self) -> TracedTensor
pub fn abs(&self) -> TracedTensor
Elementwise absolute value.
Complex inputs return real magnitudes (C32 -> F32, C64 -> F64).
§Examples
let y = x.abs();Sourcepub fn sign(&self) -> TracedTensor
pub fn sign(&self) -> TracedTensor
Sourcepub fn scale_real(&self, factor: f64) -> TracedTensor
pub fn scale_real(&self, factor: f64) -> TracedTensor
Sourcepub fn scale_complex(&self, factor: Complex64) -> Result<TracedTensor>
pub fn scale_complex(&self, factor: Complex64) -> Result<TracedTensor>
Scale by a complex scalar: y = factor * x.
Only complex tensors support complex scaling. For a real scalar factor
that should preserve the input dtype, prefer scale_real.
§Examples
use num_complex::Complex64;
let y = x.scale_complex(Complex64::new(0.0, 1.0)).unwrap(); // multiply by iSourcepub fn exp(&self) -> TracedTensor
pub fn exp(&self) -> TracedTensor
Sourcepub fn log(&self) -> TracedTensor
pub fn log(&self) -> TracedTensor
Sourcepub fn sin(&self) -> TracedTensor
pub fn sin(&self) -> TracedTensor
Sourcepub fn cos(&self) -> TracedTensor
pub fn cos(&self) -> TracedTensor
Sourcepub fn tanh(&self) -> TracedTensor
pub fn tanh(&self) -> TracedTensor
Sourcepub fn sqrt(&self) -> TracedTensor
pub fn sqrt(&self) -> TracedTensor
Sourcepub fn rsqrt(&self) -> TracedTensor
pub fn rsqrt(&self) -> TracedTensor
Sourcepub fn pow(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn pow(&self, other: &TracedTensor) -> Result<TracedTensor>
Sourcepub fn expm1(&self) -> TracedTensor
pub fn expm1(&self) -> TracedTensor
Sourcepub fn log1p(&self) -> TracedTensor
pub fn log1p(&self) -> TracedTensor
Sourcepub fn convert(&self, to: DType) -> Result<TracedTensor>
pub fn convert(&self, to: DType) -> Result<TracedTensor>
Convert the tensor to a different dtype using checked conversion.
Use cast when a lossy dtype projection is intended.
§Examples
use tenferro_runtime::DType;
let y = x.convert(DType::C64)?;§Errors
Returns an error when the requested conversion is outside tenferro’s
checked dtype-promotion lattice. Use cast for explicit
lossy dtype projection.
Sourcepub fn cast(&self, to: DType) -> TracedTensor
pub fn cast(&self, to: DType) -> TracedTensor
Cast the tensor to a different dtype using explicit dtype projection.
cast may truncate, narrow precision, project complex values to their
real component, or use boolean truthiness where the backend supports the
requested projection.
§Examples
use tenferro_runtime::DType;
let y = x.cast(DType::I32);Sourcepub fn dot_general(
&self,
other: &TracedTensor,
config: DotGeneralConfig,
) -> Result<TracedTensor>
pub fn dot_general( &self, other: &TracedTensor, config: DotGeneralConfig, ) -> Result<TracedTensor>
Sourcepub fn matmul(&self, other: &TracedTensor) -> Result<TracedTensor>
pub fn matmul(&self, other: &TracedTensor) -> Result<TracedTensor>
Matrix multiplication for rank-2 tensors.
Sourcepub fn reduce_sum(&self, axes: &[usize]) -> Result<TracedTensor>
pub fn reduce_sum(&self, axes: &[usize]) -> Result<TracedTensor>
Sourcepub fn reduce_max(&self, axes: &[usize]) -> Result<TracedTensor>
pub fn reduce_max(&self, axes: &[usize]) -> Result<TracedTensor>
Sourcepub fn reduce_min(&self, axes: &[usize]) -> Result<TracedTensor>
pub fn reduce_min(&self, axes: &[usize]) -> Result<TracedTensor>
Sourcepub fn reduce_prod(&self, axes: &[usize]) -> Result<TracedTensor>
pub fn reduce_prod(&self, axes: &[usize]) -> Result<TracedTensor>
Sourcepub fn reshape(&self, shape: &[usize]) -> TracedTensor
pub fn reshape(&self, shape: &[usize]) -> TracedTensor
Sourcepub fn sym_size(&self, axis: usize) -> Result<SymDim>
pub fn sym_size(&self, axis: usize) -> Result<SymDim>
Return a symbolic expression for the size of one axis, suitable as
an InputDim-style reference when composing with
TracedTensor::reshape_sym.
Semantics: if this tensor’s shape_hint has a symbolic
(non-constant) entry for axis, that entry is returned
verbatim. Otherwise — including when shape_hint[axis] is a
concrete SymDim::Concrete(n) — a
SymDim::tensor_axis(self.id, axis) reference is returned so the
resulting graph remains shape-polymorphic if the same graph is
later evaluated against a differently-shaped binding.
For a canonical “what is the size of this axis?” query that
reports the concrete size when it is known, prefer
Self::axis_sym_dim.
§Examples
let rows = x.sym_size(0)?;
let cols = x.sym_size(1)?;
let y = x.reshape_sym(&[rows * cols]).unwrap();§Errors
Returns an error when axis is out of bounds.
Sourcepub fn axis_sym_dim(&self, axis: usize) -> Result<SymDim>
pub fn axis_sym_dim(&self, axis: usize) -> Result<SymDim>
Return the canonical SymDim for axis — the concrete
SymDim::Concrete(n) when the size is known, otherwise a symbolic
expression identifying this tensor’s axis.
Unlike Self::sym_size, this method does not rewrite
concrete axes into TensorAxis references. It is the accessor
external composition wrappers should use when building mixed
concrete/symbolic target shapes for operations like
Self::broadcast_in_dim_sym.
§Examples
use tenferro_tensor::DType;
use tenferro_runtime::TracedTensor;
let a = TracedTensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
// Concrete axis: reports the constant size.
assert_eq!(a.axis_sym_dim(0).unwrap().constant_value(), Some(2));
let b = TracedTensor::input_symbolic_shape(DType::F64, 2).unwrap();
// Fully symbolic leaf: reports a TensorAxis reference.
assert!(b.axis_sym_dim(0).unwrap().constant_value().is_none());§Errors
Returns an error when axis is out of bounds.
Sourcepub fn sym_shape(&self) -> Option<&[SymDim]>
pub fn sym_shape(&self) -> Option<&[SymDim]>
Return the full symbolic shape of this tensor when a shape_hint
is present.
Returns None for fully-symbolic placeholders produced via
Self::input_symbolic_shape (where shape_hint is intentionally
absent). For those, build the shape axis-by-axis via
Self::axis_sym_dim.
§Examples
use tenferro_tensor::DType;
use tenferro_runtime::TracedTensor;
let a = TracedTensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
assert!(a.sym_shape().is_some());
assert_eq!(a.sym_shape().unwrap().len(), 2);
let b = TracedTensor::input_symbolic_shape(DType::F64, 2).unwrap();
assert!(b.sym_shape().is_none());Sourcepub fn reshape_sym(&self, shape: &[SymDim]) -> Result<TracedTensor>
pub fn reshape_sym(&self, shape: &[SymDim]) -> Result<TracedTensor>
Reshape using symbolic dimensions derived from traced tensor axes.
§Examples
let rows = x.sym_size(0)?;
let cols = x.sym_size(1)?;
let y = x.reshape_sym(&[rows * cols]).unwrap();Sourcepub fn broadcast_in_dim(
&self,
shape: &[usize],
dims: &[usize],
) -> Result<TracedTensor>
pub fn broadcast_in_dim( &self, shape: &[usize], dims: &[usize], ) -> Result<TracedTensor>
Broadcast into a larger shape with explicit dimension placement.
§Examples
let y = x.broadcast_in_dim(&[2, 3], &[1])?;§Errors
Returns an error when dims is not a duplicate-free mapping from every
input axis into the output rank, or when a known input dimension cannot
broadcast to the corresponding output dimension.
Sourcepub fn broadcast_in_dim_sym(
&self,
shape: &[SymDim],
dims: &[usize],
shape_refs: &[&TracedTensor],
) -> Result<TracedTensor>
pub fn broadcast_in_dim_sym( &self, shape: &[SymDim], dims: &[usize], shape_refs: &[&TracedTensor], ) -> Result<TracedTensor>
Broadcast into a symbolic target shape with explicit dimension placement.
Unlike Self::broadcast_in_dim, each axis of shape is a
SymDim, so the target shape can mix concrete sizes (via
SymDim::from(n)) with symbolic references to this tensor’s axes
(via Self::axis_sym_dim) or to axes of other traced tensors.
When shape contains a SymDim that references a traced tensor
other than self, the referenced tensor(s) must be supplied in
shape_refs. They are wired into the built op as auxiliary
shape-reference inputs — the op does not read their data, only
their runtime shape. shape_refs must be listed in the same order
in which their tensor IDs first appear when walking shape after
any references to self. Usually the simplest correct thing is to
pass each unique non-self reference tensor once.
§Examples
use tenferro_runtime::TracedTensor;
let a = TracedTensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let b = TracedTensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12]).unwrap();
let m = a.axis_sym_dim(0)?;
let k = a.axis_sym_dim(1)?;
let n = b.axis_sym_dim(1)?;
// Broadcast `a[m, k]` to `[m, k, n]`, placing `a`'s axes at 0, 1
// and taking `n` from `b` as an auxiliary shape reference.
let a_b = a.broadcast_in_dim_sym(&[m, k, n], &[0, 1], &[&b])?;
assert_eq!(a_b.rank, 3);Sourcepub fn slice(&self, config: SliceConfig) -> Result<TracedTensor>
pub fn slice(&self, config: SliceConfig) -> Result<TracedTensor>
Slice with explicit start, limit, and stride per axis.
Sourcepub fn pad(&self, config: PadConfig) -> Result<TracedTensor>
pub fn pad(&self, config: PadConfig) -> Result<TracedTensor>
Pad with zeros using StableHLO-style edge and interior padding.
Sourcepub fn reverse(&self, axes: &[usize]) -> Result<TracedTensor>
pub fn reverse(&self, axes: &[usize]) -> Result<TracedTensor>
Reverse the order of elements along the requested axes.
Sourcepub fn gather(
&self,
indices: &TracedTensor,
config: GatherConfig,
) -> Result<TracedTensor>
pub fn gather( &self, indices: &TracedTensor, config: GatherConfig, ) -> Result<TracedTensor>
Gather slices from self using integer start indices.
Sourcepub fn scatter(
&self,
indices: &TracedTensor,
updates: &TracedTensor,
config: ScatterConfig,
) -> Result<TracedTensor>
pub fn scatter( &self, indices: &TracedTensor, updates: &TracedTensor, config: ScatterConfig, ) -> Result<TracedTensor>
Scatter updates into self using StableHLO scatter semantics.
Sourcepub fn dynamic_slice(
&self,
starts: &TracedTensor,
sizes: &[usize],
) -> Result<TracedTensor>
pub fn dynamic_slice( &self, starts: &TracedTensor, sizes: &[usize], ) -> Result<TracedTensor>
Slice using runtime start indices.
Sourcepub fn tril(&self, k: i64) -> TracedTensor
pub fn tril(&self, k: i64) -> TracedTensor
Keep the lower triangle and zero the rest.
Sourcepub fn triu(&self, k: i64) -> TracedTensor
pub fn triu(&self, k: i64) -> TracedTensor
Keep the upper triangle and zero the rest.
Sourcepub fn transpose(&self, perm: &[usize]) -> Result<TracedTensor>
pub fn transpose(&self, perm: &[usize]) -> Result<TracedTensor>
Sourcepub fn extract_diag(&self, axis_a: usize, axis_b: usize) -> Result<TracedTensor>
pub fn extract_diag(&self, axis_a: usize, axis_b: usize) -> Result<TracedTensor>
Sourcepub fn embed_diag(&self, axis_a: usize, axis_b: usize) -> Result<TracedTensor>
pub fn embed_diag(&self, axis_a: usize, axis_b: usize) -> Result<TracedTensor>
Sourcepub fn shape_of(&self, axis: usize) -> Result<TracedTensor>
pub fn shape_of(&self, axis: usize) -> Result<TracedTensor>
Return the runtime size of one axis as a scalar f64 tensor.
The result is metadata-derived and therefore has no gradient.
§Examples
use tenferro_cpu::CpuBackend;
use tenferro_runtime::{GraphCompiler, GraphExecutor, TracedTensor};
let x = TracedTensor::from_vec_col_major(vec![2, 3], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
let cols = x.shape_of(1)?;
let mut compiler = GraphCompiler::new();
let program = compiler.compile(&cols).unwrap();
let out = GraphExecutor::new(CpuBackend::new()).run(&program).unwrap();
assert_eq!(out.shape(), &[] as &[usize]);§Errors
Returns an error when axis is out of bounds.
Sourcepub fn dynamic_truncate(
&self,
size: &TracedTensor,
axis: usize,
) -> Result<TracedTensor>
pub fn dynamic_truncate( &self, size: &TracedTensor, axis: usize, ) -> Result<TracedTensor>
Truncate this tensor along axis to the first size elements.
size is read at runtime from a scalar traced tensor. Values are
rounded to the nearest integer, clamped to [0, self.shape[axis]],
and the output keeps the same element dtype as the input.
§Examples
use tenferro_cpu::CpuBackend;
use tenferro_runtime::{GraphCompiler, GraphExecutor, TracedTensor};
let x = TracedTensor::from_vec_col_major(vec![4], vec![1.0_f64, 2.0, 3.0, 4.0]).unwrap();
let size = TracedTensor::from_vec_col_major(vec![], vec![2.0_f64]).unwrap();
let y = x.dynamic_truncate(&size, 0)?;
let mut compiler = GraphCompiler::new();
let program = compiler.compile(&y).unwrap();
let out = GraphExecutor::new(CpuBackend::new()).run(&program).unwrap();
assert_eq!(out.shape(), &[2]);§Errors
Returns an error when axis is out of bounds or size is not scalar.
Sourcepub fn pad_to_match(
&self,
reference: &TracedTensor,
axis: usize,
) -> Result<TracedTensor>
pub fn pad_to_match( &self, reference: &TracedTensor, axis: usize, ) -> Result<TracedTensor>
Pad this tensor with zeros along axis to match reference.shape[axis].
If reference is smaller along that axis, this is a no-op.
§Examples
use tenferro_cpu::CpuBackend;
use tenferro_runtime::{GraphCompiler, GraphExecutor, TracedTensor};
let x = TracedTensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
let reference = TracedTensor::from_vec_col_major(vec![4], vec![0.0_f64, 0.0, 0.0, 0.0]).unwrap();
let y = x.pad_to_match(&reference, 0)?;
let mut compiler = GraphCompiler::new();
let program = compiler.compile(&y).unwrap();
let out = GraphExecutor::new(CpuBackend::new()).run(&program).unwrap();
assert_eq!(out.shape(), &[4]);§Errors
Returns an error when axis is out of bounds for either tensor.
Trait Implementations§
Source§impl Add for &TracedTensor
impl Add for &TracedTensor
Source§fn add(self, rhs: &TracedTensor) -> Result<TracedTensor>
fn add(self, rhs: &TracedTensor) -> Result<TracedTensor>
+ operation. Read moreSource§impl Clone for TracedTensor
impl Clone for TracedTensor
Source§fn clone(&self) -> TracedTensor
fn clone(&self) -> TracedTensor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TracedTensor
impl Debug for TracedTensor
Source§impl Div for &TracedTensor
impl Div for &TracedTensor
Source§fn div(self, rhs: &TracedTensor) -> Result<TracedTensor>
fn div(self, rhs: &TracedTensor) -> Result<TracedTensor>
/ operation. Read moreSource§impl Mul<&TracedTensor> for f64
impl Mul<&TracedTensor> for f64
Source§type Output = TracedTensor
type Output = TracedTensor
* operator.Source§fn mul(self, rhs: &TracedTensor) -> TracedTensor
fn mul(self, rhs: &TracedTensor) -> TracedTensor
* operation. Read moreSource§impl Mul<f64> for &TracedTensor
impl Mul<f64> for &TracedTensor
Source§type Output = TracedTensor
type Output = TracedTensor
* operator.Source§impl Mul for &TracedTensor
impl Mul for &TracedTensor
Source§fn mul(self, rhs: &TracedTensor) -> Result<TracedTensor>
fn mul(self, rhs: &TracedTensor) -> Result<TracedTensor>
* operation. Read moreSource§impl Neg for &TracedTensor
impl Neg for &TracedTensor
Source§type Output = TracedTensor
type Output = TracedTensor
- operator.Source§fn neg(self) -> TracedTensor
fn neg(self) -> TracedTensor
- operation. Read moreSource§impl Sub for &TracedTensor
impl Sub for &TracedTensor
Source§fn sub(self, rhs: &TracedTensor) -> Result<TracedTensor>
fn sub(self, rhs: &TracedTensor) -> Result<TracedTensor>
- operation. Read more