mirror of
https://github.com/ml-explore/mlx.git
synced 2025-08-21 20:46:46 +08:00
Add compiler flags to disable safetensors and gguf (#1098)
* with docs * nit
This commit is contained in:
parent
06375e6605
commit
8b1906abd0
@ -17,6 +17,8 @@ option(MLX_BUILD_PYTHON_BINDINGS "Build python bindings for mlx" OFF)
|
|||||||
option(MLX_BUILD_METAL "Build metal backend" ON)
|
option(MLX_BUILD_METAL "Build metal backend" ON)
|
||||||
option(MLX_METAL_DEBUG "Enhance metal debug workflow" OFF)
|
option(MLX_METAL_DEBUG "Enhance metal debug workflow" OFF)
|
||||||
option(MLX_ENABLE_X64_MAC "Enable building for x64 macOS" OFF)
|
option(MLX_ENABLE_X64_MAC "Enable building for x64 macOS" OFF)
|
||||||
|
option(MLX_BUILD_GGUF "Include support for GGUF format" ON)
|
||||||
|
option(MLX_BUILD_SAFETENSORS "Include support for safetensors format" ON)
|
||||||
option(BUILD_SHARED_LIBS "Build mlx as a shared library" OFF)
|
option(BUILD_SHARED_LIBS "Build mlx as a shared library" OFF)
|
||||||
|
|
||||||
if(NOT MLX_VERSION)
|
if(NOT MLX_VERSION)
|
||||||
|
@ -157,7 +157,10 @@ should point to the path to the built metal library.
|
|||||||
- OFF
|
- OFF
|
||||||
* - MLX_METAL_DEBUG
|
* - MLX_METAL_DEBUG
|
||||||
- OFF
|
- OFF
|
||||||
|
* - MLX_BUILD_SAFETENSORS
|
||||||
|
- ON
|
||||||
|
* - MLX_BUILD_GGUF
|
||||||
|
- ON
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
|
2
mlx/io.h
2
mlx/io.h
@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
#include "mlx/array.h"
|
#include "mlx/array.h"
|
||||||
#include "mlx/io/load.h"
|
#include "mlx/io/load.h"
|
||||||
#include "mlx/ops.h"
|
|
||||||
#include "mlx/stream.h"
|
#include "mlx/stream.h"
|
||||||
|
#include "mlx/utils.h"
|
||||||
|
|
||||||
namespace mlx::core {
|
namespace mlx::core {
|
||||||
using GGUFMetaData =
|
using GGUFMetaData =
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
|
|
||||||
target_sources(
|
target_sources(
|
||||||
mlx
|
mlx
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/load.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/load.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/safetensor.cpp
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/gguf.cpp
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/gguf_quants.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (MLX_BUILD_SAFETENSORS)
|
||||||
MESSAGE(STATUS "Downloading json")
|
MESSAGE(STATUS "Downloading json")
|
||||||
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
|
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
|
||||||
FetchContent_MakeAvailable(json)
|
FetchContent_MakeAvailable(json)
|
||||||
@ -14,7 +13,20 @@ target_include_directories(
|
|||||||
mlx PRIVATE
|
mlx PRIVATE
|
||||||
$<BUILD_INTERFACE:${json_SOURCE_DIR}/single_include/nlohmann>
|
$<BUILD_INTERFACE:${json_SOURCE_DIR}/single_include/nlohmann>
|
||||||
)
|
)
|
||||||
|
target_sources(
|
||||||
|
mlx
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/safetensors.cpp
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
target_sources(
|
||||||
|
mlx
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/no_safetensors.cpp
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (MLX_BUILD_GGUF)
|
||||||
MESSAGE(STATUS "Downloading gguflib")
|
MESSAGE(STATUS "Downloading gguflib")
|
||||||
FetchContent_Declare(gguflib
|
FetchContent_Declare(gguflib
|
||||||
GIT_REPOSITORY https://github.com/antirez/gguf-tools/
|
GIT_REPOSITORY https://github.com/antirez/gguf-tools/
|
||||||
@ -25,9 +37,22 @@ target_include_directories(
|
|||||||
mlx PRIVATE
|
mlx PRIVATE
|
||||||
$<BUILD_INTERFACE:${gguflib_SOURCE_DIR}>
|
$<BUILD_INTERFACE:${gguflib_SOURCE_DIR}>
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(
|
add_library(
|
||||||
gguflib STATIC
|
gguflib STATIC
|
||||||
${gguflib_SOURCE_DIR}/fp16.c
|
${gguflib_SOURCE_DIR}/fp16.c
|
||||||
${gguflib_SOURCE_DIR}/gguflib.c)
|
${gguflib_SOURCE_DIR}/gguflib.c)
|
||||||
target_link_libraries(mlx $<BUILD_INTERFACE:gguflib>)
|
target_link_libraries(mlx $<BUILD_INTERFACE:gguflib>)
|
||||||
|
target_sources(
|
||||||
|
mlx
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/gguf.cpp
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/gguf_quants.cpp
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
target_sources(
|
||||||
|
mlx
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/no_gguf.cpp
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|
||||||
#include <mlx/io/gguf.h>
|
#include "mlx/io/gguf.h"
|
||||||
|
#include "mlx/ops.h"
|
||||||
|
|
||||||
namespace mlx::core {
|
namespace mlx::core {
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|
||||||
#include <mlx/io/gguf.h>
|
#include "mlx/io/gguf.h"
|
||||||
|
|
||||||
namespace mlx::core {
|
namespace mlx::core {
|
||||||
|
|
||||||
|
20
mlx/io/no_gguf.cpp
Normal file
20
mlx/io/no_gguf.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright © 2023-2024 Apple Inc.
|
||||||
|
|
||||||
|
#include "mlx/io.h"
|
||||||
|
|
||||||
|
namespace mlx::core {
|
||||||
|
|
||||||
|
GGUFLoad load_gguf(const std::string&, StreamOrDevice s) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"[load_gguf] Compile with MLX_BUILD_GGUF=ON to enable GGUF support.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void save_gguf(
|
||||||
|
std::string,
|
||||||
|
std::unordered_map<std::string, array>,
|
||||||
|
std::unordered_map<std::string, GGUFMetaData>) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"[save_gguf] Compile with MLX_BUILD_GGUF=ON to enable GGUF support.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace mlx::core
|
37
mlx/io/no_safetensors.cpp
Normal file
37
mlx/io/no_safetensors.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright © 2023-2024 Apple Inc.
|
||||||
|
|
||||||
|
#include "mlx/io.h"
|
||||||
|
|
||||||
|
namespace mlx::core {
|
||||||
|
|
||||||
|
SafetensorsLoad load_safetensors(std::shared_ptr<io::Reader>, StreamOrDevice) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"[load_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
|
||||||
|
"to enable safetensors support.");
|
||||||
|
}
|
||||||
|
|
||||||
|
SafetensorsLoad load_safetensors(const std::string&, StreamOrDevice) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"[load_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
|
||||||
|
"to enable safetensors support.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void save_safetensors(
|
||||||
|
std::shared_ptr<io::Writer>,
|
||||||
|
std::unordered_map<std::string, array>,
|
||||||
|
std::unordered_map<std::string, std::string>) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"[save_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
|
||||||
|
"to enable safetensors support.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void save_safetensors(
|
||||||
|
std::string file,
|
||||||
|
std::unordered_map<std::string, array>,
|
||||||
|
std::unordered_map<std::string, std::string>) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"[save_safetensors] Compile with MLX_BUILD_SAFETENSORS=ON "
|
||||||
|
"to enable safetensors support.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace mlx::core
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include "mlx/io.h"
|
#include "mlx/io.h"
|
||||||
#include "mlx/io/load.h"
|
#include "mlx/io/load.h"
|
||||||
|
#include "mlx/ops.h"
|
||||||
#include "mlx/primitives.h"
|
#include "mlx/primitives.h"
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
@ -149,7 +150,6 @@ SafetensorsLoad load_safetensors(const std::string& file, StreamOrDevice s) {
|
|||||||
return load_safetensors(std::make_shared<io::FileReader>(file), s);
|
return load_safetensors(std::make_shared<io::FileReader>(file), s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Save array to out stream in .npy format */
|
|
||||||
void save_safetensors(
|
void save_safetensors(
|
||||||
std::shared_ptr<io::Writer> out_stream,
|
std::shared_ptr<io::Writer> out_stream,
|
||||||
std::unordered_map<std::string, array> a,
|
std::unordered_map<std::string, array> a,
|
Loading…
Reference in New Issue
Block a user