Skip to main content

index_select_on_session

Function index_select_on_session 

Source
pub fn index_select_on_session(
    session: &mut Session<'_>,
    tensor: &IdxTensor,
    source_index: &DynIndex,
    target_index: DynIndex,
    positions: &[usize],
) -> Result<IdxTensor, IdxTensorError>
Expand description

Gather positions along one index on a caller-held explicit session.

The session counterpart of IdxTensor::index_select: the operand is published as compact host storage on session and the gather runs on that host payload, so it enters no runtime. All four floating dtypes are served.

§Errors

Returns a typed IdxTensorError when the target dimension does not match the position count, the source index is absent, a position is out of range, or the session admission refuses the operand.

§Examples

use tensor4all_core::{index_select_on_session, DynIndex, IdxTensor};
use tensor4all_tensorbackend::CpuExecutionContext;
use tensor4all_tensorbackend::CpuBackend;

let context = CpuExecutionContext::from_backend(CpuBackend::with_threads(1)?);
let source = DynIndex::new_dyn(3);
let target = DynIndex::new_dyn(2);
let tensor = IdxTensor::from_dense(vec![source.clone()], vec![10.0_f64, 20.0, 30.0])?;
let gathered = context.with_concrete_session(|session| {
    index_select_on_session(session, &tensor, &source, target, &[2, 0])
})??;
assert_eq!(gathered.dense_host_values::<f64>()?, vec![30.0, 10.0]);