Skip to main content

permute_on_session

Function permute_on_session 

Source
pub fn permute_on_session(
    session: &mut Session<'_>,
    tensor: &IdxTensor,
    perm: &[usize],
) -> Result<IdxTensor, IdxTensorError>
Expand description

Permute a dense host tensor’s axes on a caller-held explicit session.

The numerical counterpart of IdxTensor::permute: the compatibility entry point materializes an eager transpose, which is numerical work rather than metadata, so an app that holds a region open needs this route too. The permutation convention is the compatibility one - perm[new_axis] = old_axis - and the result is the same materialized dense host value with the same permuted index list. An identity permutation is returned unchanged, exactly as the compatibility route returns a clone, and performs no session work.

§Errors

Returns a typed IdxTensorError for a permutation whose length does not match the rank, an axis out of range or repeated - with the shared validator’s messages - and, for a nonidentity permutation, under the same conditions as contract_pair_on_session refuses an operand: an AD-tracked value, a device-resident operand, a diagonal, structured or compact payload, a deferred storage error, or a non-floating dtype. It also returns one when the session rejects the permutation or the result cannot be published.

An identity permutation never reaches those refusals: like the compatibility route it returns the tensor unchanged, whatever its representation or dtype, because it is metadata only. Execution - and therefore the floating-dtype domain of this seam - applies to the nonidentity case.

§Examples

use tensor4all_core::index::{DefaultIndex as Index, DynId};
use tensor4all_core::{permute_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()], (1..=6).map(|v| v as f64).collect())?;

let transposed = context.with_concrete_session(|session| {
    permute_on_session(session, &tensor, &[1, 0])
})??;
assert_eq!(transposed.indices()[0], k);
assert_eq!(transposed.indices()[1], i);
// Column-major with the axes swapped: row major in the old order.
assert_eq!(transposed.to_vec::<f64>()?, vec![1.0, 3.0, 5.0, 2.0, 4.0, 6.0]);