# CUTLASS GEMM kernels (plan.md Phase 1) -- only built when
# GGML_CUTLASS_FP8 is ON (see top-level CMakeLists.txt). Framework-
# agnostic void*-ABI, no ggml/torch dependency in this library itself.
add_library(csrc_cutlass STATIC
    fp8_gemm.cu
    fp8_gemm.cuh
    fp8_gemm_ggml.cu
    fp8_gemm_ggml.cuh
    activation_quant.cu
    activation_quant.cuh
    group_quant_ref.cu
    group_quant_ref.cuh
    group_gemm.cu
    group_gemm.cuh)

# Public headers (fp8_gemm.cuh / group_gemm.cuh) include <cuda_runtime.h>.
# .cu translation units always get this via nvcc's own default include
# path, so this went unnoticed as long as every consumer was itself a
# .cu file -- but csrc_cutlass/test/*.cpp (the correctness/bench/probe
# binaries) are plain C++ compiled with the HOST compiler, which has no
# built-in notion of the CUDA toolkit's include path at all. Whether
# that host compiler happens to also find <cuda_runtime.h> on its own
# default search path is specific to how each machine's CUDA toolkit
# was installed (a package-manager install that drops headers into
# /usr/include, say) -- not something any platform is guaranteed to do.
# Confirmed missing on a real Jetson/Thor toolchain (build failure);
# this is a genuine portability gap, not a Thor-specific workaround, so
# fix it unconditionally rather than gating it on any platform check.
find_package(CUDAToolkit REQUIRED)
target_include_directories(csrc_cutlass
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CUDAToolkit_INCLUDE_DIRS}
    PRIVATE
        ${CMAKE_SOURCE_DIR}/third_party/cutlass/include
        ${CMAKE_SOURCE_DIR}/third_party/ggml/include)
# CUDA::cudart already carries its own INTERFACE_INCLUDE_DIRECTORIES
# (usually making the explicit CUDAToolkit_INCLUDE_DIRS line above
# redundant) -- kept anyway as a second, independent path to the same
# headers rather than relying on exactly one mechanism.
target_link_libraries(csrc_cutlass PUBLIC CUDA::cudart)

# CUDA_SEPARABLE_COMPILATION (-rdc=true) is deliberately OFF: nothing
# here calls a __device__ function defined in a different .cu file (each
# CUTLASS kernel instantiation is self-contained per translation unit),
# so RDC brings no benefit -- and it actively breaks things: mixing an
# RDC-compiled static library with ggml-cuda.so (compiled without RDC,
# its own separate fatbinary) in one process caused every real kernel
# launch to fail with "invalid resource handle" (can_implement/
# initialize succeeded; only the actual launch failed) -- confirmed via
# a minimal repro that reproduced with ONLY these two libraries linked,
# reproduced even calling the CUTLASS kernel before any ggml-cuda
# function ran, and was NOT fixed by aligning both libraries' CUDA
# runtime version (a real, separate problem also found and fixed
# alongside this one -- see the top-level CMakeLists.txt's
# CUDAToolkit_ROOT note).
set_target_properties(csrc_cutlass PROPERTIES
    CUDA_SEPARABLE_COMPILATION OFF
    POSITION_INDEPENDENT_CODE ON)

target_compile_options(csrc_cutlass PRIVATE
    $<$<COMPILE_LANGUAGE:CUDA>:--expt-relaxed-constexpr>)
