Does reshape make a copy of the data? #2655
-
|
Naive question about how |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Most of the time reshape doesn't copy data and just edits the metadata of an array to edit its shape. For instance when you create a flat array of 100 elements and the reshape it to 2x5x10 there is no need to copy because the elements are still flat so we only need to edit the shape and strides. However, if we transpose the array above to 5x2x10 and then reshape to 10x10 then we have to copy because the first axis would not be flat. I use flat here to mean contiguous in memory. In general a user need not care about the contiguity of an MLX array. It may seldom make sense for some optimization in which case you can also use |
Beta Was this translation helpful? Give feedback.
Most of the time reshape doesn't copy data and just edits the metadata of an array to edit its shape.
For instance when you create a flat array of 100 elements and the reshape it to 2x5x10 there is no need to copy because the elements are still flat so we only need to edit the shape and strides.
However, if we transpose the array above to 5x2x10 and then reshape to 10x10 then we have to copy because the first axis would not be flat.
I use flat here to mean contiguous in memory. In general a user need not care about the contiguity of an MLX array. It may seldom make sense for some optimization in which case you can also use
mx.contiguouswithout the need to reshape.