added support for mac | bench 24146828

This commit is contained in:
rektdie committed 2025-08-04 19:46:55 +02:00
1 parent 8e56bd786b
commit 994f39421d
2 files changed
+79 -11

No files matched your search

+36 -8
View File
@@ -1,42 +1,70 @@
# Define directories # Define directories
SRC_DIR := source SRC_DIR := source
OBJ_DIR := obj OBJ_DIR := obj
FMT_DIR := external/fmt
CXX := clang++ CXX := clang++
EXE ?= Eleanor EXE ?= Eleanor
EVALFILE := ./nnue.bin EVALFILE := ./nnue.bin
UNAME_S := $(shell uname -s)
ARCH := $(shell uname -m)
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
EXE_EXT := .exe EXE_EXT := .exe
MKDIR := mkdir MKDIR := mkdir
RM := rmdir /s /q RM := rmdir /s /q
DEL := del /q DEL := del /q
PLATFORM := windows
else else
EXE_EXT := EXE_EXT :=
MKDIR := mkdir -p MKDIR := mkdir -p
RM := rm -rf RM := rm -rf
DEL := rm -f DEL := rm -f
ifeq ($(UNAME_S),Darwin)
PLATFORM := mac
else
PLATFORM := unix
endif
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 MAKEFLAGS += -j
# Source and object files
SRCS := $(wildcard $(SRC_DIR)/*.cpp) SRCS := $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS)) OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
# Final binary FMT_SRC := $(FMT_DIR)/format.cc
$(EXE)$(EXE_EXT): $(OBJS) FMT_OBJ := $(OBJ_DIR)/format.o
$(CXX) -DEVALFILE=\"$(EVALFILE)\" $^ ./external/fmt/format.cc -o $@ -O3 -fuse-ld=lld -march=native -flto -static
$(EXE)$(EXE_EXT): $(OBJS) $(FMT_OBJ)
$(CXX) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS)
# Compile .cpp -> .o
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR) $(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): $(OBJ_DIR):
$(MKDIR) $(OBJ_DIR) $(MKDIR) $(OBJ_DIR)
# Clean up and rebuild
.PHONY: clean .PHONY: clean
clean: clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
+43 -3
View File
@@ -74,7 +74,6 @@ using nativeVector = __m256i;
} }
#else #else
#pragma message("Using SSE NNUE inference") #pragma message("Using SSE NNUE inference")
// Assumes SSE support here
using nativeVector = __m128i; using nativeVector = __m128i;
#define set1_epi16 _mm_set1_epi16 #define set1_epi16 _mm_set1_epi16
#define load_epi16 _mm_load_si128 #define load_epi16 _mm_load_si128
@@ -93,6 +92,47 @@ using nativeVector = __m128i;
} }
#endif #endif
#elif defined(__aarch64__)
#pragma message("Using ARM64 (NEON) NNUE inference")
#include <arm_neon.h>
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) { static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t outputBucket) {
const size_t VECTOR_SIZE = sizeof(nativeVector) / sizeof(int16_t); 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"); 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); return reduce_epi32(accumulator);
} }
#endif
int Forward(const Board& board, const Network& net) { int Forward(const Board& board, const Network& net) {
bool stm = board.sideToMove; bool stm = board.sideToMove;
@@ -145,7 +184,8 @@ int Forward(const Board& board, const Network& net) {
eval /= QA; eval /= QA;
eval += net.output_bias[outputBucket]; eval += net.output_bias[outputBucket];git show --summary
return (eval * SCALE) / (QA * QB); return (eval * SCALE) / (QA * QB);
} }