|
1 | 1 | cmake_minimum_required(VERSION 3.18.2) |
| 2 | + |
| 3 | +# Pre-project MLX setup. KataGo's MLX path enforces CMake 3.27 via the guard |
| 4 | +# below (MLX itself requires only 3.25 - 3.27 is chosen to match |
| 5 | +# cmake_policy(VERSION 3.27)); the global cmake_minimum_required stays at |
| 6 | +# 3.18.2 so non-MLX backends keep building on older CMake. |
| 7 | +# |
| 8 | +# The OSX deployment target is deliberately NOT pinned here. KataGo links |
| 9 | +# Homebrew's prebuilt libmlx.dylib, whose minos reflects the macOS it was |
| 10 | +# bottled on - that dylib, not this build, sets the real minimum macOS. |
| 11 | +# Pinning a lower value only stamps a misleading minos on the executable and |
| 12 | +# triggers a "linking with dylib built for newer version" linker warning; |
| 13 | +# letting CMake default the target to the build host keeps minos honest. |
| 14 | +if(USE_BACKEND STREQUAL "MLX") |
| 15 | + if(CMAKE_VERSION VERSION_LESS 3.27) |
| 16 | + message(FATAL_ERROR "KataGo's USE_BACKEND=MLX path requires CMake 3.27 or newer. You have ${CMAKE_VERSION}. Install via: brew install cmake") |
| 17 | + endif() |
| 18 | + cmake_policy(VERSION 3.27) |
| 19 | +endif() |
| 20 | + |
2 | 21 | if(USE_BACKEND STREQUAL "METAL") |
3 | 22 | project(katago LANGUAGES CXX Swift) |
4 | 23 | else() |
@@ -44,7 +63,7 @@ endif() |
44 | 63 | set(BUILD_DISTRIBUTED 0 CACHE BOOL "Build with http support for contributing to distributed training") |
45 | 64 | set(USE_BACKEND CACHE STRING "Neural net backend") |
46 | 65 | string(TOUPPER "${USE_BACKEND}" USE_BACKEND) |
47 | | -set_property(CACHE USE_BACKEND PROPERTY STRINGS "" CUDA TENSORRT OPENCL EIGEN METAL) |
| 66 | +set_property(CACHE USE_BACKEND PROPERTY STRINGS "" CUDA TENSORRT OPENCL EIGEN MLX METAL) |
48 | 67 |
|
49 | 68 | set(USE_TCMALLOC 0 CACHE BOOL "Use TCMalloc") |
50 | 69 | set(NO_GIT_REVISION 0 CACHE BOOL "Disable embedding the git revision into the compiled exe") |
@@ -158,8 +177,35 @@ elseif(USE_BACKEND STREQUAL "EIGEN") |
158 | 177 | set(NEURALNET_BACKEND_SOURCES |
159 | 178 | neuralnet/eigenbackend.cpp |
160 | 179 | ) |
| 180 | +elseif(USE_BACKEND STREQUAL "MLX") |
| 181 | + message(STATUS "-DUSE_BACKEND=MLX, using MLX backend for Apple Silicon.") |
| 182 | + |
| 183 | + if(NOT APPLE) |
| 184 | + message(FATAL_ERROR "USE_BACKEND=MLX is only supported on macOS. Detected: ${CMAKE_SYSTEM_NAME}") |
| 185 | + endif() |
| 186 | + if(CMAKE_OSX_ARCHITECTURES) |
| 187 | + if(NOT CMAKE_OSX_ARCHITECTURES STREQUAL "arm64") |
| 188 | + message(FATAL_ERROR "USE_BACKEND=MLX requires arm64. Got: ${CMAKE_OSX_ARCHITECTURES}") |
| 189 | + endif() |
| 190 | + elseif(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") |
| 191 | + message(FATAL_ERROR "USE_BACKEND=MLX requires Apple Silicon (arm64). Detected: ${CMAKE_SYSTEM_PROCESSOR}") |
| 192 | + endif() |
| 193 | + |
| 194 | + set(MLX_MIN_VERSION "0.18") |
| 195 | + set(MLX_ROOT "" CACHE PATH "Optional path to MLX's CMake package; leave empty to use CMake's default search (e.g. Homebrew's /opt/homebrew/share/cmake/MLX/)") |
| 196 | + |
| 197 | + # Homebrew installs MLX's CMake config to /opt/homebrew/share/cmake/MLX/, which is |
| 198 | + # on CMake's default search path. MLX_ROOT, when set, is added as an extra hint. |
| 199 | + find_package(MLX ${MLX_MIN_VERSION} CONFIG REQUIRED HINTS "${MLX_ROOT}") |
| 200 | + message(STATUS "Found MLX ${MLX_VERSION} at ${MLX_LIBRARY}") |
| 201 | + |
| 202 | + set(NEURALNET_BACKEND_SOURCES |
| 203 | + neuralnet/mlxbackend.cpp |
| 204 | + neuralnet/mlxwinotuner.cpp |
| 205 | + neuralnet/mlxtests.cpp |
| 206 | + ) |
161 | 207 | elseif(USE_BACKEND STREQUAL "") |
162 | | - message(WARNING "${ColorBoldRed}WARNING: Using dummy neural net backend, intended for non-neural-net testing only, will fail on any code path requiring a neural net. To use neural net, specify -DUSE_BACKEND=CUDA or -DUSE_BACKEND=TENSORRT or -DUSE_BACKEND=OPENCL or -DUSE_BACKEND=EIGEN to compile with the respective backend.${ColorReset}") |
| 208 | + message(WARNING "${ColorBoldRed}WARNING: Using dummy neural net backend, intended for non-neural-net testing only, will fail on any code path requiring a neural net. To use neural net, specify -DUSE_BACKEND=CUDA or -DUSE_BACKEND=TENSORRT or -DUSE_BACKEND=OPENCL or -DUSE_BACKEND=EIGEN or -DUSE_BACKEND=MLX or -DUSE_BACKEND=METAL to compile with the respective backend.${ColorReset}") |
163 | 209 | set(NEURALNET_BACKEND_SOURCES neuralnet/dummybackend.cpp) |
164 | 210 | else() |
165 | 211 | message(FATAL_ERROR "Unrecognized backend: " ${USE_BACKEND}) |
@@ -496,6 +542,9 @@ elseif(USE_BACKEND STREQUAL "EIGEN") |
496 | 542 | message(STATUS "Found Eigen3 at ${EIGEN3_INCLUDE_DIRS}") |
497 | 543 | endif() |
498 | 544 | endif() |
| 545 | +elseif(USE_BACKEND STREQUAL "MLX") |
| 546 | + target_compile_definitions(katago PRIVATE USE_MLX_BACKEND) |
| 547 | + target_link_libraries(katago mlx) |
499 | 548 | endif() |
500 | 549 |
|
501 | 550 | if(USE_BIGGER_BOARDS_EXPENSIVE) |
|
0 commit comments