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)

# Experimental CUTLASS FP8 GEMM path (plan.md Phase 1 + real-model
# dispatch follow-up, issues.md ISSUE-006/007) -- OFF by default, same
# posture as amir_neowise_q8's USE_NEOWISE_Q8: an explicitly opt-in
# addition that leaves the default build untouched. csrc_cutlass is a
# standalone library (framework-agnostic kernel + activation quantizer +
# a thin ggml_tensor-facing wrapper); ggml-cuda.cu links against it and
# adds one dispatch branch for PaliGemma's qkv projection shape (see that
# file's own PI05_GGML-delimited block for the exact trigger condition
# and its known-experimental caveats). Requires the CUDA backend to
# already be enabled (GGML_CUDA=ON).
#
# enable_language(CUDA) is called HERE, at the top level, rather than
# relying on ggml-cuda's own nested call: CMake's CUDA language-rule
# variables (CMAKE_CUDA_COMPILE_OBJECT etc.) only propagate down the
# directory tree from wherever enable_language() actually runs, and
# ggml-cuda's own call lives deep in a sibling subtree (third_party/ggml/
# src/ggml-cuda/), not an ancestor of csrc_cutlass -- so csrc_cutlass
# would otherwise never see them (confirmed: omitting this causes a
# "CMAKE_CUDA_COMPILE_OBJECT not set" generate-step failure).
option(GGML_CUTLASS_FP8 "Enable experimental CUTLASS FP8 GEMM kernels (plan.md Phase 1)" OFF)
if (GGML_CUTLASS_FP8)
    if (NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
        message(FATAL_ERROR "GGML_CUTLASS_FP8 requires -DCMAKE_CUDA_ARCHITECTURES=<arch> to be set explicitly")
    endif()
    enable_language(CUDA)

    # On a machine with more than one CUDA install (e.g. an apt-packaged
    # CUDA 11.x alongside a manually-installed 12.x toolkit -- this
    # project's own dev machine has both), CMake's find_package(CUDAToolkit)
    # (called internally by ggml-cuda's own CMakeLists.txt) is NOT
    # guaranteed to resolve to the same toolkit as CMAKE_CUDA_COMPILER --
    # confirmed on this machine: passing only -DCMAKE_CUDA_COMPILER=.../
    # cuda-12.8/bin/nvcc still let ggml-cuda.so link against the OLDER
    # system libcudart.so.11.0/libcublas.so.11, while csrc_cutlass (built
    # by the pinned 12.8 nvcc) picked up 12.8's cudart -- two different
    # CUDA runtime major versions loaded in one process. Pass
    # -DCUDAToolkit_ROOT=<same path as CMAKE_CUDA_COMPILER's toolkit> to
    # force a consistent resolution; verify with
    # `readelf -d .../libggml-cuda.so | grep NEEDED` if anything about
    # this GEMM path misbehaves at runtime -- it should show
    # libcudart.so.<major matching your compiler>, not an older one.
endif()

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

if (GGML_CUTLASS_FP8)
    if (NOT TARGET ggml-cuda)
        message(FATAL_ERROR "GGML_CUTLASS_FP8 requires GGML_CUDA=ON")
    endif()
    add_subdirectory(csrc_cutlass)
    add_subdirectory(csrc_cutlass/test)
    target_link_libraries(ggml-cuda PRIVATE csrc_cutlass)
    target_compile_definitions(ggml-cuda PRIVATE GGML_CUTLASS_FP8)
    target_include_directories(ggml-cuda PRIVATE
        ${CMAKE_SOURCE_DIR}/third_party/cutlass/include
        ${CMAKE_SOURCE_DIR}/csrc_cutlass)
endif()
