mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-11-11 13:30:35 +00:00
vulkan : cmake integration (#8119)
* Add Vulkan to CMake pkg * Add Sycl to CMake pkg * Add OpenMP to CMake pkg * Split generated shader file into separate translation unit * Add CMake target for Vulkan shaders * Update README.md * Add make target for Vulkan shaders * Use pkg-config to locate vulkan library * Add vulkan SDK dep to ubuntu-22-cmake-vulkan workflow * Clean up tabs * Move sudo to apt-key invocation * Forward GGML_EXTRA_LIBS to CMake config pkg * Update vulkan obj file paths * Add shaderc to nix pkg * Add python3 to Vulkan nix build * Link against ggml in cmake pkg * Remove Python dependency from Vulkan build * code review changes * Remove trailing newline * Add cflags from pkg-config to fix w64devkit build * Update README.md * Remove trailing whitespace * Update README.md * Remove trailing whitespace * Fix doc heading * Make glslc required Vulkan component * remove clblast from nix pkg
This commit is contained in:
parent
c917b67f06
commit
17eb6aa8a9
@ -18,6 +18,7 @@
|
|||||||
vulkan-headers,
|
vulkan-headers,
|
||||||
vulkan-loader,
|
vulkan-loader,
|
||||||
curl,
|
curl,
|
||||||
|
shaderc,
|
||||||
useBlas ? builtins.all (x: !x) [
|
useBlas ? builtins.all (x: !x) [
|
||||||
useCuda
|
useCuda
|
||||||
useMetalKit
|
useMetalKit
|
||||||
@ -146,6 +147,7 @@ let
|
|||||||
vulkanBuildInputs = [
|
vulkanBuildInputs = [
|
||||||
vulkan-headers
|
vulkan-headers
|
||||||
vulkan-loader
|
vulkan-loader
|
||||||
|
shaderc
|
||||||
];
|
];
|
||||||
in
|
in
|
||||||
|
|
||||||
|
6
.github/workflows/build.yml
vendored
6
.github/workflows/build.yml
vendored
@ -355,8 +355,10 @@ jobs:
|
|||||||
- name: Dependencies
|
- name: Dependencies
|
||||||
id: depends
|
id: depends
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add -
|
||||||
sudo apt-get install build-essential libvulkan-dev
|
sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-jammy.list https://packages.lunarg.com/vulkan/lunarg-vulkan-jammy.list
|
||||||
|
sudo apt-get update -y
|
||||||
|
sudo apt-get install -y build-essential vulkan-sdk
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
id: cmake_build
|
id: cmake_build
|
||||||
|
@ -132,7 +132,16 @@ set(LLAMA_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Location o
|
|||||||
set(LLAMA_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Location of library files")
|
set(LLAMA_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Location of library files")
|
||||||
set(LLAMA_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Location of binary files")
|
set(LLAMA_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Location of binary files")
|
||||||
|
|
||||||
get_directory_property(LLAMA_TRANSIENT_DEFINES COMPILE_DEFINITIONS)
|
|
||||||
|
# At the moment some compile definitions are placed within the ggml/src
|
||||||
|
# directory but not exported on the `ggml` target. This could be improved by
|
||||||
|
# determining _precisely_ which defines are necessary for the llama-config
|
||||||
|
# package.
|
||||||
|
#
|
||||||
|
get_directory_property(GGML_DIR_DEFINES DIRECTORY ggml/src COMPILE_DEFINITIONS)
|
||||||
|
get_target_property(GGML_TARGET_DEFINES ggml COMPILE_DEFINITIONS)
|
||||||
|
set(GGML_TRANSIENT_DEFINES ${GGML_TARGET_DEFINES} ${GGML_DIR_DEFINES})
|
||||||
|
get_target_property(GGML_LINK_LIBRARIES ggml LINK_LIBRARIES)
|
||||||
|
|
||||||
set_target_properties(llama PROPERTIES PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/llama.h)
|
set_target_properties(llama PROPERTIES PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/llama.h)
|
||||||
install(TARGETS llama LIBRARY PUBLIC_HEADER)
|
install(TARGETS llama LIBRARY PUBLIC_HEADER)
|
||||||
|
35
Makefile
35
Makefile
@ -197,6 +197,10 @@ ifdef GGML_RPC
|
|||||||
BUILD_TARGETS += rpc-server
|
BUILD_TARGETS += rpc-server
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifdef GGML_VULKAN
|
||||||
|
BUILD_TARGETS += vulkan-shaders-gen
|
||||||
|
endif
|
||||||
|
|
||||||
default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)
|
default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)
|
||||||
|
|
||||||
test: $(TEST_TARGETS)
|
test: $(TEST_TARGETS)
|
||||||
@ -710,8 +714,8 @@ endif # GGML_CUDA
|
|||||||
|
|
||||||
ifdef GGML_VULKAN
|
ifdef GGML_VULKAN
|
||||||
MK_CPPFLAGS += -DGGML_USE_VULKAN
|
MK_CPPFLAGS += -DGGML_USE_VULKAN
|
||||||
MK_LDFLAGS += -lvulkan
|
MK_LDFLAGS += $(shell pkg-config --libs vulkan)
|
||||||
OBJ_GGML += ggml/src/ggml-vulkan.o
|
OBJ_GGML += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
|
||||||
|
|
||||||
ifdef GGML_VULKAN_CHECK_RESULTS
|
ifdef GGML_VULKAN_CHECK_RESULTS
|
||||||
MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
|
MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
|
||||||
@ -733,10 +737,28 @@ ifdef GGML_VULKAN_RUN_TESTS
|
|||||||
MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
|
MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ggml/src/ggml-vulkan.o: \
|
GLSLC_CMD = glslc
|
||||||
ggml/src/ggml-vulkan.cpp \
|
_ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
|
||||||
ggml/include/ggml-vulkan.h
|
_ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
|
||||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
_ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
|
||||||
|
_ggml_vk_input_dir = ggml/src/vulkan-shaders
|
||||||
|
_ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
|
||||||
|
|
||||||
|
ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
|
||||||
|
$(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
|
||||||
|
|
||||||
|
$(_ggml_vk_header): $(_ggml_vk_source)
|
||||||
|
|
||||||
|
$(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
|
||||||
|
$(_ggml_vk_genshaders_cmd) \
|
||||||
|
--glslc $(GLSLC_CMD) \
|
||||||
|
--input-dir $(_ggml_vk_input_dir) \
|
||||||
|
--target-hpp $(_ggml_vk_header) \
|
||||||
|
--target-cpp $(_ggml_vk_source)
|
||||||
|
|
||||||
|
vulkan-shaders-gen: ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
|
||||||
|
|
||||||
endif # GGML_VULKAN
|
endif # GGML_VULKAN
|
||||||
|
|
||||||
ifdef GGML_HIPBLAS
|
ifdef GGML_HIPBLAS
|
||||||
@ -1116,6 +1138,7 @@ clean:
|
|||||||
rm -vrf ggml/src/ggml-cuda/template-instances/*.o
|
rm -vrf ggml/src/ggml-cuda/template-instances/*.o
|
||||||
rm -rvf $(BUILD_TARGETS)
|
rm -rvf $(BUILD_TARGETS)
|
||||||
rm -rvf $(TEST_TARGETS)
|
rm -rvf $(TEST_TARGETS)
|
||||||
|
rm -f vulkan-shaders-gen ggml/src/ggml-vulkan-shaders.hpp ggml/src/ggml-vulkan-shaders.cpp
|
||||||
rm -rvf $(LEGACY_TARGETS_CLEAN)
|
rm -rvf $(LEGACY_TARGETS_CLEAN)
|
||||||
find examples pocs -type f -name "*.o" -delete
|
find examples pocs -type f -name "*.o" -delete
|
||||||
|
|
||||||
|
@ -8,6 +8,13 @@ set(GGML_CUDA @GGML_CUDA@)
|
|||||||
set(GGML_METAL @GGML_METAL@)
|
set(GGML_METAL @GGML_METAL@)
|
||||||
set(GGML_HIPBLAS @GGML_HIPBLAS@)
|
set(GGML_HIPBLAS @GGML_HIPBLAS@)
|
||||||
set(GGML_ACCELERATE @GGML_ACCELERATE@)
|
set(GGML_ACCELERATE @GGML_ACCELERATE@)
|
||||||
|
set(GGML_VULKAN @GGML_VULKAN@)
|
||||||
|
set(GGML_VULKAN_CHECK_RESULTS @GGML_VULKAN_CHECK_RESULTS@)
|
||||||
|
set(GGML_VULKAN_DEBUG @GGML_VULKAN_DEBUG@)
|
||||||
|
set(GGML_VULKAN_MEMORY_DEBUG @GGML_VULKAN_MEMORY_DEBUG@)
|
||||||
|
set(GGML_VULKAN_VALIDATE @GGML_VULKAN_VALIDATE@)
|
||||||
|
set(GGML_SYCL @GGML_SYCL@)
|
||||||
|
set(GGML_OPENMP @GGML_OPENMP@)
|
||||||
|
|
||||||
@PACKAGE_INIT@
|
@PACKAGE_INIT@
|
||||||
|
|
||||||
@ -37,18 +44,36 @@ if (GGML_METAL)
|
|||||||
find_library(METALKIT_FRAMEWORK MetalKit REQUIRED)
|
find_library(METALKIT_FRAMEWORK MetalKit REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (GGML_VULKAN)
|
||||||
|
find_package(Vulkan REQUIRED)
|
||||||
|
endif()
|
||||||
|
|
||||||
if (GGML_HIPBLAS)
|
if (GGML_HIPBLAS)
|
||||||
find_package(hip REQUIRED)
|
find_package(hip REQUIRED)
|
||||||
find_package(hipblas REQUIRED)
|
find_package(hipblas REQUIRED)
|
||||||
find_package(rocblas REQUIRED)
|
find_package(rocblas REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (GGML_SYCL)
|
||||||
|
find_package(IntelSYCL REQUIRED)
|
||||||
|
find_package(MKL REQUIRED)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (GGML_OPENMP)
|
||||||
|
find_package(OpenMP REQUIRED)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
find_library(ggml_LIBRARY ggml
|
||||||
|
REQUIRED
|
||||||
|
HINTS ${LLAMA_LIB_DIR})
|
||||||
|
|
||||||
find_library(llama_LIBRARY llama
|
find_library(llama_LIBRARY llama
|
||||||
REQUIRED
|
REQUIRED
|
||||||
HINTS ${LLAMA_LIB_DIR})
|
HINTS ${LLAMA_LIB_DIR})
|
||||||
|
|
||||||
set(_llama_link_deps "Threads::Threads" "@LLAMA_EXTRA_LIBS@")
|
set(_llama_link_deps "${ggml_LIBRARY}" "@GGML_LINK_LIBRARIES@")
|
||||||
set(_llama_transient_defines "@LLAMA_TRANSIENT_DEFINES@")
|
set(_llama_transient_defines "@GGML_TRANSIENT_DEFINES@")
|
||||||
|
|
||||||
add_library(llama UNKNOWN IMPORTED)
|
add_library(llama UNKNOWN IMPORTED)
|
||||||
|
|
||||||
|
@ -242,6 +242,45 @@ The following compilation options are also available to tweak performance (yes,
|
|||||||
|
|
||||||
### Vulkan
|
### Vulkan
|
||||||
|
|
||||||
|
**Windows**
|
||||||
|
|
||||||
|
#### w64devkit
|
||||||
|
|
||||||
|
Download and extract [w64devkit](https://github.com/skeeto/w64devkit/releases).
|
||||||
|
|
||||||
|
Download and install the [Vulkan SDK](https://vulkan.lunarg.com/sdk/home#windows). When selecting components, only the Vulkan SDK Core is required.
|
||||||
|
|
||||||
|
Launch `w64devkit.exe` and run the following commands to copy Vulkan dependencies:
|
||||||
|
```sh
|
||||||
|
SDK_VERSION=1.3.283.0
|
||||||
|
cp /VulkanSDK/$SDK_VERSION/Bin/glslc.exe $W64DEVKIT_HOME/bin/
|
||||||
|
cp /VulkanSDK/$SDK_VERSION/Lib/vulkan-1.lib $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/
|
||||||
|
cp -r /VulkanSDK/$SDK_VERSION/Include/* $W64DEVKIT_HOME/x86_64-w64-mingw32/include/
|
||||||
|
cat > $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/pkgconfig/vulkan.pc <<EOF
|
||||||
|
Name: Vulkan-Loader
|
||||||
|
Description: Vulkan Loader
|
||||||
|
Version: $SDK_VERSION
|
||||||
|
Libs: -lvulkan-1
|
||||||
|
EOF
|
||||||
|
|
||||||
|
```
|
||||||
|
Switch into the `llama.cpp` directory and run `make GGML_VULKAN=1`.
|
||||||
|
|
||||||
|
#### MSYS2
|
||||||
|
Install [MSYS2](https://www.msys2.org/) and then run the following commands in a UCRT terminal to install dependencies.
|
||||||
|
```sh
|
||||||
|
pacman -S git \
|
||||||
|
mingw-w64-ucrt-x86_64-gcc \
|
||||||
|
mingw-w64-ucrt-x86_64-cmake \
|
||||||
|
mingw-w64-ucrt-x86_64-vulkan-devel \
|
||||||
|
mingw-w64-ucrt-x86_64-shaderc
|
||||||
|
```
|
||||||
|
Switch into `llama.cpp` directory and build using CMake.
|
||||||
|
```sh
|
||||||
|
cmake -B build -DGGML_VULKAN=ON
|
||||||
|
cmake --build build --config Release
|
||||||
|
```
|
||||||
|
|
||||||
**With docker**:
|
**With docker**:
|
||||||
|
|
||||||
You don't need to install Vulkan SDK. It will be installed inside the container.
|
You don't need to install Vulkan SDK. It will be installed inside the container.
|
||||||
|
2
ggml/.gitignore
vendored
Normal file
2
ggml/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
src/ggml-vulkan-shaders.hpp
|
||||||
|
src/ggml-vulkan-shaders.cpp
|
@ -1,220 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import argparse
|
|
||||||
import asyncio
|
|
||||||
import os
|
|
||||||
from tempfile import gettempdir
|
|
||||||
|
|
||||||
logger = logging.getLogger("ggml-vk-generate-shaders")
|
|
||||||
|
|
||||||
GLSLC = "glslc"
|
|
||||||
|
|
||||||
type_names = [
|
|
||||||
"f32",
|
|
||||||
"f16",
|
|
||||||
"q4_0",
|
|
||||||
"q4_1",
|
|
||||||
"q5_0",
|
|
||||||
"q5_1",
|
|
||||||
"q8_0",
|
|
||||||
"q2_k",
|
|
||||||
"q3_k",
|
|
||||||
"q4_k",
|
|
||||||
"q5_k",
|
|
||||||
"q6_k",
|
|
||||||
]
|
|
||||||
|
|
||||||
ASYNCIO_CONCURRENCY = 64
|
|
||||||
|
|
||||||
input_dir = "vulkan-shaders"
|
|
||||||
output_dir = gettempdir()
|
|
||||||
|
|
||||||
lock = asyncio.Lock()
|
|
||||||
shader_fnames = []
|
|
||||||
|
|
||||||
|
|
||||||
async def string_to_spv(name, in_fname, defines, fp16=True):
|
|
||||||
name = f"{name}{'_fp32' if not fp16 else ''}"
|
|
||||||
out_fname = os.path.join(output_dir, f"{name}.spv")
|
|
||||||
|
|
||||||
in_path = os.path.join(input_dir, in_fname)
|
|
||||||
|
|
||||||
cmd = [GLSLC, "-fshader-stage=compute", "--target-env=vulkan1.2", "-O", in_path, "-o", out_fname]
|
|
||||||
|
|
||||||
cmd.extend([f"-D{key}={value}" for key, value in defines.items()])
|
|
||||||
|
|
||||||
proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
|
||||||
|
|
||||||
stdout, stderr = await proc.communicate()
|
|
||||||
|
|
||||||
stdout = stdout.decode()
|
|
||||||
error = stderr.decode()
|
|
||||||
|
|
||||||
if proc.returncode:
|
|
||||||
cmd = " ".join(cmd)
|
|
||||||
logger.error(f"cannot compile {name}\n\n{cmd}\n\n{error}")
|
|
||||||
return
|
|
||||||
|
|
||||||
async with lock:
|
|
||||||
shader_fnames.append((name, out_fname))
|
|
||||||
|
|
||||||
|
|
||||||
def matmul_shaders(tasks, fp16, matmul_id):
|
|
||||||
if fp16:
|
|
||||||
load_vec = "8"
|
|
||||||
aligned_b_type_f32 = "mat2x4"
|
|
||||||
aligned_b_type_f16 = "f16mat2x4"
|
|
||||||
else:
|
|
||||||
load_vec = "4"
|
|
||||||
aligned_b_type_f32 = "vec4"
|
|
||||||
aligned_b_type_f16 = "f16vec4"
|
|
||||||
|
|
||||||
base_dict = {"FLOAT_TYPE": "float" if not fp16 else "float16_t"}
|
|
||||||
shader_name = "matmul"
|
|
||||||
|
|
||||||
if matmul_id:
|
|
||||||
base_dict["MUL_MAT_ID"] = "1"
|
|
||||||
shader_name = "matmul_id"
|
|
||||||
|
|
||||||
if fp16:
|
|
||||||
base_dict["FLOAT16"] = "1"
|
|
||||||
|
|
||||||
# Shaders with f16 B_TYPE
|
|
||||||
tasks.append(string_to_spv(f"{shader_name}_f32_f16", "mul_mm.comp", base_dict | {"DATA_A_F32": "1", "B_TYPE": "float16_t", "D_TYPE": "float"}, fp16))
|
|
||||||
tasks.append(string_to_spv(f"{shader_name}_f32_f16_aligned", "mul_mm.comp", base_dict | {"DATA_A_F32": "1", "LOAD_VEC_A": load_vec, "LOAD_VEC_B": load_vec, "B_TYPE": aligned_b_type_f16, "D_TYPE": "float"}, fp16))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv(f"{shader_name}_f16", "mul_mm.comp", base_dict | {"DATA_A_F16": "1", "B_TYPE": "float16_t", "D_TYPE": "float"}, fp16))
|
|
||||||
tasks.append(string_to_spv(f"{shader_name}_f16_aligned", "mul_mm.comp", base_dict | {"DATA_A_F16": "1", "LOAD_VEC_A": load_vec, "LOAD_VEC_B": load_vec, "B_TYPE": aligned_b_type_f16, "D_TYPE": "float"}, fp16))
|
|
||||||
|
|
||||||
for tname in type_names:
|
|
||||||
data_a_key = f"DATA_A_{tname.upper()}"
|
|
||||||
load_vec_a = load_vec if tname in ("f32", "f16") else "2"
|
|
||||||
tasks.append(string_to_spv(f"{shader_name}_{tname}_f32", "mul_mm.comp", base_dict | {data_a_key: "1", "B_TYPE": "float", "D_TYPE": "float"}, fp16))
|
|
||||||
tasks.append(string_to_spv(f"{shader_name}_{tname}_f32_aligned", "mul_mm.comp", base_dict | {data_a_key: "2", "LOAD_VEC_A": load_vec_a, "LOAD_VEC_B": load_vec, "B_TYPE": aligned_b_type_f32, "D_TYPE": "float"}, fp16))
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
logger.info("ggml_vulkan: Generating and compiling shaders to SPIR-V")
|
|
||||||
|
|
||||||
tasks = []
|
|
||||||
|
|
||||||
base_dict = {"FLOAT_TYPE": "float"}
|
|
||||||
|
|
||||||
for fp16 in (False, True):
|
|
||||||
# MUL_MAT
|
|
||||||
matmul_shaders(tasks, fp16, False)
|
|
||||||
# MUL_MAT_ID
|
|
||||||
matmul_shaders(tasks, fp16, True)
|
|
||||||
|
|
||||||
for tname in type_names:
|
|
||||||
# mul mat vec
|
|
||||||
data_a_key = f"DATA_A_{tname.upper()}"
|
|
||||||
shader = f"mul_mat_vec_{tname}.comp" if tname.endswith("_k") else "mul_mat_vec.comp"
|
|
||||||
|
|
||||||
tasks.append(string_to_spv(f"mul_mat_vec_{tname}_f32_f32", shader, base_dict | {data_a_key: "1", "B_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv(f"mul_mat_vec_{tname}_f16_f32", shader, base_dict | {data_a_key: "1", "B_TYPE": "float16_t", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv(f"mul_mat_vec_id_{tname}_f32", shader, base_dict | {"MUL_MAT_ID": "1", data_a_key: "1", "B_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
# Dequant shaders
|
|
||||||
if tname != "f16":
|
|
||||||
tasks.append(string_to_spv(f"dequant_{tname}", f"dequant_{tname}.comp", base_dict | {data_a_key: "1", "D_TYPE": "float16_t"}))
|
|
||||||
|
|
||||||
# get_rows
|
|
||||||
if not tname.endswith("_k"):
|
|
||||||
shader = "get_rows.comp" if tname in ("f32", "f16") else "get_rows_quant.comp"
|
|
||||||
|
|
||||||
if tname == "f16":
|
|
||||||
tasks.append(string_to_spv(f"get_rows_{tname}", shader, {data_a_key: "1", "B_TYPE": "int", "D_TYPE": "float16_t", "OPTIMIZATION_ERROR_WORKAROUND": "1"}))
|
|
||||||
else:
|
|
||||||
tasks.append(string_to_spv(f"get_rows_{tname}", shader, {data_a_key: "1", "B_TYPE": "int", "D_TYPE": "float16_t"}))
|
|
||||||
tasks.append(string_to_spv(f"get_rows_{tname}_f32", shader, {data_a_key: "1", "B_TYPE": "int", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("mul_mat_vec_p021_f16_f32", "mul_mat_vec_p021.comp", {"A_TYPE": "float16_t", "B_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("mul_mat_vec_nc_f16_f32", "mul_mat_vec_nc.comp", {"A_TYPE": "float16_t", "B_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
# Norms
|
|
||||||
tasks.append(string_to_spv("norm_f32", "norm.comp", base_dict | {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("rms_norm_f32", "rms_norm.comp", base_dict | {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("cpy_f32_f32", "copy.comp", {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("cpy_f32_f16", "copy.comp", {"A_TYPE": "float", "D_TYPE": "float16_t"}))
|
|
||||||
tasks.append(string_to_spv("cpy_f16_f16", "copy.comp", {"A_TYPE": "float16_t", "D_TYPE": "float16_t", "OPTIMIZATION_ERROR_WORKAROUND": "1"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("add_f32", "add.comp", {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("split_k_reduce", "mul_mat_split_k_reduce.comp", {}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("mul_f32", "mul.comp", {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("div_f32", "div.comp", {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("scale_f32", "scale.comp", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("sqr_f32", "square.comp", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("clamp_f32", "clamp.comp", {"A_TYPE": "float", "D_TYPE": "float", "FLOAT_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("gelu_f32", "gelu.comp", {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("silu_f32", "silu.comp", {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("relu_f32", "relu.comp", {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("diag_mask_inf_f32", "diag_mask_inf.comp", {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("soft_max_f32", "soft_max.comp", base_dict | {"A_TYPE": "float", "B_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("soft_max_f32_f16", "soft_max.comp", base_dict | {"A_TYPE": "float", "B_TYPE": "float16_t", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("rope_norm_f32", "rope_norm.comp", {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("rope_norm_f16", "rope_norm.comp", {"A_TYPE": "float16_t", "D_TYPE": "float16_t"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("rope_neox_f32", "rope_neox.comp", {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
tasks.append(string_to_spv("rope_neox_f16", "rope_neox.comp", {"A_TYPE": "float16_t", "D_TYPE": "float16_t"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("argsort_f32", "argsort.comp", {"A_TYPE": "float"}))
|
|
||||||
|
|
||||||
tasks.append(string_to_spv("sum_rows_f32", "sum_rows.comp", base_dict | {"A_TYPE": "float", "D_TYPE": "float"}))
|
|
||||||
|
|
||||||
# Helper to decorate tasks with semaphore acquisition.
|
|
||||||
async def withSemaphore(sem, task):
|
|
||||||
async with sem:
|
|
||||||
return await task
|
|
||||||
|
|
||||||
# Run tasks concurrently guarded by a concurrency limit.
|
|
||||||
sem = asyncio.Semaphore(ASYNCIO_CONCURRENCY)
|
|
||||||
await asyncio.gather(*(withSemaphore(sem, task) for task in tasks))
|
|
||||||
|
|
||||||
with open("ggml-vulkan-shaders.hpp", "w") as f:
|
|
||||||
f.write("#include <cstdint>\n\n")
|
|
||||||
for name, path in sorted(shader_fnames):
|
|
||||||
|
|
||||||
with open(path, "rb") as spv:
|
|
||||||
counter = 0
|
|
||||||
newline_counter = 0
|
|
||||||
f.write(f"unsigned char {name}_data[] = {{\n")
|
|
||||||
for val in spv.read():
|
|
||||||
f.write(f"0x{val:02x},")
|
|
||||||
newline_counter += 1
|
|
||||||
counter += 1
|
|
||||||
if newline_counter >= 12:
|
|
||||||
newline_counter = 0
|
|
||||||
f.write("\n")
|
|
||||||
f.write("\n};\n")
|
|
||||||
f.write(f"const uint64_t {name}_len = {counter};\n\n")
|
|
||||||
os.remove(path)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
parser = argparse.ArgumentParser(description="GGML Vulkan Shader Generator")
|
|
||||||
|
|
||||||
parser.add_argument("--glslc", help="Path to glslc")
|
|
||||||
parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
|
|
||||||
|
|
||||||
if args.glslc:
|
|
||||||
GLSLC = args.glslc
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
@ -527,14 +527,11 @@ if (GGML_RPC)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (GGML_VULKAN)
|
if (GGML_VULKAN)
|
||||||
find_package(Vulkan)
|
find_package(Vulkan COMPONENTS glslc REQUIRED)
|
||||||
|
|
||||||
if (Vulkan_FOUND)
|
if (Vulkan_FOUND)
|
||||||
message(STATUS "Vulkan found")
|
message(STATUS "Vulkan found")
|
||||||
|
|
||||||
set(GGML_HEADERS_VULKAN ../include/ggml-vulkan.h)
|
|
||||||
set(GGML_SOURCES_VULKAN ggml-vulkan.cpp)
|
|
||||||
|
|
||||||
list(APPEND GGML_CDEF_PUBLIC GGML_USE_VULKAN)
|
list(APPEND GGML_CDEF_PUBLIC GGML_USE_VULKAN)
|
||||||
|
|
||||||
# Workaround to the "can't dereference invalidated vector iterator" bug in clang-cl debug build
|
# Workaround to the "can't dereference invalidated vector iterator" bug in clang-cl debug build
|
||||||
@ -563,7 +560,37 @@ if (GGML_VULKAN)
|
|||||||
add_compile_definitions(GGML_VULKAN_RUN_TESTS)
|
add_compile_definitions(GGML_VULKAN_RUN_TESTS)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
add_subdirectory(vulkan-shaders)
|
||||||
|
|
||||||
|
set (_ggml_vk_genshaders_cmd vulkan-shaders-gen)
|
||||||
|
set (_ggml_vk_header ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.hpp)
|
||||||
|
set (_ggml_vk_source ${CMAKE_CURRENT_BINARY_DIR}/ggml-vulkan-shaders.cpp)
|
||||||
|
set (_ggml_vk_input_dir ${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders)
|
||||||
|
set (_ggml_vk_output_dir ${CMAKE_CURRENT_BINARY_DIR}/vulkan-shaders.spv)
|
||||||
|
|
||||||
|
file(GLOB _ggml_vk_shader_deps "${_ggml_vk_input_dir}/*.comp")
|
||||||
|
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT ${_ggml_vk_header}
|
||||||
|
${_ggml_vk_source}
|
||||||
|
|
||||||
|
COMMAND ${_ggml_vk_genshaders_cmd}
|
||||||
|
--glslc ${Vulkan_GLSLC_EXECUTABLE}
|
||||||
|
--input-dir ${_ggml_vk_input_dir}
|
||||||
|
--output-dir ${_ggml_vk_output_dir}
|
||||||
|
--target-hpp ${_ggml_vk_header}
|
||||||
|
--target-cpp ${_ggml_vk_source}
|
||||||
|
--no-clean
|
||||||
|
|
||||||
|
DEPENDS ${_ggml_vk_shader_deps}
|
||||||
|
COMMENT "Generate vulkan shaders"
|
||||||
|
)
|
||||||
|
|
||||||
|
set(GGML_HEADERS_VULKAN ${CMAKE_CURRENT_SOURCE_DIR}/../include/ggml-vulkan.h ${_ggml_vk_header})
|
||||||
|
set(GGML_SOURCES_VULKAN ggml-vulkan.cpp ${_ggml_vk_source})
|
||||||
|
|
||||||
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} Vulkan::Vulkan)
|
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} Vulkan::Vulkan)
|
||||||
|
set(GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
else()
|
else()
|
||||||
message(WARNING "Vulkan not found")
|
message(WARNING "Vulkan not found")
|
||||||
endif()
|
endif()
|
||||||
|
File diff suppressed because it is too large
Load Diff
5
ggml/src/vulkan-shaders/CMakeLists.txt
Normal file
5
ggml/src/vulkan-shaders/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
set(TARGET vulkan-shaders-gen)
|
||||||
|
add_executable(${TARGET} vulkan-shaders-gen.cpp)
|
||||||
|
install(TARGETS ${TARGET} RUNTIME)
|
||||||
|
target_compile_features(${TARGET} PRIVATE cxx_std_11)
|
524
ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
Normal file
524
ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
Normal file
@ -0,0 +1,524 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <array>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
#include <future>
|
||||||
|
#include <queue>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h> // For _mkdir on Windows
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define ASYNCIO_CONCURRENCY 64
|
||||||
|
|
||||||
|
std::mutex lock;
|
||||||
|
std::vector<std::pair<std::string, std::string>> shader_fnames;
|
||||||
|
|
||||||
|
std::string GLSLC = "glslc";
|
||||||
|
std::string input_dir = "vulkan-shaders";
|
||||||
|
std::string output_dir = "/tmp";
|
||||||
|
std::string target_hpp = "ggml-vulkan-shaders.hpp";
|
||||||
|
std::string target_cpp = "ggml-vulkan-shaders.cpp";
|
||||||
|
bool no_clean = false;
|
||||||
|
|
||||||
|
const std::vector<std::string> type_names = {
|
||||||
|
"f32",
|
||||||
|
"f16",
|
||||||
|
"q4_0",
|
||||||
|
"q4_1",
|
||||||
|
"q5_0",
|
||||||
|
"q5_1",
|
||||||
|
"q8_0",
|
||||||
|
"q2_k",
|
||||||
|
"q3_k",
|
||||||
|
"q4_k",
|
||||||
|
"q5_k",
|
||||||
|
"q6_k"
|
||||||
|
};
|
||||||
|
|
||||||
|
void execute_command(const std::string& command, std::string& stdout_str, std::string& stderr_str) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
HANDLE stdout_read, stdout_write;
|
||||||
|
HANDLE stderr_read, stderr_write;
|
||||||
|
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
|
||||||
|
|
||||||
|
if (!CreatePipe(&stdout_read, &stdout_write, &sa, 0) ||
|
||||||
|
!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0)) {
|
||||||
|
throw std::runtime_error("Failed to create stdout pipe");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CreatePipe(&stderr_read, &stderr_write, &sa, 0) ||
|
||||||
|
!SetHandleInformation(stderr_read, HANDLE_FLAG_INHERIT, 0)) {
|
||||||
|
throw std::runtime_error("Failed to create stderr pipe");
|
||||||
|
}
|
||||||
|
|
||||||
|
PROCESS_INFORMATION pi;
|
||||||
|
STARTUPINFOA si = { sizeof(STARTUPINFOA) };
|
||||||
|
si.dwFlags = STARTF_USESTDHANDLES;
|
||||||
|
si.hStdOutput = stdout_write;
|
||||||
|
si.hStdError = stderr_write;
|
||||||
|
|
||||||
|
std::vector<char> cmd(command.begin(), command.end());
|
||||||
|
cmd.push_back('\0');
|
||||||
|
|
||||||
|
if (!CreateProcessA(NULL, cmd.data(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
|
||||||
|
throw std::runtime_error("Failed to create process");
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseHandle(stdout_write);
|
||||||
|
CloseHandle(stderr_write);
|
||||||
|
|
||||||
|
std::array<char, 128> buffer;
|
||||||
|
DWORD bytes_read;
|
||||||
|
|
||||||
|
while (ReadFile(stdout_read, buffer.data(), buffer.size(), &bytes_read, NULL) && bytes_read > 0) {
|
||||||
|
stdout_str.append(buffer.data(), bytes_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (ReadFile(stderr_read, buffer.data(), buffer.size(), &bytes_read, NULL) && bytes_read > 0) {
|
||||||
|
stderr_str.append(buffer.data(), bytes_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseHandle(stdout_read);
|
||||||
|
CloseHandle(stderr_read);
|
||||||
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
||||||
|
CloseHandle(pi.hProcess);
|
||||||
|
CloseHandle(pi.hThread);
|
||||||
|
#else
|
||||||
|
int stdout_pipe[2];
|
||||||
|
int stderr_pipe[2];
|
||||||
|
|
||||||
|
if (pipe(stdout_pipe) != 0 || pipe(stderr_pipe) != 0) {
|
||||||
|
throw std::runtime_error("Failed to create pipes");
|
||||||
|
}
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
|
throw std::runtime_error("Failed to fork process");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid == 0) {
|
||||||
|
close(stdout_pipe[0]);
|
||||||
|
close(stderr_pipe[0]);
|
||||||
|
dup2(stdout_pipe[1], STDOUT_FILENO);
|
||||||
|
dup2(stderr_pipe[1], STDERR_FILENO);
|
||||||
|
close(stdout_pipe[1]);
|
||||||
|
close(stderr_pipe[1]);
|
||||||
|
execl("/bin/sh", "sh", "-c", command.c_str(), (char*) nullptr);
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
} else {
|
||||||
|
close(stdout_pipe[1]);
|
||||||
|
close(stderr_pipe[1]);
|
||||||
|
|
||||||
|
std::array<char, 128> buffer;
|
||||||
|
ssize_t bytes_read;
|
||||||
|
|
||||||
|
while ((bytes_read = read(stdout_pipe[0], buffer.data(), buffer.size())) > 0) {
|
||||||
|
stdout_str.append(buffer.data(), bytes_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((bytes_read = read(stderr_pipe[0], buffer.data(), buffer.size())) > 0) {
|
||||||
|
stderr_str.append(buffer.data(), bytes_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(stdout_pipe[0]);
|
||||||
|
close(stderr_pipe[0]);
|
||||||
|
waitpid(pid, nullptr, 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool directory_exists(const std::string& path) {
|
||||||
|
struct stat info;
|
||||||
|
if (stat(path.c_str(), &info) != 0) {
|
||||||
|
return false; // Path doesn't exist or can't be accessed
|
||||||
|
}
|
||||||
|
return (info.st_mode & S_IFDIR) != 0; // Check if it is a directory
|
||||||
|
}
|
||||||
|
|
||||||
|
bool create_directory(const std::string& path) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
return _mkdir(path.c_str()) == 0 || errno == EEXIST; // EEXIST means the directory already exists
|
||||||
|
#else
|
||||||
|
return mkdir(path.c_str(), 0755) == 0 || errno == EEXIST; // 0755 is the directory permissions
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string to_uppercase(const std::string& input) {
|
||||||
|
std::string result = input;
|
||||||
|
for (char& c : result) {
|
||||||
|
c = std::toupper(c);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool string_ends_with(const std::string& str, const std::string& suffix) {
|
||||||
|
if (suffix.size() > str.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
static const char path_separator = '\\';
|
||||||
|
#else
|
||||||
|
static const char path_separator = '/';
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::string join_paths(const std::string& path1, const std::string& path2) {
|
||||||
|
return path1 + path_separator + path2;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string basename(const std::string &path) {
|
||||||
|
return path.substr(path.find_last_of("/\\") + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void string_to_spv(const std::string& _name, const std::string& in_fname, const std::map<std::string, std::string>& defines, bool fp16 = true) {
|
||||||
|
std::string name = _name + (fp16 ? "" : "_fp32");
|
||||||
|
std::string out_fname = join_paths(output_dir, name + ".spv");
|
||||||
|
std::string in_path = join_paths(input_dir, in_fname);
|
||||||
|
|
||||||
|
std::vector<std::string> cmd = {GLSLC, "-fshader-stage=compute", "--target-env=vulkan1.2", "-O", in_path, "-o", out_fname};
|
||||||
|
for (const auto& define : defines) {
|
||||||
|
cmd.push_back("-D" + define.first + "=" + define.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string command;
|
||||||
|
for (const auto& part : cmd) {
|
||||||
|
command += part + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string stdout_str, stderr_str;
|
||||||
|
try {
|
||||||
|
// std::cout << "Executing command: ";
|
||||||
|
// for (const auto& part : cmd) {
|
||||||
|
// std::cout << part << " ";
|
||||||
|
// }
|
||||||
|
// std::cout << std::endl;
|
||||||
|
|
||||||
|
execute_command(command, stdout_str, stderr_str);
|
||||||
|
if (!stderr_str.empty()) {
|
||||||
|
std::cerr << "cannot compile " << name << "\n\n" << command << "\n\n" << stderr_str << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> guard(lock);
|
||||||
|
shader_fnames.push_back(std::make_pair(name, out_fname));
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "Error executing command for " << name << ": " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string, std::string> merge_maps(const std::map<std::string, std::string>& a, const std::map<std::string, std::string>& b) {
|
||||||
|
std::map<std::string, std::string> result = a;
|
||||||
|
result.insert(b.begin(), b.end());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void matmul_shaders(std::vector<std::future<void>>& tasks, bool fp16, bool matmul_id) {
|
||||||
|
std::string load_vec = fp16 ? "8" : "4";
|
||||||
|
std::string aligned_b_type_f32 = fp16 ? "mat2x4" : "vec4";
|
||||||
|
std::string aligned_b_type_f16 = fp16 ? "f16mat2x4" : "f16vec4";
|
||||||
|
|
||||||
|
std::map<std::string, std::string> base_dict = {{"FLOAT_TYPE", fp16 ? "float16_t" : "float"}};
|
||||||
|
std::string shader_name = "matmul";
|
||||||
|
|
||||||
|
if (matmul_id) {
|
||||||
|
base_dict["MUL_MAT_ID"] = "1";
|
||||||
|
shader_name = "matmul_id";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fp16) {
|
||||||
|
base_dict["FLOAT16"] = "1";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shaders with f16 B_TYPE
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv(shader_name + "_f32_f16", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F32", "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}), fp16);
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv(shader_name + "_f32_f16_aligned", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F32", "1"}, {"LOAD_VEC_A", load_vec}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f16}, {"D_TYPE", "float"}}), fp16);
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv(shader_name + "_f16", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F16", "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}), fp16);
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv(shader_name + "_f16_aligned", "mul_mm.comp", merge_maps(base_dict, {{"DATA_A_F16", "1"}, {"LOAD_VEC_A", load_vec}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f16}, {"D_TYPE", "float"}}), fp16);
|
||||||
|
}));
|
||||||
|
|
||||||
|
for (const auto& tname : type_names) {
|
||||||
|
std::string data_a_key = "DATA_A_" + to_uppercase(tname);
|
||||||
|
std::string load_vec_a = (tname == "f32" || tname == "f16") ? load_vec : "2";
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv(shader_name + "_" + tname + "_f32", "mul_mm.comp", merge_maps(base_dict, {{data_a_key, "1"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}), fp16);
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv(shader_name + "_" + tname + "_f32_aligned", "mul_mm.comp", merge_maps(base_dict, {{data_a_key, "2"}, {"LOAD_VEC_A", load_vec_a}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f32}, {"D_TYPE", "float"}}), fp16);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void process_shaders(std::vector<std::future<void>>& tasks) {
|
||||||
|
std::cout << "ggml_vulkan: Generating and compiling shaders to SPIR-V" << std::endl;
|
||||||
|
std::map<std::string, std::string> base_dict = {{"FLOAT_TYPE", "float"}};
|
||||||
|
|
||||||
|
for (const auto& fp16 : {false, true}) {
|
||||||
|
matmul_shaders(tasks, fp16, false);
|
||||||
|
matmul_shaders(tasks, fp16, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& tname : type_names) {
|
||||||
|
// mul mat vec
|
||||||
|
std::string data_a_key = "DATA_A_" + to_uppercase(tname);
|
||||||
|
std::string shader = (string_ends_with(tname, "_k")) ? "mul_mat_vec_" + tname + ".comp" : "mul_mat_vec.comp";
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("mul_mat_vec_" + tname + "_f32_f32", shader, merge_maps(base_dict, {{data_a_key, "1"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("mul_mat_vec_" + tname + "_f16_f32", shader, merge_maps(base_dict, {{data_a_key, "1"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("mul_mat_vec_id_" + tname + "_f32", shader, merge_maps(base_dict, {{"MUL_MAT_ID", "1"}, {data_a_key, "1"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Dequant shaders
|
||||||
|
if (tname != "f16") {
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("dequant_" + tname, "dequant_" + tname + ".comp", merge_maps(base_dict, {{data_a_key, "1"}, {"D_TYPE", "float16_t"}}));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string_ends_with(tname, "_k")) {
|
||||||
|
shader = (tname == "f32" || tname == "f16") ? "get_rows.comp" : "get_rows_quant.comp";
|
||||||
|
|
||||||
|
if (tname == "f16") {
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("get_rows_" + tname, shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}});
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("get_rows_" + tname, shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float16_t"}});
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("get_rows_" + tname + "_f32", shader, {{data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("mul_mat_vec_p021_f16_f32", "mul_mat_vec_p021.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("mul_mat_vec_nc_f16_f32", "mul_mat_vec_nc.comp", {{"A_TYPE", "float16_t"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Norms
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("norm_f32", "norm.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("rms_norm_f32", "rms_norm.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("cpy_f32_f32", "copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("cpy_f32_f16", "copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float16_t"}});
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("cpy_f16_f16", "copy.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("add_f32", "add.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("split_k_reduce", "mul_mat_split_k_reduce.comp", {});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("mul_f32", "mul.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("div_f32", "div.comp", {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("scale_f32", "scale.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("sqr_f32", "square.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("clamp_f32", "clamp.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}, {"FLOAT_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("gelu_f32", "gelu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("silu_f32", "silu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("relu_f32", "relu.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("diag_mask_inf_f32", "diag_mask_inf.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("soft_max_f32", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("soft_max_f32_f16", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("rope_norm_f32", "rope_norm.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("rope_norm_f16", "rope_norm.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("rope_neox_f32", "rope_neox.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("rope_neox_f16", "rope_neox.comp", {{"A_TYPE", "float16_t"}, {"D_TYPE", "float16_t"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [] {
|
||||||
|
string_to_spv("argsort_f32", "argsort.comp", {{"A_TYPE", "float"}});
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push_back(std::async(std::launch::async, [=] {
|
||||||
|
string_to_spv("sum_rows_f32", "sum_rows.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_output_files() {
|
||||||
|
FILE* hdr = fopen(target_hpp.c_str(), "w");
|
||||||
|
FILE* src = fopen(target_cpp.c_str(), "w");
|
||||||
|
|
||||||
|
fprintf(hdr, "#include <cstdint>\n\n");
|
||||||
|
fprintf(src, "#include \"%s\"\n\n", basename(target_hpp).c_str());
|
||||||
|
|
||||||
|
for (const auto& pair : shader_fnames) {
|
||||||
|
const std::string& name = pair.first;
|
||||||
|
const std::string& path = pair.second;
|
||||||
|
FILE* spv = fopen(path.c_str(), "rb");
|
||||||
|
if (!spv) {
|
||||||
|
std::cerr << "Error opening SPIR-V file: " << path << "\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(spv, 0, SEEK_END);
|
||||||
|
size_t size = ftell(spv);
|
||||||
|
fseek(spv, 0, SEEK_SET);
|
||||||
|
|
||||||
|
std::vector<unsigned char> data(size);
|
||||||
|
size_t read_size = fread(data.data(), 1, size, spv);
|
||||||
|
fclose(spv);
|
||||||
|
if (read_size != size) {
|
||||||
|
std::cerr << "Error reading SPIR-V file: " << path << "\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(hdr, "extern unsigned char %s_data[%zu];\n", name.c_str(), size);
|
||||||
|
fprintf(hdr, "const uint64_t %s_len = %zu;\n\n", name.c_str(), size);
|
||||||
|
|
||||||
|
fprintf(src, "unsigned char %s_data[%zu] = {\n", name.c_str(), size);
|
||||||
|
for (size_t i = 0; i < size; ++i) {
|
||||||
|
fprintf(src, "0x%02x,", data[i]);
|
||||||
|
if ((i + 1) % 12 == 0) fprintf(src, "\n");
|
||||||
|
}
|
||||||
|
fprintf(src, "\n};\n\n");
|
||||||
|
|
||||||
|
if (!no_clean) {
|
||||||
|
std::remove(path.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(hdr);
|
||||||
|
fclose(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
std::map<std::string, std::string> args;
|
||||||
|
for (int i = 1; i < argc; i += 2) {
|
||||||
|
if (i + 1 < argc) {
|
||||||
|
args[argv[i]] = argv[i + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.find("--glslc") != args.end()) {
|
||||||
|
GLSLC = args["--glslc"]; // Path to glslc
|
||||||
|
}
|
||||||
|
if (args.find("--input-dir") != args.end()) {
|
||||||
|
input_dir = args["--input-dir"]; // Directory containing shader sources
|
||||||
|
}
|
||||||
|
if (args.find("--output-dir") != args.end()) {
|
||||||
|
output_dir = args["--output-dir"]; // Directory for containing SPIR-V output
|
||||||
|
}
|
||||||
|
if (args.find("--target-hpp") != args.end()) {
|
||||||
|
target_hpp = args["--target-hpp"]; // Path to generated header file
|
||||||
|
}
|
||||||
|
if (args.find("--target-cpp") != args.end()) {
|
||||||
|
target_cpp = args["--target-cpp"]; // Path to generated cpp file
|
||||||
|
}
|
||||||
|
if (args.find("--no-clean") != args.end()) {
|
||||||
|
no_clean = true; // Keep temporary SPIR-V files in output-dir after build
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!directory_exists(input_dir)) {
|
||||||
|
std::cerr << "\"" << input_dir << "\" must be a valid directory containing shader sources" << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!directory_exists(output_dir)) {
|
||||||
|
if (!create_directory(output_dir)) {
|
||||||
|
std::cerr << "Error creating output directory: " << output_dir << "\n";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::future<void>> tasks;
|
||||||
|
process_shaders(tasks);
|
||||||
|
|
||||||
|
for (auto& task : tasks) {
|
||||||
|
task.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
write_output_files();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user