Skip to content

Commit 18f9fe8

Browse files
fishingflyZhouYuyeahdongcn
committed
vulkan: remove dependency on Vulkan headers (#26)
* remove vulkan header dependencies Signed-off-by: ZhouYu <[email protected]> * recover VkPhysicalDeviceProperties struct Signed-off-by: ZhouYu <[email protected]> * refine Signed-off-by: Xiaodong Ye <[email protected]> * refine Signed-off-by: Xiaodong Ye <[email protected]> --------- Signed-off-by: ZhouYu <[email protected]> Signed-off-by: Xiaodong Ye <[email protected]> Co-authored-by: ZhouYu <[email protected]> Co-authored-by: Xiaodong Ye <[email protected]>
1 parent c9d3100 commit 18f9fe8

File tree

3 files changed

+315
-45
lines changed

3 files changed

+315
-45
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ RUN yum install -y yum-utils \
1616
&& yum-config-manager --add-repo https://dl.rockylinux.org/vault/rocky/8.5/AppStream/\$basearch/os/ \
1717
&& rpm --import https://dl.rockylinux.org/pub/rocky/RPM-GPG-KEY-Rocky-8 \
1818
&& dnf install -y yum-utils ccache gcc-toolset-10-gcc-10.2.1-8.2.el8 gcc-toolset-10-gcc-c++-10.2.1-8.2.el8 gcc-toolset-10-binutils-2.35-11.el8 \
19-
&& dnf install -y ccache vulkan-headers \
19+
&& dnf install -y ccache \
2020
&& yum-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel8/x86_64/cuda-rhel8.repo
2121
ENV PATH=/opt/rh/gcc-toolset-10/root/usr/bin:$PATH
2222

2323
FROM --platform=linux/arm64 almalinux:8 AS base-arm64
2424
# install epel-release for ccache
2525
RUN yum install -y yum-utils epel-release \
26-
&& dnf install -y clang ccache vulkan-headers \
26+
&& dnf install -y clang ccache \
2727
&& yum-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel8/sbsa/cuda-rhel8.repo
2828
ENV CC=clang CXX=clang++
2929

discover/gpu_info_vulkan.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44
#include <stdbool.h>
55
#include "gpu_info_vulkan.h"
66

7-
// Compatibility macros for older Vulkan versions
8-
#ifndef VK_API_VERSION_MAJOR
9-
#define VK_API_VERSION_MAJOR VK_VERSION_MAJOR
10-
#define VK_API_VERSION_MINOR VK_VERSION_MINOR
11-
#define VK_API_VERSION_PATCH VK_VERSION_PATCH
12-
#endif
13-
147
void vk_init(char* vk_lib_path, vk_init_resp_t *resp) {
158
const int buflen = 256;
169
char buf[buflen + 1];
@@ -62,9 +55,9 @@ void vk_init(char* vk_lib_path, vk_init_resp_t *resp) {
6255
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
6356
appInfo.pNext = NULL;
6457
appInfo.pApplicationName = "Ollama";
65-
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
58+
appInfo.applicationVersion = VK_MAKE_API_VERSION(0, 1, 0, 0);
6659
appInfo.pEngineName = "No Engine";
67-
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
60+
appInfo.engineVersion = VK_MAKE_API_VERSION(0, 1, 0, 0);
6861
appInfo.apiVersion = VK_API_VERSION_1_2;
6962

7063
VkInstanceCreateInfo createInfo = {};

discover/gpu_info_vulkan.h

Lines changed: 311 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,322 @@
22
#ifndef __GPU_INFO_VULKAN_H__
33
#define __GPU_INFO_VULKAN_H__
44

5-
#include <vulkan/vulkan.h>
65
#include "gpu_info.h"
76

7+
// Just enough typedef's to dlopen/dlsym for memory information
8+
#define VK_MAKE_API_VERSION(variant, major, minor, patch) \
9+
((((uint32_t)(variant)) << 29) | (((uint32_t)(major)) << 22) | (((uint32_t)(minor)) << 12) | ((uint32_t)(patch)))
10+
#define VK_API_VERSION_1_2 VK_MAKE_API_VERSION(0, 1, 2, 0)
11+
#define VK_API_VERSION_MAJOR(version) (((uint32_t)(version) >> 22U) & 0x7FU)
12+
#define VK_API_VERSION_MINOR(version) (((uint32_t)(version) >> 12U) & 0x3FFU)
13+
#define VK_API_VERSION_PATCH(version) ((uint32_t)(version) & 0xFFFU)
14+
#define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
15+
#define VK_MAX_PHYSICAL_DEVICE_NAME_SIZE 256U
16+
#define VK_UUID_SIZE 16U
17+
#define VK_MAX_EXTENSION_NAME_SIZE 256U
18+
#define VK_MAX_MEMORY_TYPES 32U
19+
#define VK_MAX_MEMORY_HEAPS 16U
20+
#define VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME \
21+
"VK_KHR_get_physical_device_properties2"
22+
#define VKAPI_PTR
23+
24+
VK_DEFINE_HANDLE(VkInstance)
25+
VK_DEFINE_HANDLE(VkPhysicalDevice)
26+
27+
typedef uint32_t VkBool32;
28+
typedef uint64_t VkDeviceSize;
29+
typedef uint32_t VkFlags;
30+
typedef VkFlags VkSampleCountFlags;
31+
typedef VkFlags VkMemoryHeapFlags;
32+
typedef VkFlags VkMemoryPropertyFlags;
33+
typedef VkFlags VkInstanceCreateFlags;
34+
35+
typedef enum VkResult {
36+
VK_SUCCESS = 0,
37+
} VkResult;
38+
39+
typedef enum VkMemoryHeapFlagBits {
40+
VK_MEMORY_HEAP_DEVICE_LOCAL_BIT = 0x00000001,
41+
} VkMemoryHeapFlagBits;
42+
43+
typedef enum VkPhysicalDeviceType {
44+
VK_PHYSICAL_DEVICE_TYPE_CPU = 4
45+
} VkPhysicalDeviceType;
46+
47+
typedef enum VkStructureType {
48+
VK_STRUCTURE_TYPE_APPLICATION_INFO = 0,
49+
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO = 1,
50+
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2 = 1000059006,
51+
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT = 1000237000,
52+
} VkStructureType;
53+
54+
typedef enum VkSystemAllocationScope {
55+
VK_SYSTEM_ALLOCATION_SCOPE_COMMAND = 0,
56+
} VkSystemAllocationScope;
57+
58+
typedef enum VkInternalAllocationType {
59+
VK_INTERNAL_ALLOCATION_TYPE_EXECUTABLE = 0,
60+
VK_INTERNAL_ALLOCATION_TYPE_MAX_ENUM = 0x7FFFFFFF
61+
} VkInternalAllocationType;
62+
63+
typedef struct VkPhysicalDeviceLimits {
64+
uint32_t maxImageDimension1D;
65+
uint32_t maxImageDimension2D;
66+
uint32_t maxImageDimension3D;
67+
uint32_t maxImageDimensionCube;
68+
uint32_t maxImageArrayLayers;
69+
uint32_t maxTexelBufferElements;
70+
uint32_t maxUniformBufferRange;
71+
uint32_t maxStorageBufferRange;
72+
uint32_t maxPushConstantsSize;
73+
uint32_t maxMemoryAllocationCount;
74+
uint32_t maxSamplerAllocationCount;
75+
VkDeviceSize bufferImageGranularity;
76+
VkDeviceSize sparseAddressSpaceSize;
77+
uint32_t maxBoundDescriptorSets;
78+
uint32_t maxPerStageDescriptorSamplers;
79+
uint32_t maxPerStageDescriptorUniformBuffers;
80+
uint32_t maxPerStageDescriptorStorageBuffers;
81+
uint32_t maxPerStageDescriptorSampledImages;
82+
uint32_t maxPerStageDescriptorStorageImages;
83+
uint32_t maxPerStageDescriptorInputAttachments;
84+
uint32_t maxPerStageResources;
85+
uint32_t maxDescriptorSetSamplers;
86+
uint32_t maxDescriptorSetUniformBuffers;
87+
uint32_t maxDescriptorSetUniformBuffersDynamic;
88+
uint32_t maxDescriptorSetStorageBuffers;
89+
uint32_t maxDescriptorSetStorageBuffersDynamic;
90+
uint32_t maxDescriptorSetSampledImages;
91+
uint32_t maxDescriptorSetStorageImages;
92+
uint32_t maxDescriptorSetInputAttachments;
93+
uint32_t maxVertexInputAttributes;
94+
uint32_t maxVertexInputBindings;
95+
uint32_t maxVertexInputAttributeOffset;
96+
uint32_t maxVertexInputBindingStride;
97+
uint32_t maxVertexOutputComponents;
98+
uint32_t maxTessellationGenerationLevel;
99+
uint32_t maxTessellationPatchSize;
100+
uint32_t maxTessellationControlPerVertexInputComponents;
101+
uint32_t maxTessellationControlPerVertexOutputComponents;
102+
uint32_t maxTessellationControlPerPatchOutputComponents;
103+
uint32_t maxTessellationControlTotalOutputComponents;
104+
uint32_t maxTessellationEvaluationInputComponents;
105+
uint32_t maxTessellationEvaluationOutputComponents;
106+
uint32_t maxGeometryShaderInvocations;
107+
uint32_t maxGeometryInputComponents;
108+
uint32_t maxGeometryOutputComponents;
109+
uint32_t maxGeometryOutputVertices;
110+
uint32_t maxGeometryTotalOutputComponents;
111+
uint32_t maxFragmentInputComponents;
112+
uint32_t maxFragmentOutputAttachments;
113+
uint32_t maxFragmentDualSrcAttachments;
114+
uint32_t maxFragmentCombinedOutputResources;
115+
uint32_t maxComputeSharedMemorySize;
116+
uint32_t maxComputeWorkGroupCount[3];
117+
uint32_t maxComputeWorkGroupInvocations;
118+
uint32_t maxComputeWorkGroupSize[3];
119+
uint32_t subPixelPrecisionBits;
120+
uint32_t subTexelPrecisionBits;
121+
uint32_t mipmapPrecisionBits;
122+
uint32_t maxDrawIndexedIndexValue;
123+
uint32_t maxDrawIndirectCount;
124+
float maxSamplerLodBias;
125+
float maxSamplerAnisotropy;
126+
uint32_t maxViewports;
127+
uint32_t maxViewportDimensions[2];
128+
float viewportBoundsRange[2];
129+
uint32_t viewportSubPixelBits;
130+
size_t minMemoryMapAlignment;
131+
VkDeviceSize minTexelBufferOffsetAlignment;
132+
VkDeviceSize minUniformBufferOffsetAlignment;
133+
VkDeviceSize minStorageBufferOffsetAlignment;
134+
int32_t minTexelOffset;
135+
uint32_t maxTexelOffset;
136+
int32_t minTexelGatherOffset;
137+
uint32_t maxTexelGatherOffset;
138+
float minInterpolationOffset;
139+
float maxInterpolationOffset;
140+
uint32_t subPixelInterpolationOffsetBits;
141+
uint32_t maxFramebufferWidth;
142+
uint32_t maxFramebufferHeight;
143+
uint32_t maxFramebufferLayers;
144+
VkSampleCountFlags framebufferColorSampleCounts;
145+
VkSampleCountFlags framebufferDepthSampleCounts;
146+
VkSampleCountFlags framebufferStencilSampleCounts;
147+
VkSampleCountFlags framebufferNoAttachmentsSampleCounts;
148+
uint32_t maxColorAttachments;
149+
VkSampleCountFlags sampledImageColorSampleCounts;
150+
VkSampleCountFlags sampledImageIntegerSampleCounts;
151+
VkSampleCountFlags sampledImageDepthSampleCounts;
152+
VkSampleCountFlags sampledImageStencilSampleCounts;
153+
VkSampleCountFlags storageImageSampleCounts;
154+
uint32_t maxSampleMaskWords;
155+
VkBool32 timestampComputeAndGraphics;
156+
float timestampPeriod;
157+
uint32_t maxClipDistances;
158+
uint32_t maxCullDistances;
159+
uint32_t maxCombinedClipAndCullDistances;
160+
uint32_t discreteQueuePriorities;
161+
float pointSizeRange[2];
162+
float lineWidthRange[2];
163+
float pointSizeGranularity;
164+
float lineWidthGranularity;
165+
VkBool32 strictLines;
166+
VkBool32 standardSampleLocations;
167+
VkDeviceSize optimalBufferCopyOffsetAlignment;
168+
VkDeviceSize optimalBufferCopyRowPitchAlignment;
169+
VkDeviceSize nonCoherentAtomSize;
170+
} VkPhysicalDeviceLimits;
171+
172+
typedef struct VkPhysicalDeviceSparseProperties {
173+
VkBool32 residencyStandard2DBlockShape;
174+
VkBool32 residencyStandard2DMultisampleBlockShape;
175+
VkBool32 residencyStandard3DBlockShape;
176+
VkBool32 residencyAlignedMipSize;
177+
VkBool32 residencyNonResidentStrict;
178+
} VkPhysicalDeviceSparseProperties;
179+
180+
typedef struct VkPhysicalDeviceProperties {
181+
uint32_t apiVersion;
182+
uint32_t driverVersion;
183+
uint32_t vendorID;
184+
uint32_t deviceID;
185+
VkPhysicalDeviceType deviceType;
186+
char deviceName[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
187+
uint8_t pipelineCacheUUID[VK_UUID_SIZE];
188+
VkPhysicalDeviceLimits limits;
189+
VkPhysicalDeviceSparseProperties sparseProperties;
190+
} VkPhysicalDeviceProperties;
191+
192+
typedef struct VkExtensionProperties {
193+
char extensionName[VK_MAX_EXTENSION_NAME_SIZE];
194+
uint32_t specVersion;
195+
} VkExtensionProperties;
196+
197+
typedef struct VkPhysicalDeviceMemoryBudgetPropertiesEXT {
198+
VkStructureType sType;
199+
void* pNext;
200+
VkDeviceSize heapBudget[VK_MAX_MEMORY_HEAPS];
201+
VkDeviceSize heapUsage[VK_MAX_MEMORY_HEAPS];
202+
} VkPhysicalDeviceMemoryBudgetPropertiesEXT;
203+
204+
typedef struct VkApplicationInfo {
205+
VkStructureType sType;
206+
const void* pNext;
207+
const char* pApplicationName;
208+
uint32_t applicationVersion;
209+
const char* pEngineName;
210+
uint32_t engineVersion;
211+
uint32_t apiVersion;
212+
} VkApplicationInfo;
213+
214+
typedef struct VkInstanceCreateInfo {
215+
VkStructureType sType;
216+
const void* pNext;
217+
VkInstanceCreateFlags flags;
218+
const VkApplicationInfo* pApplicationInfo;
219+
uint32_t enabledLayerCount;
220+
const char* const* ppEnabledLayerNames;
221+
uint32_t enabledExtensionCount;
222+
const char* const* ppEnabledExtensionNames;
223+
} VkInstanceCreateInfo;
224+
225+
typedef void* (VKAPI_PTR *PFN_vkAllocationFunction)(
226+
void* pUserData,
227+
size_t size,
228+
size_t alignment,
229+
VkSystemAllocationScope allocationScope);
230+
231+
typedef void* (VKAPI_PTR *PFN_vkReallocationFunction)(
232+
void* pUserData,
233+
void* pOriginal,
234+
size_t size,
235+
size_t alignment,
236+
VkSystemAllocationScope allocationScope);
237+
238+
typedef void (VKAPI_PTR *PFN_vkFreeFunction)(
239+
void* pUserData,
240+
void* pMemory);
241+
242+
typedef void (VKAPI_PTR *PFN_vkInternalFreeNotification)(
243+
void* pUserData,
244+
size_t size,
245+
VkInternalAllocationType allocationType,
246+
VkSystemAllocationScope allocationScope);
247+
248+
typedef void (VKAPI_PTR *PFN_vkInternalAllocationNotification)(
249+
void* pUserData,
250+
size_t size,
251+
VkInternalAllocationType allocationType,
252+
VkSystemAllocationScope allocationScope);
253+
254+
typedef struct VkAllocationCallbacks {
255+
void* pUserData;
256+
PFN_vkAllocationFunction pfnAllocation;
257+
PFN_vkReallocationFunction pfnReallocation;
258+
PFN_vkFreeFunction pfnFree;
259+
PFN_vkInternalAllocationNotification pfnInternalAllocation;
260+
PFN_vkInternalFreeNotification pfnInternalFree;
261+
} VkAllocationCallbacks;
262+
263+
typedef struct VkMemoryType {
264+
VkMemoryPropertyFlags propertyFlags;
265+
uint32_t heapIndex;
266+
} VkMemoryType;
267+
268+
typedef struct VkMemoryHeap {
269+
VkDeviceSize size;
270+
VkMemoryHeapFlags flags;
271+
} VkMemoryHeap;
272+
273+
typedef struct VkPhysicalDeviceMemoryProperties {
274+
uint32_t memoryTypeCount;
275+
VkMemoryType memoryTypes[VK_MAX_MEMORY_TYPES];
276+
uint32_t memoryHeapCount;
277+
VkMemoryHeap memoryHeaps[VK_MAX_MEMORY_HEAPS];
278+
} VkPhysicalDeviceMemoryProperties;
279+
280+
typedef struct VkPhysicalDeviceMemoryProperties2 {
281+
VkStructureType sType;
282+
void* pNext;
283+
VkPhysicalDeviceMemoryProperties memoryProperties;
284+
} VkPhysicalDeviceMemoryProperties2;
285+
8286
typedef struct {
9-
void* vk_handle;
10-
uint16_t verbose;
11-
12-
VkInstance vk;
13-
int num_devices;
14-
15-
void (*vkGetPhysicalDeviceProperties)(
16-
VkPhysicalDevice physicalDevice,
17-
VkPhysicalDeviceProperties* pProperties);
18-
VkResult (*vkEnumerateDeviceExtensionProperties)(
19-
VkPhysicalDevice physicalDevice,
20-
const char* pLayerName,
21-
uint32_t* pPropertyCount,
22-
VkExtensionProperties* pProperties);
23-
VkResult (*vkCreateInstance)(
24-
const VkInstanceCreateInfo* pCreateInfo,
25-
const VkAllocationCallbacks* pAllocator,
26-
VkInstance* pInstance);
27-
VkResult (*vkEnumeratePhysicalDevices)(
28-
VkInstance instance,
29-
uint32_t* pPhysicalDeviceCount,
30-
VkPhysicalDevice* pPhysicalDevices);
31-
void (*vkGetPhysicalDeviceMemoryProperties2)(
32-
VkPhysicalDevice physicalDevice,
33-
VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
34-
void (*vkDestroyInstance)(
35-
VkInstance instance,
36-
const VkAllocationCallbacks* pAllocator);
287+
void* vk_handle;
288+
uint16_t verbose;
289+
290+
VkInstance vk;
291+
int num_devices;
292+
293+
void (*vkGetPhysicalDeviceProperties)(
294+
VkPhysicalDevice physicalDevice,
295+
VkPhysicalDeviceProperties* pProperties);
296+
VkResult (*vkEnumerateDeviceExtensionProperties)(
297+
VkPhysicalDevice physicalDevice,
298+
const char* pLayerName,
299+
uint32_t* pPropertyCount,
300+
VkExtensionProperties* pProperties);
301+
VkResult (*vkCreateInstance)(
302+
const VkInstanceCreateInfo* pCreateInfo,
303+
const VkAllocationCallbacks* pAllocator,
304+
VkInstance* pInstance);
305+
VkResult (*vkEnumeratePhysicalDevices)(
306+
VkInstance instance,
307+
uint32_t* pPhysicalDeviceCount,
308+
VkPhysicalDevice* pPhysicalDevices);
309+
void (*vkGetPhysicalDeviceMemoryProperties2)(
310+
VkPhysicalDevice physicalDevice,
311+
VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
312+
void (*vkDestroyInstance)(
313+
VkInstance instance,
314+
const VkAllocationCallbacks* pAllocator);
37315
} vk_handle_t;
38316

39-
typedef struct vk_init_resp
40-
{
41-
char *err; // If err is non-null handle is invalid
42-
int num_devices;
43-
vk_handle_t ch;
317+
typedef struct vk_init_resp {
318+
char *err; // If err is non-null handle is invalid
319+
int num_devices;
320+
vk_handle_t ch;
44321
} vk_init_resp_t;
45322

46323
void vk_init(char* vk_lib_path, vk_init_resp_t *resp);

0 commit comments

Comments
 (0)