Skip to main content

EagerEinsumOps

Trait EagerEinsumOps 

Source
pub trait EagerEinsumOps {
    // Required methods
    fn einsum(
        &mut self,
        inputs: &[&EagerTensor],
        spec: &(impl EinsumSpec + ?Sized),
    ) -> Result<EagerTensor>;
    fn einsum_with(
        &mut self,
        inputs: &[&EagerTensor],
        spec: &(impl EinsumSpec + ?Sized),
        options: &EinsumOptions,
    ) -> Result<EagerTensor>;
    fn tensordot(
        &mut self,
        a: &EagerTensor,
        b: &EagerTensor,
        axes: TensorDotAxes<'_>,
    ) -> Result<EagerTensor>;
}
Expand description

Einsum and tensordot on eager tensors, with the session as receiver. Eager operations execute immediately and record the tape for reverse-mode differentiation.

Generated by api_ops! from the einsum registry.

Required Methods§

Source

fn einsum( &mut self, inputs: &[&EagerTensor], spec: &(impl EinsumSpec + ?Sized), ) -> Result<EagerTensor>

Einstein summation over N inputs: contract, trace, transpose, reduce and broadcast as the spec says.

Uses the default EinsumOptions; same as einsum_with with &EinsumOptions::default().

§Layout

Strided inputs are read in place by the contraction kernels (any strides, including transposed and negative-stride views); an operand that a kernel cannot read is packed once into a compact buffer of the same placement and dtype. A two-input contraction that maps to one dot_general writes straight into the destination. Inputs stay on their device. _into checks the destination’s dtype and shape before any write; Accumulate and Scaled read the destination, Overwrite and Scaled with beta = 0 never read it.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • RuntimeState
§Omissions
  • _into is concrete only: traced and eager results are new values
  • lanes (CpuEngine::with_lanes): einsum is unsupported in a lane
  • Scaled needs alpha and beta of the input dtype
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_einsum::{EagerEinsumOps, EinsumOptimize, EinsumOptions};
use tenferro_tensor::Tensor;

let runtime = EagerRuntime::new()?;
let (c, d) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 1.0])?)?;
    let c = s.einsum(&[&a, &b], "ij,jk->ik")?;
    let opts = EinsumOptions::default().optimize(EinsumOptimize::Path(vec![(0, 1)]));
    let d = s.einsum_with(&[&a, &b], "ij,jk->ik", &opts)?;
    Ok::<_, tenferro_ad::Error>((c, d))
})?;
assert_eq!(c.value()?.as_slice::<f64>()?, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(d.value()?.as_slice::<f64>()?, &[1.0, 2.0, 3.0, 4.0]);
Source

fn einsum_with( &mut self, inputs: &[&EagerTensor], spec: &(impl EinsumSpec + ?Sized), options: &EinsumOptions, ) -> Result<EagerTensor>

Einstein summation over N inputs: contract, trace, transpose, reduce and broadcast as the spec says.

With explicit EinsumOptions.

§Layout

Strided inputs are read in place by the contraction kernels (any strides, including transposed and negative-stride views); an operand that a kernel cannot read is packed once into a compact buffer of the same placement and dtype. A two-input contraction that maps to one dot_general writes straight into the destination. Inputs stay on their device. _into checks the destination’s dtype and shape before any write; Accumulate and Scaled read the destination, Overwrite and Scaled with beta = 0 never read it.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
  • RuntimeState
§Omissions
  • _into is concrete only: traced and eager results are new values
  • lanes (CpuEngine::with_lanes): einsum is unsupported in a lane
  • Scaled needs alpha and beta of the input dtype
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_einsum::{EagerEinsumOps, EinsumOptimize, EinsumOptions};
use tenferro_tensor::Tensor;

let runtime = EagerRuntime::new()?;
let (c, d) = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 1.0])?)?;
    let c = s.einsum(&[&a, &b], "ij,jk->ik")?;
    let opts = EinsumOptions::default().optimize(EinsumOptimize::Path(vec![(0, 1)]));
    let d = s.einsum_with(&[&a, &b], "ij,jk->ik", &opts)?;
    Ok::<_, tenferro_ad::Error>((c, d))
})?;
assert_eq!(c.value()?.as_slice::<f64>()?, &[1.0, 2.0, 3.0, 4.0]);
assert_eq!(d.value()?.as_slice::<f64>()?, &[1.0, 2.0, 3.0, 4.0]);
Source

fn tensordot( &mut self, a: &EagerTensor, b: &EagerTensor, axes: TensorDotAxes<'_>, ) -> Result<EagerTensor>

Contract a with b over the given axis pairs (or the last/first n axes); free axes of a come first.

This operation has no options.

§Layout

Lowered to one dot_general, which reads strided operands in place; an operand that the provider cannot read is packed once into a compact buffer of the same placement and dtype.

§Errors

Returns an error of kind:

  • Validation
  • Unsupported
§Omissions
  • lanes (CpuEngine::with_lanes): tensordot is unsupported in a lane
§Examples
use tenferro_ad::EagerRuntime;
use tenferro_cpu::CpuEngine;
use tenferro_einsum::{EagerEinsumOps, TensorDotAxes};
use tenferro_tensor::Tensor;

let runtime = EagerRuntime::new()?;
let c = runtime.with_eager_session(&mut CpuEngine::serial(), |s| {
    let a = s.constant_from(Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6])?)?;
    let b = s.constant_from(Tensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12])?)?;
    s.tensordot(&a, &b, TensorDotAxes::Count(1))
})?;
assert_eq!(c.shape(), &[2, 4]);
assert_eq!(c.value()?.as_slice::<f64>()?, &[3.0; 8]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl EagerEinsumOps for EagerSession<'_>

Available on crate feature autodiff only.
Source§

fn einsum( &mut self, inputs: &[&EagerTensor], spec: &(impl EinsumSpec + ?Sized), ) -> Result<EagerTensor>

Source§

fn einsum_with( &mut self, inputs: &[&EagerTensor], spec: &(impl EinsumSpec + ?Sized), options: &EinsumOptions, ) -> Result<EagerTensor>

Source§

fn tensordot( &mut self, a: &EagerTensor, b: &EagerTensor, axes: TensorDotAxes<'_>, ) -> Result<EagerTensor>

Implementors§