okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
805e11c3d
main
1# tools/vision - strata-vision, the image encoder for Strata's multimodal path (optional; setup.py builds it when 2# the vision encoder is chosen). Uses llama.cpp's mtmd library at the same pinned commit as the engine's ggml, from the vendored 3# third_party/strata-llama (own-stack phase 1B; phase 3 replaces this program with a native vision tower). 4# 5# cmake -S tools/vision -B build-vision -G Ninja -DCMAKE_BUILD_TYPE=Release [-DLLAMA_DIR=<a llama.cpp checkout: an override, kept for two releases>] 6# [-DSTRATA_VISION_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=native] [-DSTRATA_PORTABLE=OFF] 7# [-DSTRATA_VISION_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx (oneAPI sourced; Linux)] 8# [-DSTRATA_VISION_HIP=ON -DSTRATA_VISION_HIP_ARCHS="gfx1200;gfx1201" + the HIP compiler the engine build uses] 9# cmake --build build-vision --target strata-vision 10# 11# Portable (AVX2 baseline) is the default, so an encoder built without extra flags runs on any AVX2 PC: the 0.1.32 12# release's encoder was built native on an AVX-512 machine and stopped with an illegal instruction elsewhere 13# (#411 #412 #419). setup.py, which builds for the PC it runs on, passes -DSTRATA_PORTABLE=OFF. The option is 14# kept in CMakeCache.txt, so a build folder configured before this default still needs the flag (or a fresh folder). 15cmake_minimum_required ( VERSION 3.21 ) 16project ( strata_vision C CXX ) 17 18set ( LLAMA_DIR " ${ CMAKE_CURRENT_SOURCE_DIR } /../../third_party/strata-llama" CACHE PATH "llama.cpp tree: the vendored third_party/strata-llama (llama, mtmd and ggml at the pin, nothing is downloaded), or a llama.cpp checkout" ) 19option ( STRATA_VISION_CUDA "run the vision encoder on the GPU (builds ggml-cuda)" OFF ) 20option ( STRATA_VISION_HIP "run the vision encoder on an AMD GPU (builds ggml-hip); the program is named strata-vision-hip" OFF ) 21option ( STRATA_VISION_SYCL "run the vision encoder on an Intel GPU (builds ggml-sycl, needs the oneAPI icx/icpx compilers); the program is named strata-vision-sycl" OFF ) 22set ( STRATA_VISION_HIP_ARCHS "" CACHE STRING "the gfx architectures strata-vision-hip is compiled for (';'-separated)" ) 23if ( STRATA_VISION_HIPAND STRATA_VISION_CUDA) 24message ( FATAL_ERROR "strata-vision: STRATA_VISION_CUDA and STRATA_VISION_HIP are two different programs" ) 25endif () 26if ( STRATA_VISION_SYCLAND ( STRATA_VISION_CUDAOR STRATA_VISION_HIP)) 27message ( FATAL_ERROR "strata-vision: STRATA_VISION_SYCL is its own program: not together with CUDA or HIP" ) 28endif () 29if ( STRATA_VISION_HIPAND NOT STRATA_VISION_HIP_ARCHS) 30message ( FATAL_ERROR "strata-vision: STRATA_VISION_HIP needs STRATA_VISION_HIP_ARCHS, for example \"gfx1200;gfx1201\"" ) 31endif () 32option ( STRATA_VISION_METAL "a Mac: ggml's Metal backend in the program, so --gpu reads pictures on the Mac's GPU (OFF: CPU only)" ON ) 33option ( STRATA_PORTABLE "build for other PCs: AVX2 baseline, static MSVC runtime" ON ) 34if ( STRATA_PORTABLEAND MSVC) 35set ( CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" ) 36endif () 37 38set ( BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE ) 39set ( LLAMA_BUILD_COMMON OFF CACHE BOOL "" FORCE ) 40set ( LLAMA_BUILD_TOOLS OFF CACHE BOOL "" FORCE ) 41set ( LLAMA_BUILD_TESTS OFF CACHE BOOL "" FORCE ) 42set ( LLAMA_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE ) 43set ( LLAMA_BUILD_SERVER OFF CACHE BOOL "" FORCE ) 44set ( LLAMA_BUILD_APP OFF CACHE BOOL "" FORCE ) 45set ( LLAMA_BUILD_MTMD ON CACHE BOOL "" FORCE ) 46set ( LLAMA_OPENSSL OFF CACHE BOOL "" FORCE ) 47set ( LLAMA_CURL OFF CACHE BOOL "" FORCE ) 48if ( STRATA_PORTABLE) 49set ( GGML_NATIVE OFF CACHE BOOL "" FORCE ) 50set ( GGML_OPENMP OFF CACHE BOOL "" FORCE ) # no vcomp140.dll / libgomp needed on the user's PC 51foreach ( _f GGML_AVX GGML_AVX2 GGML_FMA GGML_F16C GGML_BMI2) 52set ( $ { _f } ON CACHE BOOL "" FORCE ) 53endforeach () 54# nothing above AVX2 either, even when a -D asks for it (a 9900K or a Zen 3 has neither AVX-512 nor AVX-VNNI) 55foreach ( _f GGML_AVX512 GGML_AVX512_VBMI GGML_AVX512_VNNI GGML_AVX512_BF16 GGML_AVX_VNNI 56 GGML_AMX_TILE GGML_AMX_INT8 GGML_AMX_BF16) 57set ( $ { _f } OFF CACHE BOOL "" FORCE ) 58endforeach () 59message ( STATUS "strata-vision: portable build (AVX2 baseline, runs on other PCs)" ) 60else () 61# this PC's instruction set; a build folder that was portable before gets a fresh native build's settings back 62set ( GGML_NATIVE ON CACHE BOOL "" FORCE ) 63set ( GGML_OPENMP ON CACHE BOOL "" FORCE ) 64foreach ( _f GGML_AVX GGML_AVX2 GGML_FMA GGML_F16C GGML_BMI2) 65set ( $ { _f } OFF CACHE BOOL "" FORCE ) 66endforeach () 67message ( STATUS "strata-vision: native build for this CPU (STRATA_PORTABLE=OFF) - not for other PCs" ) 68endif () 69# An older-CPU test build (the engine's STRATA_ISA_FLOOR, CMakeLists.txt at the top): ggml-cpu, and the own tower's reference, for that floor instead of this 70# machine's, so a newer box builds the program a CPU without AVX2 runs (with -DSTRATA_PORTABLE=OFF). none = SSE4.2 (Nehalem / Westmere), avx = Sandy Bridge 71# (AVX, no F16C / FMA), avx_f16c = Ivy Bridge. Empty (the default) changes nothing; on such a CPU setup's native build is this build. 72set ( STRATA_ISA_FLOOR "" CACHE STRING "older-CPU test build: empty (default), none (SSE4.2), avx or avx_f16c" ) 73set_property ( CACHE STRATA_ISA_FLOOR PROPERTY STRINGS "" none avx avx_f16c ) 74if ( STRATA_ISA_FLOOR) 75if ( NOT STRATA_ISA_FLOORMATCHES "^(none|avx|avx_f16c) $ " ) 76message ( FATAL_ERROR "STRATA_ISA_FLOOR must be empty, none, avx or avx_f16c (got ' ${ STRATA_ISA_FLOOR } ')" ) 77endif () 78if ( STRATA_PORTABLE) 79message ( FATAL_ERROR "STRATA_ISA_FLOOR (an older-CPU build) needs -DSTRATA_PORTABLE=OFF" ) 80endif () 81set ( GGML_NATIVE OFF CACHE BOOL "" FORCE ) 82set ( GGML_CPU_ALL_VARIANTS OFF CACHE BOOL "" FORCE ) 83set ( GGML_SSE42 ON CACHE BOOL "" FORCE ) 84foreach ( _f GGML_AVX GGML_F16C GGML_AVX2 GGML_FMA GGML_BMI2 GGML_AVX_VNNI GGML_AVX512 GGML_AVX512_VBMI GGML_AVX512_VNNI 85 GGML_AVX512_BF16 GGML_AMX_TILE GGML_AMX_INT8 GGML_AMX_BF16) 86set ( $ { _f } OFF CACHE BOOL "" FORCE ) 87endforeach () 88if ( STRATA_ISA_FLOORMATCHES "^avx" ) 89set ( GGML_AVX ON CACHE BOOL "" FORCE ) 90endif () 91if ( STRATA_ISA_FLOORSTREQUAL "avx_f16c" ) 92set ( GGML_F16C ON CACHE BOOL "" FORCE ) 93endif () 94message ( STATUS "strata-vision: ggml-cpu for the floor ${ STRATA_ISA_FLOOR } (an older-CPU test build)" ) 95endif () 96set ( GGML_BACKEND_DL OFF CACHE BOOL "" FORCE ) 97set ( GGML_CUDA ${ STRATA_VISION_CUDA } CACHE BOOL "" FORCE ) 98set ( GGML_HIP ${ STRATA_VISION_HIP } CACHE BOOL "" FORCE ) 99if ( APPLE) 100# One program for both readers: strata-vision --gpu runs the encoder on the Metal GPU, without --gpu on the CPU (Apple Silicon always 101# has the Metal framework, the engine needs it too). The Metal shaders are embedded in the binary and compiled by macOS when the 102# reader starts, so nothing but the program has to be shipped. No -march flag: the default target (an M1) runs on every Apple 103# Silicon Mac, and ggml's CPU path has no BF16 vector kernel on ARM (measured on an M4: the same ~50 s for a 260-token picture with 104# -march=armv8.6-a+bf16+i8mm as without), so a faster target would only risk the M1. 105set ( GGML_METAL ${ STRATA_VISION_METAL } CACHE BOOL "" FORCE ) 106set ( GGML_METAL_EMBED_LIBRARY ${ STRATA_VISION_METAL } CACHE BOOL "" FORCE ) 107endif () 108set ( GGML_SYCL ${ STRATA_VISION_SYCL } CACHE BOOL "" FORCE ) 109if ( STRATA_VISION_SYCL) 110set ( GGML_SYCL_TARGET "INTEL" CACHE STRING "" FORCE ) 111endif () 112if ( STRATA_VISION_HIP) 113# the engine's HIP build says the same (cmake/hip_backend.cmake); ggml-hip reads AMDGPU_TARGETS / CMAKE_HIP_ARCHITECTURES 114set ( AMDGPU_TARGETS " ${ STRATA_VISION_HIP_ARCHS } " CACHE STRING "" FORCE ) 115set ( GPU_TARGETS " ${ STRATA_VISION_HIP_ARCHS } " CACHE STRING "" FORCE ) 116set ( CMAKE_HIP_ARCHITECTURES " ${ STRATA_VISION_HIP_ARCHS } " CACHE STRING "" FORCE ) 117# #3359: a Linux toolchain links a PIE; ggml-hip's objects must be position independent (the engine's HIP build says the same in 118# cmake/hip_backend.cmake). Only for the HIP reader: the other readers' builds stay as they were. 119set ( CMAKE_POSITION_INDEPENDENT_CODE ON ) 120endif () 121 122add_subdirectory ( $ { LLAMA_DIR } llama EXCLUDE_FROM_ALL ) 123 124add_executable ( strata-vision strata_vision.cpp ) 125if ( MSVC) # #3202: the ANSI code page of the process is UTF-8, so a Cyrillic model folder reaches the reader intact (see ../../CMakeLists.txt) 126target_sources ( strata-vision PRIVATE ${ CMAKE_CURRENT_SOURCE_DIR } /strata-utf8.manifest ) 127endif () 128target_link_libraries ( strata-vision PRIVATE mtmd llama strata_vit_own ) 129target_compile_features ( strata-vision PRIVATE cxx_std_17 ) 130set_target_properties ( strata-vision PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ CMAKE_BINARY_DIR } /bin ) 131if ( STRATA_VISION_HIP) 132# beside strata (and, in a component, the CPU reader "strata-vision"): the GPU reader is its own program, so a PC where 133# the HIP runtime does not load still has the CPU one to fall back on 134set_target_properties ( strata-vision PROPERTIES OUTPUT_NAME strata-vision-hip ) 135string ( REPLACE ";" "," _hip_archs " ${ STRATA_VISION_HIP_ARCHS } " ) 136target_compile_definitions ( strata-vision PRIVATE STRATA_VISION_HIP STRATA_VISION_HIP_ARCHS= " ${ _hip_archs } " ) 137endif () 138if ( APPLEAND STRATA_VISION_METAL) 139target_compile_definitions ( strata-vision PRIVATE STRATA_VISION_METAL ) 140endif () 141if ( STRATA_VISION_SYCL) 142# the Intel GPU reader is its own program too, beside the CPU reader "strata-vision" it falls back on 143set_target_properties ( strata-vision PROPERTIES OUTPUT_NAME strata-vision-sycl ) 144target_compile_definitions ( strata-vision PRIVATE STRATA_VISION_SYCL ) 145endif () 146if ( WIN32AND STRATA_VISION_HIP) 147# hipblas.dll (and rocblas.dll behind it) live in rocm\bin of the engine package: loaded on first use, after main() has put that folder on 148# the DLL search path, so the reader starts from any PATH and says "no GPU" itself where there is none 149target_link_libraries ( strata-vision PRIVATE delayimp ) 150target_link_options ( strata-vision PRIVATE -Wl,/delayload:hipblas.dll ) 151endif () 152if ( UNIXAND NOT APPLE) 153# the libraries it needs are the ones in its own folder (a component folder is relocatable): RUNPATH $ORIGIN 154set_target_properties ( strata-vision PROPERTIES BUILD_WITH_INSTALL_RPATH ON INSTALL_RPATH " $ ORIGIN" ) 155target_link_options ( strata-vision PRIVATE -Wl,--enable-new-dtags ) 156endif () 157if ( STRATA_VISION_SYCLAND UNIXAND NOT APPLE) 158# libsycl and the OpenMP runtime live in the oneAPI compiler's lib folder, which only a sourced setvars.sh puts on the search path: 159# recorded in the program, it starts from a plain shell (the server starts it with the PC's own environment) 160get_filename_component ( _icpx_bin " ${ CMAKE_CXX_COMPILER } " REALPATH ) 161get_filename_component ( _icpx_bin " ${ _icpx_bin } " DIRECTORY ) 162get_filename_component ( _icpx_lib " ${ _icpx_bin } /../lib" ABSOLUTE ) 163set_target_properties ( strata-vision PROPERTIES INSTALL_RPATH " $ ORIGIN; ${ _icpx_lib } " ) 164endif () 165 166# our own Qwen vision tower (own-stack phase 3): tools/vision/own, no ggml or llama.cpp inside 167add_subdirectory ( own ) 168 169# mtmd's side of the picture-preparation check (see own/oracle_prep.cpp): the same preprocessor class mtmd_tokenize uses 170add_executable ( strata-vision-prep-oracle EXCLUDE_FROM_ALL own/oracle_prep.cpp ) 171target_link_libraries ( strata-vision-prep-oracle PRIVATE mtmd llama ) 172target_include_directories ( strata-vision-prep-oracle PRIVATE ${ LLAMA_DIR } /tools/mtmd ${ LLAMA_DIR } /ggml/include ${ LLAMA_DIR } /include ) 173target_compile_features ( strata-vision-prep-oracle PRIVATE cxx_std_17 ) 174set_target_properties ( strata-vision-prep-oracle PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ CMAKE_BINARY_DIR } /bin ) 175 176# opens a GGUF that has tensor types llama.cpp does not know (a Gyro quantization's TQK6 / TQK7) for its vocabulary only 177add_executable ( strata-vision-gguf-test EXCLUDE_FROM_ALL gguf_meta_test.cpp ) 178target_link_libraries ( strata-vision-gguf-test PRIVATE llama ) 179target_compile_features ( strata-vision-gguf-test PRIVATE cxx_std_17 ) 180set_target_properties ( strata-vision-gguf-test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ CMAKE_BINARY_DIR } /bin ) 181 182# the HIP reader's architecture check (gpu_arch.h), without a GPU 183add_executable ( strata-vision-arch-test EXCLUDE_FROM_ALL gpu_arch_test.cpp ) 184target_compile_features ( strata-vision-arch-test PRIVATE cxx_std_17 ) 185set_target_properties ( strata-vision-arch-test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ CMAKE_BINARY_DIR } /bin )