cubecl_std/tensor/contiguous/
launch.rs1use crate::tensor::{TensorHandle, copy_gpu_ref, launch_copy_perpendicular_ref};
2use cubecl_core::{client::Client, ir::ElemType, prelude::TensorBinding};
3
4pub fn into_contiguous(client: &Client, input: TensorBinding, dtype: ElemType) -> TensorHandle {
6 let num_elems: usize = input.shape.iter().product();
7
8 let handle = client.empty(num_elems * dtype.size());
9 let output = TensorHandle::new_contiguous(input.shape.to_vec(), handle, dtype);
10
11 copy_into(client, input, output.clone().binding(), dtype);
12
13 output
14}
15
16pub fn into_contiguous_pitched(
19 client: &Client,
20 input: TensorBinding,
21 dtype: ElemType,
22) -> TensorHandle {
23 if input.shape.len() <= 1 {
24 return into_contiguous(client, input, dtype);
25 }
26
27 let output = TensorHandle::empty(client, input.shape.clone(), dtype);
28
29 copy_into(client, input, output.clone().binding(), dtype);
30
31 output
32}
33
34pub fn copy_into(client: &Client, input: TensorBinding, output: TensorBinding, dtype: ElemType) {
36 let rank = input.strides.len();
37
38 let is_cpu = client.properties().hardware.num_cpu_cores.is_some();
41 let same_rank = input.strides.len() == output.strides.len();
42 if input.strides[rank - 1] != 1 && is_cpu && same_rank {
43 launch_copy_perpendicular_ref(client, input, output, dtype);
44 } else {
45 copy_gpu_ref(client, input, output, dtype);
46 };
47}