okami.mom/strata

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

Niko1221Version 0.2.14 / engine 0.1.60: Terminal and app 0.2.14, Control and the engine packages 0.1.6038f63bc59

main
8.7 KiB139 linesraw
1# metal/ - Strata on Apple Silicon (Metal), MAC-PLAN.md.  Its own CMake project like sycl/, macOS only, never part of
2# the CUDA / HIP / SYCL builds.
3#
4# Two parts:
5#   * the kernel tree (mac-kern): the runtime face src/mtl_rt.*, the MSL kernels kernels/*.metal (compiled at run time),
6#     CPU references src/ref_*.cpp and the parity / bench programs tests/*.cpp;
7#   * the engine (mac-design): the shared host sources from ../src, UNCHANGED - include/metal_compat goes first on the
8#     include path and gives them the CUDA runtime names on Metal, as include/strata/hip_compat does on HIP - over
9#     the Metal device runtime src/gpu/runtime.cpp (metal-cpp) and the kernel library src/engine/*.
10#
11#   cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=... [-DSTRATA_GGML_DIR=...]
12#   ninja -C build-metal
13cmake_minimum_required(VERSION 3.24)
14project(strata_metal LANGUAGES C CXX OBJCXX)
15include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/strata_geometry.cmake)   # -DSTRATA_GEOMETRY=<name> (default flash_next, no flag added)
16
17if(NOT APPLE)
18  message(FATAL_ERROR "metal/ builds on macOS only")
19endif()
20
21set(CMAKE_CXX_STANDARD 20)
22set(CMAKE_CXX_STANDARD_REQUIRED ON)
23set(CMAKE_OBJCXX_STANDARD 20)
24if(NOT CMAKE_BUILD_TYPE)
25  set(CMAKE_BUILD_TYPE Release)
26endif()
27set(CMAKE_CXX_FLAGS_RELEASE "-O2")
28set(CMAKE_OBJCXX_FLAGS_RELEASE "-O2")
29
30get_filename_component(STRATA_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE)
31set(GGML_COMMON "${STRATA_ROOT}/third_party/strata-ggml/strata-quant-common.h")   # the quant header's text (third_party/ggml/ggml-common.h forwards to it)
32
33# ---- the kernel tree (mac-kern) ------------------------------------------------------------------------------------
34add_library(mtl_rt STATIC src/mtl_rt.mm src/ref_quant.cpp src/gguf_lite.cpp)
35target_include_directories(mtl_rt PUBLIC src ${STRATA_ROOT}/third_party/ggml ${STRATA_ROOT}/include)
36target_compile_options(mtl_rt PRIVATE $<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-arc>)
37target_compile_definitions(mtl_rt PRIVATE
38  STRATA_GGML_COMMON_DEFAULT="${GGML_COMMON}"
39  STRATA_METAL_KERNELS_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/kernels")
40target_link_libraries(mtl_rt PUBLIC "-framework Metal" "-framework Foundation")
41
42file(GLOB STRATA_METAL_TESTS CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp)
43list(REMOVE_ITEM STRATA_METAL_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/runtime_hello.cpp)
44foreach(src ${STRATA_METAL_TESTS})
45  get_filename_component(t ${src} NAME_WE)
46  add_executable(${t} ${src})
47  target_link_libraries(${t} PRIVATE mtl_rt)
48endforeach()
49
50# ---- the device runtime and the engine (mac-design) ---------------------------------------------------------------
51set(METAL_CPP_DIR "" CACHE PATH "metal-cpp headers (https://developer.apple.com/metal/cpp/), e.g. metal-cpp_26")
52if(NOT EXISTS "${METAL_CPP_DIR}/Metal/Metal.hpp")
53  message(STATUS "strata metal: METAL_CPP_DIR is not set: the device runtime and the engine are not built")
54  return()
55endif()
56
57add_library(strata_gpu_metal STATIC src/gpu/runtime.cpp)
58target_include_directories(strata_gpu_metal BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/metal_compat
59                           ${CMAKE_CURRENT_SOURCE_DIR}/include ${STRATA_ROOT}/include)
60target_include_directories(strata_gpu_metal SYSTEM PUBLIC ${METAL_CPP_DIR})
61target_compile_definitions(strata_gpu_metal PUBLIC STRATA_GPU_METAL=1)
62target_link_libraries(strata_gpu_metal PUBLIC "-framework Metal" "-framework Foundation" "-framework QuartzCore")
63
64add_library(strata_core_metal STATIC ${STRATA_ROOT}/src/core/graph.cpp)
65target_link_libraries(strata_core_metal PUBLIC strata_gpu_metal)
66add_executable(runtime_hello tests/runtime_hello.cpp)
67target_link_libraries(runtime_hello PRIVATE strata_core_metal)
68enable_testing()
69add_test(NAME runtime_hello COMMAND runtime_hello)
70
71# The engine: ggml (the native expert formats' type traits and ggml-cpu) from the tree vendored in third_party/strata-ggml (frozen at the
72# commit the PC build pins, 3cf03257).  -DSTRATA_GGML_DIR=<llama.cpp checkout> builds from that checkout instead (an override kept for two releases).
73option(STRATA_METAL_ENGINE "Build the engine (strata) for Metal" ON)
74set(STRATA_GGML_DIR "" CACHE PATH "llama.cpp checkout to take ggml from instead of the vendored third_party/strata-ggml (empty: the vendored tree)")
75if(NOT STRATA_METAL_ENGINE)
76  return()
77endif()
78include(${STRATA_ROOT}/cmake/strata_ggml.cmake)        # -DSTRATA_GGML_DIR, or the vendored third_party/strata-ggml
79include(${STRATA_ROOT}/cmake/backends/metal.cmake)
80strata_ggml_resolve("${STRATA_ROOT}")
81if(NOT EXISTS "${_strata_ggml_root}/ggml/CMakeLists.txt")
82  message(FATAL_ERROR "ggml sources not found in '${_strata_ggml_root}' (third_party/strata-ggml, or the -DSTRATA_GGML_DIR checkout)")
83endif()
84strata_metal_ggml()
85# OWN-2N: the CPU expert rows and the --mtp-q4 requantizer on the Strata quant layer (src/quant, its Arm NEON tier, no ggml-cpu
86# call from the pool) instead of ggml-cpu's type traits.  OFF by default on Metal until the layer's oracle (tests/quant/run_oracle.sh, Apple Clang) and unit tests
87# have passed on the Mac; -DSTRATA_OWN_CPU_QUANT=ON builds the engine on the layer.
88option(STRATA_OWN_CPU_QUANT "CPU expert pool and --mtp-q4 on the Strata quant layer instead of ggml-cpu's type traits" OFF)
89option(STRATA_BUILD_TESTS "Build the quant layer's self-test and ggml oracle test (needs STRATA_OWN_CPU_QUANT)" OFF)
90if(STRATA_OWN_CPU_QUANT)
91  set(STRATA_QUANT_ROOT ${STRATA_ROOT})
92  set(STRATA_CPU_X86 OFF)
93  include(${STRATA_ROOT}/cmake/backends/quant.cmake)
94  strata_quant_layer()
95endif()
96
97set(S ${STRATA_ROOT}/src)
98set(STRATA_METAL_HOST_SOURCES
99  ${S}/core/device.cu ${S}/core/pinned.cu ${S}/platform/memory.cpp ${S}/core/graph.cpp ${S}/core/weights.cpp ${S}/core/layout.cpp
100  ${S}/core/layer.cpp ${S}/core/session.cpp ${S}/core/expert_source.cpp ${S}/core/remote_experts.cpp ${S}/core/expert_cache.cpp
101  ${S}/core/native_head.cpp ${S}/core/native_dense.cpp ${S}/core/verify.cpp ${S}/core/mtp.cpp
102  ${S}/core/conversation_snapshot.cpp ${S}/core/conversation_state.cpp ${S}/core/conversation_memory.cpp
103  ${S}/core/peer_experts.cpp ${S}/core/arch_defaults.cpp ${S}/core/vmm.cpp ${S}/core/conversation_file.cpp
104  ${S}/kernels/ngram.cpp ${S}/ngram/ple_reader.cpp ${S}/platform/direct_file.cpp ${S}/platform/io_priority.cpp
105  ${S}/prefill/prefill.cpp
106  ${S}/spec/suffix_drafter.cpp ${S}/spec/controller.cpp ${S}/spec/draft_policy.cpp ${S}/spec/draft_source.cpp
107  ${S}/kernels/cpu/pool.cpp ${S}/kernels/cpu/expert_layout.cpp ${S}/kernels/cpu/native_expert.cpp
108  ${S}/kernels/cpu/hadamard.cpp ${S}/kernels/cpu/tq_cpu.cpp)   # Gyro: the Hadamard rotation, the scalar trellis rows
109set_source_files_properties(${S}/core/device.cu ${S}/core/pinned.cu
110                            PROPERTIES LANGUAGE CXX)
111
112# The kernel library: Metal implementations (src/engine/*.cpp) and, for every entry point not ported yet, a stub
113# that stops the engine with the entry point's name (src/engine/stubs.cpp, generated by tools/gen_stubs.py).
114file(GLOB STRATA_METAL_ENGINE_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/engine/*.cpp)
115
116add_library(strata_metal_host STATIC ${STRATA_METAL_HOST_SOURCES})
117target_include_directories(strata_metal_host PUBLIC ${STRATA_ROOT}/include ${S}/core ${STRATA_ROOT}/third_party/ggml
118                           ${_strata_ggml_root}/ggml/include ${_strata_ggml_root}/ggml/src ${_strata_ggml_root}/ggml/src/ggml-cpu)
119target_compile_definitions(strata_metal_host PUBLIC STRATA_NATIVE_EXPERTS=1 STRATA_HAVE_DFLASH2=1 STRATA_VERSION="0.1.60-metal")
120target_compile_options(strata_metal_host PRIVATE -Wno-unused-parameter -Wno-deprecated-declarations)
121target_link_libraries(strata_metal_host PUBLIC strata_gpu_metal ggml-cpu ggml-base)
122if(STRATA_OWN_CPU_QUANT)
123  target_link_libraries(strata_metal_host PUBLIC strata_quant)
124  target_compile_definitions(strata_metal_host PUBLIC STRATA_OWN_CPU_QUANT=1)
125endif()
126
127add_library(strata_metal_kernels STATIC ${STRATA_METAL_ENGINE_SOURCES})
128target_include_directories(strata_metal_kernels PRIVATE src ${STRATA_ROOT}/third_party/ggml)
129target_compile_definitions(strata_metal_kernels PRIVATE
130  STRATA_GGML_COMMON_DEFAULT="${GGML_COMMON}" STRATA_METAL_KERNELS_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/kernels"
131  STRATA_METAL_ENGINE_KERNELS_DEFAULT="${CMAKE_CURRENT_SOURCE_DIR}/src/engine/msl"
132  STRATA_METAL_DATA_DEFAULT="${STRATA_ROOT}/data")
133target_link_libraries(strata_metal_kernels PUBLIC strata_metal_host mtl_rt)
134
135add_executable(strata ${S}/program/generate.cpp)
136target_link_libraries(strata PRIVATE strata_metal_kernels strata_metal_host strata_metal_kernels)
137# the decode window on its own (probe/window_probe.cpp): the engine's process state without the engine, for looking at a Metal fault
138add_executable(window_probe probe/window_probe.cpp)
139target_link_libraries(window_probe PRIVATE strata_metal_kernels strata_metal_host strata_metal_kernels)