use crate::tensor::{TensorHandle, copy_gpu_ref, try_launch_copy_perpendicular};
use cubecl_core::{client::Client, ir::ElemType, prelude::TensorBinding};
pub fn into_contiguous(client: &Client, input: TensorBinding, dtype: ElemType) -> TensorHandle {
let num_elems: usize = input.shape.iter().product();
let handle = client.empty(num_elems * dtype.size());
let output = TensorHandle::new_contiguous(input.shape.to_vec(), handle, dtype);
copy_into(client, input, output.clone().binding(), dtype);
output
}
pub fn into_contiguous_pitched(
client: &Client,
input: TensorBinding,
dtype: ElemType,
) -> TensorHandle {
if input.shape.len() <= 1 {
return into_contiguous(client, input, dtype);
}
let output = TensorHandle::empty(client, input.shape.clone(), dtype);
copy_into(client, input, output.clone().binding(), dtype);
output
}
pub fn copy_into(client: &Client, input: TensorBinding, output: TensorBinding, dtype: ElemType) {
let rank = input.strides.len();
let same_shape = input.shape == output.shape;
if input.strides[rank - 1] == 1 || !same_shape {
return copy_gpu_ref(client, input, output, dtype);
}
if !try_launch_copy_perpendicular(client, &input, &output, dtype) {
copy_gpu_ref(client, input, output, dtype);
}
}