blob: 0133538e77834485d3c3e9ac8791f110467d8695 [file] [log] [blame] [view]
Scott Todd9313ccc2020-04-06 15:23:17 -07001# Getting Started on Linux with Vulkan
2
3[Vulkan](https://www.khronos.org/vulkan/) is a new generation graphics and
4compute API that provides high-efficiency, cross-platform access to modern GPUs
5used in a wide variety of devices from PCs and consoles to mobile phones and
6embedded platforms.
7
8IREE includes a Vulkan/[SPIR-V](https://www.khronos.org/registry/spir-v/) HAL
9backend designed for executing advanced ML models in a deeply pipelined and
10tightly integrated fashion on accelerators like GPUs.
11
12This guide will walk you through using IREE's compiler and runtime Vulkan
Lei Zhang653bfc62020-05-12 15:15:44 -070013components. For generic Vulkan development environment set up and trouble
14shooting, please see [this doc](generic_vulkan_env_setup.md).
Scott Todd9313ccc2020-04-06 15:23:17 -070015
16## Prerequisites
17
18You should already have IREE cloned and building on your Linux machine. See the
19[Getting Started on Linux with CMake](getting_started_linux_cmake.md) or
20[Getting Started on Linux with Bazel](getting_started_linux_bazel.md) guide for
21instructions.
22
23You may have a physical GPU with drivers supporting Vulkan. We also support
24using [SwiftShader](https://swiftshader.googlesource.com/SwiftShader/) (a high
25performance CPU-based implementation of Vulkan).
26
scotttodd354b6912020-06-24 16:29:35 -070027Vulkan drivers implementing API version >= 1.2 are recommended. IREE requires
28the `VK_KHR_timeline_semaphore` extension (part of Vulkan 1.2), though it is
29able to emulate it, with performance costs, as necessary.
Scott Todd9313ccc2020-04-06 15:23:17 -070030
31## Vulkan Setup
32
33### Background
34
Lei Zhang653bfc62020-05-12 15:15:44 -070035Please see
36[Generic Vulkan Development Environment Setup and Troubleshooting](generic_vulkan_env_setup.md)
37for generic Vulkan concepts and development environment setup.
Scott Todd9313ccc2020-04-06 15:23:17 -070038
39### Quick Start
40
41The
Scott Todd4e5167d2020-06-29 09:30:35 -070042[dynamic_symbols_test](https://github.com/google/iree/blob/main/iree/hal/vulkan/dynamic_symbols_test.cc)
Scott Todd9313ccc2020-04-06 15:23:17 -070043checks if the Vulkan loader and a valid ICD are accessible.
44
45Run the test:
46
47```shell
48# -- CMake --
Stella Laurenzo354b34a2020-04-21 11:47:55 -070049$ export VK_LOADER_DEBUG=all
Scott Todd2edc7d62020-10-23 16:37:16 -070050$ cmake --build ../iree-build/ --target iree_hal_vulkan_dynamic_symbols_test
51$ ../iree-build/iree/hal/vulkan/iree_hal_vulkan_dynamic_symbols_test
Scott Todd9313ccc2020-04-06 15:23:17 -070052
53# -- Bazel --
Scott Todd9c4aea22020-04-07 13:24:17 -070054$ bazel test iree/hal/vulkan:dynamic_symbols_test --test_env=VK_LOADER_DEBUG=all
Scott Todd9313ccc2020-04-06 15:23:17 -070055```
56
57Tests in IREE's HAL "Conformance Test Suite" (CTS) actually exercise the Vulkan
58HAL, which includes checking for supported layers and extensions.
59
60Run the
Lei Zhang6171a982020-09-08 12:48:25 -040061[driver test](https://github.com/google/iree/blob/main/iree/hal/cts/driver_test.cc):
Scott Todd9313ccc2020-04-06 15:23:17 -070062
63```shell
64# -- CMake --
Stella Laurenzo354b34a2020-04-21 11:47:55 -070065$ export VK_LOADER_DEBUG=all
Scott Todd2edc7d62020-10-23 16:37:16 -070066$ cmake --build ../iree-build/ --target iree_hal_cts_driver_test
67$ ../iree-build/iree/hal/cts/iree_hal_cts_driver_test
Scott Todd9313ccc2020-04-06 15:23:17 -070068
69# -- Bazel --
Lei Zhang6171a982020-09-08 12:48:25 -040070$ bazel test iree/hal/cts:driver_test --test_env=VK_LOADER_DEBUG=all --test_output=all
Scott Todd9313ccc2020-04-06 15:23:17 -070071```
72
73If these tests pass, you can skip down to the next section.
74
Scott Todd9313ccc2020-04-06 15:23:17 -070075### Setting up SwiftShader
76
77If your system lacks a physical GPU with compatible Vulkan drivers, or you just
78want to use a software driver for predictable performance, you can set up
79SwiftShader's Vulkan ICD (Installable Client Driver).
80
81IREE has a
Scott Todd4e5167d2020-06-29 09:30:35 -070082[helper script](https://github.com/google/iree/blob/main/build_tools/third_party/swiftshader/build_vk_swiftshader.sh)
Scott Todd9313ccc2020-04-06 15:23:17 -070083for building SwiftShader from source using CMake:
84
85```shell
86$ bash build_tools/third_party/swiftshader/build_vk_swiftshader.sh
87```
88
89<!-- TODO(scotttodd): Steps to download prebuilt binaries when they exist -->
90
Phoenix Meadowlarkb214d232020-10-28 13:08:48 -070091After building, the script will prompt your to add a variable `VK_ICD_FILENAMES`
92to your environment to tell the Vulkan loader to use the ICD. Assuming it was
93installed in the default location, this can be done via:
Scott Todd9313ccc2020-04-06 15:23:17 -070094
95```shell
Phoenix Meadowlarkb214d232020-10-28 13:08:48 -070096$ export VK_ICD_FILENAMES="${HOME?}/.swiftshader/Linux/vk_swiftshader_icd.json"
Scott Todd9313ccc2020-04-06 15:23:17 -070097```
98
Scott Todd9313ccc2020-04-06 15:23:17 -070099### Support in Bazel Tests
100
101Bazel tests run in a sandbox, which environment variables may be forwarded to
102using the `--test_env` flag. A user.bazelrc file supporting each of the steps
Scott Todd9c4aea22020-04-07 13:24:17 -0700103above looks like this (substitute for the `{}` paths):
Scott Todd9313ccc2020-04-06 15:23:17 -0700104
105```
106test --test_env="LD_LIBRARY_PATH={PATH_TO_VULKAN_SDK}/x86_64/lib/"
107test --test_env="LD_PRELOAD=libvulkan.so.1"
108test --test_env="VK_ICD_FILENAMES={PATH_TO_IREE}/build-swiftshader/Linux/vk_swiftshader_icd.json"
Scott Todd9313ccc2020-04-06 15:23:17 -0700109```
110
111## Using IREE's Vulkan Compiler Target and Runtime Driver
112
113### Compiling for the Vulkan HAL
114
115Pass the flag `-iree-hal-target-backends=vulkan-spirv` to `iree-translate`:
116
117```shell
118# -- CMake --
Scott Todd2edc7d62020-10-23 16:37:16 -0700119$ cmake --build ../iree-build/ --target iree_tools_iree-translate
120$ ../iree-build/iree/tools/iree-translate -iree-mlir-to-vm-bytecode-module -iree-hal-target-backends=vulkan-spirv ./iree/tools/test/simple.mlir -o /tmp/module.vmfb
Scott Todd9313ccc2020-04-06 15:23:17 -0700121
122# -- Bazel --
Scott Todd2edc7d62020-10-23 16:37:16 -0700123$ bazel run iree/tools:iree-translate -- -iree-mlir-to-vm-bytecode-module -iree-hal-target-backends=vulkan-spirv $PWD/iree/tools/test/simple.mlir -o /tmp/module.vmfb
Scott Todd9313ccc2020-04-06 15:23:17 -0700124```
125
126> Tip:<br>
127> &nbsp;&nbsp;&nbsp;&nbsp;If successful, this may have no output. You can pass
128> other flags like `-print-ir-after-all` to control the program.
129
130### Executing modules with the Vulkan driver
131
132Pass the flag `-driver=vulkan` to `iree-run-module`:
133
134```shell
135# -- CMake --
Scott Todd2edc7d62020-10-23 16:37:16 -0700136$ cmake --build ../iree-build/ --target iree_tools_iree-run-module
137$ ../iree-build/iree/tools/iree-run-module -module_file=/tmp/module.vmfb -driver=vulkan -entry_function=abs -function_inputs="i32=-2"
Scott Todd9313ccc2020-04-06 15:23:17 -0700138
139# -- Bazel --
Scott Todd2edc7d62020-10-23 16:37:16 -0700140$ bazel run iree/tools:iree-run-module -- -module_file=/tmp/module.vmfb -driver=vulkan -entry_function=abs -function_inputs="i32=-2"
Scott Todd9313ccc2020-04-06 15:23:17 -0700141```
142
143## Running IREE's Vulkan Samples
144
145Install the [Vulkan SDK](https://www.lunarg.com/vulkan-sdk/), then run:
146
147```shell
148# -- CMake --
Scott Todd2edc7d62020-10-23 16:37:16 -0700149$ cmake --build ../iree-build/ --target iree_samples_vulkan_vulkan_inference_gui
150$ ../iree-build/iree/samples/vulkan/vulkan_inference_gui
Scott Todd9313ccc2020-04-06 15:23:17 -0700151
152# -- Bazel --
153$ bazel run iree/samples/vulkan:vulkan_inference_gui
154```