Added mlx::core::version() returning std::string(MLX_VERSION) (#1819)

* Added version.h providing mlx::core::version() returning std::string(MLX_VERSION)

Also, added MLX_VERSION_MAJOR, MLX_VERSION_MINOR, MLX_VERSION_PATCH, MLX_VERSION_NUMERIC, and accompanying functions.

* Added version.h to mlx.h

* Changed version int functions to be constexpr

* Formatting

* Added handling of MLX_VERSION where only the prefix has major.minor.patch format

* Changed version function to be constexpr
This commit is contained in:
Jesper Stemann Andersen 2025-02-20 05:30:19 +01:00 committed by GitHub
parent c707b2b0a6
commit c86422bdd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 9 deletions

View File

@ -1,6 +1,17 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(mlx LANGUAGES C CXX) if(NOT MLX_VERSION)
set(MLX_VERSION 0.23.1)
set(MLX_PROJECT_VERSION ${MLX_VERSION})
else()
string(REGEX REPLACE "^([0-9]+\.[0-9]+\.[0-9]+).*" "\\1" MLX_PROJECT_VERSION
${MLX_VERSION})
endif()
project(
mlx
LANGUAGES C CXX
VERSION ${MLX_PROJECT_VERSION})
# ----------------------------- Setup ----------------------------- # ----------------------------- Setup -----------------------------
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
@ -24,10 +35,17 @@ option(MLX_BUILD_BLAS_FROM_SOURCE "Build OpenBLAS from source code" OFF)
option(MLX_METAL_JIT "Use JIT compilation for Metal kernels" OFF) option(MLX_METAL_JIT "Use JIT compilation for Metal kernels" OFF)
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) math(
set(MLX_VERSION 0.23.1) EXPR
endif() MLX_VERSION_NUMERIC
add_compile_definitions("MLX_VERSION=${MLX_VERSION}") "1000000 * ${PROJECT_VERSION_MAJOR} + 1000 * ${PROJECT_VERSION_MINOR} + ${PROJECT_VERSION_PATCH}"
OUTPUT_FORMAT DECIMAL)
add_compile_definitions(
"MLX_VERSION=${MLX_VERSION}"
"MLX_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}"
"MLX_VERSION_MINOR=${PROJECT_VERSION_MINOR}"
"MLX_VERSION_PATCH=${PROJECT_VERSION_PATCH}"
"MLX_VERSION_NUMERIC=${MLX_VERSION_NUMERIC}")
# --------------------- Processor tests ------------------------- # --------------------- Processor tests -------------------------
message( message(

View File

@ -4,9 +4,7 @@
#include "mlx/fast_primitives.h" #include "mlx/fast_primitives.h"
#include "mlx/primitives.h" #include "mlx/primitives.h"
#include "mlx/utils.h" #include "mlx/utils.h"
#include "mlx/version.h"
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
// clang-format off // clang-format off
#define SERIALIZE_PRIMITIVE(primitive, ...) \ #define SERIALIZE_PRIMITIVE(primitive, ...) \
@ -379,7 +377,7 @@ struct PrimitiveFactory {
}; };
void write_header(Writer& os, int count, bool shapeless) { void write_header(Writer& os, int count, bool shapeless) {
serialize(os, std::string(TOSTRING(MLX_VERSION))); serialize(os, std::string(version()));
serialize(os, count); serialize(os, count);
serialize(os, shapeless); serialize(os, shapeless);
} }

View File

@ -19,3 +19,4 @@
#include "mlx/stream.h" #include "mlx/stream.h"
#include "mlx/transforms.h" #include "mlx/transforms.h"
#include "mlx/utils.h" #include "mlx/utils.h"
#include "mlx/version.h"

32
mlx/version.h Normal file
View File

@ -0,0 +1,32 @@
// Copyright © 2023-2024 Apple Inc.
#pragma once
#include <string>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
namespace mlx::core {
constexpr const char* version() {
return TOSTRING(MLX_VERSION);
}
constexpr int version_major() {
return MLX_VERSION_MAJOR;
}
constexpr int version_minor() {
return MLX_VERSION_MINOR;
}
constexpr int version_patch() {
return MLX_VERSION_PATCH;
}
constexpr int version_numeric() {
return MLX_VERSION_NUMERIC;
}
} // namespace mlx::core