Skip to main content

Op

Enum Op 

Source
pub enum Op<B: Backend> {
Show 22 variants None, Binary { lhs: Tensor<B>, rhs: Tensor<B>, op: BinaryOp, }, Unary { input: Tensor<B>, op: UnaryOp, }, Reduce { input: Tensor<B>, op: ReduceOp, dims: Vec<usize>, keep_dim: bool, }, Matmul { lhs: Tensor<B>, rhs: Tensor<B>, }, Reshape { input: Tensor<B>, src_shape: Shape, }, Transpose { input: Tensor<B>, dim0: usize, dim1: usize, }, Narrow { input: Tensor<B>, dim: usize, start: usize, len: usize, }, Affine { input: Tensor<B>, mul: f64, add: f64, }, Contiguous { input: Tensor<B>, }, Conv2d { input: Tensor<B>, weight: Tensor<B>, bias: Option<Tensor<B>>, stride: [usize; 2], padding: [usize; 2], }, MaxPool2d { input: Tensor<B>, kernel_size: [usize; 2], stride: [usize; 2], padding: [usize; 2], indices: Vec<usize>, }, Cat { inputs: Vec<Tensor<B>>, dim: usize, sizes: Vec<usize>, }, Powf { input: Tensor<B>, exponent: f64, }, Clamp { input: Tensor<B>, min: f64, max: f64, }, WhereCond { mask: Tensor<B>, on_true: Tensor<B>, on_false: Tensor<B>, }, Gather { input: Tensor<B>, index: Tensor<B>, dim: usize, }, Pad { input: Tensor<B>, padding: Vec<[usize; 2]>, }, AvgPool2d { input: Tensor<B>, kernel_size: [usize; 2], stride: [usize; 2], padding: [usize; 2], }, Conv1d { input: Tensor<B>, weight: Tensor<B>, bias: Option<Tensor<B>>, stride: usize, padding: usize, }, IndexSelect { input: Tensor<B>, indices: Tensor<B>, dim: usize, }, ToDtype { input: Tensor<B>, src_dtype: DType, },
}
Expand description

Records the operation that produced a tensor, storing references to inputs.

Each variant holds the actual input Tensor(s) (Arc-wrapped, cheap to clone) plus the operation parameters. backward() uses these to compute gradients via the chain rule.

Op is generic over the Backend because it stores Tensor.

Variants§

§

None

No operation — this is a leaf tensor (input data or trainable parameter).

§

Binary

Element-wise binary: result = op(lhs, rhs)

Fields

§lhs: Tensor<B>
§rhs: Tensor<B>
§

Unary

Element-wise unary: result = op(input)

Fields

§input: Tensor<B>
§

Reduce

Reduction: result = reduce(input, dims)

Fields

§input: Tensor<B>
§dims: Vec<usize>
§keep_dim: bool
§

Matmul

Matrix multiplication: result = lhs @ rhs

Fields

§lhs: Tensor<B>
§rhs: Tensor<B>
§

Reshape

Reshape (includes squeeze/unsqueeze): same data, different shape. src_shape records the original shape so backward can reshape gradients back.

Fields

§input: Tensor<B>
§src_shape: Shape
§

Transpose

Transpose: swap two dimensions

Fields

§input: Tensor<B>
§dim0: usize
§dim1: usize
§

Narrow

Narrow/slice along a dimension

Fields

§input: Tensor<B>
§dim: usize
§start: usize
§len: usize
§

Affine

Affine transform: result = input * mul + add

Fields

§input: Tensor<B>
§mul: f64
§add: f64
§

Contiguous

Contiguous copy: same logical values, but data is now contiguous in memory. Gradient passes through unchanged.

Fields

§input: Tensor<B>
§

Conv2d

2D convolution: result = conv2d(input, weight) + bias input: [N, C_in, H, W], weight: [C_out, C_in, kH, kW]

Fields

§input: Tensor<B>
§weight: Tensor<B>
§bias: Option<Tensor<B>>
§stride: [usize; 2]
§padding: [usize; 2]
§

MaxPool2d

2D max-pooling. input: [N, C, H, W] indices stores the argmax positions for backward.

Fields

§input: Tensor<B>
§kernel_size: [usize; 2]
§stride: [usize; 2]
§padding: [usize; 2]
§indices: Vec<usize>
§

Cat

Concatenation along a dimension. inputs are the original tensors that were concatenated. dim is the concatenation dimension. sizes stores the size of each input along dim (needed by backward to slice the gradient back into per-input pieces via narrow).

Fields

§inputs: Vec<Tensor<B>>
§dim: usize
§sizes: Vec<usize>
§

Powf

Element-wise power: result = input ^ exponent.

Fields

§input: Tensor<B>
§exponent: f64
§

Clamp

Element-wise clamp: result = clamp(input, min, max).

Fields

§input: Tensor<B>
§min: f64
§max: f64
§

WhereCond

Conditional select: result[i] = if mask[i] { on_true[i] } else { on_false[i] }.

Fields

§mask: Tensor<B>
§on_true: Tensor<B>
§on_false: Tensor<B>
§

Gather

Gather elements along a dimension using index tensor.

Fields

§input: Tensor<B>
§index: Tensor<B>
§dim: usize
§

Pad

Constant padding.

Fields

§input: Tensor<B>
§padding: Vec<[usize; 2]>
§

AvgPool2d

2D average-pooling. input: [N, C, H, W]

Fields

§input: Tensor<B>
§kernel_size: [usize; 2]
§stride: [usize; 2]
§padding: [usize; 2]
§

Conv1d

1D convolution: result = conv1d(input, weight) + bias input: [N, C_in, L], weight: [C_out, C_in, K]

Fields

§input: Tensor<B>
§weight: Tensor<B>
§bias: Option<Tensor<B>>
§stride: usize
§padding: usize
§

IndexSelect

Index select along a dimension: result = input.index_select(dim, indices) Backward = scatter-add of grad_output into grad_input at index positions.

Fields

§input: Tensor<B>
§indices: Tensor<B>
§dim: usize
§

ToDtype

Dtype conversion: result = input.to_dtype(target_dtype) Backward casts gradient back to the original dtype.

Fields

§input: Tensor<B>
§src_dtype: DType

Implementations§

Source§

impl<B: Backend> Op<B>

Source

pub fn inputs(&self) -> Vec<&Tensor<B>>

Return references to all input tensors of this operation. Used by topological sort in backward() to traverse the graph.

Trait Implementations§

Source§

impl<B: Backend> Clone for Op<B>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<B: Backend> Debug for Op<B>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<B> Freeze for Op<B>

§

impl<B> RefUnwindSafe for Op<B>
where <B as Backend>::Device: RefUnwindSafe,

§

impl<B> Send for Op<B>

§

impl<B> Sync for Op<B>

§

impl<B> Unpin for Op<B>

§

impl<B> UnsafeUnpin for Op<B>

§

impl<B> UnwindSafe for Op<B>
where <B as Backend>::Device: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.