okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d39d20f52
main
1# tools/vision/own - Strata's own Qwen vision tower (own-stack phase 3). No ggml, no llama.cpp: plain C++17 and one CUDA / HIP source. 2# 3# Standalone (quick; this is how the reference, the GPU tower and the unit tests are built on the Linux test boxes): 4# cmake -S tools/vision/own -B build-own -DCMAKE_BUILD_TYPE=Release [-DSTRATA_VIT_OWN_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=86] && cmake --build build-own -j 5# build-own/strata-vision-own --mmproj <mmproj.gguf> [--gpu] build-own/strata-vision-own-test [mmproj.gguf ...] 6# From tools/vision/CMakeLists.txt it is added as a subdirectory, and strata-vision links strata_vit_own (the GPU part follows STRATA_VISION_CUDA). 7cmake_minimum_required ( VERSION 3.21 ) 8if ( CMAKE_SOURCE_DIRSTREQUAL CMAKE_CURRENT_SOURCE_DIR) 9project ( strata_vision_own C CXX ) 10set ( STRATA_VIT_OWN_STANDALONE ON ) 11endif () 12 13option ( STRATA_VIT_OWN_CUDA "the own tower on an NVIDIA GPU (vit_gpu.cu with cuBLAS)" OFF ) 14set ( _vit_cuda OFF ) 15if ( STRATA_VIT_OWN_CUDAOR STRATA_VISION_CUDA) 16set ( _vit_cuda ON ) 17enable_language ( CUDA ) 18find_package ( CUDAToolkit REQUIRED ) 19if ( NOT CMAKE_CUDA_ARCHITECTURES) 20set ( CMAKE_CUDA_ARCHITECTURES native ) 21endif () 22endif () 23 24option ( STRATA_VIT_OWN_HIP "the own tower on an AMD GPU (vit_gpu.cu built as HIP, with hipBLAS)" OFF ) 25set ( STRATA_VIT_OWN_HIP_ARCHS "" CACHE STRING "gfx architectures for a standalone HIP build (';'-separated); the helper build uses STRATA_VISION_HIP_ARCHS" ) 26set ( _vit_hip OFF ) 27if ( STRATA_VIT_OWN_HIPOR STRATA_VISION_HIP) 28if ( _vit_cuda) 29message ( FATAL_ERROR "strata_vit_own: CUDA and HIP are two different programs" ) 30endif () 31set ( _vit_hip ON ) 32if ( STRATA_VISION_HIP) 33set ( _vit_hip_archs " ${ STRATA_VISION_HIP_ARCHS } " ) 34else () 35set ( _vit_hip_archs " ${ STRATA_VIT_OWN_HIP_ARCHS } " ) 36endif () 37if ( NOT _vit_hip_archs) 38message ( FATAL_ERROR "strata_vit_own: a HIP build needs the gfx architectures (STRATA_VIT_OWN_HIP_ARCHS or STRATA_VISION_HIP_ARCHS)" ) 39endif () 40set ( CMAKE_HIP_ARCHITECTURES " ${ _vit_hip_archs } " ) 41enable_language ( HIP ) 42find_package ( hip REQUIRED ) 43find_package ( hipblas REQUIRED ) 44endif () 45 46option ( STRATA_VIT_OWN_SYCL "the own tower on an Intel GPU (vit_sycl.cpp with oneMKL; needs the oneAPI icpx compiler)" OFF ) 47set ( _vit_sycl OFF ) 48if ( STRATA_VIT_OWN_SYCLOR STRATA_VISION_SYCL) 49if ( _vit_cudaOR _vit_hip) 50message ( FATAL_ERROR "strata_vit_own: SYCL is its own program, not together with CUDA or HIP" ) 51endif () 52set ( _vit_sycl ON ) 53find_package ( MKL REQUIRED ) # the oneMKL ggml-sycl links too (MKL::MKL_SYCL::BLAS) 54endif () 55 56option ( STRATA_VIT_OWN_METAL "the own tower on a Mac's GPU (vit_metal.mm, Metal; macOS only)" OFF ) 57set ( _vit_metal OFF ) 58if ( APPLEAND ( STRATA_VIT_OWN_METALOR STRATA_VISION_METAL)) 59if ( _vit_cudaOR _vit_hipOR _vit_sycl) 60message ( FATAL_ERROR "strata_vit_own: Metal is its own program, not together with CUDA, HIP or SYCL" ) 61endif () 62set ( _vit_metal ON ) 63# the kernels are embedded in the program as strings and compiled when the reader starts (nothing to ship beside it; the Command Line Tools have no 64# offline `metal` compiler): a header generated at configure time, redone when a .metal file changes 65file ( READ ${ CMAKE_CURRENT_SOURCE_DIR } /vit_metal.metal _vit_msl ) 66file ( READ ${ CMAKE_CURRENT_SOURCE_DIR } /vit_metal_mpp.metal _vit_msl_mpp ) 67file ( WRITE ${ CMAKE_CURRENT_BINARY_DIR } /vit_metal_src.h 68"// generated from vit_metal.metal and vit_metal_mpp.metal by tools/vision/own/CMakeLists.txt\nstatic const char kVitMetalSrc[] = R\"VITMSL( ${ _vit_msl } )VITMSL\";\nstatic const char kVitMetalMppSrc[] = R\"VITMSL( ${ _vit_msl_mpp } )VITMSL\";\n" ) 69set_property ( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${ CMAKE_CURRENT_SOURCE_DIR } /vit_metal.metal ${ CMAKE_CURRENT_SOURCE_DIR } /vit_metal_mpp.metal ) 70endif () 71 72set ( STB_DIR " ${ CMAKE_CURRENT_SOURCE_DIR } /../../../third_party/stb" CACHE PATH "stb_image.h (third_party/stb)" ) 73 74add_library ( strata_vit_own STATIC gguf_lite.cpp vit_geometry.cpp image_prep.cpp vit_ref.cpp own_reader.cpp 75vit_cpu.cpp cpu_ops.cpp cpu_kernels_scalar.cpp cpu_kernels_sse41.cpp cpu_kernels_avx.cpp cpu_kernels_avx2.cpp cpu_kernels_avx512.cpp cpu_kernels_neon.cpp ) 76find_package ( Threads REQUIRED ) 77target_link_libraries ( strata_vit_own PUBLIC Threads::Threads ) 78if ( NOT MSVC) 79# the reference's loops are written for the compiler's vectoriser: -O3, and this machine's instruction set when asked (the unit-test and 80# reference builds on the Linux boxes); the helper's portable build keeps the AVX2 baseline its other code has 81option ( STRATA_VIT_OWN_NATIVE "build the reference for this CPU (-march=native)" ${ STRATA_VIT_OWN_STANDALONE }) 82set_source_files_properties ( vit_ref.cpp vit_cpu.cpp cpu_kernels_scalar.cpp cpu_kernels_neon.cpp PROPERTIES COMPILE_OPTIONS "-O3" ) 83# The reference (VitRef, a test oracle) is the only generic file compiled above the baseline, and only where the whole program has that floor anyway: an 84# instruction-set flag on a file that uses the standard library also reaches its inline / template copies (COMDAT), which the linker may keep for the WHOLE 85# program, so a wider flag here would put AVX2 code on the path of a CPU without it (RESULTS-OWN-3V.md PART H). 86# STRATA_VIT_OWN_NATIVE (standalone test builds) or a native helper build (setup, STRATA_PORTABLE=OFF): -march=native, this CPU; 87# the portable helper (STRATA_PORTABLE=ON, an AVX2 baseline for the whole program): -mavx2 -mfma as before; 88# an older-CPU test build (STRATA_ISA_FLOOR, tools/vision): that floor. 89if ( CMAKE_SYSTEM_PROCESSORMATCHES "x86_64|AMD64" ) 90if ( STRATA_ISA_FLOORSTREQUAL "avx" OR STRATA_ISA_FLOORSTREQUAL "avx_f16c" ) 91set_source_files_properties ( vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx" ) 92elseif ( STRATA_ISA_FLOOR) 93set_source_files_properties ( vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3" ) 94elseif ( STRATA_VIT_OWN_NATIVEOR ( DEFINED STRATA_PORTABLEAND NOT STRATA_PORTABLE)) 95set_source_files_properties ( vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-march=native" ) 96elseif ( STRATA_PORTABLE) 97set_source_files_properties ( vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma" ) 98endif () 99elseif ( STRATA_VIT_OWN_NATIVE) 100set_source_files_properties ( vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-march=native" ) 101endif () 102# the CPU tower: the generic part (vit_cpu.cpp) keeps the baseline, each kernel tier is compiled for its own instruction set and only called when the CPU 103# has it (cpu_caps.h); the tiers' files include no standard-library code (cpu_tier.h) 104if ( CMAKE_SYSTEM_PROCESSORMATCHES "x86_64|AMD64" ) 105set_source_files_properties ( cpu_kernels_sse41.cpp PROPERTIES COMPILE_OPTIONS "-O3;-msse4.1" ) 106set_source_files_properties ( cpu_kernels_avx.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx" ) 107set_source_files_properties ( cpu_kernels_avx2.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma;-mf16c" ) 108set_source_files_properties ( cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma;-mf16c;-mavx512f;-mavx512bw;-mavx512dq;-mavx512vl" ) 109else () 110set_source_files_properties ( cpu_kernels_sse41.cpp cpu_kernels_avx.cpp cpu_kernels_avx2.cpp cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "-O3" ) 111endif () 112else () 113set_source_files_properties ( cpu_kernels_avx.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX" ) # (the SSE4.1 tier needs no flag on x64) 114set_source_files_properties ( cpu_kernels_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2" ) 115set_source_files_properties ( cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512" ) 116endif () 117target_include_directories ( strata_vit_own PUBLIC ${ CMAKE_CURRENT_SOURCE_DIR }) 118target_include_directories ( strata_vit_own PRIVATE ${ STB_DIR }) 119target_compile_features ( strata_vit_own PUBLIC cxx_std_17 ) 120if ( _vit_cuda) 121target_sources ( strata_vit_own PRIVATE vit_gpu.cu ) 122target_compile_definitions ( strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_CUDA ) 123target_link_libraries ( strata_vit_own PUBLIC CUDA::cublas CUDA::cudart ) 124set_target_properties ( strata_vit_own PROPERTIES CUDA_STANDARD 17 CUDA_SEPARABLE_COMPILATION OFF ) 125endif () 126 127if ( _vit_hip) 128set_source_files_properties ( vit_gpu.cu PROPERTIES LANGUAGE HIP ) 129target_sources ( strata_vit_own PRIVATE vit_gpu.cu ) 130target_compile_definitions ( strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_HIP ) 131target_link_libraries ( strata_vit_own PUBLIC hip::host roc::hipblas ) 132# hipBLASLt (optional, as for the engine): the products through it with STRATA_VIT_LT / STRATA_VIT_LT_ATT (vit_gpu.cu); off by default 133find_package ( hipblaslt CONFIG QUIET ) 134if ( TARGET roc::hipblaslt) 135target_compile_definitions ( strata_vit_own PRIVATE STRATA_VIT_HIPBLASLT ) 136target_link_libraries ( strata_vit_own PUBLIC roc::hipblaslt ) 137endif () 138endif () 139 140if ( _vit_sycl) 141# only this file is SYCL code, with the precise floating-point model (as the engine's SYCL port). No device-link options here: in the helper they would 142# also change how mtmd's ggml-sycl kernels are linked and compiled (its numerics and start time stay as they are) 143# An object library of its own: oneMKL's SYCL target carries -fsycl as a COMPILE option, which must not reach the other files (their x86 code, cpuid 144# builtins included, would be compiled for the device too); the rest of the program only links oneMKL ($<LINK_ONLY:...>). 145add_library ( strata_vit_sycl OBJECT vit_sycl.cpp ) 146target_include_directories ( strata_vit_sycl PRIVATE ${ CMAKE_CURRENT_SOURCE_DIR }) 147target_compile_features ( strata_vit_sycl PRIVATE cxx_std_17 ) 148target_compile_options ( strata_vit_sycl PRIVATE -fsycl -fp-model=precise ) 149target_compile_definitions ( strata_vit_sycl PRIVATE STRATA_VIT_GPU STRATA_VIT_SYCL ) 150target_link_libraries ( strata_vit_sycl PRIVATE MKL::MKL_SYCL::BLAS ) 151target_sources ( strata_vit_own PRIVATE $ <TARGET_OBJECTS:strata_vit_sycl> ) 152target_compile_definitions ( strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_SYCL ) 153target_link_libraries ( strata_vit_own INTERFACE $ <LINK_ONLY:MKL::MKL_SYCL::BLAS> ) # INTERFACE + LINK_ONLY: the programs link oneMKL, nothing else compiles with its options 154target_link_options ( strata_vit_own PUBLIC -fsycl ) 155endif () 156 157if ( _vit_metal) 158target_sources ( strata_vit_own PRIVATE vit_metal.mm ) 159# compiled as C++ with the Objective-C++ switch (an OBJCXX language enabled only in this subdirectory leaves the helper's build without its rules) 160set_source_files_properties ( vit_metal.mm PROPERTIES LANGUAGE CXX COMPILE_OPTIONS "-xobjective-c++;-fobjc-arc;-O2" ) 161target_include_directories ( strata_vit_own PRIVATE ${ CMAKE_CURRENT_BINARY_DIR }) 162target_compile_definitions ( strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_METAL ) 163target_link_libraries ( strata_vit_own PUBLIC "-framework Metal" "-framework Foundation" ) 164endif () 165 166# the own-tower-only reader, the unit tests, and with a picture list the comparison with mtmd's preparation (oracle_prep.cpp is built in 167# tools/vision's mtmd build) and the embedding comparison 168add_executable ( strata-vision-own own_main.cpp ) 169target_link_libraries ( strata-vision-own PRIVATE strata_vit_own ) 170add_executable ( strata-vision-own-test test_gguf_geometry.cpp ) 171target_link_libraries ( strata-vision-own-test PRIVATE strata_vit_own ) 172add_executable ( strata-vision-own-cpu-test test_cpu_kernels.cpp ) 173target_link_libraries ( strata-vision-own-cpu-test PRIVATE strata_vit_own ) 174add_executable ( strata-vision-own-prep-check test_image_prep.cpp ) 175target_link_libraries ( strata-vision-own-prep-check PRIVATE strata_vit_own ) 176add_executable ( strata-vision-own-compare compare_sve.cpp ) 177if ( NOT STRATA_VIT_OWN_STANDALONE) 178set_target_properties ( strata-vision-own strata-vision-own-test strata-vision-own-cpu-test strata-vision-own-prep-check strata-vision-own-compare PROPERTIES EXCLUDE_FROM_ALL ON ) 179endif ()