Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vulkan : cmake integration #8119

Merged
merged 40 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4eadfb1
Add Vulkan to CMake pkg
bandoti Jun 24, 2024
c46a789
Add Sycl to CMake pkg
bandoti Jun 24, 2024
36b081a
Add OpenMP to CMake pkg
bandoti Jun 24, 2024
29ba5a8
Split generated shader file into separate translation unit
bandoti Jun 25, 2024
1b4759f
Add CMake target for Vulkan shaders
bandoti Jun 25, 2024
cb3ec88
Merge branch 'ggerganov:master' into vulkan-build-integration
bandoti Jun 25, 2024
37bcad7
Update README.md
bandoti Jun 25, 2024
b1a70fc
Merge branch 'ggerganov:master' into vulkan-build-integration
bandoti Jun 25, 2024
dd198ce
Merge branch 'ggerganov:master' into vulkan-build-integration
bandoti Jun 25, 2024
491a967
Add make target for Vulkan shaders
bandoti Jun 25, 2024
99c3027
Use pkg-config to locate vulkan library
bandoti Jun 26, 2024
8859546
Add vulkan SDK dep to ubuntu-22-cmake-vulkan workflow
bandoti Jun 26, 2024
c61cd05
Clean up tabs
bandoti Jun 26, 2024
2318cad
Move sudo to apt-key invocation
bandoti Jun 26, 2024
a1495e7
Merge branch 'master' into vulkan-build-integration
bandoti Jun 26, 2024
6571046
Forward GGML_EXTRA_LIBS to CMake config pkg
bandoti Jun 26, 2024
d053004
Update vulkan obj file paths
bandoti Jun 27, 2024
d0d825f
Add shaderc to nix pkg
bandoti Jun 27, 2024
eec17a6
Add python3 to Vulkan nix build
bandoti Jun 27, 2024
8590508
Link against ggml in cmake pkg
bandoti Jun 27, 2024
ac9a065
Remove Python dependency from Vulkan build
bandoti Jun 30, 2024
4eab311
Merge branch 'ggerganov:master' into vulkan-build-integration
bandoti Jun 30, 2024
9bca872
code review changes
bandoti Jul 1, 2024
422bfb3
Merge branch 'master' into vulkan-build-integration
bandoti Jul 1, 2024
22323d5
Merge branch 'master' into vulkan-build-integration
bandoti Jul 2, 2024
2f5a0e8
Remove trailing newline
bandoti Jul 2, 2024
a85b5d8
Merge branch 'master' into vulkan-build-integration
bandoti Jul 2, 2024
019e4a3
Add cflags from pkg-config to fix w64devkit build
bandoti Jul 2, 2024
3a554ae
Update README.md
bandoti Jul 2, 2024
dafcaf1
Remove trailing whitespace
bandoti Jul 2, 2024
4226103
Update README.md
bandoti Jul 3, 2024
613a3c6
Remove trailing whitespace
bandoti Jul 3, 2024
f638ade
Merge branch 'master' into vulkan-build-integration
bandoti Jul 5, 2024
02e65ad
Fix doc heading
bandoti Jul 5, 2024
6b5c5af
Merge branch 'ggerganov:master' into vulkan-build-integration
bandoti Jul 7, 2024
50da329
Merge branch 'master' into vulkan-build-integration
bandoti Jul 8, 2024
f47829a
Merge branch 'master' into vulkan-build-integration
bandoti Jul 10, 2024
9501f66
Make glslc required Vulkan component
bandoti Jul 10, 2024
88fd99b
remove clblast from nix pkg
bandoti Jul 12, 2024
24fd7d3
Merge branch 'master' into vulkan-build-integration
bandoti Jul 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .devops/nix/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
vulkan-headers,
vulkan-loader,
curl,
shaderc,
clblast,
0cc4m marked this conversation as resolved.
Show resolved Hide resolved
useBlas ? builtins.all (x: !x) [
useCuda
useMetalKit
Expand Down Expand Up @@ -146,6 +148,7 @@ let
vulkanBuildInputs = [
vulkan-headers
vulkan-loader
shaderc
];
in

Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,10 @@ jobs:
- name: Dependencies
id: depends
run: |
sudo apt-get update
sudo apt-get install build-essential libvulkan-dev
wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add -
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
id: cmake_build
Expand Down
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is needed - an example with a specific flag that is not exported would help. It's OK to merge as it is though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggerganov I will take a closer look at this. The flags which should be exported should be all interface defines (public interface only) on the ggml/llama libraries. I will follow up with a new pull request.


set_target_properties(llama PROPERTIES PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/llama.h)
install(TARGETS llama LIBRARY PUBLIC_HEADER)
Expand Down
35 changes: 29 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ ifdef GGML_RPC
BUILD_TARGETS += rpc-server
endif

ifdef GGML_VULKAN
BUILD_TARGETS += vulkan-shaders-gen
endif

default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)

test: $(TEST_TARGETS)
Expand Down Expand Up @@ -704,8 +708,8 @@ endif # GGML_CUDA

ifdef GGML_VULKAN
MK_CPPFLAGS += -DGGML_USE_VULKAN
MK_LDFLAGS += -lvulkan
OBJ_GGML += ggml/src/ggml-vulkan.o
MK_LDFLAGS += $(shell pkg-config --libs vulkan)
OBJ_GGML += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o

ifdef GGML_VULKAN_CHECK_RESULTS
MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
Expand All @@ -727,10 +731,28 @@ ifdef GGML_VULKAN_RUN_TESTS
MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
endif

ggml/src/ggml-vulkan.o: \
ggml/src/ggml-vulkan.cpp \
ggml/include/ggml-vulkan.h
$(CXX) $(CXXFLAGS) -c $< -o $@
GLSLC_CMD = glslc
_ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
_ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
_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

ifdef GGML_HIPBLAS
Expand Down Expand Up @@ -1102,6 +1124,7 @@ clean:
rm -vrf ggml/src/ggml-cuda/template-instances/*.o
rm -rvf $(BUILD_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)
find examples pocs -type f -name "*.o" -delete

Expand Down
29 changes: 27 additions & 2 deletions cmake/llama-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ set(GGML_CUDA @GGML_CUDA@)
set(GGML_METAL @GGML_METAL@)
set(GGML_HIPBLAS @GGML_HIPBLAS@)
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@

Expand Down Expand Up @@ -37,18 +44,36 @@ if (GGML_METAL)
find_library(METALKIT_FRAMEWORK MetalKit REQUIRED)
endif()

if (GGML_VULKAN)
find_package(Vulkan REQUIRED)
endif()

if (GGML_HIPBLAS)
find_package(hip REQUIRED)
find_package(hipblas REQUIRED)
find_package(rocblas REQUIRED)
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
REQUIRED
HINTS ${LLAMA_LIB_DIR})

set(_llama_link_deps "Threads::Threads" "@LLAMA_EXTRA_LIBS@")
set(_llama_transient_defines "@LLAMA_TRANSIENT_DEFINES@")
set(_llama_link_deps "${ggml_LIBRARY}" "@GGML_LINK_LIBRARIES@")
set(_llama_transient_defines "@GGML_TRANSIENT_DEFINES@")

add_library(llama UNKNOWN IMPORTED)

Expand Down
39 changes: 39 additions & 0 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,45 @@ The following compilation options are also available to tweak performance (yes,

### 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**:

You don't need to install Vulkan SDK. It will be installed inside the container.
Expand Down
2 changes: 2 additions & 0 deletions ggml/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/ggml-vulkan-shaders.hpp
src/ggml-vulkan-shaders.cpp
Loading
Loading