Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,56 @@ jobs:
name: katago-macos-opencl
path: cpp/katago

build-macos-coreml:
runs-on: macos-latest
permissions:
contents: read

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install dependencies
run: |
brew install ninja zlib libzip
brew tap chinchangyang/katagocoreml-cpp
brew install katagocoreml

- name: Cache CMake build
uses: actions/cache@v4
with:
path: |
cpp/CMakeCache.txt
cpp/CMakeFiles
cpp/build.ninja
cpp/.ninja_deps
cpp/.ninja_log
key: ${{ runner.os }}-cmake-coreml-${{ hashFiles('**/CMakeLists.txt') }}
restore-keys: |
${{ runner.os }}-cmake-coreml-

- name: Configure CMake
working-directory: cpp
run: |
cmake . -G Ninja -DUSE_BACKEND=COREML -DCMAKE_BUILD_TYPE=Release

- name: Build
working-directory: cpp
run: |
ninja

- name: Run tests
working-directory: cpp
run: |
./katago runtests

- name: Upload artifact
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
uses: actions/upload-artifact@v4
with:
name: katago-macos-coreml
path: cpp/katago

Comment on lines +100 to +149
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to merge macOS build into a single configuration? As I see some build steps are overlapping.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! It can be merged into a single configuration. Would you like to review it? ChinChangYang#9

build-windows:
runs-on: windows-latest
permissions:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ cpp/.ninja_log
cpp/build.ninja
cpp/KataGoSwift.*
cpp/include/KataGoSwift/KataGoSwift-swift.h

# For CoreML Backend
cpp/KataGoCoreML.*
cpp/include/KataGoCoreML/KataGoCoreML-swift.h
20 changes: 16 additions & 4 deletions Compiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ As also mentioned in the instructions below but repeated here for visibility, if
* If using OpenCL, you will want to verify that KataGo is picking up the correct device (e.g. some systems may have both an Intel CPU OpenCL and GPU OpenCL, if KataGo appears to pick the wrong one, you can correct this by specifying `openclGpuToUse` in `configs/gtp_example.cfg`).

## MacOS
* TLDR:
* TLDR (Metal backend - recommended for most users):
```
git clone https://github.com/lightvector/KataGo.git
cd KataGo/cpp
Expand All @@ -127,26 +127,38 @@ As also mentioned in the instructions below but repeated here for visibility, if
cmake -G Ninja -DUSE_BACKEND=METAL -DBUILD_DISTRIBUTED=1
ninja
```
* TLDR (CoreML backend - hybrid CPU+GPU+Neural Engine for maximum throughput):
```
# First, install the katagocoreml library via Homebrew
brew tap chinchangyang/katagocoreml-cpp
brew install katagocoreml

git clone https://github.com/lightvector/KataGo.git
cd KataGo/cpp
cmake -G Ninja -DUSE_BACKEND=COREML -DBUILD_DISTRIBUTED=1
ninja
```
* Requirements
* [Homebrew](https://brew.sh): `/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"`
* CMake with a minimum version of 3.18.2: `brew install cmake`.
* AppleClang and Swift compilers: `xcode-select --install`.
* If using the Metal backend, [Ninja](https://ninja-build.org): `brew install ninja`
* If using the Metal or CoreML backend, [Ninja](https://ninja-build.org): `brew install ninja`
* If using the CoreML backend, katagocoreml library: `brew tap chinchangyang/katagocoreml-cpp && brew install katagocoreml`
* libzip: `brew install libzip`.
* If you want to do self-play training and research, probably Google perftools `brew install gperftools` for TCMalloc or some other better malloc implementation. For unknown reasons, the allocation pattern in self-play with large numbers of threads and parallel games causes a lot of memory fragmentation under glibc malloc that will eventually run your machine out of memory, but better mallocs handle it fine.
* If compiling to contribute to public distributed training runs, OpenSSL is required (`brew install openssl`).
* Clone this repo:
* `git clone https://github.com/lightvector/KataGo.git`
* Compile using CMake and make in the cpp directory:
* `cd KataGo/cpp`
* `cmake . -G Ninja -DUSE_BACKEND=METAL` or `cmake . -DUSE_BACKEND=OPENCL` or `cmake . -DUSE_BACKEND=EIGEN` depending on which backend you want.
* `cmake . -G Ninja -DUSE_BACKEND=METAL` or `cmake . -G Ninja -DUSE_BACKEND=COREML` or `cmake . -DUSE_BACKEND=OPENCL` or `cmake . -DUSE_BACKEND=EIGEN` depending on which backend you want.
* Specify also `-DUSE_TCMALLOC=1` if using TCMalloc.
* Compiling will also call git commands to embed the git hash into the compiled executable, specify also `-DNO_GIT_REVISION=1` to disable it if this is causing issues for you.
* Specify `-DUSE_AVX2=1` to also compile Eigen with AVX2 and FMA support, which will make it incompatible with old CPUs but much faster. Intel-based Macs with new processors support AVX2, but Apple Silicon Macs do not support AVX2 natively. (If you want to go further, you can also add `-DCMAKE_CXX_FLAGS='-march=native'` which will specialize to precisely your machine's CPU, but the exe might not run on other machines at all).
* Specify `-DBUILD_DISTRIBUTED=1` to compile with support for contributing data to public distributed training runs.
* If building distributed, you will also need to build with Git revision support, including building within a clone of the repo, as opposed to merely an unzipped copy of its source.
* Only builds from specific tagged versions or branches can contribute, in particular, instead of the `master` branch, use either the latest [release](https://github.com/lightvector/KataGo/releases) tag or the tip of the `stable` branch. To minimize the chance of any data incompatibilities or bugs, please do NOT attempt to contribute with custom changes or circumvent these limitations.
* `ninja` for Metal backend, or `make` for other backends.
* `ninja` for Metal or CoreML backend, or `make` for other backends.
* Done! You should now have a compiled `katago` executable in your working directory.
* Pre-trained neural nets are available at [the main training website](https://katagotraining.org/).
* You will probably want to edit `configs/gtp_example.cfg` (see "Tuning for Performance" above).
Expand Down
51 changes: 47 additions & 4 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.18.2)
if(USE_BACKEND STREQUAL "METAL")
if(USE_BACKEND STREQUAL "METAL" OR USE_BACKEND STREQUAL "COREML")
project(katago LANGUAGES CXX Swift)
else()
project(katago)
Expand Down Expand Up @@ -32,7 +32,7 @@ endif()
set(BUILD_DISTRIBUTED 0 CACHE BOOL "Build with http support for contributing to distributed training")
set(USE_BACKEND CACHE STRING "Neural net backend")
string(TOUPPER "${USE_BACKEND}" USE_BACKEND)
set_property(CACHE USE_BACKEND PROPERTY STRINGS "" CUDA TENSORRT OPENCL EIGEN)
set_property(CACHE USE_BACKEND PROPERTY STRINGS "" CUDA TENSORRT OPENCL EIGEN METAL COREML)

set(USE_TCMALLOC 0 CACHE BOOL "Use TCMalloc")
set(NO_GIT_REVISION 0 CACHE BOOL "Disable embedding the git revision into the compiled exe")
Expand Down Expand Up @@ -118,12 +118,42 @@ elseif(USE_BACKEND STREQUAL "METAL")
neuralnet/metalbackend.swift)
_swift_generate_cxx_header(
KataGoSwift
"${CMAKE_CURRENT_BINARY_DIR}/include/KataGoSwift/KataGoSwift-swift.h"
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/neuralnet/metalbackend.swift")
"${CMAKE_CURRENT_BINARY_DIR}/include/KataGoSwift/KataGoSwift-swift.h")
target_include_directories(KataGoSwift PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")
set_target_properties(KataGoSwift PROPERTIES Swift_MODULE_NAME "KataGoSwift")
target_compile_options(KataGoSwift PUBLIC
"$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
elseif(USE_BACKEND STREQUAL "COREML")
message(STATUS "-DUSE_BACKEND=COREML, using Core ML backend.")
if(NOT "${CMAKE_GENERATOR}" STREQUAL "Ninja")
message(FATAL_ERROR "Bidirectional C++ Interop requires Ninja generator. Have ${CMAKE_GENERATOR}")
endif()
if("${CMAKE_Swift_COMPILER_VERSION}" VERSION_LESS 5.9)
message(FATAL_ERROR "Bidirectional C++ Interop requires Swift 5.9 or greater. Have ${CMAKE_Swift_COMPILER_VERSION}")
endif()
if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
message(FATAL_ERROR "Project requires building with AppleClang. Have ${CMAKE_CXX_COMPILER_ID}")
endif()
# Find katagocoreml library for native Core ML model conversion using pkg-config
find_package(PkgConfig REQUIRED)
pkg_check_modules(KATAGOCOREML REQUIRED katagocoreml)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/external/macos/cmake/modules")
include(InitializeSwift)
include(AddSwift)
set(CMAKE_OSX_DEPLOYMENT_TARGET 13.0)
set(NEURALNET_BACKEND_SOURCES
neuralnet/coremlbackend.cpp
)
add_library(KataGoCoreML STATIC
neuralnet/coremlbackend.swift
neuralnet/mpsgraphlayers.swift)
_swift_generate_cxx_header(
KataGoCoreML
"${CMAKE_CURRENT_BINARY_DIR}/include/KataGoCoreML/KataGoCoreML-swift.h")
target_include_directories(KataGoCoreML PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")
set_target_properties(KataGoCoreML PROPERTIES Swift_MODULE_NAME "KataGoCoreML")
target_compile_options(KataGoCoreML PUBLIC
"$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
elseif(USE_BACKEND STREQUAL "OPENCL")
message(STATUS "-DUSE_BACKEND=OPENCL, using OpenCL backend.")
set(NEURALNET_BACKEND_SOURCES
Expand Down Expand Up @@ -403,6 +433,19 @@ elseif(USE_BACKEND STREQUAL "METAL")
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
message(WARNING "You are currently running cmake on an Intel-based processor. It is known that running KataGo in this configuration may encounter performance issues. It is recommended to switch to a cmake version designed for ARM64 architecture for optimal performance.")
endif()
elseif(USE_BACKEND STREQUAL "COREML")
target_compile_definitions(katago PRIVATE USE_COREML_BACKEND)
target_include_directories(katago PRIVATE ${KATAGOCOREML_INCLUDE_DIRS})
# Link katagocoreml with full path and add all required library directories
find_library(KATAGOCOREML_LIB katagocoreml HINTS /usr/local/lib REQUIRED)
target_link_directories(katago PRIVATE ${KATAGOCOREML_LIBRARY_DIRS})
# Link MetalPerformanceShadersGraph for hybrid MPSGraph + CoreML backend
target_link_libraries(katago KataGoCoreML ${KATAGOCOREML_LIB} ${KATAGOCOREML_LDFLAGS}
"-framework MetalPerformanceShaders"
"-framework MetalPerformanceShadersGraph")
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
message(WARNING "You are currently running cmake on an Intel-based processor. Core ML backend may not work optimally. ARM64 architecture is recommended.")
endif()
elseif(USE_BACKEND STREQUAL "OPENCL")
target_compile_definitions(katago PRIVATE USE_OPENCL_BACKEND)
find_package(OpenCL)
Expand Down
3 changes: 3 additions & 0 deletions cpp/command/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ int MainCmds::benchmark(const vector<string>& args) {
#ifdef USE_METAL_BACKEND
cout << "You are currently using the Metal version of KataGo." << endl;
#endif
#ifdef USE_COREML_BACKEND
cout << "You are currently using the Core ML version of KataGo." << endl;
#endif
#ifdef USE_OPENCL_BACKEND
cout << "You are currently using the OpenCL version of KataGo." << endl;
cout << "If you have a strong GPU capable of FP16 tensor cores (e.g. RTX2080), "
Expand Down
4 changes: 4 additions & 0 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ string Version::getKataGoVersionFullInfo() {
out << "Using TensorRT backend" << endl;
#elif defined(USE_METAL_BACKEND)
out << "Using Metal backend" << endl;
#elif defined(USE_COREML_BACKEND)
out << "Using Core ML backend" << endl;
#elif defined(USE_OPENCL_BACKEND)
out << "Using OpenCL backend" << endl;
#elif defined(USE_EIGEN_BACKEND)
Expand Down Expand Up @@ -280,6 +282,8 @@ string Version::getGitRevisionWithBackend() {
s += "-trt";
#elif defined(USE_METAL_BACKEND)
s += "-metal";
#elif defined(USE_COREML_BACKEND)
s += "-coreml";
#elif defined(USE_OPENCL_BACKEND)
s += "-opencl";
#elif defined(USE_EIGEN_BACKEND)
Expand Down
Loading
Loading