use crate::backend_ops::BackendOps;
use coeus_core::{CpuAddressableStorage, CpuAddressableStorageMut, Scalar};
use coeus_tensor::Tensor;
#[inline]
pub fn index_select<T: Scalar, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
dim: usize,
index: &Tensor<T, B>,
_backend: &B,
) -> Tensor<T, B>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
let ndim = input.ndim();
assert!(
dim < ndim,
"index_select: dim {dim} out of range for {ndim}-D tensor"
);
assert_eq!(
index.ndim(),
1,
"index_select: index must be 1-D, got {}-D",
index.ndim()
);
let in_shape = input.shape();
let k = index.shape()[0];
let mut out_shape = in_shape.to_vec();
out_shape[dim] = k;
let in_cont = input.to_contiguous();
let idx_cont = index.to_contiguous();
let in_s = in_cont.as_slice();
let idx_s = idx_cont.as_slice();
if k == in_shape[dim] {
let mut is_identity = true;
for (i, &v) in idx_s.iter().enumerate() {
if (<T as Scalar>::to_f64(v) as usize) != i {
is_identity = false;
break;
}
}
if is_identity {
return input.to_contiguous();
}
}
let mut in_strides = vec![1usize; ndim];
for d in (0..ndim - 1).rev() {
in_strides[d] = in_strides[d + 1] * in_shape[d + 1];
}
let out_numel: usize = out_shape.iter().product();
let mut out_strides = vec![1usize; ndim];
for d in (0..ndim - 1).rev() {
out_strides[d] = out_strides[d + 1] * out_shape[d + 1];
}
let out_data: Vec<T> = (0..out_numel)
.map(|out_flat| {
let mut rem = out_flat;
let mut in_flat = 0usize;
let mut dim_coord = 0usize;
for d in 0..ndim {
let coord = rem / out_strides[d];
rem %= out_strides[d];
if d == dim {
dim_coord = coord;
} else {
in_flat += coord * in_strides[d];
}
}
let sel = <T as Scalar>::to_f64(idx_s[dim_coord]) as usize;
assert!(
sel < in_shape[dim],
"index_select: index value {sel} out of bounds for dim {dim} size {}",
in_shape[dim]
);
in_s[in_flat + sel * in_strides[dim]]
})
.collect();
Tensor::from_slice(out_shape, &out_data)
}
#[cfg(test)]
mod tests {
use super::*;
use coeus_core::SequentialBackend;
use coeus_tensor::Tensor;
#[test]
fn index_select_1d_selects_correct_elements() {
let b = SequentialBackend::new();
let x = Tensor::from_slice(vec![5], &[10.0f32, 20.0, 30.0, 40.0, 50.0]);
let idx = Tensor::from_slice(vec![3], &[4.0f32, 0.0, 2.0]);
let out = index_select(&x, 0, &idx, &b);
assert_eq!(out.shape(), &[3]);
assert_eq!(out.as_slice(), &[50.0, 10.0, 30.0]);
}
#[test]
fn index_select_2d_selects_rows() {
let b = SequentialBackend::new();
let x = Tensor::from_slice(
vec![4, 3],
&[
1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
],
);
let idx = Tensor::from_slice(vec![2], &[3.0f32, 1.0]);
let out = index_select(&x, 0, &idx, &b);
assert_eq!(out.shape(), &[2, 3]);
assert_eq!(out.as_slice(), &[10.0, 11.0, 12.0, 4.0, 5.0, 6.0]);
}
#[test]
fn index_select_2d_selects_cols() {
let b = SequentialBackend::new();
let x = Tensor::from_slice(vec![2, 4], &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
let idx = Tensor::from_slice(vec![2], &[3.0f32, 0.0]);
let out = index_select(&x, 1, &idx, &b);
assert_eq!(out.shape(), &[2, 2]);
assert_eq!(out.as_slice(), &[4.0, 1.0, 8.0, 5.0]);
}
#[test]
fn index_select_identity_returns_shared_storage() {
let b = SequentialBackend::new();
let x = Tensor::from_slice(vec![2, 3], &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0]);
let idx = Tensor::from_slice(vec![2], &[0.0f32, 1.0]);
let out = index_select(&x, 0, &idx, &b);
assert_eq!(out.shape(), &[2, 3]);
assert_eq!(out.as_slice(), x.as_slice());
assert_eq!(out.as_slice().as_ptr(), x.as_slice().as_ptr());
}
}