Skip to content

Commit db04992

Browse files
committed
Vulkan2 Runtime API
1 parent 88f9bfd commit db04992

File tree

13 files changed

+1671
-1440
lines changed

13 files changed

+1671
-1440
lines changed

apps/android_rpc/app/src/main/jni/tvm_runtime.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@
6262
#endif
6363

6464
#ifdef TVM_VULKAN_RUNTIME
65-
#include "../src/runtime/vulkan/vulkan_device_api.cc"
66-
#include "../src/runtime/vulkan/vulkan_module.cc"
65+
#include "../src/runtime/vulkan/vulkan.cc"
6766
#endif
6867

6968
#ifdef USE_SORT

cmake/modules/Vulkan.cmake

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
# Be compatible with older version of CMake
1919
find_vulkan(${USE_VULKAN})
2020

21+
# Extra Vulkan runtime options, exposed for advanced users.
22+
tvm_option(USE_VULKAN_IMMEDIATE_MODE "Use Vulkan Immediate mode
23+
(KHR_push_descriptor extension)" ON IF USE_VULKAN)
24+
tvm_option(USE_VULKAN_DEDICATED_ALLOCATION "Use Vulkan dedicated allocations" ON
25+
IF USE_VULKAN)
26+
tvm_option(USE_VULKAN_VALIDATION "Enable Vulkan API validation layers" OFF
27+
IF USE_VULKAN)
28+
2129
if(Vulkan_FOUND)
2230
# always set the includedir
2331
# avoid global retrigger of cmake
@@ -28,12 +36,24 @@ if(USE_VULKAN)
2836
if(NOT Vulkan_FOUND)
2937
message(FATAL_ERROR "Cannot find Vulkan, USE_VULKAN=" ${USE_VULKAN})
3038
endif()
31-
message(STATUS "Build with VULKAN support")
32-
file(GLOB RUNTIME_VULKAN_SRCS src/runtime/vulkan/*.cc)
39+
message(STATUS "Build with Vulkan support")
40+
file(GLOB RUNTIME_VULKAN_SRCS src/runtime/vulkan/vulkan.cc)
3341
file(GLOB COMPILER_VULKAN_SRCS src/codegen/spirv/*.cc)
3442
list(APPEND RUNTIME_SRCS ${RUNTIME_VULKAN_SRCS})
3543
list(APPEND COMPILER_SRCS ${COMPILER_VULKAN_SRCS})
36-
3744
list(APPEND TVM_LINKER_LIBS ${Vulkan_SPIRV_TOOLS_LIBRARY})
3845
list(APPEND TVM_RUNTIME_LINKER_LIBS ${Vulkan_LIBRARY})
46+
47+
if(USE_VULKAN_IMMEDIATE_MODE)
48+
message(STATUS "Build with Vulkan immediate mode")
49+
add_definitions(-DUSE_VULKAN_IMMEDIATE_MODE=1)
50+
endif()
51+
if(USE_VULKAN_DEDICATED_ALLOCATION)
52+
message(STATUS "Build with Vulkan dedicated allocation")
53+
add_definitions(-DUSE_VULKAN_DEDICATED_ALLOCATION=1)
54+
endif()
55+
if(USE_VULKAN_VALIDATION)
56+
message(STATUS "Build with Vulkan API validation")
57+
add_definitions(-DUSE_VULKAN_VALIDATION=1)
58+
endif()
3959
endif(USE_VULKAN)

src/codegen/spirv/build_vulkan.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "codegen_spirv.h"
3131
#include "../build_common.h"
32+
33+
#include "../../runtime/vulkan/vulkan_shader.h"
3234
#include "../../runtime/vulkan/vulkan_module.h"
3335

3436
namespace tvm {

src/codegen/spirv/ir_builder.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ namespace spirv {
3333
void IRBuilder::InitHeader() {
3434
CHECK_EQ(header_.size(), 0U);
3535
header_.push_back(spv::MagicNumber);
36-
header_.push_back(spv::Version);
36+
// Use SPIR-V v1.0. This needs to be kept in sync (or at least behind)
37+
// `VkApplicationInfo.apiVersion` in `vulkan.cc` to ensure Vulkan API
38+
// validation passes.
39+
header_.push_back(0x10000);
3740
// generator: set to 0, unknown
3841
header_.push_back(0U);
3942
// Bound: set during Finalize

src/runtime/vulkan/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
2+
<!--- or more contributor license agreements. See the NOTICE file -->
3+
<!--- distributed with this work for additional information -->
4+
<!--- regarding copyright ownership. The ASF licenses this file -->
5+
<!--- to you under the Apache License, Version 2.0 (the -->
6+
<!--- "License"); you may not use this file except in compliance -->
7+
<!--- with the License. You may obtain a copy of the License at -->
8+
9+
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
10+
11+
<!--- Unless required by applicable law or agreed to in writing, -->
12+
<!--- software distributed under the License is distributed on an -->
13+
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
14+
<!--- KIND, either express or implied. See the License for the -->
15+
<!--- specific language governing permissions and limitations -->
16+
<!--- under the License. -->
17+
18+
19+
## Components
20+
21+
### VulkanDeviceAPI
22+
23+
Implements the TVM DeviceAPI interface. Owns the core Vulkan datastructures. Is
24+
responsible for initializing the Vulkan instance and devices, querying for
25+
possible extensions.
26+
27+
### VulkanThreadEntry
28+
29+
Thread-local state for the Vulkan runtime. Maintains a staging buffer (for
30+
copies), and a VulkanStream per device.
31+
32+
### VulkanWrappedFunc
33+
34+
Responsible for launching computation kernels. Responsible for obtaining a
35+
VulkanPipeline instance (from the VulkanModuleNode), and launches the kernel
36+
(via immediate or deferred mode) on the active VulkanStream instance.
37+
38+
## Stream execution in the Vulkan programming model.
39+
40+
The natural model for TVM DeviceAPI implementation and runtime follows the CUDA
41+
API model. That is, we launch "kernels" onto a (implicit or explicit) "stream"
42+
(which execute asynchronously with respect to the host, but ordered with respect
43+
to the stream), and explicitly synchronize the stream with respect to the host.
44+
We simulate this behaviour in the Vulkan model by maintaining a thread-local
45+
`vkCommandBuffer` instance, and queueing up (or eagerly executing, depending on
46+
the availability of the `VK_KHR_push_descriptor` extension). When we synchronize
47+
the stream, we end the command buffer recording, submit it to the device queue,
48+
and wait on the corresponding fence.

0 commit comments

Comments
 (0)