cmake_minimum_required(VERSION 3.14)
project(pi0.5_ggml C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# sentencepiece's own CMakeLists.txt declares cmake_minimum_required below 3.5,
# which recent CMake rejects unless a policy floor is set explicitly.
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)

if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

find_package(Threads REQUIRED)

add_subdirectory(third_party/ggml)
add_subdirectory(third_party/sentencepiece EXCLUDE_FROM_ALL)
add_subdirectory(src)

# ----------------------------------------------------------------------------
# NEOWISE Q8 — out-of-tree INT8 Q8_0 matmul variants injected into ggml-cuda.
#
# OFF by default: with the option off, nothing below runs and the dispatch hook
# in third_party/ggml/src/ggml-cuda/ggml-cuda.cu compiles to nothing (it is all
# inside `#if defined(CUSTOM_KERNEL_VERSION)`), so this branch builds
# byte-identically to master.
#
# Added after src/ so the consumer targets (pi05_core, pi05) already exist.
#
#   git submodule update --init --recursive
#   cmake --preset thor-neowise          # see CMakePresets.json
#
# ...or explicitly:
#   cmake -S . -B build -DGGML_CUDA=ON \
#         -DUSE_NEOWISE_Q8=ON \
#         -DCUSTOM_KERNEL_VERSION=6 \
#         -DCMAKE_CUDA_ARCHITECTURES=110a-real
#
# NOTE: this kernel changes numerical output. See docs/neowise.md before using it
# for robot control.
# ----------------------------------------------------------------------------
option(USE_NEOWISE_Q8 "Inject the NEOWISE Q8 INT8 matmul variants into ggml-cuda" OFF)
set(NEOWISE_Q8_DIR "${CMAKE_CURRENT_LIST_DIR}/third_party/neowise-q8" CACHE PATH
    "Path to the neowise-q8 component (defaults to the submodule)")

if (USE_NEOWISE_Q8)
    if (NOT EXISTS "${NEOWISE_Q8_DIR}/CMakeLists.txt")
        message(FATAL_ERROR
            "USE_NEOWISE_Q8=ON but no CMakeLists.txt at\n  ${NEOWISE_Q8_DIR}\n"
            "The kernel lives in a separate private repository, added here as a "
            "submodule. Fix with:\n"
            "  git submodule update --init --recursive\n"
            "If you do not have access to it, build without -DUSE_NEOWISE_Q8=ON; "
            "everything else in this branch works unchanged.")
    endif()
    set(NEOWISE_Q8_CONSUMER_TARGETS pi05_core pi05)
    add_subdirectory(${NEOWISE_Q8_DIR} neowise-q8)
endif()
