okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-3c 0991006d2 into f-own-intd39d20f52

main
11.4 KiB179 linesraw
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_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
9  project(strata_vision_own C CXX)
10  set(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_CUDA OR STRATA_VISION_CUDA)
16  set(_vit_cuda ON)
17  enable_language(CUDA)
18  find_package(CUDAToolkit REQUIRED)
19  if(NOT CMAKE_CUDA_ARCHITECTURES)
20    set(CMAKE_CUDA_ARCHITECTURES native)
21  endif()
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_HIP OR STRATA_VISION_HIP)
28  if(_vit_cuda)
29    message(FATAL_ERROR "strata_vit_own: CUDA and HIP are two different programs")
30  endif()
31  set(_vit_hip ON)
32  if(STRATA_VISION_HIP)
33    set(_vit_hip_archs "${STRATA_VISION_HIP_ARCHS}")
34  else()
35    set(_vit_hip_archs "${STRATA_VIT_OWN_HIP_ARCHS}")
36  endif()
37  if(NOT _vit_hip_archs)
38    message(FATAL_ERROR "strata_vit_own: a HIP build needs the gfx architectures (STRATA_VIT_OWN_HIP_ARCHS or STRATA_VISION_HIP_ARCHS)")
39  endif()
40  set(CMAKE_HIP_ARCHITECTURES "${_vit_hip_archs}")
41  enable_language(HIP)
42  find_package(hip REQUIRED)
43  find_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_SYCL OR STRATA_VISION_SYCL)
49  if(_vit_cuda OR _vit_hip)
50    message(FATAL_ERROR "strata_vit_own: SYCL is its own program, not together with CUDA or HIP")
51  endif()
52  set(_vit_sycl ON)
53  find_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(APPLE AND (STRATA_VIT_OWN_METAL OR STRATA_VISION_METAL))
59  if(_vit_cuda OR _vit_hip OR _vit_sycl)
60    message(FATAL_ERROR "strata_vit_own: Metal is its own program, not together with CUDA, HIP or SYCL")
61  endif()
62  set(_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
65  file(READ ${CMAKE_CURRENT_SOURCE_DIR}/vit_metal.metal _vit_msl)
66  file(READ ${CMAKE_CURRENT_SOURCE_DIR}/vit_metal_mpp.metal _vit_msl_mpp)
67  file(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")
69  set_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
75            vit_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
81  option(STRATA_VIT_OWN_NATIVE "build the reference for this CPU (-march=native)" ${STRATA_VIT_OWN_STANDALONE})
82  set_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.
89  if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
90    if(STRATA_ISA_FLOOR STREQUAL "avx" OR STRATA_ISA_FLOOR STREQUAL "avx_f16c")
91      set_source_files_properties(vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx")
92    elseif(STRATA_ISA_FLOOR)
93      set_source_files_properties(vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3")
94    elseif(STRATA_VIT_OWN_NATIVE OR (DEFINED STRATA_PORTABLE AND NOT STRATA_PORTABLE))
95      set_source_files_properties(vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-march=native")
96    elseif(STRATA_PORTABLE)
97      set_source_files_properties(vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma")
98    endif()
99  elseif(STRATA_VIT_OWN_NATIVE)
100    set_source_files_properties(vit_ref.cpp PROPERTIES COMPILE_OPTIONS "-O3;-march=native")
101  endif()
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)
104  if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
105    set_source_files_properties(cpu_kernels_sse41.cpp PROPERTIES COMPILE_OPTIONS "-O3;-msse4.1")
106    set_source_files_properties(cpu_kernels_avx.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx")
107    set_source_files_properties(cpu_kernels_avx2.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma;-mf16c")
108    set_source_files_properties(cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma;-mf16c;-mavx512f;-mavx512bw;-mavx512dq;-mavx512vl")
109  else()
110    set_source_files_properties(cpu_kernels_sse41.cpp cpu_kernels_avx.cpp cpu_kernels_avx2.cpp cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "-O3")
111  endif()
112else()
113  set_source_files_properties(cpu_kernels_avx.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX")      # (the SSE4.1 tier needs no flag on x64)
114  set_source_files_properties(cpu_kernels_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
115  set_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)
121  target_sources(strata_vit_own PRIVATE vit_gpu.cu)
122  target_compile_definitions(strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_CUDA)
123  target_link_libraries(strata_vit_own PUBLIC CUDA::cublas CUDA::cudart)
124  set_target_properties(strata_vit_own PROPERTIES CUDA_STANDARD 17 CUDA_SEPARABLE_COMPILATION OFF)
125endif()
126
127if(_vit_hip)
128  set_source_files_properties(vit_gpu.cu PROPERTIES LANGUAGE HIP)
129  target_sources(strata_vit_own PRIVATE vit_gpu.cu)
130  target_compile_definitions(strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_HIP)
131  target_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
133  find_package(hipblaslt CONFIG QUIET)
134  if(TARGET roc::hipblaslt)
135    target_compile_definitions(strata_vit_own PRIVATE STRATA_VIT_HIPBLASLT)
136    target_link_libraries(strata_vit_own PUBLIC roc::hipblaslt)
137  endif()
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:...>).
145  add_library(strata_vit_sycl OBJECT vit_sycl.cpp)
146  target_include_directories(strata_vit_sycl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
147  target_compile_features(strata_vit_sycl PRIVATE cxx_std_17)
148  target_compile_options(strata_vit_sycl PRIVATE -fsycl -fp-model=precise)
149  target_compile_definitions(strata_vit_sycl PRIVATE STRATA_VIT_GPU STRATA_VIT_SYCL)
150  target_link_libraries(strata_vit_sycl PRIVATE MKL::MKL_SYCL::BLAS)
151  target_sources(strata_vit_own PRIVATE $<TARGET_OBJECTS:strata_vit_sycl>)
152  target_compile_definitions(strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_SYCL)
153  target_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
154  target_link_options(strata_vit_own PUBLIC -fsycl)
155endif()
156
157if(_vit_metal)
158  target_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)
160  set_source_files_properties(vit_metal.mm PROPERTIES LANGUAGE CXX COMPILE_OPTIONS "-xobjective-c++;-fobjc-arc;-O2")
161  target_include_directories(strata_vit_own PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
162  target_compile_definitions(strata_vit_own PUBLIC STRATA_VIT_GPU STRATA_VIT_METAL)
163  target_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)
178  set_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()