python313Packages.mlx: 0.21.1 -> 0.25.2

Fixes failing build, adds testing, upgrades to latest version.

Co-authored-by: Sarah Clark <seclark@nextquestion.net>

Changed files
+145 -83
pkgs
+15
pkgs/development/python-modules/mlx/darwin-build-fixes.patch
···
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
+
index ab8aea44..9e1b06f4 100644
+
--- a/CMakeLists.txt
+
+++ b/CMakeLists.txt
+
@@ -100,9 +100,7 @@ elseif(MLX_BUILD_METAL)
+
endif()
+
+
# Throw an error if xcrun not found
+
- execute_process(
+
- COMMAND zsh "-c" "/usr/bin/xcrun -sdk macosx --show-sdk-version"
+
- OUTPUT_VARIABLE MACOS_SDK_VERSION COMMAND_ERROR_IS_FATAL ANY)
+
+ set(MACOS_SDK_VERSION @sdkVersion@)
+
+
if(${MACOS_SDK_VERSION} LESS 14.0)
+
message(
+130 -70
pkgs/development/python-modules/mlx/default.nix
···
{
+
buildPythonPackage,
+
fetchFromGitHub,
lib,
-
fetchFromGitHub,
-
buildPythonPackage,
+
replaceVars,
+
stdenv,
+
+
# build-system
+
setuptools,
+
+
# nativeBuildInputs
+
cmake,
+
+
# buildInputs
+
apple-sdk_14,
+
fmt_10,
+
nanobind,
+
nlohmann_json,
pybind11,
-
cmake,
-
xcbuild,
-
zsh,
-
blas,
-
lapack,
-
setuptools,
+
+
# tests
+
numpy,
+
pytestCheckHook,
+
python,
+
runCommand,
}:
let
···
gguf-tools = fetchFromGitHub {
owner = "antirez";
repo = "gguf-tools";
-
rev = "af7d88d808a7608a33723fba067036202910acb3";
-
hash = "sha256-LqNvnUbmq0iziD9VP5OTJCSIy+y/hp5lKCUV7RtKTvM=";
+
rev = "8fa6eb65236618e28fd7710a0fba565f7faa1848";
+
hash = "sha256-15FvyPOFqTOr5vdWQoPnZz+mYH919++EtghjozDlnSA=";
};
-
nlohmann_json = fetchFromGitHub {
-
owner = "nlohmann";
-
repo = "json";
-
rev = "v3.11.3";
-
hash = "sha256-7F0Jon+1oWL7uqet5i1IgHX0fUw/+z0QwEcA3zs5xHg=";
-
};
-
in
-
buildPythonPackage rec {
-
pname = "mlx";
-
version = "0.21.1";
-
src = fetchFromGitHub {
-
owner = "ml-explore";
-
repo = "mlx";
-
rev = "refs/tags/v${version}";
-
hash = "sha256-wxv9bA9e8VyFv/FMh63sUTTNgkXHGQJNQhLuVynczZA=";
-
};
+
mlx = buildPythonPackage rec {
+
pname = "mlx";
+
version = "0.25.2";
+
pyproject = true;
-
pyproject = true;
+
src = fetchFromGitHub {
+
owner = "ml-explore";
+
repo = "mlx";
+
tag = "v${version}";
+
hash = "sha256-fkf/kKATr384WduFG/X81c5InEAZq5u5+hwrAJIg7MI=";
+
};
-
patches = [
-
# With Darwin SDK 11 we cannot include vecLib/cblas_new.h, this needs to wait for PR #229210
-
# In the meantime, pretend Accelerate is not available and use blas/lapack instead.
-
./disable-accelerate.patch
-
];
+
patches = [
+
(replaceVars ./darwin-build-fixes.patch {
+
sdkVersion = apple-sdk_14.version;
+
})
+
];
-
postPatch = ''
-
substituteInPlace CMakeLists.txt \
-
--replace "/usr/bin/xcrun" "${xcbuild}/bin/xcrun" \
-
'';
+
postPatch = ''
+
substituteInPlace pyproject.toml \
+
--replace-fail "nanobind==2.4.0" "nanobind>=2.4.0"
+
+
substituteInPlace mlx/backend/cpu/jit_compiler.cpp \
+
--replace-fail "g++" "$CXX"
+
'';
-
dontUseCmakeConfigure = true;
+
dontUseCmakeConfigure = true;
-
# updates the wrong fetcher rev attribute
-
passthru.skipBulkUpdate = true;
+
enableParallelBuilding = true;
+
+
# Allows multiple cores to be used in Python builds.
+
postUnpack = ''
+
export MAKEFLAGS+="''${enableParallelBuilding:+-j$NIX_BUILD_CORES}"
+
'';
-
env = {
-
PYPI_RELEASE = version;
-
# we can't use Metal compilation with Darwin SDK 11
-
CMAKE_ARGS = toString [
-
(lib.cmakeBool "MLX_BUILD_METAL" false)
-
(lib.cmakeOptionType "filepath" "FETCHCONTENT_SOURCE_DIR_GGUFLIB" "${gguf-tools}")
-
(lib.cmakeOptionType "filepath" "FETCHCONTENT_SOURCE_DIR_JSON" "${nlohmann_json}")
+
# updates the wrong fetcher rev attribute
+
passthru.skipBulkUpdate = true;
+
+
env = {
+
PYPI_RELEASE = version;
+
CMAKE_ARGS = toString [
+
# NOTE The `metal` command-line utility used to build the Metal kernels is not open-source.
+
# To build mlx with Metal support in Nix, you'd need to use one of the sandbox escape
+
# hatches which let you interact with a native install of Xcode, such as `composeXcodeWrapper`
+
# or by changing the upstream (e.g., https://github.com/zed-industries/zed/discussions/7016).
+
(lib.cmakeBool "MLX_BUILD_METAL" false)
+
(lib.cmakeOptionType "filepath" "FETCHCONTENT_SOURCE_DIR_GGUFLIB" "${gguf-tools}")
+
(lib.cmakeOptionType "filepath" "FETCHCONTENT_SOURCE_DIR_JSON" "${nlohmann_json.src}")
+
(lib.cmakeOptionType "filepath" "FETCHCONTENT_SOURCE_DIR_FMT" "${fmt_10.src}")
+
];
+
};
+
+
build-system = [
+
setuptools
];
-
};
-
nativeBuildInputs = [
-
cmake
-
pybind11
-
xcbuild
-
zsh
-
gguf-tools
-
nlohmann_json
-
setuptools
-
];
+
nativeBuildInputs = [
+
cmake
+
];
-
buildInputs = [
-
blas
-
lapack
-
];
+
buildInputs = [
+
apple-sdk_14
+
fmt_10
+
gguf-tools
+
nanobind
+
nlohmann_json
+
pybind11
+
];
-
meta = with lib; {
-
homepage = "https://github.com/ml-explore/mlx";
-
description = "Array framework for Apple silicon";
-
changelog = "https://github.com/ml-explore/mlx/releases/tag/v${version}";
-
license = licenses.mit;
-
platforms = [ "aarch64-darwin" ];
-
maintainers = with maintainers; [
-
viraptor
-
Gabriella439
+
pythonImportsCheck = [ "mlx" ];
+
+
# Run the mlx Python test suite.
+
nativeCheckInputs = [
+
numpy
+
pytestCheckHook
+
];
+
+
pytestFlagsArray = [
+
"python/tests/"
];
+
+
# Additional testing by executing the example Python scripts supplied with mlx
+
# using the version of the library we've built.
+
passthru.tests = {
+
mlxTest =
+
runCommand "run-mlx-examples"
+
{
+
buildInputs = [ mlx ];
+
nativeBuildInputs = [ python ];
+
}
+
''
+
cp ${src}/examples/python/logistic_regression.py .
+
${python.interpreter} logistic_regression.py
+
rm logistic_regression.py
+
+
cp ${src}/examples/python/linear_regression.py .
+
${python.interpreter} linear_regression.py
+
rm linear_regression.py
+
+
touch $out
+
'';
+
};
+
+
meta = {
+
homepage = "https://github.com/ml-explore/mlx";
+
description = "Array framework for Apple silicon";
+
changelog = "https://github.com/ml-explore/mlx/releases/tag/${src.tag}";
+
license = lib.licenses.mit;
+
platforms = [ "aarch64-darwin" ];
+
maintainers = with lib.maintainers; [
+
viraptor
+
Gabriella439
+
cameronyule
+
];
+
};
};
-
}
+
in
+
mlx
-13
pkgs/development/python-modules/mlx/disable-accelerate.patch
···
-
diff --git a/CMakeLists.txt b/CMakeLists.txt
-
index 2d6bef9..d099673 100644
-
--- a/CMakeLists.txt
-
+++ b/CMakeLists.txt
-
@@ -104,7 +104,7 @@ elseif (MLX_BUILD_METAL)
-
${QUARTZ_LIB})
-
endif()
-
-
-find_library(ACCELERATE_LIBRARY Accelerate)
-
+#find_library(ACCELERATE_LIBRARY Accelerate)
-
if (MLX_BUILD_ARM AND ACCELERATE_LIBRARY)
-
message(STATUS "Accelerate found ${ACCELERATE_LIBRARY}")
-
set(MLX_BUILD_ACCELERATE ON)