diff --git a/.github/scripts/setup+build-linux-container-cmake.sh b/.github/scripts/setup+build-linux-container-cmake.sh index 8b365bc42..93f8ce870 100755 --- a/.github/scripts/setup+build-linux-container-cmake.sh +++ b/.github/scripts/setup+build-linux-container-cmake.sh @@ -2,6 +2,10 @@ set -ex # [Setup] Install dependencies inside the container. +# Note: mlx-c requires a C++20 toolchain with 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 \ @@ -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 \ @@ -23,6 +30,11 @@ elif command -v dnf >/dev/null 2>&1; then clang \ ninja-build + # Fedora's default gcc (13+) has ; 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 @@ -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 diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 56452955c..225cd2207 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -155,15 +155,20 @@ jobs: 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 41404a01d..fe4370bb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ endif() 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 diff --git a/Source/Cmlx/include-framework/Metal.hpp b/Source/Cmlx/include-framework/Metal.hpp index 717aadc89..a44ec5666 100644 --- a/Source/Cmlx/include-framework/Metal.hpp +++ b/Source/Cmlx/include-framework/Metal.hpp @@ -2,7 +2,7 @@ // // Metal.hpp.in // -// Autogenerated from commit 68ad1c14e2bae5b8cca5aefe3a5eb91650c0147a. +// Autogenerated from commit 8f784840de21cacd0c5c237ea860179e47dcec91. // // Copyright 2020-2024 Apple Inc. // diff --git a/Source/Cmlx/include-framework/mlx-c-device.h b/Source/Cmlx/include-framework/mlx-c-device.h index 0a41ed3ab..09e799610 100644 --- a/Source/Cmlx/include-framework/mlx-c-device.h +++ b/Source/Cmlx/include-framework/mlx-c-device.h @@ -7,7 +7,6 @@ #include #include -#include #ifdef __cplusplus extern "C" { @@ -26,6 +25,12 @@ typedef struct mlx_device_ { void* ctx; } mlx_device; +/** + * A MLX vector of strings. + * Forward declaration to avoid circular references. + */ +typedef struct mlx_vector_string_ mlx_vector_string; + /** * Device type. */ diff --git a/Source/Cmlx/include-framework/mlx-c-io_types.h b/Source/Cmlx/include-framework/mlx-c-io_types.h index 4eccc95a9..7b6ae483a 100644 --- a/Source/Cmlx/include-framework/mlx-c-io_types.h +++ b/Source/Cmlx/include-framework/mlx-c-io_types.h @@ -37,10 +37,10 @@ typedef struct mlx_io_vtable_ { bool (*is_open)(void*); bool (*good)(void*); size_t (*tell)(void*); - void (*seek)(void*, int64_t off, int whence); - void (*read)(void*, char* data, size_t n); - void (*read_at_offset)(void*, char* data, size_t n, size_t off); - void (*write)(void*, const char* data, size_t n); + int (*seek)(void*, int64_t off, int whence); + size_t (*read)(void*, char* data, size_t n); + size_t (*read_at_offset)(void*, char* data, size_t n, size_t off); + size_t (*write)(void*, const char* data, size_t n); const char* (*label)(void*); void (*free)(void*); } mlx_io_vtable; diff --git a/Source/Cmlx/include-framework/mlx-c-stream.h b/Source/Cmlx/include-framework/mlx-c-stream.h index 039d4b60a..4115e7e91 100644 --- a/Source/Cmlx/include-framework/mlx-c-stream.h +++ b/Source/Cmlx/include-framework/mlx-c-stream.h @@ -4,6 +4,7 @@ #define MLX_STREAM_H #include +#include #include @@ -24,6 +25,22 @@ typedef struct mlx_stream_ { void* ctx; } mlx_stream; +/** + * A per-thread stream object. + * + * The same thread-local stream can be used from any number of threads; the + * stream's GPU command encoder is registered once per thread. + */ +typedef struct mlx_stream_thread_local_ { + void* ctx; +} mlx_stream_thread_local; + +/** + * A vector of mlx_stream. + * Forward declaration to avoid circular references. + */ +typedef struct mlx_vector_stream_ mlx_vector_stream; + /** * Returns a new empty stream. */ @@ -45,6 +62,36 @@ mlx_stream mlx_stream_new_device(mlx_device dev); * responsibility to ensure there are no data races on them. */ mlx_stream mlx_stream_new_thread_unsafe(mlx_device dev); + +/** + * Returns a new per-thread stream on a device. + * + * Streams created this way are unique per thread: evaluating the same + * `mlx_stream_thread_local` from different threads yields distinct streams. + */ +mlx_stream_thread_local mlx_stream_thread_local_new(mlx_device dev); +/** + * Set a per-thread stream to the provided src stream. + */ +int mlx_stream_thread_local_set( + mlx_stream_thread_local* tls, + const mlx_stream_thread_local src); +/** + * Free a per-thread stream. + */ +int mlx_stream_thread_local_free(mlx_stream_thread_local tls); +/** + * Returns the concrete stream backing a per-thread stream on the current + * thread. + */ +int mlx_stream_from_thread_local( + mlx_stream* res, + const mlx_stream_thread_local tls); + +/** + * Get the vector of available streams. + */ +int mlx_get_streams(mlx_vector_stream* res); /** * Set stream to provided src stream. */ @@ -73,6 +120,18 @@ int mlx_stream_get_index(int* index, mlx_stream stream); * Synchronize with the provided stream. */ int mlx_synchronize(mlx_stream stream); +/** + * Synchronize with the default stream. + */ +int mlx_synchronize_default(void); +/** + * Synchronize with the stream corresponding to the current thread. + */ +int mlx_synchronize_thread_local(mlx_stream_thread_local tls); +/** + * Destroy all streams created in the current thread. + */ +int mlx_clear_streams(void); /** * Returns the default stream on the given device. */ diff --git a/Source/Cmlx/include-framework/mlx-c-vector.h b/Source/Cmlx/include-framework/mlx-c-vector.h index 1954c6d0b..b290edc92 100644 --- a/Source/Cmlx/include-framework/mlx-c-vector.h +++ b/Source/Cmlx/include-framework/mlx-c-vector.h @@ -7,6 +7,7 @@ #define MLX_VECTOR_H #include +#include #include #ifdef __cplusplus @@ -124,6 +125,35 @@ int mlx_vector_string_append_value(mlx_vector_string vec, const char* val); size_t mlx_vector_string_size(mlx_vector_string vec); int mlx_vector_string_get(char** res, const mlx_vector_string vec, size_t idx); +/** + * A vector of stream. + */ +typedef struct mlx_vector_stream_ { + void* ctx; +} mlx_vector_stream; +mlx_vector_stream mlx_vector_stream_new(void); +int mlx_vector_stream_set(mlx_vector_stream* vec, const mlx_vector_stream src); +int mlx_vector_stream_free(mlx_vector_stream vec); +mlx_vector_stream mlx_vector_stream_new_data( + const mlx_stream* data, + size_t size); +mlx_vector_stream mlx_vector_stream_new_value(const mlx_stream val); +int mlx_vector_stream_set_data( + mlx_vector_stream* vec, + const mlx_stream* data, + size_t size); +int mlx_vector_stream_set_value(mlx_vector_stream* vec, const mlx_stream val); +int mlx_vector_stream_append_data( + mlx_vector_stream vec, + const mlx_stream* data, + size_t size); +int mlx_vector_stream_append_value(mlx_vector_stream vec, const mlx_stream val); +size_t mlx_vector_stream_size(mlx_vector_stream vec); +int mlx_vector_stream_get( + mlx_stream* res, + const mlx_vector_stream vec, + size_t idx); + /**@}*/ #ifdef __cplusplus diff --git a/Source/Cmlx/include/mlx/c/device.h b/Source/Cmlx/include/mlx/c/device.h index 4b74e39d3..705aea9db 100644 --- a/Source/Cmlx/include/mlx/c/device.h +++ b/Source/Cmlx/include/mlx/c/device.h @@ -7,7 +7,6 @@ #include #include "mlx/c/string.h" -#include "mlx/c/vector.h" #ifdef __cplusplus extern "C" { @@ -26,6 +25,12 @@ typedef struct mlx_device_ { void* ctx; } mlx_device; +/** + * A MLX vector of strings. + * Forward declaration to avoid circular references. + */ +typedef struct mlx_vector_string_ mlx_vector_string; + /** * Device type. */ diff --git a/Source/Cmlx/include/mlx/c/io_types.h b/Source/Cmlx/include/mlx/c/io_types.h index 5382e7342..3f15873e3 100644 --- a/Source/Cmlx/include/mlx/c/io_types.h +++ b/Source/Cmlx/include/mlx/c/io_types.h @@ -37,10 +37,10 @@ typedef struct mlx_io_vtable_ { bool (*is_open)(void*); bool (*good)(void*); size_t (*tell)(void*); - void (*seek)(void*, int64_t off, int whence); - void (*read)(void*, char* data, size_t n); - void (*read_at_offset)(void*, char* data, size_t n, size_t off); - void (*write)(void*, const char* data, size_t n); + int (*seek)(void*, int64_t off, int whence); + size_t (*read)(void*, char* data, size_t n); + size_t (*read_at_offset)(void*, char* data, size_t n, size_t off); + size_t (*write)(void*, const char* data, size_t n); const char* (*label)(void*); void (*free)(void*); } mlx_io_vtable; diff --git a/Source/Cmlx/include/mlx/c/stream.h b/Source/Cmlx/include/mlx/c/stream.h index abd26701b..6df1a63c9 100644 --- a/Source/Cmlx/include/mlx/c/stream.h +++ b/Source/Cmlx/include/mlx/c/stream.h @@ -4,6 +4,7 @@ #define MLX_STREAM_H #include +#include #include "mlx/c/device.h" @@ -24,6 +25,22 @@ typedef struct mlx_stream_ { void* ctx; } mlx_stream; +/** + * A per-thread stream object. + * + * The same thread-local stream can be used from any number of threads; the + * stream's GPU command encoder is registered once per thread. + */ +typedef struct mlx_stream_thread_local_ { + void* ctx; +} mlx_stream_thread_local; + +/** + * A vector of mlx_stream. + * Forward declaration to avoid circular references. + */ +typedef struct mlx_vector_stream_ mlx_vector_stream; + /** * Returns a new empty stream. */ @@ -45,6 +62,36 @@ mlx_stream mlx_stream_new_device(mlx_device dev); * responsibility to ensure there are no data races on them. */ mlx_stream mlx_stream_new_thread_unsafe(mlx_device dev); + +/** + * Returns a new per-thread stream on a device. + * + * Streams created this way are unique per thread: evaluating the same + * `mlx_stream_thread_local` from different threads yields distinct streams. + */ +mlx_stream_thread_local mlx_stream_thread_local_new(mlx_device dev); +/** + * Set a per-thread stream to the provided src stream. + */ +int mlx_stream_thread_local_set( + mlx_stream_thread_local* tls, + const mlx_stream_thread_local src); +/** + * Free a per-thread stream. + */ +int mlx_stream_thread_local_free(mlx_stream_thread_local tls); +/** + * Returns the concrete stream backing a per-thread stream on the current + * thread. + */ +int mlx_stream_from_thread_local( + mlx_stream* res, + const mlx_stream_thread_local tls); + +/** + * Get the vector of available streams. + */ +int mlx_get_streams(mlx_vector_stream* res); /** * Set stream to provided src stream. */ @@ -73,6 +120,18 @@ int mlx_stream_get_index(int* index, mlx_stream stream); * Synchronize with the provided stream. */ int mlx_synchronize(mlx_stream stream); +/** + * Synchronize with the default stream. + */ +int mlx_synchronize_default(void); +/** + * Synchronize with the stream corresponding to the current thread. + */ +int mlx_synchronize_thread_local(mlx_stream_thread_local tls); +/** + * Destroy all streams created in the current thread. + */ +int mlx_clear_streams(void); /** * Returns the default stream on the given device. */ diff --git a/Source/Cmlx/include/mlx/c/vector.h b/Source/Cmlx/include/mlx/c/vector.h index 81bcf7495..30d1b1838 100644 --- a/Source/Cmlx/include/mlx/c/vector.h +++ b/Source/Cmlx/include/mlx/c/vector.h @@ -7,6 +7,7 @@ #define MLX_VECTOR_H #include "mlx/c/array.h" +#include "mlx/c/stream.h" #include "mlx/c/string.h" #ifdef __cplusplus @@ -124,6 +125,35 @@ int mlx_vector_string_append_value(mlx_vector_string vec, const char* val); size_t mlx_vector_string_size(mlx_vector_string vec); int mlx_vector_string_get(char** res, const mlx_vector_string vec, size_t idx); +/** + * A vector of stream. + */ +typedef struct mlx_vector_stream_ { + void* ctx; +} mlx_vector_stream; +mlx_vector_stream mlx_vector_stream_new(void); +int mlx_vector_stream_set(mlx_vector_stream* vec, const mlx_vector_stream src); +int mlx_vector_stream_free(mlx_vector_stream vec); +mlx_vector_stream mlx_vector_stream_new_data( + const mlx_stream* data, + size_t size); +mlx_vector_stream mlx_vector_stream_new_value(const mlx_stream val); +int mlx_vector_stream_set_data( + mlx_vector_stream* vec, + const mlx_stream* data, + size_t size); +int mlx_vector_stream_set_value(mlx_vector_stream* vec, const mlx_stream val); +int mlx_vector_stream_append_data( + mlx_vector_stream vec, + const mlx_stream* data, + size_t size); +int mlx_vector_stream_append_value(mlx_vector_stream vec, const mlx_stream val); +size_t mlx_vector_stream_size(mlx_vector_stream vec); +int mlx_vector_stream_get( + mlx_stream* res, + const mlx_vector_stream vec, + size_t idx); + /**@}*/ #ifdef __cplusplus diff --git a/Source/Cmlx/mlx-c b/Source/Cmlx/mlx-c index c74db5307..ebc88f10c 160000 --- a/Source/Cmlx/mlx-c +++ b/Source/Cmlx/mlx-c @@ -1 +1 @@ -Subproject commit c74db5307cc8ce122f48d97ef951b30578674e7f +Subproject commit ebc88f10caa1b625e6b581437a8dea6df8a70085 diff --git a/Source/Cmlx/mlx-generated/gemm_nax.cpp b/Source/Cmlx/mlx-generated/gemm_nax.cpp index d3eac6d9c..290dea4ca 100644 --- a/Source/Cmlx/mlx-generated/gemm_nax.cpp +++ b/Source/Cmlx/mlx-generated/gemm_nax.cpp @@ -213,72 +213,6 @@ METAL_FUNC constexpr auto sum(T x, Us... us) { #pragma METAL internals : disable -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsBase.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsBase.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/private/var/run/com.apple.security.cryptexd/mnt/com.apple.MobileAsset.MetalToolchain-v17.6.42.0.n257DK/Metal.xctoolchain/usr/metal/32023/lib/clang/32023.883/include/metal/__exec/units.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/private/var/run/com.apple.security.cryptexd/mnt/com.apple.MobileAsset.MetalToolchain-v17.6.42.0.n257DK/Metal.xctoolchain/usr/metal/32023/lib/clang/32023.883/include/metal/__exec/units.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTraits.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTraits.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTypes.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTypes.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsUtility.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsUtility.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsConvolution2dImpl.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsConvolution2dImpl.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsConvolution2d.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsConvolution2d.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsAvailability.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsAvailability.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsMatMul2dImpl.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsMatMul2dImpl.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsMatMul2d.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsMatMul2d.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MetalPerformancePrimitives.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MetalPerformancePrimitives.h" - /////////////////////////////////////////////////////////////////////////////// // Contents from "mlx/backend/metal/kernels/steel/gemm/nax.h" /////////////////////////////////////////////////////////////////////////////// diff --git a/Source/Cmlx/mlx-generated/steel_attention_nax.cpp b/Source/Cmlx/mlx-generated/steel_attention_nax.cpp index 4ca7573a8..df0831701 100644 --- a/Source/Cmlx/mlx-generated/steel_attention_nax.cpp +++ b/Source/Cmlx/mlx-generated/steel_attention_nax.cpp @@ -213,72 +213,6 @@ METAL_FUNC constexpr auto sum(T x, Us... us) { #pragma METAL internals : disable -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsBase.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsBase.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/private/var/run/com.apple.security.cryptexd/mnt/com.apple.MobileAsset.MetalToolchain-v17.6.42.0.n257DK/Metal.xctoolchain/usr/metal/32023/lib/clang/32023.883/include/metal/__exec/units.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/private/var/run/com.apple.security.cryptexd/mnt/com.apple.MobileAsset.MetalToolchain-v17.6.42.0.n257DK/Metal.xctoolchain/usr/metal/32023/lib/clang/32023.883/include/metal/__exec/units.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTraits.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTraits.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTypes.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsTypes.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsUtility.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsUtility.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsConvolution2dImpl.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsConvolution2dImpl.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsConvolution2d.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsConvolution2d.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsAvailability.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsAvailability.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsMatMul2dImpl.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/__impl/MPPTensorOpsMatMul2dImpl.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsMatMul2d.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MPPTensorOpsMatMul2d.h" - -/////////////////////////////////////////////////////////////////////////////// -// Contents from "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MetalPerformancePrimitives.h" -/////////////////////////////////////////////////////////////////////////////// - -#line 1 "/Applications/XcodeWonderF.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.5.sdk/System/Library/Frameworks/MetalPerformancePrimitives.framework/Headers/MetalPerformancePrimitives.h" - /////////////////////////////////////////////////////////////////////////////// // Contents from "mlx/backend/metal/kernels/steel/attn/nax.h" /////////////////////////////////////////////////////////////////////////////// diff --git a/Source/MLX/IO.swift b/Source/MLX/IO.swift index 83c897d3c..98347ade4 100644 --- a/Source/MLX/IO.swift +++ b/Source/MLX/IO.swift @@ -210,36 +210,64 @@ private func new_mlx_io_vtable_dataIO() -> mlx_io_vtable { switch whence { case SEEK_SET: + if offset < 0 || offset > state.data.count { + return -1 + } state.offset = Int(offset) + return 0 case SEEK_CUR: - state.offset += Int(offset) + let proposed = state.offset &+ Int(offset) + if proposed < 0 || proposed > state.data.count { + return -1 + } + state.offset = proposed + return 0 case SEEK_END: // offset is relative to the end of the data, not the current position. // mlx's load_safetensors uses seek(0, end) + tell() to size the input. - state.offset = state.data.count + Int(offset) + let proposed = state.data.count &+ Int(offset) + if proposed < 0 || proposed > state.data.count { + return -1 + } + state.offset = proposed + return 0 default: - break + return -1 } } read: { ptr, data, n in let state = Unmanaged.fromOpaque(ptr!).takeUnretainedValue() + if n <= 0 || (n &+ state.offset < state.offset) { + return 0 + } + if n + state.offset <= state.data.count { - guard let data = data else { return } + guard let data = data else { return 0 } _ = state.data.withUnsafeBytes { buffer in memcpy(data, buffer.baseAddress!.advanced(by: state.offset), n) } state.offset += n + + return n + } else { + return 0 } } read_at_offset: { ptr, data, n, offset in let state = Unmanaged.fromOpaque(ptr!).takeUnretainedValue() + if n <= 0 || offset < 0 || (n &+ offset < offset) { + return 0 + } + if n + offset <= state.data.count { - guard let data = data else { return } + guard let data = data else { return 0 } _ = state.data.withUnsafeBytes { buffer in memcpy(data, buffer.baseAddress!.advanced(by: offset), n) } - state.offset = offset + return n + } else { + return 0 } } write: { ptr, data, n in @@ -248,6 +276,7 @@ private func new_mlx_io_vtable_dataIO() -> mlx_io_vtable { let buffer = UnsafeBufferPointer(start: data, count: n) state.data.append(buffer) state.offset += n + return n } label: { ptr in UnsafeRawPointer(label.utf8Start).assumingMemoryBound(to: Int8.self)