pub struct LazyTensor { /* private fields */ }Expand description
A deferred-execution tensor that records operations without running them.
Operations on LazyTensor build up a computation graph (via LazyOp) that
is only executed when materialize() is called. Before materialization, the
graph can be optimized via optimize() to apply algebraic simplifications
such as constant folding, identity elimination, and inverse cancellation.
Implementations§
Source§impl LazyTensor
impl LazyTensor
Sourcepub fn from_tensor(tensor: Tensor<f32>) -> Self
pub fn from_tensor(tensor: Tensor<f32>) -> Self
Wraps a concrete tensor into a lazy tensor.
The tensor is stored internally and referenced by a LazyOp::Tensor node.
Sourcepub fn zeros(shape: &[usize]) -> Self
pub fn zeros(shape: &[usize]) -> Self
Creates a lazy tensor filled with zeros (materialized on demand).
Sourcepub fn ones(shape: &[usize]) -> Self
pub fn ones(shape: &[usize]) -> Self
Creates a lazy tensor filled with ones (materialized on demand).
Sourcepub fn neg(&self) -> LazyTensor
pub fn neg(&self) -> LazyTensor
Element-wise negation.
Sourcepub fn relu(&self) -> LazyTensor
pub fn relu(&self) -> LazyTensor
ReLU activation: max(0, x).
Sourcepub fn sigmoid(&self) -> LazyTensor
pub fn sigmoid(&self) -> LazyTensor
Sigmoid activation: 1 / (1 + exp(-x)).
Sourcepub fn exp(&self) -> LazyTensor
pub fn exp(&self) -> LazyTensor
Element-wise exponential.
Sourcepub fn log(&self) -> LazyTensor
pub fn log(&self) -> LazyTensor
Element-wise natural logarithm.
Sourcepub fn sqrt(&self) -> LazyTensor
pub fn sqrt(&self) -> LazyTensor
Element-wise square root.
Sourcepub fn abs(&self) -> LazyTensor
pub fn abs(&self) -> LazyTensor
Element-wise absolute value.
Sourcepub fn add(&self, other: &LazyTensor) -> LazyTensor
pub fn add(&self, other: &LazyTensor) -> LazyTensor
Element-wise addition. Shapes must match.
Sourcepub fn sub(&self, other: &LazyTensor) -> LazyTensor
pub fn sub(&self, other: &LazyTensor) -> LazyTensor
Element-wise subtraction. Shapes must match.
Sourcepub fn mul(&self, other: &LazyTensor) -> LazyTensor
pub fn mul(&self, other: &LazyTensor) -> LazyTensor
Element-wise multiplication. Shapes must match.
Sourcepub fn div(&self, other: &LazyTensor) -> LazyTensor
pub fn div(&self, other: &LazyTensor) -> LazyTensor
Element-wise division. Shapes must match.
Sourcepub fn add_scalar(&self, s: f32) -> LazyTensor
pub fn add_scalar(&self, s: f32) -> LazyTensor
Adds a scalar to every element.
Sourcepub fn mul_scalar(&self, s: f32) -> LazyTensor
pub fn mul_scalar(&self, s: f32) -> LazyTensor
Multiplies every element by a scalar.
Sourcepub fn sum(&self) -> LazyTensor
pub fn sum(&self) -> LazyTensor
Sum of all elements (reduces to scalar shape).
Sourcepub fn mean(&self) -> LazyTensor
pub fn mean(&self) -> LazyTensor
Mean of all elements (reduces to scalar shape).
Sourcepub fn reshape(&self, shape: &[usize]) -> LazyTensor
pub fn reshape(&self, shape: &[usize]) -> LazyTensor
Reshapes the lazy tensor to a new shape.
The total number of elements must remain the same.
Sourcepub fn op_count(&self) -> usize
pub fn op_count(&self) -> usize
Counts the number of operations in the computation graph.
Leaf Tensor nodes count as 0 operations; every other node counts as 1
plus the count of its children.
Sourcepub fn materialize(&self) -> Tensor<f32>
pub fn materialize(&self) -> Tensor<f32>
Executes the recorded computation graph and returns a concrete Tensor<f32>.
This recursively evaluates every node in the LazyOp tree, starting from
leaves and combining results upward.
Sourcepub fn optimize(&self) -> LazyTensor
pub fn optimize(&self) -> LazyTensor
Applies algebraic simplifications to the computation graph.
Currently supported optimizations:
- Identity elimination:
x + 0 -> x,x * 1 -> x - Zero multiplication:
x * 0 -> zeros - Double negation:
neg(neg(x)) -> x - Inverse cancellation:
exp(log(x)) -> x,log(exp(x)) -> x - Scalar folding:
x * s1 * s2 -> x * (s1*s2),x + s1 + s2 -> x + (s1+s2)
Trait Implementations§
Source§impl Clone for LazyTensor
impl Clone for LazyTensor
Source§fn clone(&self) -> LazyTensor
fn clone(&self) -> LazyTensor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for LazyTensor
impl !RefUnwindSafe for LazyTensor
impl Send for LazyTensor
impl Sync for LazyTensor
impl Unpin for LazyTensor
impl UnsafeUnpin for LazyTensor
impl !UnwindSafe for LazyTensor
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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>
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