Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 1 | # Getting Started on Linux with Vulkan |
| 2 | |
| 3 | [Vulkan](https://www.khronos.org/vulkan/) is a new generation graphics and |
| 4 | compute API that provides high-efficiency, cross-platform access to modern GPUs |
| 5 | used in a wide variety of devices from PCs and consoles to mobile phones and |
| 6 | embedded platforms. |
| 7 | |
| 8 | IREE includes a Vulkan/[SPIR-V](https://www.khronos.org/registry/spir-v/) HAL |
| 9 | backend designed for executing advanced ML models in a deeply pipelined and |
| 10 | tightly integrated fashion on accelerators like GPUs. |
| 11 | |
| 12 | This guide will walk you through using IREE's compiler and runtime Vulkan |
Lei Zhang | 653bfc6 | 2020-05-12 15:15:44 -0700 | [diff] [blame] | 13 | components. For generic Vulkan development environment set up and trouble |
| 14 | shooting, please see [this doc](generic_vulkan_env_setup.md). |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 15 | |
| 16 | ## Prerequisites |
| 17 | |
| 18 | You 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 |
| 21 | instructions. |
| 22 | |
| 23 | You may have a physical GPU with drivers supporting Vulkan. We also support |
| 24 | using [SwiftShader](https://swiftshader.googlesource.com/SwiftShader/) (a high |
| 25 | performance CPU-based implementation of Vulkan). |
| 26 | |
scotttodd | 354b691 | 2020-06-24 16:29:35 -0700 | [diff] [blame] | 27 | Vulkan drivers implementing API version >= 1.2 are recommended. IREE requires |
| 28 | the `VK_KHR_timeline_semaphore` extension (part of Vulkan 1.2), though it is |
| 29 | able to emulate it, with performance costs, as necessary. |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 30 | |
| 31 | ## Vulkan Setup |
| 32 | |
| 33 | ### Background |
| 34 | |
Lei Zhang | 653bfc6 | 2020-05-12 15:15:44 -0700 | [diff] [blame] | 35 | Please see |
| 36 | [Generic Vulkan Development Environment Setup and Troubleshooting](generic_vulkan_env_setup.md) |
| 37 | for generic Vulkan concepts and development environment setup. |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 38 | |
| 39 | ### Quick Start |
| 40 | |
| 41 | The |
Scott Todd | 4e5167d | 2020-06-29 09:30:35 -0700 | [diff] [blame] | 42 | [dynamic_symbols_test](https://github.com/google/iree/blob/main/iree/hal/vulkan/dynamic_symbols_test.cc) |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 43 | checks if the Vulkan loader and a valid ICD are accessible. |
| 44 | |
| 45 | Run the test: |
| 46 | |
| 47 | ```shell |
| 48 | # -- CMake -- |
Stella Laurenzo | 354b34a | 2020-04-21 11:47:55 -0700 | [diff] [blame] | 49 | $ export VK_LOADER_DEBUG=all |
Scott Todd | 2edc7d6 | 2020-10-23 16:37:16 -0700 | [diff] [blame] | 50 | $ cmake --build ../iree-build/ --target iree_hal_vulkan_dynamic_symbols_test |
| 51 | $ ../iree-build/iree/hal/vulkan/iree_hal_vulkan_dynamic_symbols_test |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 52 | |
| 53 | # -- Bazel -- |
Scott Todd | 9c4aea2 | 2020-04-07 13:24:17 -0700 | [diff] [blame] | 54 | $ bazel test iree/hal/vulkan:dynamic_symbols_test --test_env=VK_LOADER_DEBUG=all |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 55 | ``` |
| 56 | |
| 57 | Tests in IREE's HAL "Conformance Test Suite" (CTS) actually exercise the Vulkan |
| 58 | HAL, which includes checking for supported layers and extensions. |
| 59 | |
| 60 | Run the |
Lei Zhang | 6171a98 | 2020-09-08 12:48:25 -0400 | [diff] [blame] | 61 | [driver test](https://github.com/google/iree/blob/main/iree/hal/cts/driver_test.cc): |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 62 | |
| 63 | ```shell |
| 64 | # -- CMake -- |
Stella Laurenzo | 354b34a | 2020-04-21 11:47:55 -0700 | [diff] [blame] | 65 | $ export VK_LOADER_DEBUG=all |
Scott Todd | 2edc7d6 | 2020-10-23 16:37:16 -0700 | [diff] [blame] | 66 | $ cmake --build ../iree-build/ --target iree_hal_cts_driver_test |
| 67 | $ ../iree-build/iree/hal/cts/iree_hal_cts_driver_test |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 68 | |
| 69 | # -- Bazel -- |
Lei Zhang | 6171a98 | 2020-09-08 12:48:25 -0400 | [diff] [blame] | 70 | $ bazel test iree/hal/cts:driver_test --test_env=VK_LOADER_DEBUG=all --test_output=all |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 71 | ``` |
| 72 | |
| 73 | If these tests pass, you can skip down to the next section. |
| 74 | |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 75 | ### Setting up SwiftShader |
| 76 | |
| 77 | If your system lacks a physical GPU with compatible Vulkan drivers, or you just |
| 78 | want to use a software driver for predictable performance, you can set up |
| 79 | SwiftShader's Vulkan ICD (Installable Client Driver). |
| 80 | |
| 81 | IREE has a |
Scott Todd | 4e5167d | 2020-06-29 09:30:35 -0700 | [diff] [blame] | 82 | [helper script](https://github.com/google/iree/blob/main/build_tools/third_party/swiftshader/build_vk_swiftshader.sh) |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 83 | for 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 Meadowlark | b214d23 | 2020-10-28 13:08:48 -0700 | [diff] [blame] | 91 | After building, the script will prompt your to add a variable `VK_ICD_FILENAMES` |
| 92 | to your environment to tell the Vulkan loader to use the ICD. Assuming it was |
| 93 | installed in the default location, this can be done via: |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 94 | |
| 95 | ```shell |
Phoenix Meadowlark | b214d23 | 2020-10-28 13:08:48 -0700 | [diff] [blame] | 96 | $ export VK_ICD_FILENAMES="${HOME?}/.swiftshader/Linux/vk_swiftshader_icd.json" |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 97 | ``` |
| 98 | |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 99 | ### Support in Bazel Tests |
| 100 | |
| 101 | Bazel tests run in a sandbox, which environment variables may be forwarded to |
| 102 | using the `--test_env` flag. A user.bazelrc file supporting each of the steps |
Scott Todd | 9c4aea2 | 2020-04-07 13:24:17 -0700 | [diff] [blame] | 103 | above looks like this (substitute for the `{}` paths): |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 104 | |
| 105 | ``` |
| 106 | test --test_env="LD_LIBRARY_PATH={PATH_TO_VULKAN_SDK}/x86_64/lib/" |
| 107 | test --test_env="LD_PRELOAD=libvulkan.so.1" |
| 108 | test --test_env="VK_ICD_FILENAMES={PATH_TO_IREE}/build-swiftshader/Linux/vk_swiftshader_icd.json" |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 109 | ``` |
| 110 | |
| 111 | ## Using IREE's Vulkan Compiler Target and Runtime Driver |
| 112 | |
| 113 | ### Compiling for the Vulkan HAL |
| 114 | |
| 115 | Pass the flag `-iree-hal-target-backends=vulkan-spirv` to `iree-translate`: |
| 116 | |
| 117 | ```shell |
| 118 | # -- CMake -- |
Scott Todd | 2edc7d6 | 2020-10-23 16:37:16 -0700 | [diff] [blame] | 119 | $ 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 Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 121 | |
| 122 | # -- Bazel -- |
Scott Todd | 2edc7d6 | 2020-10-23 16:37:16 -0700 | [diff] [blame] | 123 | $ 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 Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 124 | ``` |
| 125 | |
| 126 | > Tip:<br> |
| 127 | > 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 | |
| 132 | Pass the flag `-driver=vulkan` to `iree-run-module`: |
| 133 | |
| 134 | ```shell |
| 135 | # -- CMake -- |
Scott Todd | 2edc7d6 | 2020-10-23 16:37:16 -0700 | [diff] [blame] | 136 | $ 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 Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 138 | |
| 139 | # -- Bazel -- |
Scott Todd | 2edc7d6 | 2020-10-23 16:37:16 -0700 | [diff] [blame] | 140 | $ bazel run iree/tools:iree-run-module -- -module_file=/tmp/module.vmfb -driver=vulkan -entry_function=abs -function_inputs="i32=-2" |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 141 | ``` |
| 142 | |
| 143 | ## Running IREE's Vulkan Samples |
| 144 | |
| 145 | Install the [Vulkan SDK](https://www.lunarg.com/vulkan-sdk/), then run: |
| 146 | |
| 147 | ```shell |
| 148 | # -- CMake -- |
Scott Todd | 2edc7d6 | 2020-10-23 16:37:16 -0700 | [diff] [blame] | 149 | $ cmake --build ../iree-build/ --target iree_samples_vulkan_vulkan_inference_gui |
| 150 | $ ../iree-build/iree/samples/vulkan/vulkan_inference_gui |
Scott Todd | 9313ccc | 2020-04-06 15:23:17 -0700 | [diff] [blame] | 151 | |
| 152 | # -- Bazel -- |
| 153 | $ bazel run iree/samples/vulkan:vulkan_inference_gui |
| 154 | ``` |