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
16 changes: 14 additions & 2 deletions .github/scripts/setup+build-linux-container-cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
set -ex

# [Setup] Install dependencies inside the container.
# Note: mlx-c requires a C++20 toolchain with <format> support:
# - libstdc++ >= 13 (Ubuntu noble, Fedora 39+)
# - or libc++ >= 17
# Debian bookworm (libstdc++ 12) is *not* sufficient.
if command -v apt-get >/dev/null 2>&1; then
apt-get update -y
apt-get install -y \
Expand All @@ -12,6 +16,9 @@ if command -v apt-get >/dev/null 2>&1; then
liblapacke-dev \
libopenblas-dev

export CC=/usr/bin/gcc
export CXX=/usr/bin/g++

elif command -v dnf >/dev/null 2>&1; then
dnf update -y
dnf install -y \
Expand All @@ -23,6 +30,11 @@ elif command -v dnf >/dev/null 2>&1; then
clang \
ninja-build

# Fedora's default gcc (13+) has <format>; keep it as the compiler so this
# build keeps matching what has been passing in CI.
export CC=/usr/bin/gcc
export CXX=/usr/bin/g++

else
echo "No supported package manager found (apt-get, dnf)"
exit 1
Expand All @@ -31,8 +43,8 @@ fi
# [CMake] CI Build Sanity Check: Verifies code compilation, not for release.
export CMAKE_ARGS="-DCMAKE_COMPILE_WARNING_AS_ERROR=ON"
export DEBUG=1
export CMAKE_C_COMPILER=/usr/bin/clang
export CMAKE_CXX_COMPILER=/usr/bin/clang++

"$CXX" --version

rm -rf build
mkdir -p build
Expand Down
15 changes: 12 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Build and Test

on: pull_request
on:
pull_request:
workflow_dispatch:

permissions:
contents: read
Expand Down Expand Up @@ -172,20 +174,26 @@ jobs:
linux_build_cmake_container:
name: Linux Container CMake Swift Build (${{ matrix.container }} ${{ matrix.arch }})
needs: lint
if: github.repository == 'ml-explore/mlx-swift'
strategy:
fail-fast: false
matrix:
include:
# See https://github.com/swiftlang/swift-docker/tree/main/
#
# Note: mlx-c uses std::format, which requires libstdc++ >= 13 (or
# libc++ >= 17). Debian bookworm only ships gcc/libstdc++ 12 (and
# gcc-13 is not available in bookworm-backports), so use Ubuntu
# noble (24.04, gcc/libstdc++ 13) for the Debian-family build.
- host: ubuntu-22.04
arch: x86_64
container: swift:bookworm
container: swift:noble
- host: ubuntu-22.04
arch: x86_64
container: swift:fedora39
- host: ubuntu-22.04-arm
arch: aarch64
container: swift:bookworm
container: swift:noble
- host: ubuntu-22.04-arm
arch: aarch64
container: swift:fedora39
Expand All @@ -204,6 +212,7 @@ jobs:
linux_build_cmake_cuda:
name: Linux CUDA CMake Swift Build (cuda-12.9, swift-6.2.3-RELEASE, ubuntu-24.04, x86_64)
needs: lint
if: github.repository == 'ml-explore/mlx-swift'
runs-on: "gpu-t4-4-core"
env:
# Note 12/18/2025: the CI machine does not meet CUDA 13's driver requirement.
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
run: xcodebuild -showComponent MetalToolchain

- name: Setup gh
if: github.event.release.tag_name || inputs.tag
shell: sh
run: |
brew install gh
Expand All @@ -44,10 +45,19 @@ jobs:
shell: bash
working-directory: xcode/build/output
run: |
for framework in MLX Cmlx MLXNN MLXOptimizers; do
for framework in MLX Cmlx cLogSupport MLXNN MLXOptimizers; do
zip -r -y "${framework}.xcframework.zip" "${framework}.xcframework"
done

- name: Upload XCFrameworks as workflow artifacts
if: ${{ !(github.event.release.tag_name || inputs.tag) }}
uses: actions/upload-artifact@v4
with:
name: xcframeworks
if-no-files-found: error
retention-days: 7
path: xcode/build/output/*.xcframework.zip

- name: Upload XCFrameworks to release
if: github.event.release.tag_name || inputs.tag
env:
Expand All @@ -56,5 +66,6 @@ jobs:
gh release upload "${{ github.event.release.tag_name || inputs.tag }}" \
xcode/build/output/MLX.xcframework.zip \
xcode/build/output/Cmlx.xcframework.zip \
xcode/build/output/cLogSupport.xcframework.zip \
xcode/build/output/MLXNN.xcframework.zip \
xcode/build/output/MLXOptimizers.xcframework.zip
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
url = https://github.com/numen-tech/mlx.git
[submodule "submodules/mlx-c"]
path = Source/Cmlx/mlx-c
url = https://github.com/ml-explore/mlx-c
url = https://github.com/numen-tech/mlx-c.git
19 changes: 17 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ set(MLX_C_BUILD_EXAMPLES OFF)
FetchContent_Declare(
mlx-c
GIT_REPOSITORY "https://github.com/ml-explore/mlx-c.git"
GIT_TAG "c74db5307cc8ce122f48d97ef951b30578674e7f")
GIT_TAG "ebc88f10caa1b625e6b581437a8dea6df8a70085")
FetchContent_MakeAvailable(mlx-c)

# swift-numerics
Expand Down Expand Up @@ -99,6 +99,21 @@ if(MLX_BUILD_CUDA)
CUDNN::cudnn_all # Promoted from PRIVATE, required by mlx
)
endif()

# cLogSupport package small C shim that exposes os_log_type_enabled() to Swift
# (see #491)
set(cLogSupport_include ${CMAKE_CURRENT_LIST_DIR}/Source/cLogSupport/include)
set(cLogSupport_module_dir ${CMAKE_CURRENT_BINARY_DIR}/cLogSupport-module)
file(
WRITE ${cLogSupport_module_dir}/module.modulemap
"module cLogSupport {\n header \"${cLogSupport_include}/cLogSupport.h\"\n export *\n}\n"
)

add_library(cLogSupport STATIC
${CMAKE_CURRENT_LIST_DIR}/Source/cLogSupport/cLogSupport.c)
target_include_directories(cLogSupport PUBLIC ${cLogSupport_include}
${cLogSupport_module_dir})

file(GLOB MLX-src ${CMAKE_CURRENT_LIST_DIR}/Source/MLX/*.swift)

if(NOT MLX_BUILD_METAL)
Expand All @@ -114,7 +129,7 @@ add_library(MLX STATIC ${MLX-src})
target_include_directories(MLX
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/Source/Cmlx/include)
# Note: mlxc is target name exported by mlx-c
target_link_libraries(MLX PUBLIC mlxc Numerics)
target_link_libraries(MLX PUBLIC mlxc Numerics cLogSupport)
target_compile_options(MLX PRIVATE -package-name MLX)

# MLXRandom package
Expand Down
18 changes: 16 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ let noMetalCmlxExcludes = [
"mlx/mlx/backend/metal/compiled.cpp",
"mlx/mlx/backend/metal/conv.cpp",
"mlx/mlx/backend/metal/copy.cpp",
"mlx/mlx/backend/metal/cross_entropy.cpp",
"mlx/mlx/backend/metal/custom_kernel.cpp",
"mlx/mlx/backend/metal/device.cpp",
"mlx/mlx/backend/metal/device_info.cpp",
Expand All @@ -49,6 +50,7 @@ let noMetalCmlxExcludes = [
"mlx/mlx/backend/metal/event.cpp",
"mlx/mlx/backend/metal/fence.cpp",
"mlx/mlx/backend/metal/fft.cpp",
"mlx/mlx/backend/metal/gated_delta_update.cpp",
"mlx/mlx/backend/metal/hadamard.cpp",
"mlx/mlx/backend/metal/indexing.cpp",
"mlx/mlx/backend/metal/jit_kernels.cpp",
Expand All @@ -66,6 +68,7 @@ let noMetalCmlxExcludes = [
"mlx/mlx/backend/metal/slicing.cpp",
"mlx/mlx/backend/metal/softmax.cpp",
"mlx/mlx/backend/metal/sort.cpp",
"mlx/mlx/backend/metal/spec_decode.cpp",
"mlx/mlx/backend/metal/ternary.cpp",
"mlx/mlx/backend/metal/unary.cpp",
"mlx/mlx/backend/metal/utils.cpp",
Expand All @@ -81,6 +84,7 @@ let noCudaCmlxExcludes = [
"mlx/mlx/backend/cuda/conv.cpp",
"mlx/mlx/backend/cuda/cublas_utils.cpp",
"mlx/mlx/backend/cuda/cudnn_utils.cpp",
"mlx/mlx/backend/cuda/cusolver_utils.cpp",
"mlx/mlx/backend/cuda/custom_kernel.cpp",
"mlx/mlx/backend/cuda/delayload.cpp",
"mlx/mlx/backend/cuda/device_info.cpp",
Expand All @@ -97,6 +101,7 @@ let noCudaCmlxExcludes = [
"mlx/mlx/backend/cuda/scaled_dot_product_attention.cpp",
"mlx/mlx/backend/cuda/slicing.cpp",
"mlx/mlx/backend/cuda/utils.cpp",
"mlx/mlx/backend/cuda/wddm.cpp",
"mlx/mlx/backend/cuda/worker.cpp",
"mlx/mlx/backend/cuda/binary",
"mlx/mlx/backend/cuda/conv",
Expand Down Expand Up @@ -134,6 +139,7 @@ let noCudaCmlxExcludes = [
"mlx-c/mlx/c/metal.cpp",

"mlx/mlx/backend/cuda/delayload.cpp", // For Windows
"mlx/mlx/backend/cuda/wddm.cpp", // For Windows

// built by the CudaBuild plugin (nvcc), not by SwiftPM
"mlx/mlx/backend/cuda/quantized/qmm",
Expand All @@ -155,6 +161,7 @@ let noCudaCmlxExcludes = [
.linkedLibrary("cudnn"),
.linkedLibrary("cublas"),
.linkedLibrary("cublasLt"),
.linkedLibrary("cusolver"),
.linkedLibrary("nvrtc"),
.linkedLibrary("cudart"),
.linkedLibrary("cuda"),
Expand Down Expand Up @@ -267,7 +274,6 @@ let cmlx = Target.target(
// mlx files that are not part of the build
"mlx/ACKNOWLEDGMENTS.md",
"mlx/CMakeLists.txt",
"mlx/CODE_OF_CONDUCT.md",
"mlx/CONTRIBUTING.md",
"mlx/LICENSE",
"mlx/MANIFEST.in",
Expand All @@ -281,6 +287,7 @@ let cmlx = Target.target(
"mlx/python",
"mlx/setup.py",
"mlx/tests",
"mlx/build",

// build variants (we are opting _out_ of these)
"mlx/mlx/io/no_safetensors.cpp",
Expand Down Expand Up @@ -308,7 +315,7 @@ let cmlx = Target.target(
.headerSearchPath("mlx-c"),
.headerSearchPath("json/single_include/nlohmann"),
.headerSearchPath("fmt/include"),
.define("MLX_VERSION", to: "\"0.32.2\""),
.define("MLX_VERSION", to: "\"0.32.4\""),
],
linkerSettings: linkerSettings,
plugins: cudaBuildPlugins,
Expand Down Expand Up @@ -345,10 +352,17 @@ let package = Package(
dependencies: ["Cmlx"]
),

.target(
name: "cLogSupport",
path: "Source/cLogSupport",
publicHeadersPath: "include"
),

.target(
name: "MLX",
dependencies: [
"Cmlx",
"cLogSupport",
.product(name: "Numerics", package: "swift-numerics"),
],
exclude: mlxSwiftExcludes,
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ MLX is an array framework for machine learning on Apple silicon. MLX Swift
expands MLX to the Swift language, making research and experimentation easier
on Apple silicon.

[View Current Release Planning](https://github.com/ml-explore/mlx-swift/issues?q=is%3Aissue+is%3Aopen+label%3Arelease-planning) and read about [Contributing](#contributing).

## Language Models

LLM and VLM implementations are available in [mlx-swift-lm](https://github.com/ml-explore/mlx-swift-lm).
Expand Down Expand Up @@ -185,6 +187,8 @@ contributors](ACKNOWLEDGMENTS.md#Individual-Contributors). If you contribute
to MLX Swift and wish to be acknowledged, please add your name to the list in your
pull request.

Smaller, focused PRs are easier and quicker to review. Sometimes larger PRs can't be helped. If you want to discuss, feel free to open an issue or ask questions in a PR.

MLX Swift was initially developed by David Koski and Ronan Collobert, and is
now maintained by David Koski. MLX Swift is built on top of
[MLX](https://github.com/ml-explore/mlx), which was initially developed with
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 6b011b957794d7fa0e291e86f3dd87e18b101e7c.
// Autogenerated from commit 084da487f5c3c2043102834a94b9b210245fd2c4.
//
// Copyright 2020-2024 Apple Inc.
//
Expand Down
56 changes: 51 additions & 5 deletions Source/Cmlx/include-framework/mlx-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@
#pragma once

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

#include <Cmlx/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 @@ -52,12 +63,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
Loading