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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ set(MLX_BUILD_PYTHON_BINDINGS OFF)
FetchContent_Declare(
mlx
GIT_REPOSITORY "https://github.com/ml-explore/mlx.git"
GIT_TAG "v0.32.4")
GIT_TAG "v0.32.5")
FetchContent_MakeAvailable(mlx)

# mlx-c
set(MLX_C_BUILD_EXAMPLES OFF)
FetchContent_Declare(
mlx-c
GIT_REPOSITORY "https://github.com/ml-explore/mlx-c.git"
GIT_TAG "v0.8.0")
GIT_TAG "663004b70284a9b0dae055bd7e6391224446d7d9")
FetchContent_MakeAvailable(mlx-c)

# swift-numerics
Expand Down
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ let noCudaCmlxExcludes = [
"mlx/mlx/backend/cpu/jit_compiler.cpp",
"mlx/mlx/distributed/jaccl/no_jaccl.cpp",

// TEMPORARY: replaced by mlx-conditional/quantized_conditional.cpp
// (Mac Catalyst build fix) until the fix lands upstream
"mlx/mlx/backend/cpu/quantized.cpp",

// opt-out of these backends (using metal)
"mlx/mlx/backend/no_gpu",
"mlx/mlx/backend/no_cpu",
Expand Down
3 changes: 2 additions & 1 deletion Source/Cmlx/include-framework/Cmlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Cmlx/mlx-backend-cuda-cuda.h>
#include <Cmlx/mlx-backend-metal-metal.h>
#include <Cmlx/mlx-compile.h>
#include <Cmlx/mlx-config.h>
#include <Cmlx/mlx-device.h>
#include <Cmlx/mlx-distributed-distributed.h>
#include <Cmlx/mlx-distributed-ops.h>
Expand Down Expand Up @@ -41,7 +42,7 @@
#include <Cmlx/mlx-primitives.h>
#include <Cmlx/mlx-backend-metal-device.h>
#include <Cmlx/mlx-backend-metal-resident.h>
#include <Cmlx/mlx-backend-common-metal_kernel.h>
#include <Cmlx/mlx-backend-common-custom_kernel.h>
#include <Cmlx/mlx-backend-common-utils.h>
#include <Cmlx/mlx-backend-cpu-encoder.h>
#include <Cmlx/mlx-backend-gpu-eval.h>
Expand Down
2 changes: 1 addition & 1 deletion Source/Cmlx/include-framework/Metal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Metal.hpp.in
//
// Autogenerated from commit 26091dacff5333233dda0be4bce5297fb3d18ec9.
// Autogenerated from commit 1b5b52ca743824694555923def99f715aee3c003.
//
// Copyright 2020-2024 Apple Inc.
//
Expand Down
26 changes: 26 additions & 0 deletions Source/Cmlx/include-framework/mlx-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include <functional>
#include <utility>

#ifndef NDEBUG
#include <map>
#include <shared_mutex>
#include <vector>
#endif

#include <Cmlx/mlx-api.h>

namespace mlx::core::allocator {
Expand Down Expand Up @@ -53,16 +59,36 @@ class MLX_API Allocator {
};
virtual void release(Buffer buffer) {}

#ifndef NDEBUG
bool has_invalid_memory(const std::vector<void*>& ptrs);
#endif

Allocator() = default;
Allocator(const Allocator& other) = delete;
Allocator(Allocator&& other) = delete;
Allocator& operator=(const Allocator& other) = delete;
Allocator& operator=(Allocator&& other) = delete;
virtual ~Allocator() = default;

protected:
#ifndef NDEBUG
void track_allocation(void* data_ptr, size_t size);
void untrack_allocation(void* data_ptr);
void swap_allocation(void* data_ptr_a, void* data_ptr_b);
#endif

private:
#ifndef NDEBUG
std::map<void*, size_t> allocations_;
std::shared_mutex allocations_mutex_;
#endif
};

MLX_API Allocator& allocator();

// Whether the active memory is above the memory limit.
MLX_API bool over_memory_limit();

using Deleter = std::function<void(Buffer)>;

// The allocator owns data from malloc
Expand Down
12 changes: 6 additions & 6 deletions Source/Cmlx/include-framework/mlx-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <functional>
#include <memory>
#include <utility>
Expand Down Expand Up @@ -566,20 +567,19 @@ T array::item() {
throw std::invalid_argument("item can only be called on arrays of size 1.");
}
eval();
return *data<T>();
// Use memcpy to read unaligned data.
T value;
std::memcpy(&value, data<void>(), sizeof(T));
return value;
}

template <typename T>
T array::item() const {
if (size() != 1) {
throw std::invalid_argument("item can only be called on arrays of size 1.");
}
if (status() == Status::unscheduled) {
throw std::invalid_argument(
"item() const can only be called on evaled arrays");
}
const_cast<array*>(this)->eval();
return *data<T>();
return const_cast<array*>(this)->item<T>();
}

template <typename It>
Expand Down
61 changes: 61 additions & 0 deletions Source/Cmlx/include-framework/mlx-backend-common-custom_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifdef __cplusplus
// Copyright © 2026 Apple Inc.

#pragma once

#include <mutex>
#include <unordered_map>
#include <variant>

#include <Cmlx/mlx-dtype.h>

namespace mlx::core::fast {

using TemplateArg = std::variant<int, bool, Dtype>;
using ScalarArg = std::variant<bool, int, float>;

enum class MathMode {
Safe = 0,
Relaxed = 1,
Fast = 2,
};

struct CompileOptions {
MathMode math_mode = MathMode::Safe;

CompileOptions() = default;
bool operator==(const CompileOptions&) const = default;

// A simple way to make export work, needs more work when adding new options.
using Data = int;
CompileOptions(Data data) : math_mode(static_cast<MathMode>(data)) {}
Data serialize() const {
return static_cast<int>(math_mode);
}
};

std::string template_arguments_hash(
const std::vector<std::pair<std::string, TemplateArg>>& template_args);

class GeneratedSourceCache {
public:
template <typename F>
const auto& get(const std::string& name, F&& make) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = cache_.find(name);
if (it != cache_.end()) {
return it->second;
}
auto source = make();
auto hash = std::hash<std::string>{}(source);
return cache_.emplace(name, std::make_pair(std::move(source), hash))
.first->second;
}

private:
std::mutex mutex_;
std::unordered_map<std::string, std::pair<std::string, size_t>> cache_;
};

} // namespace mlx::core::fast
#endif
29 changes: 0 additions & 29 deletions Source/Cmlx/include-framework/mlx-backend-common-metal_kernel.h

This file was deleted.

4 changes: 4 additions & 0 deletions Source/Cmlx/include-framework/mlx-backend-cuda-cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>

#include <Cmlx/mlx-api.h>

Expand All @@ -14,6 +15,9 @@ namespace mlx::core::cu {
/* Check if the CUDA backend is available. */
MLX_API bool is_available();

/* Get the CUDA architectures MLX was compiled for. */
MLX_API std::vector<std::string> get_arch_list();

/* Get information about a CUDA device. */
MLX_API const
std::unordered_map<std::string, std::variant<std::string, size_t>>&
Expand Down
21 changes: 16 additions & 5 deletions Source/Cmlx/include-framework/mlx-backend-metal-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include <unordered_set>

#include <Cmlx/mlx-array.h>
#include <Cmlx/mlx-backend-common-metal_kernel.h>
#include <Cmlx/mlx-backend-common-custom_kernel.h>
#include <Cmlx/mlx-backend-metal-metal.h>
#include <Cmlx/mlx-backend-metal-resident.h>
#include <Cmlx/mlx-device.h>

Expand Down Expand Up @@ -101,6 +102,12 @@ class MLX_API CommandEncoder {
bool needs_commit() const;
void commit(std::function<void()> completion = nullptr);
void synchronize();
#ifndef NDEBUG
void verify_graph(
const char* primitive_name,
const std::vector<array>& inputs,
const std::vector<array>& outputs);
#endif

MTL::CommandBuffer* get_command_buffer() const {
return buffer_.get();
Expand Down Expand Up @@ -144,6 +151,9 @@ class MLX_API CommandEncoder {
std::unordered_set<MTL::Resource*> concurrent_outputs_;
std::unordered_set<const void*> all_inputs_;
std::unordered_set<const void*> all_outputs_;
#ifndef NDEBUG
std::vector<void*> registered_data_ptrs_;
#endif

// A map of prior command encoder outputs to their corresponding fence.
std::unordered_map<const void*, NS::SharedPtr<MTL::Fence>> prev_ce_outputs_;
Expand All @@ -167,7 +177,7 @@ class MLX_API Device {
int get_architecture_gen() const {
return arch_gen_;
}
std::tuple<int, int> get_max_ops_mb_per_buffer() const {
std::tuple<int, int> get_default_max_ops_mb_per_buffer() const {
return std::make_tuple(max_ops_per_buffer_, max_mb_per_buffer_);
}

Expand All @@ -177,7 +187,7 @@ class MLX_API Device {

MTL::Library* get_library(
const std::string& name,
const CompileOptions& compile_options,
const fast::CompileOptions& compile_options,
const std::function<std::string(void)>& builder);

MTL::Library* get_library(
Expand Down Expand Up @@ -208,7 +218,7 @@ class MLX_API Device {
private:
NS::SharedPtr<MTL::Library> build_library_(
const std::string& source_string,
const CompileOptions& compile_options = {});
const fast::CompileOptions& compile_options = {});

NS::SharedPtr<MTL::Function> get_function_(
const std::string& name,
Expand Down Expand Up @@ -261,7 +271,8 @@ std::unordered_map<int, CommandEncoder>& get_command_encoders();
std::unordered_map<int, CommandEncoder>& get_global_command_encoders();
NS::SharedPtr<NS::AutoreleasePool> new_scoped_memory_pool();

bool is_nax_available();
// Number of GPU cores, or 0 when it is not known.
int gpu_core_count();

} // namespace mlx::core::metal
#endif
3 changes: 3 additions & 0 deletions Source/Cmlx/include-framework/mlx-backend-metal-metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace mlx::core::metal {
/* Check if the Metal backend is available. */
MLX_API bool is_available();

/* Check if MLX kernels can use the GPU neural accelerators (NAX). */
MLX_API bool is_nax_available();

/** Capture a GPU trace, saving it to an absolute file `path` */
MLX_API void start_capture(std::string path = "");
MLX_API void stop_capture();
Expand Down
4 changes: 2 additions & 2 deletions Source/Cmlx/include-framework/mlx-backend-metal-resident.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace mlx::core::metal {
// size of each set bounds the cost of one such event. Every set holds a
// standing requestResidency() and is attached to every command queue.
//
// MLX_RESIDENCY_SET_MAX_PCT (env::residency_set_max_pct) sets the per-set cap.
// The total wired budget is unaffected by it: that is still `set_wired_limit`.
// MLX_RESIDENCY_SET_MAX_PCT sets the per-set cap. The total wired budget is
// unaffected by it: that is still `set_wired_limit`.
class ResidencySets {
public:
ResidencySets(MTL::Device* d);
Expand Down
1 change: 1 addition & 0 deletions Source/Cmlx/include-framework/mlx-c-cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {
*/
/**@{*/

int mlx_cuda_get_arch_list(mlx_vector_string* res);
int mlx_cuda_is_available(bool* res);

/**@}*/
Expand Down
1 change: 1 addition & 0 deletions Source/Cmlx/include-framework/mlx-c-fast.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ int mlx_fast_scaled_dot_product_attention(
const mlx_array keys,
const mlx_array values,
float scale,
int window_size,
const char* mask_mode,
const mlx_array mask_arr /* may be null */,
const mlx_array sinks /* may be null */,
Expand Down
1 change: 1 addition & 0 deletions Source/Cmlx/include-framework/mlx-c-metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern "C" {

int mlx_metal_get_metallib_path(mlx_string* res);
int mlx_metal_is_available(bool* res);
int mlx_metal_is_nax_available(bool* res);
int mlx_metal_set_metallib_path(const char* path);
int mlx_metal_start_capture(const char* path);
int mlx_metal_stop_capture(void);
Expand Down
Loading
Loading