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
56 changes: 51 additions & 5 deletions mlx/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
#pragma once

#include <cstdlib>
#include <functional>
#include <utility>

#include "mlx/api.h"

namespace mlx::core::allocator {
class Data;
} // namespace mlx::core::allocator

namespace mlx::core::cu {
class CommandEncoder;
allocator::Data malloc_async(size_t size, CommandEncoder& encoder);
} // namespace mlx::core::cu

namespace mlx::core::allocator {

// Simple wrapper around buffer pointers
Expand Down Expand Up @@ -51,12 +62,47 @@ class MLX_API Allocator {

MLX_API Allocator& allocator();

inline Buffer malloc(size_t size) {
return allocator().malloc(size);
}
using Deleter = std::function<void(Buffer)>;

// The allocator owns data from malloc
class Data {
public:
Data(Buffer buffer, Deleter d) : buffer_(buffer), deleter_(std::move(d)) {}
Data(const Data& other) = delete;
Data& operator=(const Data& other) = delete;
Data(Data&& other) noexcept
: buffer_(std::exchange(other.buffer_, Buffer(nullptr))),
deleter_(std::exchange(other.deleter_, nullptr)),
owned_(std::exchange(other.owned_, false)) {}
~Data() {
if (owned_) {
allocator().free(buffer_);
} else if (deleter_) {
deleter_(buffer_);
}
}

Buffer buffer() const& {
return buffer_;
}
Buffer buffer() const&& = delete;

bool is_owned() const {
return owned_;
}

private:
Buffer buffer_;
Deleter deleter_;
bool owned_{false};

explicit Data(Buffer buffer) : buffer_(buffer), owned_(true) {}
friend Data malloc(size_t size);
friend Data cu::malloc_async(size_t size, cu::CommandEncoder& encoder);
};

inline void free(Buffer buffer) {
allocator().free(buffer);
inline Data malloc(size_t size) {
return Data(allocator().malloc(size));
}

// Make a Buffer from a raw pointer of the given size without a copy. If a
Expand Down
24 changes: 10 additions & 14 deletions mlx/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ std::vector<array> array::make_arrays(
array array::unsafe_weak_copy(const array& other) {
auto cpy = array(other.shape(), other.dtype(), nullptr, {});
cpy.set_data(
other.buffer(),
Data(other.buffer(), nullptr),
other.data_size(),
other.strides(),
other.flags(),
0,
[](auto) {});
cpy.array_desc_->offset = other.array_desc_->offset;
other.offset());
return cpy;
}

Expand Down Expand Up @@ -104,14 +102,13 @@ array::array(
allocator::release(buffer);
return deleter(ptr);
};
set_data(buffer, std::move(wrapped_deleter));
set_data(Data(buffer, std::move(wrapped_deleter)));
}
}

/* Build an array from a shared buffer */
array::array(allocator::Buffer data, Shape shape, Dtype dtype, Deleter deleter)
array::array(allocator::Data data, Shape shape, Dtype dtype)
: array_desc_(std::make_shared<ArrayDesc>(std::move(shape), dtype)) {
set_data(data, deleter);
set_data(std::move(data));
}

void array::detach() {
Expand Down Expand Up @@ -166,8 +163,8 @@ bool array::is_tracer() const {
detail::retain_graph();
}

void array::set_data(allocator::Buffer buffer, Deleter d) {
array_desc_->data = std::make_shared<Data>(buffer, d);
void array::set_data(allocator::Data data) {
array_desc_->data = std::make_shared<Data>(std::move(data));
array_desc_->offset = 0;
array_desc_->data_size = size();
array_desc_->flags.contiguous = true;
Expand All @@ -177,13 +174,12 @@ void array::set_data(allocator::Buffer buffer, Deleter d) {
}

void array::set_data(
allocator::Buffer buffer,
allocator::Data data,
size_t data_size,
Strides strides,
Flags flags,
int64_t offset,
Deleter d) {
array_desc_->data = std::make_shared<Data>(buffer, d);
int64_t offset) {
array_desc_->data = std::make_shared<Data>(std::move(data));
array_desc_->offset = offset;
array_desc_->data_size = data_size;
array_desc_->strides = std::move(strides);
Expand Down
52 changes: 10 additions & 42 deletions mlx/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace mlx::core {
// Forward declaration
class Primitive;

using Deleter = std::function<void(allocator::Buffer)>;
using ShapeElem = int32_t;
using Shape = SmallVector<ShapeElem>;
using Strides = SmallVector<int64_t>;
Expand Down Expand Up @@ -69,12 +68,8 @@ class MLX_API array {
Dtype dtype,
const std::function<void(void*)>& deleter);

/* Build an array from a buffer */
explicit array(
allocator::Buffer data,
Shape shape,
Dtype dtype,
Deleter deleter = allocator::free);
/* Build an array from data */
explicit array(allocator::Data data, Shape shape, Dtype dtype);

/** Assignment to rvalue does not compile. */
array& operator=(const array& other) && = delete;
Expand Down Expand Up @@ -238,22 +233,7 @@ class MLX_API array {
return reinterpret_cast<std::uintptr_t>(array_desc_->primitive.get());
}

struct Data {
allocator::Buffer buffer;
Deleter d;
Data(allocator::Buffer buffer, Deleter d = allocator::free)
: buffer(buffer), d(d) {}
// Not copyable
Data(const Data& d) = delete;
Data& operator=(const Data& d) = delete;
Data(Data&& o) : buffer(o.buffer), d(o.d) {
o.buffer = allocator::Buffer(nullptr);
o.d = [](allocator::Buffer) {};
}
~Data() {
d(buffer);
}
};
using Data = allocator::Data;

struct Flags {
// True iff there are no gaps in the underlying data. Each item
Expand Down Expand Up @@ -302,7 +282,8 @@ class MLX_API array {

/** True indicates the arrays buffer is safe to reuse */
bool is_donatable() const {
return array_desc_.use_count() == 1 && (array_desc_->data.use_count() == 1);
return array_desc_.use_count() == 1 &&
(array_desc_->data.use_count() == 1) && array_desc_->data->is_owned();
}

/** The array's siblings. */
Expand Down Expand Up @@ -359,11 +340,8 @@ class MLX_API array {
return array_desc_->data_size;
}

allocator::Buffer& buffer() {
return array_desc_->data->buffer;
}
const allocator::Buffer& buffer() const {
return array_desc_->data->buffer;
allocator::Buffer buffer() const {
return array_desc_->data->buffer();
}

size_t buffer_size() const {
Expand Down Expand Up @@ -447,24 +425,14 @@ class MLX_API array {
// Check if the array is a tracer array
bool is_tracer() const;

void set_data(allocator::Buffer buffer, Deleter d = allocator::free);
void set_data(allocator::Data data);

void set_data(
allocator::Buffer buffer,
allocator::Data data,
size_t data_size,
Strides strides,
Flags flags,
Deleter d) {
set_data(buffer, data_size, std::move(strides), flags, 0, std::move(d));
}

void set_data(
allocator::Buffer buffer,
size_t data_size,
Strides strides,
Flags flags,
int64_t offset = 0,
Deleter d = allocator::free);
int64_t offset = 0);

void copy_shared_buffer(
const array& other,
Expand Down
2 changes: 1 addition & 1 deletion mlx/backend/common/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ inline void set_binary_op_output_data(
const array& b,
array& out,
BinaryOpType bopt,
std::function<allocator::Buffer(size_t)> mallocfn = allocator::malloc) {
std::function<allocator::Data(size_t)> mallocfn = allocator::malloc) {
bool b_donatable = is_donatable(b, out);
bool a_donatable = is_donatable(a, out);
switch (bopt) {
Expand Down
2 changes: 1 addition & 1 deletion mlx/backend/common/compiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void compiled_allocate_outputs(
std::vector<array>& outputs,
const std::function<bool(size_t)>& is_constant,
bool contiguous,
const std::function<allocator::Buffer(size_t)>&
const std::function<allocator::Data(size_t)>&
mallocfn /* = allocator::malloc */) {
if (contiguous) {
int o = 0;
Expand Down
3 changes: 1 addition & 2 deletions mlx/backend/common/compiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ void compiled_allocate_outputs(
std::vector<array>& outputs,
const std::function<bool(size_t)>& is_constant,
bool contiguous,
const std::function<allocator::Buffer(size_t)>& mallocfn =
allocator::malloc);
const std::function<allocator::Data(size_t)>& mallocfn = allocator::malloc);

// Collapse contiguous dims ignoring scalars and constants.
std::tuple<bool, bool, Shape, std::vector<Strides>>
Expand Down
2 changes: 1 addition & 1 deletion mlx/backend/common/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ inline bool set_copy_output_data(
const array& in,
array& out,
CopyType ctype,
std::function<allocator::Buffer(size_t)> mallocfn = allocator::malloc) {
std::function<allocator::Data(size_t)> mallocfn = allocator::malloc) {
if (ctype == CopyType::Vector) {
// If the input is donateable, we are doing a vector copy and the types
// have the same size, then the input buffer can hold the output.
Expand Down
2 changes: 1 addition & 1 deletion mlx/backend/common/ternary.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ inline void set_ternary_op_output_data(
const array& c,
array& out,
TernaryOpType topt,
std::function<allocator::Buffer(size_t)> mallocfn = allocator::malloc) {
std::function<allocator::Data(size_t)> mallocfn = allocator::malloc) {
auto maybe_donate = [&out](const array& x) {
if (is_donatable(x, out)) {
out.copy_shared_buffer(x);
Expand Down
2 changes: 1 addition & 1 deletion mlx/backend/common/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace mlx::core {
inline void set_unary_output_data(
const array& in,
array& out,
std::function<allocator::Buffer(size_t)> mallocfn = allocator::malloc) {
std::function<allocator::Data(size_t)> mallocfn = allocator::malloc) {
if (in.flags().contiguous) {
if (is_donatable(in, out)) {
out.copy_shared_buffer(in);
Expand Down
10 changes: 5 additions & 5 deletions mlx/backend/cpu/eig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ struct EigWork<
}

void run(T* a, O* values, O* vectors) {
auto eig_tmp = static_cast<T*>(buffers[0].buffer.raw_ptr());
auto eig_tmp = static_cast<T*>(buffers[0].buffer().raw_ptr());
T* vec_tmp = nullptr;
if (vectors) {
vec_tmp = static_cast<T*>(buffers[1].buffer.raw_ptr());
vec_tmp = static_cast<T*>(buffers[1].buffer().raw_ptr());
}
auto work = static_cast<T*>(buffers.back().buffer.raw_ptr());
auto work = static_cast<T*>(buffers.back().buffer().raw_ptr());

int n_vecs_l = vectors ? N : 1;
int n_vecs_r = 1;
Expand Down Expand Up @@ -166,9 +166,9 @@ struct EigWork<std::complex<float>> {
&n_vecs_l,
nullptr,
&n_vecs_r,
static_cast<T*>(buffers[0].buffer.raw_ptr()),
static_cast<T*>(buffers[0].buffer().raw_ptr()),
&lwork,
static_cast<R*>(buffers[1].buffer.raw_ptr()),
static_cast<R*>(buffers[1].buffer().raw_ptr()),
&info);
}
};
Expand Down
10 changes: 5 additions & 5 deletions mlx/backend/cpu/eigh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ struct EighWork<
vectors,
&N,
values,
static_cast<T*>(buffers[0].buffer.raw_ptr()),
static_cast<T*>(buffers[0].buffer().raw_ptr()),
&lwork,
static_cast<int*>(buffers[1].buffer.raw_ptr()),
static_cast<int*>(buffers[1].buffer().raw_ptr()),
&liwork,
&info);
}
Expand Down Expand Up @@ -116,11 +116,11 @@ struct EighWork<std::complex<float>> {
vectors,
&N,
values,
static_cast<T*>(buffers[0].buffer.raw_ptr()),
static_cast<T*>(buffers[0].buffer().raw_ptr()),
&lwork,
static_cast<R*>(buffers[1].buffer.raw_ptr()),
static_cast<R*>(buffers[1].buffer().raw_ptr()),
&lrwork,
static_cast<int*>(buffers[2].buffer.raw_ptr()),
static_cast<int*>(buffers[2].buffer().raw_ptr()),
&liwork,
&info);
if (jobz == 'V') {
Expand Down
10 changes: 5 additions & 5 deletions mlx/backend/cpu/inverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace mlx::core {
template <typename T>
void general_inv(T* inv, int N) {
int info;
auto ipiv = array::Data{allocator::malloc(sizeof(int) * N)};
auto ipiv = allocator::malloc(sizeof(int) * N);
// Compute LU factorization.
getrf<T>(
/* m = */ &N,
/* n = */ &N,
/* a = */ inv,
/* lda = */ &N,
/* ipiv = */ static_cast<int*>(ipiv.buffer.raw_ptr()),
/* ipiv = */ static_cast<int*>(ipiv.buffer().raw_ptr()),
/* info = */ &info);

if (info != 0) {
Expand Down Expand Up @@ -49,15 +49,15 @@ void general_inv(T* inv, int N) {
}

const int lwork = workspace_size;
auto scratch = array::Data{allocator::malloc(sizeof(T) * lwork)};
auto scratch = allocator::malloc(sizeof(T) * lwork);

// Compute inverse.
getri<T>(
/* m = */ &N,
/* a = */ inv,
/* lda = */ &N,
/* ipiv = */ static_cast<int*>(ipiv.buffer.raw_ptr()),
/* work = */ static_cast<T*>(scratch.buffer.raw_ptr()),
/* ipiv = */ static_cast<int*>(ipiv.buffer().raw_ptr()),
/* work = */ static_cast<T*>(scratch.buffer().raw_ptr()),
/* lwork = */ &lwork,
/* info = */ &info);

Expand Down
Loading
Loading