Skip to main content

sum_on_session

Function sum_on_session 

Source
pub fn sum_on_session(
    session: &mut Session<'_>,
    tensor: &IdxTensor,
) -> Result<AnyScalar, IdxTensorError>
Expand description

Sum every element of a dense host tensor on a caller-held explicit session.

The reduction companion of the F1 seam for the dense host representations this slice serves, so an app can reduce a value inside a region it holds open. The result is a plain scalar, published from the concrete value rather than adopted into any eager runtime.

A zero-length axis folds to the operation’s identity: the compatibility route accumulates from zero over the empty payload and answers 0, and the session’s reduction answers the same 0, because the backend treats an empty reduction domain as the fold’s unit rather than as invalid input (tenferro #2453). The two routes therefore agree over the whole served domain. The unit is the fold’s own - sum answers 0, prod would answer 1 - and a reduction with no finite unit (max/min) keeps the backend’s typed refusal; this seam serves sum only, so it never exercises that boundary.

§Errors

Returns a typed IdxTensorError under the same conditions as contract_pair_on_session refuses an operand - a tracked value, a device-resident operand, a diagonal, structured or compact payload, a deferred storage error, an unsupported dtype - when the session rejects the reduction, or when the result cannot be published.

§Examples

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

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

let total = context.with_concrete_session(|session| sum_on_session(session, &tensor))??;
assert_eq!(total.real(), 21.0);