python: fix bytes() on non-contiguous arrays - #4449
Conversation
simeetnayan81
left a comment
There was a problem hiding this comment.
Hey, we need tests to catch these issues with calling bytes on a view. The issue was not caught earlier because test only considered packed array.
We might need these cases:
x[::2]
x[::-1]
m.T (transpose view)
m[:, 1]
broadcast_to
x[1:6] as a packed view with offset
Thanks
| "__bytes__", | ||
| [](mx::array& a) { | ||
| a.eval(); | ||
| auto c = mx::contiguous(a); |
There was a problem hiding this comment.
Won't this be better? We might want to save a second call if a is already packed/contiguous :
a.flags().row_contiguous ? a : mx::contiguous(a);There was a problem hiding this comment.
When a is not evaluated a.flags().row_contiguous would return garbage so we have to use mx::contiguous.
There was a problem hiding this comment.
Oh alright. Then we may skip this. Thanks.
|
@axiom-of-choice Can you add a test for bytes() non-contiguous arrays? |
@zcbenz Done! |
__bytes__reads the raw buffer pointer, which for a strided view (slice, transpose, broadcast) points into the parent array, sobytes(x[::2])dumps neighboring memory instead of the view's values.Make the array row-contiguous before reading, which is a no-op copy for already-packed arrays. Fixes #4445.