Convert multi dimensional array into 1-d slice

I have a multi dimensional array as below.

let mut my_array = [[[0f32; 15]; 15]; 4];

I need create a tensor by copying the data from this array. The Tendor structure provides a with_values method to initialize from a 1-D slice.

pub fn with_values(self, value: &[T]) -> Result<Self>

I dislike the inefficient approach to copy the element one by one. Perhaps it is better to take use memcpy internally. The multi-dimensional array is a continuous area of memory. The dimensions do not change the memory layout if I am correct.

let state_tensor = Tensor::<f32>::new(&[1, 4, 15, 15]).with_values(...);

Is there a way to efficiently initialize tensor using the multi dimensional array?

bytemuck - Rust can help you reinterpret a nested array of numbers as a slice without any copying.

1 Like

If the tensor elements are plain old data, then the compiler will almost certainly optimize the copy to be a single memcpy() anyway. Don't assume what the compiler does, and don't assume what is fast and what is not. Optimize only if you measured that what you get by default isn't fast enough.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.