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 is not parity with IdxTensor::sum on purpose in this slice: the compatibility route accumulates from zero over the payload and answers 0, while the session’s reduction rejects a zero-length axis. The session keeps its backend’s refusal rather than inventing a special case, and the difference is covered by a regression test. Everything else in the served domain matches.

§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);