Skip to content
Open
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
52 changes: 2 additions & 50 deletions mlx/backend/metal/normalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,7 @@ void RMSNorm::eval_gpu(
auto& out = outputs[0];

// Make sure that the last dimension is contiguous
auto set_output = [&s, &out](const array& x) {
bool no_copy = x.flags().contiguous && x.strides()[x.ndim() - 1] == 1;
if (no_copy && x.ndim() > 1) {
auto s = x.strides()[x.ndim() - 2];
no_copy &= (s == 0 || s == x.shape().back() || x.shape(-2) == 1);
}
if (no_copy) {
if (x.is_donatable()) {
out.copy_shared_buffer(x);
} else {
out.set_data(
allocator::malloc(x.data_size() * x.itemsize()),
x.data_size(),
x.strides(),
x.flags());
}
return x;
} else {
array x_copy = contiguous_copy_gpu(x, s);
out.copy_shared_buffer(x_copy);
return x_copy;
}
};

const array x = set_output(inputs[0]);
const array x = ensure_innermost_contiguous(out, inputs[0], s, true);
const array& w = inputs[1];

auto axis_size = static_cast<uint32_t>(x.shape().back());
Expand Down Expand Up @@ -224,31 +200,7 @@ void LayerNorm::eval_gpu(
auto& out = outputs[0];

// Make sure that the last dimension is contiguous
auto set_output = [&s, &out](const array& x) {
bool no_copy = x.flags().contiguous && x.strides()[x.ndim() - 1] == 1;
if (no_copy && x.ndim() > 1) {
auto s = x.strides()[x.ndim() - 2];
no_copy &= (s == 0 || s == x.shape().back() || x.shape(-2) == 1);
}
if (no_copy) {
if (x.is_donatable()) {
out.copy_shared_buffer(x);
} else {
out.set_data(
allocator::malloc(x.data_size() * x.itemsize()),
x.data_size(),
x.strides(),
x.flags());
}
return x;
} else {
array x_copy = contiguous_copy_gpu(x, s);
out.copy_shared_buffer(x_copy);
return x_copy;
}
};

const array x = set_output(inputs[0]);
const array x = ensure_innermost_contiguous(out, inputs[0], s, true);
const array& w = inputs[1];
const array& b = inputs[2];

Expand Down
21 changes: 1 addition & 20 deletions mlx/backend/metal/softmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,7 @@ void Softmax::eval_gpu(const std::vector<array>& inputs, array& out) {
auto& d = metal::device(s.device);

// Make sure that the last dimension is contiguous
auto set_output = [&s, &out](const array& x) {
if (x.flags().contiguous && x.strides()[x.ndim() - 1] == 1) {
if (x.is_donatable()) {
out.copy_shared_buffer(x);
} else {
out.set_data(
allocator::malloc(x.data_size() * x.itemsize()),
x.data_size(),
x.strides(),
x.flags());
}
return x;
} else {
array x_copy = contiguous_copy_gpu(x, s);
out.copy_shared_buffer(x_copy);
return x_copy;
}
};

const array in = set_output(inputs[0]);
const array in = ensure_innermost_contiguous(out, inputs[0], s);

int axis_size = in.shape().back();
int n_rows = in.data_size() / axis_size;
Expand Down
28 changes: 28 additions & 0 deletions mlx/backend/metal/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <type_traits>

#include "mlx/array.h"
#include "mlx/backend/gpu/copy.h"
#include "mlx/backend/metal/device.h"
#include "mlx/primitives.h"

Expand Down Expand Up @@ -81,6 +82,33 @@ inline size_t ceildiv(size_t n, size_t m) {
return (n + m - 1) / m;
}

inline array ensure_innermost_contiguous(
array& out,
const array& x,
const Stream& s,
bool relax_prev_stride = false) {
bool no_copy = x.flags().contiguous && x.strides()[x.ndim() - 1] == 1;
if (relax_prev_stride && no_copy && x.ndim() > 1) {
auto stride = x.strides()[x.ndim() - 2];
no_copy &= (stride == 0 || stride == x.shape().back() || x.shape(-2) == 1);
}
if (no_copy) {
if (x.is_donatable()) {
out.copy_shared_buffer(x);
} else {
out.set_data(
allocator::malloc(x.data_size() * x.itemsize()),
x.data_size(),
x.strides(),
x.flags());
}
return x;
}
array x_copy = contiguous_copy_gpu(x, s);
out.copy_shared_buffer(x_copy);
return x_copy;
}

inline void check_kernel_threadgroup_size(
const MTL::ComputePipelineState* kernel,
MTL::Size group_dims,
Expand Down
Loading