Skip to content

Commit 8b1906a

Browse files
authored
Add compiler flags to disable safetensors and gguf (#1098)
* with docs * nit
1 parent 06375e6 commit 8b1906a

File tree

9 files changed

+118
-30
lines changed

9 files changed

+118
-30
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ option(MLX_BUILD_PYTHON_BINDINGS "Build python bindings for mlx" OFF)
1717
option(MLX_BUILD_METAL "Build metal backend" ON)
1818
option(MLX_METAL_DEBUG "Enhance metal debug workflow" OFF)
1919
option(MLX_ENABLE_X64_MAC "Enable building for x64 macOS" OFF)
20+
option(MLX_BUILD_GGUF "Include support for GGUF format" ON)
21+
option(MLX_BUILD_SAFETENSORS "Include support for safetensors format" ON)
2022
option(BUILD_SHARED_LIBS "Build mlx as a shared library" OFF)
2123

2224
if(NOT MLX_VERSION)

docs/src/install.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ should point to the path to the built metal library.
157157
- OFF
158158
* - MLX_METAL_DEBUG
159159
- OFF
160-
160+
* - MLX_BUILD_SAFETENSORS
161+
- ON
162+
* - MLX_BUILD_GGUF
163+
- ON
161164

162165
.. note::
163166

mlx/io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include "mlx/array.h"
88
#include "mlx/io/load.h"
9-
#include "mlx/ops.h"
109
#include "mlx/stream.h"
10+
#include "mlx/utils.h"
1111

1212
namespace mlx::core {
1313
using GGUFMetaData =

mlx/io/CMakeLists.txt

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
1+
12
target_sources(
23
mlx
34
PRIVATE
45
${CMAKE_CURRENT_SOURCE_DIR}/load.cpp
5-
${CMAKE_CURRENT_SOURCE_DIR}/safetensor.cpp
6-
${CMAKE_CURRENT_SOURCE_DIR}/gguf.cpp
7-
${CMAKE_CURRENT_SOURCE_DIR}/gguf_quants.cpp
86
)
97

10-
MESSAGE(STATUS "Downloading json")
11-
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
12-
FetchContent_MakeAvailable(json)
13-
target_include_directories(
14-
mlx PRIVATE
15-
$<BUILD_INTERFACE:${json_SOURCE_DIR}/single_include/nlohmann>
16-
)
8+
if (MLX_BUILD_SAFETENSORS)
9+
MESSAGE(STATUS "Downloading json")
10+
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
11+
FetchContent_MakeAvailable(json)
12+
target_include_directories(
13+
mlx PRIVATE
14+
$<BUILD_INTERFACE:${json_SOURCE_DIR}/single_include/nlohmann>
15+
)
16+
target_sources(
17+
mlx
18+
PRIVATE
19+
${CMAKE_CURRENT_SOURCE_DIR}/safetensors.cpp
20+
)
21+
else()
22+
target_sources(
23+
mlx
24+
PRIVATE
25+
${CMAKE_CURRENT_SOURCE_DIR}/no_safetensors.cpp
26+
)
27+
endif()
1728

18-
MESSAGE(STATUS "Downloading gguflib")
19-
FetchContent_Declare(gguflib
20-
GIT_REPOSITORY https://github.com/antirez/gguf-tools/
21-
GIT_TAG af7d88d808a7608a33723fba067036202910acb3
22-
)
23-
FetchContent_MakeAvailable(gguflib)
24-
target_include_directories(
25-
mlx PRIVATE
26-
$<BUILD_INTERFACE:${gguflib_SOURCE_DIR}>
27-
)
29+
if (MLX_BUILD_GGUF)
30+
MESSAGE(STATUS "Downloading gguflib")
31+
FetchContent_Declare(gguflib
32+
GIT_REPOSITORY https://github.com/antirez/gguf-tools/
33+
GIT_TAG af7d88d808a7608a33723fba067036202910acb3
34+
)
35+
FetchContent_MakeAvailable(gguflib)
36+
target_include_directories(
37+
mlx PRIVATE
38+
$<BUILD_INTERFACE:${gguflib_SOURCE_DIR}>
39+
)
40+
add_library(
41+
gguflib STATIC
42+
${gguflib_SOURCE_DIR}/fp16.c
43+
${gguflib_SOURCE_DIR}/gguflib.c)
44+
target_link_libraries(mlx $<BUILD_INTERFACE:gguflib>)
45+
target_sources(
46+
mlx
47+
PRIVATE
48+
${CMAKE_CURRENT_SOURCE_DIR}/gguf.cpp
49+
${CMAKE_CURRENT_SOURCE_DIR}/gguf_quants.cpp
50+
)
51+
else()
52+
target_sources(
53+
mlx
54+
PRIVATE
55+
${CMAKE_CURRENT_SOURCE_DIR}/no_gguf.cpp
56+
)
57+
endif()
2858

29-
add_library(
30-
gguflib STATIC
31-
${gguflib_SOURCE_DIR}/fp16.c
32-
${gguflib_SOURCE_DIR}/gguflib.c)
33-
target_link_libraries(mlx $<BUILD_INTERFACE:gguflib>)

mlx/io/gguf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <cstring>
55
#include <numeric>
66

7-
#include <mlx/io/gguf.h>
7+
#include "mlx/io/gguf.h"
8+
#include "mlx/ops.h"
89

910
namespace mlx::core {
1011

mlx/io/gguf_quants.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <cstring>
55
#include <numeric>
66

7-
#include <mlx/io/gguf.h>
7+
#include "mlx/io/gguf.h"
88

99
namespace mlx::core {
1010

mlx/io/no_gguf.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright © 2023-2024 Apple Inc.
2+
3+
#include "mlx/io.h"
4+
5+
namespace mlx::core {
6+
7+
GGUFLoad load_gguf(const std::string&, StreamOrDevice s) {
8+
throw std::runtime_error(
9+
"[load_gguf] Compile with MLX_BUILD_GGUF=ON to enable GGUF support.");
10+
}
11+
12+
void save_gguf(
13+
std::string,
14+
std::unordered_map<std::string, array>,
15+
std::unordered_map<std::string, GGUFMetaData>) {
16+
throw std::runtime_error(
17+
"[save_gguf] Compile with MLX_BUILD_GGUF=ON to enable GGUF support.");
18+
}
19+
20+
} // namespace mlx::core

mlx/io/no_safetensors.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright © 2023-2024 Apple Inc.
2+
3+
#include "mlx/io.h"
4+
5+
namespace mlx::core {
6+
7+
SafetensorsLoad load_safetensors(std::shared_ptr<io::Reader>, StreamOrDevice) {
8+
throw std::runtime_error(
9+
"[load_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
10+
"to enable safetensors support.");
11+
}
12+
13+
SafetensorsLoad load_safetensors(const std::string&, StreamOrDevice) {
14+
throw std::runtime_error(
15+
"[load_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
16+
"to enable safetensors support.");
17+
}
18+
19+
void save_safetensors(
20+
std::shared_ptr<io::Writer>,
21+
std::unordered_map<std::string, array>,
22+
std::unordered_map<std::string, std::string>) {
23+
throw std::runtime_error(
24+
"[save_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
25+
"to enable safetensors support.");
26+
}
27+
28+
void save_safetensors(
29+
std::string file,
30+
std::unordered_map<std::string, array>,
31+
std::unordered_map<std::string, std::string>) {
32+
throw std::runtime_error(
33+
"[save_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
34+
"to enable safetensors support.");
35+
}
36+
37+
} // namespace mlx::core
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "mlx/io.h"
77
#include "mlx/io/load.h"
8+
#include "mlx/ops.h"
89
#include "mlx/primitives.h"
910

1011
using json = nlohmann::json;
@@ -149,7 +150,6 @@ SafetensorsLoad load_safetensors(const std::string& file, StreamOrDevice s) {
149150
return load_safetensors(std::make_shared<io::FileReader>(file), s);
150151
}
151152

152-
/** Save array to out stream in .npy format */
153153
void save_safetensors(
154154
std::shared_ptr<io::Writer> out_stream,
155155
std::unordered_map<std::string, array> a,

0 commit comments

Comments
 (0)