CoolFace
Modelpublic

cwenzi/neuroflow-cpp

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
1likes
CMakeLists.txt256 linesDownload Raw Back to root
1cmake_minimum_required(VERSION 3.14)2 3option(NEUROFLOW_USE_CUDA "Enable CUDA/cuBLAS backend" OFF)4 5if(NEUROFLOW_USE_CUDA)6    project(neuroflow_core VERSION 1.0.0 LANGUAGES CXX CUDA)7else()8    project(neuroflow_core VERSION 1.0.0 LANGUAGES CXX)9endif()10 11set(CMAKE_CXX_STANDARD 17)12set(CMAKE_CXX_STANDARD_REQUIRED ON)13set(CMAKE_EXPORT_COMPILE_COMMANDS ON)14 15option(NEUROFLOW_BUILD_TESTS "Build unit tests" OFF)16option(NEUROFLOW_BUILD_PYTHON "Build Python bindings" OFF)17option(NEUROFLOW_USE_AVX2 "Enable AVX2 SIMD" ON)18option(NEUROFLOW_USE_BLAS "Use external BLAS (OpenBLAS/MKL) for GEMM" ON)19option(NEUROFLOW_USE_AMP "Enable mixed precision training (FP16/BF16)" OFF)20 21if(MSVC)22    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /GR /EHsc /utf-8")23    if(NEUROFLOW_USE_AVX2)24        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")25        message(STATUS "AVX2 SIMD enabled (MSVC)")26    endif()27    set(CMAKE_CXX_FLAGS_RELEASE "/O2 /DNDEBUG /GL")28    set(CMAKE_CXX_FLAGS_DEBUG "/Od /Zi /DDEBUG")29    find_package(OpenMP)30else()31    find_package(OpenMP REQUIRED)32 33    if(NEUROFLOW_USE_AVX2)34        include(CheckCXXCompilerFlag)35        check_cxx_compiler_flag("-mavx2" COMPILER_SUPPORTS_AVX2)36        if(COMPILER_SUPPORTS_AVX2)37            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx2")38            message(STATUS "AVX2 SIMD enabled")39        else()40            message(STATUS "AVX2 not supported by compiler")41        endif()42    endif()43 44    set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -march=native -mtune=native -funroll-loops -ffast-math -ftree-vectorize")45    set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DDEBUG")46endif()47 48 49add_library(neuroflow_core STATIC50    src/tensor.cpp51    src/model.cpp52    src/weight_io.cpp53    src/tokenizer.cpp54    src/sampling.cpp55    src/causal_lm.cpp56    src/tensor_ops.cpp57    src/generative_model.cpp58    src/rope.cpp59    src/swiglu.cpp60    src/rms_norm.cpp61    src/adamw.cpp62    src/scheduler.cpp63    src/train_lm.cpp64    src/grad_scaler.cpp65    src/sft_train.cpp66    src/dpo_train.cpp67)68 69if(NEUROFLOW_USE_CUDA)70    find_package(CUDAToolkit REQUIRED)71    if(CUDAToolkit_VERSION VERSION_LESS "11.0")72        message(FATAL_ERROR "CUDA >= 11.0 required, found ${CUDAToolkit_VERSION}")73    endif()74    target_compile_definitions(neuroflow_core PUBLIC USE_CUDA)75    target_include_directories(neuroflow_core PUBLIC ${CUDAToolkit_INCLUDE_DIRS})76    target_link_libraries(neuroflow_core PUBLIC CUDA::cudart CUDA::cublas)77    target_sources(neuroflow_core PRIVATE src/cuda_context.cpp)78    set_source_files_properties(src/cuda_context.cpp PROPERTIES LANGUAGE CUDA)79    target_compile_options(neuroflow_core PRIVATE80        $<$<COMPILE_LANGUAGE:CUDA>:--generate-code=arch=compute_80,code=[sm_80,compute_80]>81        $<$<COMPILE_LANGUAGE:CUDA>:--expt-relaxed-constexpr>82        $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>83    )84    set(NEUROFLOW_CUDA_FOUND TRUE)85    message(STATUS "CUDA backend enabled: ${CUDAToolkit_VERSION}")86else()87    set(NEUROFLOW_CUDA_FOUND FALSE)88endif()89 90target_include_directories(neuroflow_core PUBLIC91    ${CMAKE_CURRENT_SOURCE_DIR}/include92    ${CMAKE_CURRENT_SOURCE_DIR}/src93)94 95target_compile_definitions(neuroflow_core PRIVATE96    $<$<CONFIG:DEBUG>:DEBUG>97)98 99if(NEUROFLOW_USE_BLAS)100    find_package(BLAS)101    if(BLAS_FOUND)102        target_compile_definitions(neuroflow_core PUBLIC USE_CBLAS)103        target_link_libraries(neuroflow_core PUBLIC ${BLAS_LIBRARIES})104        if(BLAS_INCLUDE_DIRS)105            target_include_directories(neuroflow_core PUBLIC ${BLAS_INCLUDE_DIRS})106        endif()107        set(NEUROFLOW_BLAS_FOUND TRUE)108        message(STATUS "External BLAS found: ${BLAS_LIBRARIES}")109        message(STATUS "  NOTE: Set OPENBLAS_NUM_THREADS=1 at runtime to avoid thread oversubscription with OpenMP")110    else()111        set(OPENBLAS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/openblas")112        if(EXISTS "${OPENBLAS_DIR}/include/cblas.h" AND EXISTS "${OPENBLAS_DIR}/lib/libscipy_openblas.lib")113            target_compile_definitions(neuroflow_core PUBLIC USE_CBLAS)114            target_include_directories(neuroflow_core PUBLIC "${OPENBLAS_DIR}/include")115            target_link_libraries(neuroflow_core PUBLIC "${OPENBLAS_DIR}/lib/libscipy_openblas.lib")116            set(NEUROFLOW_BLAS_FOUND TRUE)117            message(STATUS "Using bundled OpenBLAS from: ${OPENBLAS_DIR}")118            message(STATUS "  NOTE: Set OPENBLAS_NUM_THREADS=1 at runtime to avoid thread oversubscription with OpenMP")119        else()120            set(NEUROFLOW_BLAS_FOUND FALSE)121            message(STATUS "External BLAS not found, using built-in GEMM")122        endif()123    endif()124else()125    set(NEUROFLOW_BLAS_FOUND FALSE)126endif()127 128if(NEUROFLOW_USE_AMP)129    if(NOT NEUROFLOW_USE_CUDA)130        message(WARNING "NEUROFLOW_USE_AMP requires CUDA, enabling CUDA automatically")131        set(NEUROFLOW_USE_CUDA ON)132    endif()133    target_compile_definitions(neuroflow_core PUBLIC NEUROFLOW_USE_AMP)134    message(STATUS "Mixed precision training (AMP) enabled")135endif()136 137if(OpenMP_CXX_FOUND)138    target_link_libraries(neuroflow_core PUBLIC OpenMP::OpenMP_CXX)139endif()140 141add_executable(neuroflow_train_v2 src/train_v2.cpp)142target_link_libraries(neuroflow_train_v2 PRIVATE neuroflow_core)143if(OpenMP_CXX_FOUND)144    target_link_libraries(neuroflow_train_v2 PRIVATE OpenMP::OpenMP_CXX)145endif()146 147 148add_executable(neuroflow_infer_v2 src/infer_v2.cpp)149target_link_libraries(neuroflow_infer_v2 PRIVATE neuroflow_core)150if(OpenMP_CXX_FOUND)151    target_link_libraries(neuroflow_infer_v2 PRIVATE OpenMP::OpenMP_CXX)152endif()153 154add_executable(neuroflow_sft src/sft_main.cpp)155target_link_libraries(neuroflow_sft PRIVATE neuroflow_core)156if(OpenMP_CXX_FOUND)157    target_link_libraries(neuroflow_sft PRIVATE OpenMP::OpenMP_CXX)158endif()159 160add_executable(neuroflow_dpo src/dpo_main.cpp)161target_link_libraries(neuroflow_dpo PRIVATE neuroflow_core)162if(OpenMP_CXX_FOUND)163    target_link_libraries(neuroflow_dpo PRIVATE OpenMP::OpenMP_CXX)164endif()165 166if(NEUROFLOW_BUILD_TESTS)167    enable_testing()168 169    set(NEUROFLOW_TEST_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/tests)170 171    add_executable(neuroflow_test_lm tests/test_lm_pathway.cpp)172    target_link_libraries(neuroflow_test_lm PRIVATE neuroflow_core)173    target_include_directories(neuroflow_test_lm PRIVATE ${NEUROFLOW_TEST_INCLUDE})174    add_test(NAME lm_pathway_test COMMAND neuroflow_test_lm)175 176    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_tensor.cpp)177        add_executable(neuroflow_tensor_test tests/test_tensor.cpp)178        target_link_libraries(neuroflow_tensor_test PRIVATE neuroflow_core)179        target_include_directories(neuroflow_tensor_test PRIVATE ${NEUROFLOW_TEST_INCLUDE})180        add_test(NAME tensor_test COMMAND neuroflow_tensor_test)181    endif()182 183    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_model.cpp)184        add_executable(neuroflow_model_test tests/test_model.cpp)185        target_link_libraries(neuroflow_model_test PRIVATE neuroflow_core)186        target_include_directories(neuroflow_model_test PRIVATE ${NEUROFLOW_TEST_INCLUDE})187        add_test(NAME model_test COMMAND neuroflow_model_test)188    endif()189 190    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_rope.cpp)191        add_executable(neuroflow_test_rope tests/test_rope.cpp)192        target_link_libraries(neuroflow_test_rope PRIVATE neuroflow_core)193        target_include_directories(neuroflow_test_rope PRIVATE ${NEUROFLOW_TEST_INCLUDE})194        add_test(NAME rope_test COMMAND neuroflow_test_rope)195    endif()196 197    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_swiglu.cpp)198        add_executable(neuroflow_test_swiglu tests/test_swiglu.cpp)199        target_link_libraries(neuroflow_test_swiglu PRIVATE neuroflow_core)200        target_include_directories(neuroflow_test_swiglu PRIVATE ${NEUROFLOW_TEST_INCLUDE})201        add_test(NAME swiglu_test COMMAND neuroflow_test_swiglu)202    endif()203 204    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_rms_norm.cpp)205        add_executable(neuroflow_test_rms_norm tests/test_rms_norm.cpp)206        target_link_libraries(neuroflow_test_rms_norm PRIVATE neuroflow_core)207        target_include_directories(neuroflow_test_rms_norm PRIVATE ${NEUROFLOW_TEST_INCLUDE})208        add_test(NAME rms_norm_test COMMAND neuroflow_test_rms_norm)209    endif()210 211    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_adamw.cpp)212        add_executable(neuroflow_test_adamw tests/test_adamw.cpp)213        target_link_libraries(neuroflow_test_adamw PRIVATE neuroflow_core)214        target_include_directories(neuroflow_test_adamw PRIVATE ${NEUROFLOW_TEST_INCLUDE})215        add_test(NAME adamw_test COMMAND neuroflow_test_adamw)216    endif()217 218    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_scheduler.cpp)219        add_executable(neuroflow_test_scheduler tests/test_scheduler.cpp)220        target_link_libraries(neuroflow_test_scheduler PRIVATE neuroflow_core)221        target_include_directories(neuroflow_test_scheduler PRIVATE ${NEUROFLOW_TEST_INCLUDE})222        add_test(NAME scheduler_test COMMAND neuroflow_test_scheduler)223    endif()224 225    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_gqa.cpp)226        add_executable(neuroflow_test_gqa tests/test_gqa.cpp)227        target_link_libraries(neuroflow_test_gqa PRIVATE neuroflow_core)228        target_include_directories(neuroflow_test_gqa PRIVATE ${NEUROFLOW_TEST_INCLUDE})229        add_test(NAME gqa_test COMMAND neuroflow_test_gqa)230    endif()231 232    message(STATUS "Unit tests enabled (built-in test framework)")233endif()234 235install(TARGETS neuroflow_core236    ARCHIVE DESTINATION lib237    LIBRARY DESTINATION lib238)239 240install(DIRECTORY include/neuroflow241    DESTINATION include242)243 244message(STATUS "")245message(STATUS "NeuroFlow Core Build Configuration:")246message(STATUS "  Version:        ${PROJECT_VERSION}")247message(STATUS "  C++ Standard:   ${CMAKE_CXX_STANDARD}")248message(STATUS "  Build Type:     ${CMAKE_BUILD_TYPE}")249message(STATUS "  AVX2:           ${NEUROFLOW_USE_AVX2}")250message(STATUS "  AMP:            ${NEUROFLOW_USE_AMP}")251message(STATUS "  Tests:          ${NEUROFLOW_BUILD_TESTS}")252message(STATUS "  Python:         ${NEUROFLOW_BUILD_PYTHON}")253message(STATUS "  OpenMP:         ${OpenMP_CXX_FOUND}")254message(STATUS "  BLAS:           ${NEUROFLOW_BLAS_FOUND}")255message(STATUS "  CUDA:           ${NEUROFLOW_CUDA_FOUND}")256