einsums::compute_graph::einsum#

Note

Generated from the C++ headers by apiary --emit-cpp-docs-json.

Defined in headers Einsums/ComputeGraph/NaryEinsum.hpp, Einsums/ComputeGraph/Operations.hpp.

template<BasicTensorConcept CType, BasicTensorConcept... Operands>
requires (sizeof...(Operands) >= 3) && (std::is_same_v<typename CType::ValueType, typename Operands::ValueType> && ...) && (!(TiledTensorConcept<CType> || (TiledTensorConcept<Operands> || ...)))
void einsum(NaryEinsumFormatString spec, typename CType::ValueType c_pf, CType *C, typename CType::ValueType ab_pf, const Operands&... operands)#

Graph-aware einsum over three or more operands: C = c_pf * C + ab_pf * (A B D ...).

Contracted two at a time, in the order plan_nary_einsum finds cheapest, through the two-operand einsum. Captured, each partial product is a graph intermediate that later passes may slice or keep whole; eager, each is a temporary held to eager_einsum_intermediate_limit. The prefactors apply to the last contraction alone, so c_pf scales C once.

// The DF particle ladder, written as the equation reads
cg::einsum("i,j,a,b <- Q,a,e ; Q,b,f ; i,j,e,f", &r2, Bvv, Bvv, tau);

Every operand and C must share one element type, and none may be tiled.

Added in version 2.0.0.

Parameters:
  • spec – The spec, its operands in the order operands are given.

  • c_pf – The prefactor on C.

  • C – The output.

  • ab_pf – The prefactor on the product.

  • operands – Three or more tensors.

template<BasicTensorConcept CType, BasicTensorConcept... Operands>
requires (sizeof...(Operands) >= 3) && (std::is_same_v<typename CType::ValueType, typename Operands::ValueType> && ...) && (!(TiledTensorConcept<CType> || (TiledTensorConcept<Operands> || ...)))
void einsum(NaryEinsumFormatString spec, CType *C, const Operands&... operands)#

Graph-aware einsum over three or more operands with the default prefactors: C is overwritten.

Added in version 2.0.0.

template<BasicTensorConcept AType, BasicTensorConcept BType, BasicTensorConcept CType>
requires std::is_same_v<typename AType::ValueType, typename BType::ValueType> && std::is_same_v<typename AType::ValueType, typename CType::ValueType> && (!detail::any_tiled_v<AType, BType, CType>)
void einsum(EinsumFormatString spec, typename AType::ValueType c_pf, CType *C, typename AType::ValueType ab_pf, const AType &A, const BType &B, bool conj_a, bool conj_b)#

String-based einsum with explicit prefactors.

The spec names each operand’s indices in a string:

cg::einsum("ij <- ik ; kj", 0.0, &C, 1.0, A, B);     // GEMM
cg::einsum("ij <- ki ; kj", 0.0, &C, 1.0, A, B);     // GEMM with transposed A
cg::einsum("i <- ij ; j", 0.0, &y, 1.0, A, x);       // GEMV
cg::einsum(" <- i ; i", 0.0, &dot, 1.0, x, y);        // DOT product
cg::einsum("ij <- i ; j", 0.0, &C, 1.0, x, y);       // GER (outer product)
cg::einsum("ij <- ij ; ij", 0.0, &C, 1.0, A, B);     // Element-wise (direct product)
cg::einsum("mu,nu <- mu,rho ; rho,nu", 0.0, &C, 1.0, A, B);  // Multi-char indices

During graph capture, records the operation as a node. Outside capture, dispatches to the appropriate BLAS routine based on the contraction pattern.

Template Parameters:
  • AType – First input tensor type (must satisfy BasicTensorConcept).

  • BType – Second input tensor type.

  • CType – Output tensor type.

Parameters:
  • spec – Einsum specification string.

  • c_pf – Scalar prefactor for C accumulation: C = c_pf * C + ab_pf * contract(A, B).

  • C – Output tensor.

  • ab_pf – Scalar prefactor for the A*B contraction.

  • A – First input tensor.

  • B – Second input tensor.

  • conj_a – Conjugate A; a no-op on a real one. ORed with a conj(...) wrapper in the spec.

  • conj_b – Conjugate B; a no-op on a real one. ORed with a conj(...) wrapper in the spec.

template<TiledTensorConcept AType, TiledTensorConcept BType, TiledTensorConcept CType>
requires std::is_same_v<typename AType::ValueType, typename BType::ValueType> && std::is_same_v<typename AType::ValueType, typename CType::ValueType>
void einsum(EinsumFormatString spec, typename AType::ValueType c_pf, CType *C, typename AType::ValueType ab_pf, const AType &A, const BType &B, bool conj_a, bool conj_b)#

Tiled einsum (Tier B1): einsum over TiledRuntimeTensor operands.

Selected when any operand is tiled. Walks the tile grid and composes dense per-tile contractions (see detail::tiled_runtime_einsum). All three operands must be tiled; the contraction is recorded as a single opaque Custom node so the einsum-rewriting passes don’t try to synthesize dense intermediates for it.

template<BasicTensorConcept AType, BasicTensorConcept BType, BasicTensorConcept CType>
requires std::is_same_v<typename AType::ValueType, typename BType::ValueType> && std::is_same_v<typename AType::ValueType, typename CType::ValueType>
void einsum(EinsumFormatString spec, CType *C, const AType &A, const BType &B)#

String-based einsum with default prefactors (c_pf=0, ab_pf=1).

String literals are validated at compile time. For runtime strings, use EinsumFormatString(string_view) explicitly.

cg::einsum("ij <- ik ; kj", &C, A, B);  // Compile-time validated

std::string spec = build_spec();
cg::einsum(EinsumFormatString(spec), &C, A, B);  // Runtime string
template<TiledTensorConcept AType, TiledTensorConcept BType, TiledTensorConcept CType>
requires std::is_same_v<typename AType::ValueType, typename BType::ValueType> && std::is_same_v<typename AType::ValueType, typename CType::ValueType>
void einsum(EinsumFormatString spec, CType *C, const AType &A, const BType &B)#

Default-prefactor tiled einsum (delegates to the tiled prefactor overload).

template<BasicTensorConcept AType, BasicTensorConcept BType, BasicTensorConcept CType>
requires detail::EinsumElement<typename AType::ValueType> && detail::EinsumElement<typename BType::ValueType> && detail::EinsumElement<typename CType::ValueType> && (!(std::is_same_v<typename AType::ValueType, typename BType::ValueType> && std::is_same_v<typename AType::ValueType, typename CType::ValueType>)) && (!detail::any_tiled_v<AType, BType, CType>)
void einsum(EinsumFormatString spec, typename CType::ValueType c_pf, CType *C, detail::PromoteT<typename AType::ValueType, typename BType::ValueType> ab_pf, const AType &A, const BType &B, bool conj_a, bool conj_b)#

Graph-aware einsum whose operands do not all share one element type.

cg::einsum("ij <- ik ; kj", 0.0, &C, 1.0, A, B);  // C double, A float, B complex<double>...

The products and the sum are formed in the type promoted from A and B, complex if either is and at the wider of their precisions, and the result is rounded to C’s type. Precision may narrow; a complex result cannot be stored in a real C, and that does not compile. No BLAS or PackedGemm routine takes mixed types, so these contractions always run the generic loop. Permutation operators in the spec are not supported for them.

Added in version 2.0.0.

Parameters:
  • spec – The contraction, as for the single-type einsum.

  • c_pf – Scale applied to C, in C’s type.

  • C – The output.

  • ab_pf – Scale applied to the contraction, in the promoted type.

  • A,B – The inputs.

  • conj_a,conj_b – Conjugate an input; a no-op on a real one.

template<BasicTensorConcept AType, BasicTensorConcept BType, BasicTensorConcept CType>
requires detail::EinsumElement<typename AType::ValueType> && detail::EinsumElement<typename BType::ValueType> && detail::EinsumElement<typename CType::ValueType> && (!(std::is_same_v<typename AType::ValueType, typename BType::ValueType> && std::is_same_v<typename AType::ValueType, typename CType::ValueType>)) && (!detail::any_tiled_v<AType, BType, CType>)
void einsum(EinsumFormatString spec, CType *C, const AType &A, const BType &B)#

Mixed-precision einsum with the default prefactors: C is overwritten with the contraction.

Added in version 2.0.0.