Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mlx/backend/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ void shared_buffer_reshape(
const Strides& out_strides,
array& out) {
auto flags = in.flags();
auto data_size = in.data_size();
if (flags.row_contiguous) {
// For row contiguous reshapes:
// - Shallow copy the buffer
Expand All @@ -160,8 +161,12 @@ void shared_buffer_reshape(
check_contiguity(out.shape(), out_strides);
flags.row_contiguous = row_contiguous;
flags.col_contiguous = col_contiguous;
if (row_contiguous || col_contiguous) {
flags.contiguous = true;
data_size = out.size();
}
}
out.copy_shared_buffer(in, out_strides, flags, in.data_size());
out.copy_shared_buffer(in, out_strides, flags, data_size);
}

void Split::eval(
Expand Down
8 changes: 6 additions & 2 deletions mlx/backend/cpu/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,19 @@ void View::eval_cpu(const std::vector<array>& inputs, array& out) {
strides.back() = 1;
}
auto flags = in.flags();
auto data_size = in.data_size() * ibytes / obytes;
if (ibytes != obytes) {
// Last axis size can change contiguity
auto [_, row_contiguous, col_contiguous] =
check_contiguity(out.shape(), strides);
flags.row_contiguous = row_contiguous;
flags.col_contiguous = col_contiguous;
if (row_contiguous || col_contiguous) {
flags.contiguous = true;
data_size = out.size();
}
}
out.copy_shared_buffer(
in, strides, flags, in.data_size() * ibytes / obytes);
out.copy_shared_buffer(in, strides, flags, data_size);
} else {
auto tmp = array(
in.shape(), in.dtype() == bool_ ? uint8 : in.dtype(), nullptr, {});
Expand Down
8 changes: 6 additions & 2 deletions mlx/backend/gpu/primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,19 @@ void View::eval_gpu(const std::vector<array>& inputs, array& out) {
strides.back() = 1;
}
auto flags = in.flags();
auto data_size = in.data_size() * ibytes / obytes;
if (ibytes != obytes) {
// Last axis size can change contiguity
auto [_, row_contiguous, col_contiguous] =
check_contiguity(out.shape(), strides);
flags.row_contiguous = row_contiguous;
flags.col_contiguous = col_contiguous;
if (row_contiguous || col_contiguous) {
flags.contiguous = true;
data_size = out.size();
}
}
out.copy_shared_buffer(
in, strides, flags, in.data_size() * ibytes / obytes);
out.copy_shared_buffer(in, strides, flags, data_size);
} else {
auto tmp = array(in.shape(), in.dtype(), nullptr, {});
tmp.set_data(allocator::malloc(tmp.nbytes()));
Expand Down
8 changes: 7 additions & 1 deletion mlx/backend/metal/conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,14 @@ void pad_and_slice_conv_3D_gpu(
implicit_gemm_conv_3D_gpu(s, d, in, wt, intermediate, new_params);

// Slice out
auto [_, row_contiguous, col_contiguous] =
check_contiguity(out.shape(), intermediate.strides());
bool contiguous = row_contiguous || col_contiguous;
out.copy_shared_buffer(
intermediate, intermediate.strides(), {0}, intermediate.data_size());
intermediate,
intermediate.strides(),
{contiguous, row_contiguous, col_contiguous},
contiguous ? out.size() : intermediate.data_size());
}

void conv_2D_gpu(
Expand Down
10 changes: 10 additions & 0 deletions python/tests/test_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,16 @@ def run_conv3D(
N, C, OC, idim, kdim, stride, padding, dilation=(2, 2, 2), dtype=dtype
)

def test_conv_3D_output_reshape_transpose(self):
# A dense output with no contiguity flags must stay correct after a
# reshape and transpose.
x = mx.random.normal((2, 12, 15, 18, 2))
w = mx.random.normal((240, 4, 5, 6, 2))
y = mx.conv_general(x, w, stride=(4, 5, 6))
out = y.reshape(-1, 240).T.reshape(-1)
expected = np.array(y).reshape(-1, 240).T.reshape(-1)
self.assertTrue(np.array_equal(out, expected))

@unittest.skipIf(not has_torch, "requires Torch")
def test_torch_conv_3D_grad(self):
def run_conv3D_grad(
Expand Down
28 changes: 28 additions & 0 deletions tests/ops_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ TEST_CASE("test reshape") {
eval(y);
CHECK(!y.flags().row_contiguous);
CHECK(y.flags().col_contiguous);

// Dense data with no contiguity flags becomes contiguous after reshape
x = arange(32, float32);
eval(x);
auto dense = array({1, 1, 20}, float32, nullptr, {});
dense.copy_shared_buffer(x, {32, 32, 1}, {false, false, false}, 32);
dense.set_status(array::Status::available);
y = reshape(dense, {4, 5});
eval(y);
CHECK(y.flags().contiguous);
CHECK(y.flags().row_contiguous);
CHECK_EQ(y.data_size(), 20);
CHECK(
array_equal(transpose(y), transpose(reshape(arange(20, float32), {4, 5})))
.item<bool>());
}

TEST_CASE("test flatten") {
Expand Down Expand Up @@ -4546,6 +4561,19 @@ TEST_CASE("test view") {
in = array({1, 2, 3, 4}, int64);
auto out = view(in, int32);
CHECK(array_equal(out, array({1, 0, 2, 0, 3, 0, 4, 0})).item<bool>());

// Dense data with no contiguity flags becomes contiguous after a view
auto x = arange(8, int64);
eval(x);
in = array({2, 4}, int64, nullptr, {});
in.copy_shared_buffer(x, {4, 1}, {false, false, false}, 8);
in.set_status(array::Status::available);
out = view(in, int32);
eval(out);
CHECK(out.flags().contiguous);
CHECK(out.flags().row_contiguous);
CHECK(array_equal(transpose(out), transpose(view(reshape(x, {2, 4}), int32)))
.item<bool>());
}

TEST_CASE("test roll") {
Expand Down