use coeus_core::{ComputeBackend, CpuAddressableStorage, CpuAddressableStorageMut, Scalar};
use coeus_tensor::Tensor;
#[inline]
pub fn split<T: Scalar, B: ComputeBackend + Default>(
x: &Tensor<T, B>,
chunk_size: usize,
dim: usize,
) -> Result<Vec<Tensor<T, B>>, B::Error>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
assert!(chunk_size > 0, "split: chunk_size must be > 0");
let ndim = x.ndim();
assert!(
dim < ndim,
"split: dim {dim} out of range for {ndim}D tensor"
);
let backend = B::default();
let dim_size = x.shape()[dim];
let sizes: Vec<_> = (0..dim_size)
.step_by(chunk_size)
.map(|start| (dim_size - start).min(chunk_size))
.collect();
let (flat, offsets) = coeus_cpu::split_values(x.layout(), x.storage().as_slice(), dim, &sizes)
.expect("coeus-cpu split failed");
offsets
.windows(2)
.zip(sizes)
.map(|(bounds, chunk_dim)| {
let mut out_shape = x.shape_cloned();
out_shape[dim] = chunk_dim;
Tensor::from_slice_on(out_shape, &flat[bounds[0]..bounds[1]], &backend)
})
.collect()
}