Skip to main content

TracedEinsumOps

Trait TracedEinsumOps 

Source
pub trait TracedEinsumOps {
    // Required methods
    fn einsum(
        &self,
        inputs: &[&TracedTensor],
        spec: &(impl EinsumSpec + ?Sized),
    ) -> Result<TracedTensor>;
    fn einsum_with(
        &self,
        inputs: &[&TracedTensor],
        spec: &(impl EinsumSpec + ?Sized),
        options: &EinsumOptions,
    ) -> Result<TracedTensor>;
}
Expand description

Einsum recorded on a trace: the trace is the receiver, the inputs are traced tensors of that trace, and the result is a traced tensor. Execution happens when the graph is prepared and run.

Generated by api_ops! from the einsum registry.

Required Methods§

Source

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

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_cpu::CpuEngine;
use tenferro_einsum::{EinsumOptimize, EinsumOptions, TracedEinsumOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0])?;
let b = trace.from_vec_col_major(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 1.0])?;
let c = trace.einsum(&[&a, &b], "ij,jk->ik")?;
let d = trace.einsum_with(&[&a, &b, &b], "ij,jk,kl->il", &EinsumOptions::default().optimize(EinsumOptimize::False))?;

let program = GraphCompiler::new().compile_many(&[&c, &d])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_einsum::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
for output in outputs {
    let t = output.into_tensor(&mut engine)?;
    assert_eq!(t.as_slice::<f64>()?, &[1.0, 2.0, 3.0, 4.0]);
}
Source

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

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_cpu::CpuEngine;
use tenferro_einsum::{EinsumOptimize, EinsumOptions, TracedEinsumOps};
use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, TraceContext};

let trace = TraceContext::new();
let a = trace.from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0])?;
let b = trace.from_vec_col_major(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 1.0])?;
let c = trace.einsum(&[&a, &b], "ij,jk->ik")?;
let d = trace.einsum_with(&[&a, &b, &b], "ij,jk,kl->il", &EinsumOptions::default().optimize(EinsumOptimize::False))?;

let program = GraphCompiler::new().compile_many(&[&c, &d])?;
let mut engine = CpuEngine::serial();
let mut builder = Runtime::builder();
builder.register_engine(tenferro_cpu::runtime_engine_registration(&engine)?)?;
builder.install_extension_module(tenferro_einsum::extension_module::<CpuEngine>(
    tenferro_cpu::runtime_engine_id()?,
)?)?;
let runtime = builder.build()?;
let prepared = runtime.prepare(&program, &[], &[engine.target()])?;
let outputs = {
    let mut engines: [&mut dyn ExecutionEngine; 1] = [&mut engine];
    runtime.run(&mut engines, &prepared, &[])?
};
for output in outputs {
    let t = output.into_tensor(&mut engine)?;
    assert_eq!(t.as_slice::<f64>()?, &[1.0, 2.0, 3.0, 4.0]);
}

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§