From 994f39421df14d2e6cfbb4a1444014dccdf15c1e Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 4 Aug 2025 19:46:55 +0200 Subject: [PATCH] added support for mac | bench 24146828 --- Makefile | 44 ++++++++++++++++++++++++++++++++++++-------- source/nnue.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 0f436e5..4f1b6e5 100644 --- a/Makefile +++ b/Makefile @@ -1,42 +1,70 @@ # Define directories SRC_DIR := source OBJ_DIR := obj +FMT_DIR := external/fmt CXX := clang++ EXE ?= Eleanor EVALFILE := ./nnue.bin +UNAME_S := $(shell uname -s) +ARCH := $(shell uname -m) + ifeq ($(OS),Windows_NT) EXE_EXT := .exe MKDIR := mkdir RM := rmdir /s /q DEL := del /q + PLATFORM := windows else EXE_EXT := MKDIR := mkdir -p RM := rm -rf DEL := rm -f + ifeq ($(UNAME_S),Darwin) + PLATFORM := mac + else + PLATFORM := unix + endif endif +CXXFLAGS := -std=c++20 -O3 -flto +LDFLAGS := -O3 -flto + +ifeq ($(PLATFORM),mac) + ifeq ($(ARCH),arm64) + ARCH_FLAGS := + else + ARCH_FLAGS := -march=native + endif +else + ARCH_FLAGS := -march=native + LDFLAGS += -fuse-ld=lld -static +endif + +CXXFLAGS += $(ARCH_FLAGS) +LDFLAGS += $(ARCH_FLAGS) + MAKEFLAGS += -j -# Source and object files SRCS := $(wildcard $(SRC_DIR)/*.cpp) OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS)) -# Final binary -$(EXE)$(EXE_EXT): $(OBJS) - $(CXX) -DEVALFILE=\"$(EVALFILE)\" $^ ./external/fmt/format.cc -o $@ -O3 -fuse-ld=lld -march=native -flto -static +FMT_SRC := $(FMT_DIR)/format.cc +FMT_OBJ := $(OBJ_DIR)/format.o + +$(EXE)$(EXE_EXT): $(OBJS) $(FMT_OBJ) + $(CXX) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS) -# Compile .cpp -> .o $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR) - $(CXX) -std=c++20 -c -o $@ $< -O3 -march=native -flto -static + $(CXX) $(CXXFLAGS) -c -o $@ $< + +$(FMT_OBJ): $(FMT_SRC) | $(OBJ_DIR) + $(CXX) $(CXXFLAGS) -c -o $@ $< -# Create object directory $(OBJ_DIR): $(MKDIR) $(OBJ_DIR) -# Clean up and rebuild .PHONY: clean clean: ifeq ($(OS),Windows_NT) diff --git a/source/nnue.cpp b/source/nnue.cpp index 2d62538..25f1542 100644 --- a/source/nnue.cpp +++ b/source/nnue.cpp @@ -35,7 +35,7 @@ void Network::Load(const std::string& path) { // Output biases for (size_t i = 0; i < output_bias.size(); i++) { file.read(reinterpret_cast(&output_bias[i]), sizeof(int16_t)); - } + } } #if defined(__x86_64__) || defined(__amd64__) || (defined(_WIN64) && (defined(_M_X64) || defined(_M_AMD64))) @@ -74,7 +74,6 @@ using nativeVector = __m256i; } #else #pragma message("Using SSE NNUE inference") -// Assumes SSE support here using nativeVector = __m128i; #define set1_epi16 _mm_set1_epi16 #define load_epi16 _mm_load_si128 @@ -93,6 +92,47 @@ using nativeVector = __m128i; } #endif +#elif defined(__aarch64__) +#pragma message("Using ARM64 (NEON) NNUE inference") +#include +using nativeVector = int16x8_t; +static inline nativeVector set1_epi16(int16_t v) { + return vdupq_n_s16(v); +} +static inline nativeVector load_epi16(const nativeVector* ptr) { + return *ptr; +} +static inline nativeVector min_epi16(nativeVector a, nativeVector b) { + return vminq_s16(a, b); +} +static inline nativeVector max_epi16(nativeVector a, nativeVector b) { + return vmaxq_s16(a, b); +} +static inline int32x4_t madd_epi16(nativeVector a, nativeVector b) { + int32x4_t lo = vmull_s16(vget_low_s16(a), vget_low_s16(b)); + int32x4_t hi = vmull_s16(vget_high_s16(a), vget_high_s16(b)); + int32x4_t sum_lo = vpaddq_s32(lo, lo); + int32x4_t sum_hi = vpaddq_s32(hi, hi); + return vcombine_s32(vget_low_s32(sum_lo), vget_low_s32(sum_hi)); +} +static inline nativeVector mullo_epi16(nativeVector a, nativeVector b) { + return vmulq_s16(a, b); +} +static inline int32x4_t add_epi32(int32x4_t a, int32x4_t b) { + return vaddq_s32(a, b); +} +static inline int reduce_epi32(int32x4_t v) { + int32x2_t pair = vadd_s32(vget_low_s32(v), vget_high_s32(v)); + int32x2_t total = vpadd_s32(pair, pair); + return vget_lane_s32(total, 0); +} + +#else +#pragma message("Using fallback scalar NNUE inference") +using nativeVector = void*; +#endif + + static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t outputBucket) { const size_t VECTOR_SIZE = sizeof(nativeVector) / sizeof(int16_t); static_assert(HL_SIZE % VECTOR_SIZE == 0, "HL size must be divisible by the native register size of your CPU for vectorization to work"); @@ -128,7 +168,6 @@ static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t o return reduce_epi32(accumulator); } -#endif int Forward(const Board& board, const Network& net) { bool stm = board.sideToMove; @@ -145,7 +184,8 @@ int Forward(const Board& board, const Network& net) { eval /= QA; - eval += net.output_bias[outputBucket]; + eval += net.output_bias[outputBucket];git show --summary + return (eval * SCALE) / (QA * QB); }