Skip to main content

contract_pair_on_session

Function contract_pair_on_session 

Source
pub fn contract_pair_on_session(
    session: &mut Session<'_>,
    lhs: &IdxTensor,
    rhs: &IdxTensor,
    options: PairwiseContractionOptions,
) -> Result<IdxTensor, IdxTensorError>
Expand description

Contract two dense host tensors on a caller-held explicit concrete session.

The planning is the compatibility route’s own (operand_indices_for_contraction plus prepare_contraction), so the result’s indices - id, prime level and tags - are the ones contract_pair_with_operand_options produces. The execution is the caller’s session (Session::contraction, which also promotes mixed f64/c64 operands on that session), and the result is published as dense host storage, so neither the process-global session nor an ambient eager runtime is entered and no graph is compiled.

Operands are adapted by representation rather than by materialization: dense host storage goes through the existing host/native bridge, and an untracked eager host value through its borrowed read representation, which the session consumes itself.

§Errors

Returns a typed IdxTensorError for an operand whose representation this slice does not serve - an AD-tracked value, a device-resident operand, or a diagonal, structured or compact payload - for a non-floating dtype, for an operand whose index list repeats an index, for incompatible index spaces (the planner’s shape and dimension errors), when the session rejects the contraction, or when the result cannot be published. Every floating dtype (f32, f64, c32, c64) is served and the result keeps the session’s dtype. A deferred storage error is returned as IdxTensorError::Storage with its original source.

§Examples

use tensor4all_core::index::{DefaultIndex as Index, DynId};
use tensor4all_core::{contract_pair_on_session, IdxTensor, PairwiseContractionOptions};
use tensor4all_tensorbackend::CpuExecutionContext;
use tensor4all_tensorbackend::CpuBackend;

let context = CpuExecutionContext::from_backend(CpuBackend::with_threads(1)?);
let i = Index::new_dyn(2);
let j = Index::new_dyn(3);
let k = Index::new_dyn(2);
let lhs: IdxTensor =
    IdxTensor::from_dense(vec![i.clone(), k.clone()], vec![1.0_f64, 2.0, 3.0, 4.0])?;
let rhs: IdxTensor = IdxTensor::from_dense(
    vec![k.clone(), j.clone()],
    vec![1.0_f64, 1.0, 1.0, 1.0, 1.0, 1.0],
)?;

let product = context.with_concrete_session(|session| {
    contract_pair_on_session(session, &lhs, &rhs, PairwiseContractionOptions::new())
})??;
assert_eq!(product.indices().len(), 2);
// Column-major over `[i, j]`: each `j` column sums the two `k` entries of that `i` row.
assert_eq!(product.to_vec::<f64>()?, vec![4.0, 6.0, 4.0, 6.0, 4.0, 6.0]);