Skip to main content

norm_on_session

Function norm_on_session 

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

Compute the Euclidean norm of a dense host tensor on a caller-held explicit session.

Uses the same scaled accumulation as norm_squared_on_session and IdxTensor::norm, finishing directly as a norm so its square cannot underflow or overflow before the result is returned.

§Errors

Returns IdxTensorError::NaNInput when an element or the accumulated result is NaN, and IdxTensorError::Operation for the representation and dtype refusals the shared admission applies (a tracked value, a device-resident operand, a diagonal, structured or compact payload, a deferred storage error, or a non-floating dtype) and for a materialization, session-copy or host-read failure. f32 and c32 operands are served by this route, promoted into the same f64 accumulation the compatibility route uses. Errors carry norm_on_session as their operation name.

§Examples

use tensor4all_core::index::{DefaultIndex as Index, DynId};
use tensor4all_core::{norm_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 tensor: IdxTensor = IdxTensor::from_dense(vec![i.clone()], vec![3.0_f64, 4.0])?;

let norm = context.with_concrete_session(|session| norm_on_session(session, &tensor))??;
assert!((norm - 5.0).abs() < 1e-12);