ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-vulkan / ggml-vulkan-types.h
| 1 | #pragma once |
| 2 | |
| 3 | #include "ggml-vulkan.h" |
| 4 | |
| 5 | #include <vulkan/vulkan_core.h> |
| 6 | |
| 7 | #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS) |
| 8 | #include <chrono> |
| 9 | #include "ggml-cpu.h" |
| 10 | #endif |
| 11 | |
| 12 | #define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1 |
| 13 | |
| 14 | #if VK_HEADER_VERSION >= 301 |
| 15 | namespace vk::detail { class DispatchLoaderDynamic; } |
| 16 | using vk::detail::DispatchLoaderDynamic; |
| 17 | #else |
| 18 | namespace vk { class DispatchLoaderDynamic; } |
| 19 | using vk::DispatchLoaderDynamic; |
| 20 | #endif |
| 21 | |
| 22 | DispatchLoaderDynamic & ggml_vk_default_dispatcher(); |
| 23 | |
| 24 | #define VULKAN_HPP_DEFAULT_DISPATCHER ggml_vk_default_dispatcher() |
| 25 | |
| 26 | #include <vulkan/vulkan.hpp> |
| 27 | |
| 28 | #ifndef VK_NV_cooperative_matrix_decode_vector |
| 29 | #define VK_NV_cooperative_matrix_decode_vector 1 |
| 30 | #define VK_NV_COOPERATIVE_MATRIX_DECODE_VECTOR_EXTENSION_NAME "VK_NV_cooperative_matrix_decode_vector" |
| 31 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_DECODE_VECTOR_FEATURES_NV ((VkStructureType)1000689000) |
| 32 | typedef struct VkPhysicalDeviceCooperativeMatrixDecodeVectorFeaturesNV { |
| 33 | VkStructureType sType; |
| 34 | void* pNext; |
| 35 | VkBool32 cooperativeMatrixDecodeVector; |
| 36 | } VkPhysicalDeviceCooperativeMatrixDecodeVectorFeaturesNV; |
| 37 | #endif |
| 38 | |
| 39 | #if __has_include(<spirv/unified1/spirv.hpp>) |
| 40 | # include <spirv/unified1/spirv.hpp> |
| 41 | #elif __has_include(<spirv-headers/spirv.hpp>) |
| 42 | # include <spirv-headers/spirv.hpp> |
| 43 | #elif __has_include(<spirv.hpp>) |
| 44 | # include <spirv.hpp> |
| 45 | #else |
| 46 | // Fallback to let the compiler throw a standard "file not found" error |
| 47 | # include <spirv/unified1/spirv.hpp> |
| 48 | #endif |
| 49 | |
| 50 | #include <algorithm> |
| 51 | #include <cmath> |
| 52 | #include <functional> |
| 53 | #include <iomanip> |
| 54 | #include <iostream> |
| 55 | #include <tuple> |
| 56 | #include <vector> |
| 57 | #include <deque> |
| 58 | #include <sstream> |
| 59 | #include <utility> |
| 60 | #include <memory> |
| 61 | #include <limits> |
| 62 | #include <map> |
| 63 | #include <set> |
| 64 | #include <unordered_map> |
| 65 | #include <shared_mutex> |
| 66 | #include <mutex> |
| 67 | #include <future> |
| 68 | #include <condition_variable> |
| 69 | #include <thread> |
| 70 | |
| 71 | #if defined(_MSC_VER) |
| 72 | # define NOMINMAX 1 |
| 73 | # include <windows.h> |
| 74 | # define YIELD() YieldProcessor() |
| 75 | #elif defined(__clang__) || defined(__GNUC__) |
| 76 | # if defined(__x86_64__) ||defined(__i386__) |
| 77 | # include <immintrin.h> |
| 78 | # define YIELD() _mm_pause() |
| 79 | # elif defined(__arm__) || defined(__aarch64__) |
| 80 | # if defined(__clang__) |
| 81 | # include <arm_acle.h> |
| 82 | # define YIELD() __yield() |
| 83 | # else |
| 84 | # define YIELD() asm volatile("yield") |
| 85 | # endif |
| 86 | # endif |
| 87 | #endif |
| 88 | |
| 89 | #if !defined(YIELD) |
| 90 | #define YIELD() |
| 91 | #endif |
| 92 | |
| 93 | #include "ggml-impl.h" |
| 94 | #include "ggml-backend-impl.h" |
| 95 | #include "ggml-vulkan-shaders.hpp" |
| 96 | |
| 97 | #if !defined(VK_KHR_shader_bfloat16) |
| 98 | |
| 99 | #define VK_KHR_shader_bfloat16 1 |
| 100 | #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1 |
| 101 | #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16" |
| 102 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000) |
| 103 | #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000) |
| 104 | |
| 105 | typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR { |
| 106 | VkStructureType sType; |
| 107 | void* pNext; |
| 108 | VkBool32 shaderBFloat16Type; |
| 109 | VkBool32 shaderBFloat16DotProduct; |
| 110 | VkBool32 shaderBFloat16CooperativeMatrix; |
| 111 | } VkPhysicalDeviceShaderBfloat16FeaturesKHR; |
| 112 | #endif |
| 113 | |
| 114 | #if !defined(VK_VALVE_shader_mixed_float_dot_product) |
| 115 | #define VK_VALVE_shader_mixed_float_dot_product 1 |
| 116 | #define VK_VALVE_SHADER_MIXED_FLOAT_DOT_PRODUCT_SPEC_VERSION 1 |
| 117 | #define VK_VALVE_SHADER_MIXED_FLOAT_DOT_PRODUCT_EXTENSION_NAME "VK_VALVE_shader_mixed_float_dot_product" |
| 118 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_MIXED_FLOAT_DOT_PRODUCT_FEATURES_VALVE ((VkStructureType)1000673000) |
| 119 | typedef struct VkPhysicalDeviceShaderMixedFloatDotProductFeaturesVALVE { |
| 120 | VkStructureType sType; |
| 121 | void* pNext; |
| 122 | VkBool32 shaderMixedFloatDotProductFloat16AccFloat32; |
| 123 | VkBool32 shaderMixedFloatDotProductFloat16AccFloat16; |
| 124 | VkBool32 shaderMixedFloatDotProductBFloat16Acc; |
| 125 | VkBool32 shaderMixedFloatDotProductFloat8AccFloat32; |
| 126 | } VkPhysicalDeviceShaderMixedFloatDotProductFeaturesVALVE; |
| 127 | #endif |
| 128 | |
| 129 | #if !defined(VK_EXT_shader_ocp_microscaling_types) |
| 130 | #define VK_EXT_shader_ocp_microscaling_types 1 |
| 131 | #define VK_EXT_SHADER_OCP_MICROSCALING_TYPES_SPEC_VERSION 1 |
| 132 | #define VK_EXT_SHADER_OCP_MICROSCALING_TYPES_EXTENSION_NAME "VK_EXT_shader_ocp_microscaling_types" |
| 133 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_OCP_MICROSCALING_TYPES_FEATURES_EXT ((VkStructureType)1000672000) |
| 134 | typedef struct VkPhysicalDeviceShaderOCPMicroscalingTypesFeaturesEXT { |
| 135 | VkStructureType sType; |
| 136 | void* pNext; |
| 137 | VkBool32 shaderFloat4; |
| 138 | VkBool32 shaderFloat6; |
| 139 | VkBool32 shaderFloat8UnsignedE8M0; |
| 140 | VkBool32 shaderMXInt8; |
| 141 | } VkPhysicalDeviceShaderOCPMicroscalingTypesFeaturesEXT; |
| 142 | #endif |
| 143 | |
| 144 | #if !defined(VK_EXT_shader_float8) |
| 145 | #define VK_EXT_shader_float8 1 |
| 146 | #define VK_EXT_SHADER_FLOAT8_SPEC_VERSION 1 |
| 147 | #define VK_EXT_SHADER_FLOAT8_EXTENSION_NAME "VK_EXT_shader_float8" |
| 148 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT8_FEATURES_EXT ((VkStructureType)1000567000) |
| 149 | typedef struct VkPhysicalDeviceShaderFloat8FeaturesEXT { |
| 150 | VkStructureType sType; |
| 151 | void* pNext; |
| 152 | VkBool32 shaderFloat8; |
| 153 | VkBool32 shaderFloat8CooperativeMatrix; |
| 154 | } VkPhysicalDeviceShaderFloat8FeaturesEXT; |
| 155 | #endif |
| 156 | |
| 157 | #ifndef VK_KHR_INTERNALLY_SYNCHRONIZED_QUEUES_EXTENSION_NAME |
| 158 | #define VK_KHR_INTERNALLY_SYNCHRONIZED_QUEUES_EXTENSION_NAME "VK_KHR_internally_synchronized_queues" |
| 159 | #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INTERNALLY_SYNCHRONIZED_QUEUES_FEATURES_KHR ((VkStructureType)1000504000) |
| 160 | #define VK_DEVICE_QUEUE_CREATE_INTERNALLY_SYNCHRONIZED_BIT_KHR ((VkDeviceQueueCreateFlagBits)0x00000004) |
| 161 | |
| 162 | // Compile-time constant guaranteed; no runtime initialization overhead |
| 163 | static constexpr vk::DeviceQueueCreateFlagBits eInternallySynchronizedKHR = |
| 164 | static_cast<vk::DeviceQueueCreateFlagBits>(0x00000004); |
| 165 | |
| 166 | typedef struct VkPhysicalDeviceInternallySynchronizedQueuesFeaturesKHR { |
| 167 | VkStructureType sType; |
| 168 | void* pNext; |
| 169 | VkBool32 internallySynchronizedQueues; |
| 170 | } VkPhysicalDeviceInternallySynchronizedQueuesFeaturesKHR; |
| 171 | #else |
| 172 | static constexpr vk::DeviceQueueCreateFlagBits eInternallySynchronizedKHR = vk::DeviceQueueCreateFlagBits::eInternallySynchronizedKHR; |
| 173 | #endif |
| 174 | |
| 175 | #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1)) |
| 176 | |
| 177 | #define CEIL_DIV(M, N) (((M) / (N)) + (((M) % (N)) != 0)) |
| 178 | |
| 179 | static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; } |
| 180 | |
| 181 | #define VK_VENDOR_ID_AMD 0x1002 |
| 182 | |
| 183 | #define VK_VENDOR_ID_APPLE 0x106b |
| 184 | |
| 185 | #define VK_VENDOR_ID_INTEL 0x8086 |
| 186 | |
| 187 | #define VK_VENDOR_ID_NVIDIA 0x10de |
| 188 | |
| 189 | #define VK_VENDOR_ID_QUALCOMM 0x5143 |
| 190 | |
| 191 | #define VK_VENDOR_ID_SAMSUNG 0x144d |
| 192 | |
| 193 | #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256 |
| 194 | |
| 195 | #define VK_CHECK(err, msg, dev) \ |
| 196 | do { \ |
| 197 | vk::Result err_; \ |
| 198 | try { \ |
| 199 | err_ = (err); \ |
| 200 | } catch (vk::DeviceLostError &) { \ |
| 201 | ggml_vk_print_device_lost_info(dev); \ |
| 202 | GGML_LOG_ERROR("ggml_vulkan: %s at %s:%d\n", \ |
| 203 | #err, __FILE__, __LINE__); \ |
| 204 | throw; \ |
| 205 | } \ |
| 206 | if (err_ != vk::Result::eSuccess) { \ |
| 207 | GGML_LOG_ERROR("ggml_vulkan: %s error %s at %s:%d\n", \ |
| 208 | #err, to_string(err_).c_str(), __FILE__, __LINE__); \ |
| 209 | throw vk::SystemError(vk::make_error_code(err_), \ |
| 210 | "ggml_vulkan: " msg); \ |
| 211 | } \ |
| 212 | } while (0) |
| 213 | |
| 214 | #ifdef GGML_VULKAN_DEBUG |
| 215 | #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl |
| 216 | #else |
| 217 | #define VK_LOG_DEBUG(msg) ((void) 0) |
| 218 | #endif // GGML_VULKAN_DEBUG |
| 219 | |
| 220 | #define MAX_PARAMETER_COUNT 12 |
| 221 | |
| 222 | #define MAX_FUSED_ADDS (MAX_PARAMETER_COUNT - 3) |
| 223 | |
| 224 | struct vk_pipeline_struct; |
| 225 | |
| 226 | typedef std::shared_ptr<struct vk_pipeline_struct> vk_pipeline; |
| 227 | |
| 228 | struct vk_pipeline_struct { |
| 229 | std::string name; |
| 230 | vk::ShaderModule shader_module; |
| 231 | vk::PipelineLayout layout; |
| 232 | vk::Pipeline pipeline; |
| 233 | uint32_t push_constant_size; |
| 234 | uint32_t parameter_count; |
| 235 | std::array<uint32_t, 3> wg_denoms; |
| 236 | uint32_t align; |
| 237 | // true if fields have been set by ggml_vk_create_pipeline |
| 238 | bool initialized {}; |
| 239 | // true while a compile is in flight, used to dedupe concurrent claims. |
| 240 | // Protected by device->compile_mutex. |
| 241 | bool compile_pending {}; |
| 242 | // set to true when the shader has been compiled |
| 243 | std::atomic<bool> compiled {}; |
| 244 | // number of registers used, extracted from pipeline executable properties |
| 245 | uint32_t register_count {}; |
| 246 | |
| 247 | #if defined(VK_EXT_shader_64bit_indexing) |
| 248 | bool is_64b_indexing {}; |
| 249 | #endif |
| 250 | // linked list of pipelines for multiple compilation variants. |
| 251 | // currently only used to compile a 64-bit indexing variant. |
| 252 | vk_pipeline next; |
| 253 | }; |
| 254 | |
| 255 | typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref; |
| 256 | |
| 257 | struct vk_matmul_pipeline_key { |
| 258 | ggml_type type_a; |
| 259 | ggml_type type_b; |
| 260 | bool mul_mat_id; |
| 261 | bool f16acc; |
| 262 | |
| 263 | bool operator<(const vk_matmul_pipeline_key & o) const { |
| 264 | return std::tie(type_a, type_b, mul_mat_id, f16acc) |
| 265 | < std::tie(o.type_a, o.type_b, o.mul_mat_id, o.f16acc); |
| 266 | } |
| 267 | }; |
| 268 | |
| 269 | struct vk_matmul_pipeline_pair { |
| 270 | vk_pipeline unaligned; |
| 271 | vk_pipeline aligned; |
| 272 | uint32_t align; |
| 273 | }; |
| 274 | |
| 275 | struct vk_tile_config { |
| 276 | std::vector<uint32_t> warptile; |
| 277 | std::array<uint32_t, 3> wg_denoms; |
| 278 | uint32_t align; |
| 279 | }; |
| 280 | |
| 281 | using matmul_tile_selector_t = std::function<uint32_t( |
| 282 | uint32_t m, uint32_t n, uint32_t k, uint32_t shader_core_count, |
| 283 | const std::vector<vk_matmul_pipeline_pair>& configs)>; |
| 284 | |
| 285 | struct vk_device_struct; |
| 286 | |
| 287 | typedef std::shared_ptr<vk_device_struct> vk_device; |
| 288 | |
| 289 | typedef std::weak_ptr<vk_device_struct> vk_device_ref; |
| 290 | |
| 291 | struct vk_buffer_struct; |
| 292 | |
| 293 | typedef std::shared_ptr<vk_buffer_struct> vk_buffer; |
| 294 | |
| 295 | typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref; |
| 296 | |
| 297 | struct ggml_backend_vk_buffer_type_context { |
| 298 | std::string name; |
| 299 | vk_device device; |
| 300 | }; |
| 301 | |
| 302 | struct vk_command_buffer { |
| 303 | vk::CommandBuffer buf; |
| 304 | uint64_t use_counter = 0; |
| 305 | bool in_use = false; |
| 306 | }; |
| 307 | |
| 308 | struct vk_queue; |
| 309 | |
| 310 | struct vk_command_pool { |
| 311 | void init(vk_device& device, vk_queue *q_); |
| 312 | void destroy(vk::Device& device); |
| 313 | |
| 314 | vk::CommandPool pool; |
| 315 | // Using deque so the pointers to command buffers |
| 316 | // remain valid even if we add more |
| 317 | std::deque<vk_command_buffer> cmd_buffers; |
| 318 | |
| 319 | vk_queue *q; |
| 320 | |
| 321 | size_t buffers_in_use() const { |
| 322 | return std::count_if(cmd_buffers.begin(), cmd_buffers.end(), |
| 323 | [](const auto& cb) { return cb.in_use; }); |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | struct vk_queue_handle { |
| 328 | vk::Queue queue; |
| 329 | vk_device_ref device; |
| 330 | std::mutex * device_submit_mutex = nullptr; |
| 331 | virtual void submit(vk::ArrayProxy<const vk::SubmitInfo> submits, vk::Fence fence) = 0; |
| 332 | virtual void lock() {} // no-op by default (internally synchronized case) |
| 333 | virtual void unlock() {} |
| 334 | virtual ~vk_queue_handle() = default; |
| 335 | }; |
| 336 | |
| 337 | struct vk_queue_handle_synchronized : vk_queue_handle { |
| 338 | std::mutex mutex; |
| 339 | void submit(vk::ArrayProxy<const vk::SubmitInfo> submits, vk::Fence fence) override; |
| 340 | |
| 341 | void lock() override { mutex.lock(); } |
| 342 | void unlock() override { mutex.unlock(); } |
| 343 | }; |
| 344 | |
| 345 | struct vk_queue_handle_unsynchronized : vk_queue_handle { |
| 346 | void submit(vk::ArrayProxy<const vk::SubmitInfo> submits, vk::Fence fence) override; |
| 347 | |
| 348 | // lock()/unlock() inherited no-ops |
| 349 | }; |
| 350 | |
| 351 | struct vk_queue { |
| 352 | uint32_t queue_family_index; |
| 353 | std::shared_ptr<vk_queue_handle> handle; |
| 354 | |
| 355 | vk_command_pool cmd_pool; |
| 356 | |
| 357 | vk::PipelineStageFlags stage_flags; |
| 358 | |
| 359 | bool transfer_only; |
| 360 | }; |
| 361 | |
| 362 | static constexpr uint32_t mul_mat_vec_max_cols = 8; |
| 363 | |
| 364 | static constexpr uint32_t p021_max_gqa_ratio = 8; |
| 365 | |
| 366 | enum vk_device_architecture { |
| 367 | OTHER, |
| 368 | AMD_GCN, |
| 369 | AMD_RDNA1, |
| 370 | AMD_RDNA2, |
| 371 | AMD_RDNA3, |
| 372 | AMD_RDNA4, |
| 373 | INTEL_XE1, |
| 374 | INTEL_XE2, |
| 375 | NVIDIA_PRE_TURING, |
| 376 | NVIDIA_TURING, |
| 377 | QUALCOMM_ADRENO, |
| 378 | }; |
| 379 | |
| 380 | enum vk_conv_shapes { |
| 381 | CONV_SHAPE_128x128, |
| 382 | CONV_SHAPE_64x32, |
| 383 | CONV_SHAPE_32x256, |
| 384 | CONV_SHAPE_64x128, |
| 385 | CONV_SHAPE_256x128, |
| 386 | CONV_SHAPE_COUNT, |
| 387 | }; |
| 388 | |
| 389 | struct vk_conv_block_size { |
| 390 | uint32_t K; |
| 391 | uint32_t NPQ; |
| 392 | uint32_t CRS; |
| 393 | }; |
| 394 | |
| 395 | inline vk_conv_block_size vk_conv_block_sizes[CONV_SHAPE_COUNT] = { |
| 396 | // K NPQ CRS |
| 397 | { 128, 128, 16 }, // CONV_SHAPE_128x128 |
| 398 | { 64, 32, 32 }, // CONV_SHAPE_64x32 |
| 399 | { 32, 256, 16 }, // CONV_SHAPE_32x256 |
| 400 | { 64, 128, 16 }, // CONV_SHAPE_64x128 |
| 401 | { 256, 128, 16 }, // CONV_SHAPE_256x128 |
| 402 | }; |
| 403 | |
| 404 | enum dmmv_wg_sizes { |
| 405 | DMMV_WG_SIZE_SUBGROUP, |
| 406 | DMMV_WG_SIZE_LARGE, |
| 407 | DMMV_WG_SIZE_COUNT, |
| 408 | }; |
| 409 | |
| 410 | enum FaCodePath { |
| 411 | FA_SCALAR, |
| 412 | FA_COOPMAT1, |
| 413 | FA_COOPMAT2, |
| 414 | }; |
| 415 | |
| 416 | struct vk_fa_pipeline_state { |
| 417 | uint32_t HSK, HSV; |
| 418 | uint32_t Br, Bc; |
| 419 | uint32_t D_split, row_split; |
| 420 | bool shmem_staging; |
| 421 | FaCodePath path; |
| 422 | uint32_t workgroup_size, subgroup_size; |
| 423 | bool aligned; |
| 424 | bool f32acc; |
| 425 | uint32_t flags; |
| 426 | uint32_t limit_occupancy_shmem; |
| 427 | ggml_type k_type; |
| 428 | ggml_type v_type; |
| 429 | |
| 430 | bool operator<(const vk_fa_pipeline_state &b) const { |
| 431 | return std::tie(HSK, HSV, Br, Bc, D_split, row_split, shmem_staging, path, workgroup_size, subgroup_size, aligned, f32acc, flags, limit_occupancy_shmem, k_type, v_type) < |
| 432 | std::tie(b.HSK, b.HSV, b.Br, b.Bc, b.D_split, b.row_split, b.shmem_staging, b.path, b.workgroup_size, b.subgroup_size, b.aligned, b.f32acc, b.flags, b.limit_occupancy_shmem, b.k_type, b.v_type); |
| 433 | } |
| 434 | }; |
| 435 | |
| 436 | struct vk_conv2d_pipeline_state { |
| 437 | vk_conv2d_pipeline_state(uint32_t s0, uint32_t s1, uint32_t p0, uint32_t p1, uint32_t d0, uint32_t d1, uint32_t KW, uint32_t KH, uint32_t aligned) |
| 438 | : s0(s0), s1(s1), p0(p0), p1(p1), d0(d0), d1(d1), KW(KW), KH(KH), aligned(aligned) {} |
| 439 | |
| 440 | uint32_t s0, s1, p0, p1, d0, d1, KW, KH; |
| 441 | // when set, shader can skip K/CRS/NPQ bounds checks and address clamps |
| 442 | uint32_t aligned; |
| 443 | |
| 444 | bool operator<(const vk_conv2d_pipeline_state &b) const { |
| 445 | return std::tie(s0, s1, p0, p1, d0, d1, KW, KH, aligned) < |
| 446 | std::tie(b.s0, b.s1, b.p0, b.p1, b.d0, b.d1, b.KW, b.KH, b.aligned); |
| 447 | } |
| 448 | }; |
| 449 | |
| 450 | struct vk_conv3d_pipeline_state { |
| 451 | vk_conv3d_pipeline_state(uint32_t s0, uint32_t s1, uint32_t s2, uint32_t p0, uint32_t p1, uint32_t p2, |
| 452 | uint32_t d0, uint32_t d1, uint32_t d2, uint32_t KW, uint32_t KH, uint32_t KD, uint32_t aligned) |
| 453 | : s0(s0), s1(s1), s2(s2), p0(p0), p1(p1), p2(p2), d0(d0), d1(d1), d2(d2), KW(KW), KH(KH), KD(KD), aligned(aligned) {} |
| 454 | |
| 455 | uint32_t s0, s1, s2, p0, p1, p2, d0, d1, d2, KW, KH, KD; |
| 456 | uint32_t aligned; |
| 457 | |
| 458 | bool operator<(const vk_conv3d_pipeline_state &b) const { |
| 459 | return std::tie(s0, s1, s2, p0, p1, p2, d0, d1, d2, KW, KH, KD, aligned) < |
| 460 | std::tie(b.s0, b.s1, b.s2, b.p0, b.p1, b.p2, b.d0, b.d1, b.d2, b.KW, b.KH, b.KD, b.aligned); |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | struct vk_solve_tri_pipeline_state { |
| 465 | vk_solve_tri_pipeline_state(uint32_t N, uint32_t K) |
| 466 | : N(N), K(K) {} |
| 467 | |
| 468 | uint32_t N, K; |
| 469 | |
| 470 | bool operator<(const vk_solve_tri_pipeline_state &b) const { |
| 471 | return std::tie(N, K) < |
| 472 | std::tie(b.N, b.K); |
| 473 | } |
| 474 | }; |
| 475 | |
| 476 | enum shader_reduction_mode { |
| 477 | SHADER_REDUCTION_MODE_SHMEM, |
| 478 | SHADER_REDUCTION_MODE_HYBRID, |
| 479 | SHADER_REDUCTION_MODE_SUBGROUP, |
| 480 | SHADER_REDUCTION_MODE_COUNT, |
| 481 | }; |
| 482 | |
| 483 | static constexpr uint32_t num_argsort_pipelines = 11; |
| 484 | |
| 485 | static constexpr uint32_t num_topk_moe_pipelines = 10; |
| 486 | |
| 487 | static constexpr uint32_t num_topk_pipelines = 11; |
| 488 | |
| 489 | static constexpr std::initializer_list<ggml_op> topk_moe_early_softmax_norm{ GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT, |
| 490 | GGML_OP_VIEW, GGML_OP_GET_ROWS, GGML_OP_RESHAPE, |
| 491 | GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, |
| 492 | GGML_OP_RESHAPE }; |
| 493 | |
| 494 | static constexpr std::initializer_list<ggml_op> topk_moe_sigmoid_norm_bias{ GGML_OP_UNARY, GGML_OP_RESHAPE, GGML_OP_ADD, |
| 495 | GGML_OP_ARGSORT, GGML_OP_VIEW, GGML_OP_GET_ROWS, |
| 496 | GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, |
| 497 | GGML_OP_DIV, GGML_OP_RESHAPE }; |
| 498 | |
| 499 | static constexpr std::initializer_list<ggml_op> topk_moe_sqrt_softplus_norm_bias{ GGML_OP_UNARY, GGML_OP_SQRT, |
| 500 | GGML_OP_RESHAPE, GGML_OP_ADD, |
| 501 | GGML_OP_ARGSORT, GGML_OP_VIEW, |
| 502 | GGML_OP_GET_ROWS, GGML_OP_RESHAPE, |
| 503 | GGML_OP_SUM_ROWS, GGML_OP_CLAMP, |
| 504 | GGML_OP_DIV, GGML_OP_RESHAPE }; |
| 505 | |
| 506 | static constexpr std::initializer_list<ggml_op> topk_moe_early_softmax { GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT, |
| 507 | GGML_OP_VIEW, GGML_OP_GET_ROWS }; |
| 508 | |
| 509 | static constexpr std::initializer_list<ggml_op> topk_moe_late_softmax { GGML_OP_ARGSORT, GGML_OP_VIEW, |
| 510 | GGML_OP_GET_ROWS, GGML_OP_RESHAPE, |
| 511 | GGML_OP_SOFT_MAX, GGML_OP_RESHAPE }; |
| 512 | |
| 513 | static constexpr std::initializer_list<ggml_op> snake_pattern { GGML_OP_MUL, GGML_OP_SIN, |
| 514 | GGML_OP_SQR, GGML_OP_MUL, |
| 515 | GGML_OP_ADD }; |
| 516 | |
| 517 | static constexpr std::initializer_list<ggml_op> topk_qsa_pattern { GGML_OP_GET_ROWS, GGML_OP_PERMUTE, |
| 518 | GGML_OP_CONT, GGML_OP_CPY, |
| 519 | GGML_OP_RESHAPE, GGML_OP_ADD, |
| 520 | GGML_OP_TOP_K }; |
| 521 | |
| 522 | static constexpr std::initializer_list<std::array<int, 3>> topk_qsa_edges { |
| 523 | { 1, 0, 0 }, // permute->src[0] == get_rows |
| 524 | { 2, 0, 1 }, // cont->src[0] == permute |
| 525 | { 4, 0, 3 }, // reshape->src[0] == cpy (mask cast) |
| 526 | { 5, 0, 2 }, // add->src[0] == cont |
| 527 | { 5, 1, 4 }, // add->src[1] == reshape |
| 528 | { 6, 0, 5 }, // top_k->src[0] == add |
| 529 | }; |
| 530 | |
| 531 | static constexpr std::initializer_list<ggml_op> rms_norm_mul_add_mul_pattern { GGML_OP_RMS_NORM, GGML_OP_MUL, GGML_OP_ADD, GGML_OP_MUL }; |
| 532 | |
| 533 | static constexpr std::initializer_list<ggml_op> rms_norm_mul_add_pattern { GGML_OP_RMS_NORM, GGML_OP_MUL, GGML_OP_ADD }; |
| 534 | |
| 535 | static constexpr std::initializer_list<ggml_op> rms_norm_mul_rope_view_set_rows_pattern { GGML_OP_RMS_NORM, GGML_OP_MUL, GGML_OP_ROPE, GGML_OP_VIEW, GGML_OP_SET_ROWS }; |
| 536 | |
| 537 | static constexpr std::initializer_list<ggml_op> rms_norm_view_set_rows_pattern { GGML_OP_RMS_NORM, GGML_OP_VIEW, GGML_OP_SET_ROWS }; |
| 538 | |
| 539 | static constexpr std::initializer_list<ggml_op> rope_view_set_rows_pattern { GGML_OP_ROPE, GGML_OP_VIEW, GGML_OP_SET_ROWS }; |
| 540 | |
| 541 | // scale_out*sigmoid(scale_in*x) as the hc_post weights (qwen4exp hc_combine) |
| 542 | static constexpr std::initializer_list<ggml_op> hc_post_gate_pattern { GGML_OP_SCALE, GGML_OP_UNARY, GGML_OP_SCALE, GGML_OP_DSV4_HC_POST }; |
| 543 | |
| 544 | static constexpr std::initializer_list<std::array<int, 3>> hc_post_gate_edges { |
| 545 | { 1, 0, 0 }, // sigmoid->src[0] == scale |
| 546 | { 2, 0, 1 }, // scale->src[0] == sigmoid |
| 547 | { 3, 2, 2 }, // hc_post->src[2] == scale (post) |
| 548 | }; |
| 549 | |
| 550 | static constexpr std::initializer_list<std::array<int, 3>> topk_moe_early_softmax_norm_edges { |
| 551 | { 1, 0, 0 }, // reshape->src[0] == softmax |
| 552 | { 2, 0, 0 }, // argsort->src[0] == softmax |
| 553 | { 3, 0, 2 }, // view->src[0] == argsort |
| 554 | { 4, 0, 1 }, // get_rows->src[0] == reshape |
| 555 | { 4, 1, 3 }, // get_rows->src[1] == view |
| 556 | { 5, 0, 4 }, // reshape->src[0] == get_rows |
| 557 | { 6, 0, 5 }, // sum_rows->src[0] == reshape |
| 558 | { 7, 0, 6 }, // clamp->src[0] == sum_rows |
| 559 | { 8, 0, 5 }, // div->src[0] == reshape |
| 560 | { 8, 1, 7 }, // div->src[1] == clamp |
| 561 | { 9, 0, 8 }, // reshape->src[0] == div |
| 562 | }; |
| 563 | |
| 564 | static constexpr std::initializer_list<std::array<int, 3>> topk_moe_sigmoid_norm_bias_edges { |
| 565 | { 1, 0, 0 }, // reshape->src[0] == sigmoid |
| 566 | { 2, 0, 0 }, // add->src[0] == sigmoid |
| 567 | { 3, 0, 2 }, // argsort->src[0] == add |
| 568 | { 4, 0, 3 }, // view->src[0] == argsort |
| 569 | { 5, 0, 1 }, // get_rows->src[0] == reshape |
| 570 | { 5, 1, 4 }, // get_rows->src[1] == view |
| 571 | { 6, 0, 5 }, // reshape->src[0] == get_rows |
| 572 | { 7, 0, 6 }, // sum_rows->src[0] == reshape |
| 573 | { 8, 0, 7 }, // clamp->src[0] == sum_rows |
| 574 | { 9, 0, 6 }, // div->src[0] == reshape |
| 575 | { 9, 1, 8 }, // div->src[1] == clamp |
| 576 | {10, 0, 9 }, // reshape->src[0] == div |
| 577 | }; |
| 578 | |
| 579 | static constexpr std::initializer_list<std::array<int, 3>> topk_moe_sqrt_softplus_norm_bias_edges { |
| 580 | { 1, 0, 0 }, // sqrt->src[0] == softplus |
| 581 | { 2, 0, 1 }, // reshape->src[0] == sqrt |
| 582 | { 3, 0, 1 }, // add->src[0] == sqrt |
| 583 | { 4, 0, 3 }, // argsort->src[0] == add |
| 584 | { 5, 0, 4 }, // view->src[0] == argsort |
| 585 | { 6, 0, 2 }, // get_rows->src[0] == reshape |
| 586 | { 6, 1, 5 }, // get_rows->src[1] == view |
| 587 | { 7, 0, 6 }, // reshape->src[0] == get_rows |
| 588 | { 8, 0, 7 }, // sum_rows->src[0] == reshape |
| 589 | { 9, 0, 8 }, // clamp->src[0] == sum_rows |
| 590 | {10, 0, 7 }, // div->src[0] == reshape |
| 591 | {10, 1, 9 }, // div->src[1] == clamp |
| 592 | {11, 0,10 }, // reshape->src[0] == div |
| 593 | }; |
| 594 | |
| 595 | static constexpr std::initializer_list<std::array<int, 3>> topk_moe_early_softmax_edges { |
| 596 | { 1, 0, 0 }, // reshape->src[0] == softmax |
| 597 | { 2, 0, 0 }, // argsort->src[0] == softmax |
| 598 | { 3, 0, 2 }, // view->src[0] == argsort |
| 599 | { 4, 0, 1 }, // get_rows->src[0] == reshape |
| 600 | { 4, 1, 3 }, // get_rows->src[1] == view |
| 601 | }; |
| 602 | |
| 603 | static constexpr std::initializer_list<std::array<int, 3>> topk_moe_late_softmax_edges { |
| 604 | { 1, 0, 0 }, // view->src[0] == argsort |
| 605 | { 2, 1, 1 }, // get_rows->src[1] == view |
| 606 | { 3, 0, 2 }, // reshape->src[0] == get_rows |
| 607 | { 4, 0, 3 }, // soft_max->src[0] == reshape |
| 608 | { 5, 0, 4 }, // reshape->src[0] == soft_max |
| 609 | }; |
| 610 | |
| 611 | enum topk_moe_mode { |
| 612 | TOPK_MOE_EARLY_SOFTMAX, |
| 613 | TOPK_MOE_EARLY_SOFTMAX_NORM, |
| 614 | TOPK_MOE_LATE_SOFTMAX, |
| 615 | TOPK_MOE_SIGMOID_NORM_BIAS, |
| 616 | TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS, |
| 617 | TOPK_MOE_COUNT, |
| 618 | }; |
| 619 | |
| 620 | enum rms_norm_mode { |
| 621 | RMS_NORM_MUL, |
| 622 | RMS_NORM_MUL_ADD, |
| 623 | RMS_NORM_MUL_ADD_MUL, |
| 624 | RMS_NORM_MUL_ROPE, |
| 625 | RMS_NORM_MUL_ROPE_VIEW_SET_ROWS, |
| 626 | RMS_NORM_VIEW_SET_ROWS, |
| 627 | RMS_NORM_COUNT, |
| 628 | }; |
| 629 | |
| 630 | static constexpr std::initializer_list<std::array<int, 3>> rope_view_set_rows_edges { |
| 631 | { 1, 0, 0 }, // view->src[0] == rope |
| 632 | { 2, 0, 1 }, // set_rows->src[0] == view |
| 633 | }; |
| 634 | |
| 635 | static constexpr std::initializer_list<std::array<int, 3>> rms_norm_mul_rope_view_set_rows_edges { |
| 636 | { 1, 0, 0 }, // mul->src[0] == rms |
| 637 | { 2, 0, 1 }, // rope->src[0] == mul |
| 638 | { 3, 0, 2 }, // view->src[0] == rope |
| 639 | { 4, 0, 3 }, // set_rows->src[0] == view |
| 640 | }; |
| 641 | |
| 642 | static constexpr std::initializer_list<std::array<int, 3>> rms_norm_view_set_rows_edges { |
| 643 | { 1, 0, 0 }, // view->src[0] == rms_norm |
| 644 | { 2, 0, 1 }, // set_rows->src[0] == view |
| 645 | }; |
| 646 | |
| 647 | static constexpr std::array<ggml_type, 9> lightning_indexer_k_types = { |
| 648 | GGML_TYPE_F32, |
| 649 | GGML_TYPE_F16, |
| 650 | GGML_TYPE_BF16, |
| 651 | GGML_TYPE_Q8_0, |
| 652 | GGML_TYPE_Q5_1, |
| 653 | GGML_TYPE_Q5_0, |
| 654 | GGML_TYPE_Q4_1, |
| 655 | GGML_TYPE_Q4_0, |
| 656 | GGML_TYPE_IQ4_NL, |
| 657 | }; |
| 658 | |
| 659 | class vk_memory_logger; |
| 660 | |
| 661 | struct vk_device_struct { |
| 662 | std::recursive_mutex mutex; |
| 663 | std::mutex queue_submit_mutex; |
| 664 | |
| 665 | // Guards compile_pending, all_pipelines, and the dynamic pipeline maps |
| 666 | // (flash_attn, fa_mask_opt, solve_tri, conv2d, etc). The actual compile |
| 667 | // runs with no lock held, so different pipelines can compile in parallel. |
| 668 | // Lock order is device->mutex -> compile_mutex, never the reverse. |
| 669 | std::mutex compile_mutex; |
| 670 | std::condition_variable compile_cv; |
| 671 | |
| 672 | uint32_t debug_cmdbuf_idx {}; |
| 673 | |
| 674 | vk::PhysicalDevice physical_device; |
| 675 | vk::PhysicalDeviceProperties properties; |
| 676 | std::string name; |
| 677 | uint64_t max_memory_allocation_size; |
| 678 | uint64_t max_buffer_size; |
| 679 | uint64_t suballocation_block_size; |
| 680 | uint64_t min_imported_host_pointer_alignment; |
| 681 | bool external_memory_host {}; |
| 682 | bool external_semaphore {}; |
| 683 | bool fp16; |
| 684 | bool bf16; |
| 685 | bool pipeline_robustness; |
| 686 | bool memory_priority; |
| 687 | vk::Device device; |
| 688 | uint32_t vendor_id; |
| 689 | vk::DriverId driver_id; |
| 690 | vk_device_architecture architecture; |
| 691 | std::unique_ptr<vk_queue> compute_queue; |
| 692 | std::unique_ptr<vk_queue> transfer_queue; |
| 693 | bool single_queue; |
| 694 | bool support_async; |
| 695 | bool async_use_transfer_queue; |
| 696 | bool has_internally_synchronized_queues = false; |
| 697 | uint32_t subgroup_size; |
| 698 | uint32_t subgroup_size_log2; |
| 699 | uint32_t shader_core_count; |
| 700 | bool uma; |
| 701 | bool prefer_host_memory; |
| 702 | bool float_controls_rte_fp16; |
| 703 | bool float_controls_denorm_preserve_fp16; |
| 704 | bool subgroup_basic; |
| 705 | bool subgroup_arithmetic; |
| 706 | bool subgroup_shuffle; |
| 707 | bool subgroup_ballot; |
| 708 | bool subgroup_clustered; |
| 709 | bool subgroup_vote; |
| 710 | bool multi_add; |
| 711 | bool shader_int64; |
| 712 | bool buffer_device_address; |
| 713 | bool vulkan_memory_model; |
| 714 | |
| 715 | bool add_rms_fusion; |
| 716 | uint32_t partials_binding_alignment; |
| 717 | uint32_t max_nodes_per_submit; |
| 718 | |
| 719 | bool shader_64b_indexing; |
| 720 | |
| 721 | bool integer_dot_product; |
| 722 | // 0: default, 1: force mmvq, -1: disable mmvq |
| 723 | int32_t mmvq_mode; |
| 724 | |
| 725 | bool subgroup_size_control; |
| 726 | uint32_t subgroup_min_size; |
| 727 | uint32_t subgroup_max_size; |
| 728 | bool subgroup_require_full_support; |
| 729 | |
| 730 | // floor(log2(maxComputeWorkGroupInvocations)) |
| 731 | uint32_t max_workgroup_size_log2 {}; |
| 732 | |
| 733 | bool coopmat_support; |
| 734 | bool coopmat_acc_f32_support {}; |
| 735 | bool coopmat_acc_f16_support {}; |
| 736 | bool coopmat_bf16_support {}; |
| 737 | bool coopmat_support_16x16x16_f16acc {}; |
| 738 | bool coopmat_support_16x16x16_f32acc {}; |
| 739 | bool coopmat1_fa_support {}; |
| 740 | uint32_t coopmat_m; |
| 741 | uint32_t coopmat_n; |
| 742 | uint32_t coopmat_k; |
| 743 | |
| 744 | bool coopmat_int_support; |
| 745 | uint32_t coopmat_int_m; |
| 746 | uint32_t coopmat_int_n; |
| 747 | uint32_t coopmat_int_k; |
| 748 | |
| 749 | bool coopmat2; |
| 750 | bool coopmat2_bf16_support {}; |
| 751 | bool coopmat2_decode_vector; |
| 752 | |
| 753 | bool dot2_f16 {}; |
| 754 | bool ocp_fp4 {}; |
| 755 | |
| 756 | bool pipeline_executable_properties_support {}; |
| 757 | |
| 758 | bool device_fault {}; |
| 759 | PFN_vkGetDeviceFaultInfoEXT pfn_vkGetDeviceFaultInfoEXT {}; |
| 760 | |
| 761 | bool serialize_submissions {}; |
| 762 | |
| 763 | const ggml_cgraph * diag_cgraph {}; |
| 764 | int diag_prev_start = -1; |
| 765 | int diag_prev_end = -1; |
| 766 | |
| 767 | size_t idx; |
| 768 | |
| 769 | bool mul_mat_l[GGML_TYPE_COUNT]; |
| 770 | bool mul_mat_m[GGML_TYPE_COUNT]; |
| 771 | bool mul_mat_s[GGML_TYPE_COUNT]; |
| 772 | bool mul_mat_id_l[GGML_TYPE_COUNT]; |
| 773 | bool mul_mat_id_m[GGML_TYPE_COUNT]; |
| 774 | bool mul_mat_id_s[GGML_TYPE_COUNT]; |
| 775 | |
| 776 | // Separate flags for the q8_1 (integer dot) mmq path, whose shader uses |
| 777 | // a different shared-memory layout than the float matmul shaders. |
| 778 | bool mul_mat_l_int[GGML_TYPE_COUNT]; |
| 779 | bool mul_mat_m_int[GGML_TYPE_COUNT]; |
| 780 | bool mul_mat_s_int[GGML_TYPE_COUNT]; |
| 781 | bool mul_mat_id_l_int[GGML_TYPE_COUNT]; |
| 782 | bool mul_mat_id_m_int[GGML_TYPE_COUNT]; |
| 783 | bool mul_mat_id_s_int[GGML_TYPE_COUNT]; |
| 784 | |
| 785 | vk::DescriptorSetLayout dsl; |
| 786 | |
| 787 | std::map<vk_matmul_pipeline_key, std::vector<vk_matmul_pipeline_pair>> pipeline_matmul; |
| 788 | matmul_tile_selector_t matmul_tile_selector; |
| 789 | matmul_tile_selector_t matmul_id_tile_selector; |
| 790 | |
| 791 | vk_pipeline pipeline_matmul_split_k_reduce; |
| 792 | vk_pipeline pipeline_quantize_q8_1_x4; |
| 793 | |
| 794 | vk_pipeline pipeline_dequant[GGML_TYPE_COUNT]; |
| 795 | vk_pipeline pipeline_dequant_transpose[GGML_TYPE_COUNT]; // fused dequant+transpose for FA quant-KV |
| 796 | vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols]; |
| 797 | vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols]; |
| 798 | vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT]; |
| 799 | |
| 800 | vk_pipeline pipeline_dequant_mul_mat_vec_q8_1_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols]; |
| 801 | vk_pipeline pipeline_dequant_mul_mat_vec_id_q8_1_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT]; |
| 802 | |
| 803 | vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio]; |
| 804 | vk_pipeline pipeline_mul_mat_vec_nc_f16_f32; |
| 805 | vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT]; |
| 806 | vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT]; |
| 807 | vk_pipeline pipeline_get_rows_back_f32; |
| 808 | vk_pipeline pipeline_acc_f32; |
| 809 | vk_pipeline pipeline_set_f32; |
| 810 | |
| 811 | // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16] |
| 812 | vk_pipeline pipeline_add[2][2][2]; |
| 813 | vk_pipeline pipeline_add_norepeat[2][2][2]; |
| 814 | vk_pipeline pipeline_sub[2][2][2]; |
| 815 | vk_pipeline pipeline_sub_norepeat[2][2][2]; |
| 816 | vk_pipeline pipeline_mul[2][2][2]; |
| 817 | vk_pipeline pipeline_mul_norepeat[2][2][2]; |
| 818 | vk_pipeline pipeline_div[2][2][2]; |
| 819 | vk_pipeline pipeline_div_norepeat[2][2][2]; |
| 820 | vk_pipeline pipeline_add_rms[2][2][2]; |
| 821 | vk_pipeline pipeline_add_rms_norepeat[2][2][2]; |
| 822 | |
| 823 | // indexed by num_additional_fused_ops == num_adds - 1 |
| 824 | vk_pipeline pipeline_multi_add[MAX_FUSED_ADDS]; |
| 825 | vk_pipeline pipeline_multi_add_rms[MAX_FUSED_ADDS]; |
| 826 | |
| 827 | vk_pipeline pipeline_add_id_f32; |
| 828 | |
| 829 | vk_pipeline pipeline_concat_i8, pipeline_concat_i16, pipeline_concat_i32, pipeline_concat_i64; |
| 830 | vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32, pipeline_upscale_bicubic_f32, pipeline_upscale_bilinear_antialias_f32; |
| 831 | vk_pipeline pipeline_scale_f32; |
| 832 | vk_pipeline pipeline_log[2]; |
| 833 | vk_pipeline pipeline_tri[2]; |
| 834 | vk_pipeline pipeline_diag[2]; |
| 835 | vk_pipeline pipeline_clamp[2]; |
| 836 | vk_pipeline pipeline_pad_f32; |
| 837 | vk_pipeline pipeline_pad_reflect_1d_f32; |
| 838 | vk_pipeline pipeline_roll_f32; |
| 839 | vk_pipeline pipeline_repeat_i32, pipeline_repeat_back_f32; |
| 840 | vk_pipeline pipeline_repeat_i16; |
| 841 | vk_pipeline pipeline_cpy_32, pipeline_cpy_f32_f16, pipeline_cpy_16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16, pipeline_cpy_bf16_f32, pipeline_cpy_bf16_f16, pipeline_cpy_f32_i32, pipeline_cpy_i32_f32; |
| 842 | vk_pipeline pipeline_contig_cpy_32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16, pipeline_contig_cpy_bf16_f32, pipeline_contig_cpy_bf16_f16, pipeline_contig_cpy_f32_i32, pipeline_contig_cpy_i32_f32; |
| 843 | vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT]; |
| 844 | vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT]; |
| 845 | vk_pipeline pipeline_cpy_transpose_16, pipeline_cpy_transpose_32; |
| 846 | vk_pipeline pipeline_cpy_transpose_02_16, pipeline_cpy_transpose_02_32; |
| 847 | // [src0 0=fp32,1=fp16][dst] |
| 848 | vk_pipeline pipeline_set_rows_i32[2][GGML_TYPE_COUNT]; |
| 849 | vk_pipeline pipeline_set_rows_i64[2][GGML_TYPE_COUNT]; |
| 850 | vk_pipeline pipeline_norm_f32; |
| 851 | vk_pipeline pipeline_group_norm_f32; |
| 852 | std::array<vk_pipeline, 3> pipeline_rms_norm_f32; |
| 853 | std::array<vk_pipeline, 3> pipeline_rms_norm_mul_f32; |
| 854 | std::array<vk_pipeline, 3> pipeline_rms_norm_mul_add_f32; |
| 855 | std::array<vk_pipeline, 3> pipeline_rms_norm_mul_add_mul_f32; |
| 856 | vk_pipeline pipeline_rms_norm_mul_add_partials_f32; |
| 857 | vk_pipeline pipeline_rms_norm_mul_add_mul_partials_f32; |
| 858 | vk_pipeline pipeline_rms_norm_set_rows_f32_f32; |
| 859 | vk_pipeline pipeline_rms_norm_set_rows_f32_f16; |
| 860 | vk_pipeline pipeline_rms_norm_partials_f32; |
| 861 | vk_pipeline pipeline_rms_norm_mul_partials_f32; |
| 862 | std::array<vk_pipeline, 3> pipeline_rms_norm_mul_rope_f32_f32; |
| 863 | std::array<vk_pipeline, 3> pipeline_rms_norm_mul_rope_f32_f16; |
| 864 | vk_pipeline pipeline_rms_norm_back_f32; |
| 865 | vk_pipeline pipeline_l2_norm_f32; |
| 866 | |
| 867 | // [src/dst 0=fp32,1=fp16] |
| 868 | vk_pipeline pipeline_exp[2]; |
| 869 | vk_pipeline pipeline_expm1[2]; |
| 870 | vk_pipeline pipeline_elu[2]; |
| 871 | vk_pipeline pipeline_gelu[2]; |
| 872 | vk_pipeline pipeline_gelu_erf[2]; |
| 873 | vk_pipeline pipeline_gelu_quick[2]; |
| 874 | vk_pipeline pipeline_silu[2]; |
| 875 | vk_pipeline pipeline_relu[2]; |
| 876 | vk_pipeline pipeline_sqr[2]; |
| 877 | vk_pipeline pipeline_sqrt[2]; |
| 878 | vk_pipeline pipeline_sin[2]; |
| 879 | vk_pipeline pipeline_cos[2]; |
| 880 | vk_pipeline pipeline_xielu[2]; |
| 881 | vk_pipeline pipeline_neg[2]; |
| 882 | vk_pipeline pipeline_tanh[2]; |
| 883 | vk_pipeline pipeline_sigmoid[2]; |
| 884 | vk_pipeline pipeline_hardsigmoid[2]; |
| 885 | vk_pipeline pipeline_hardswish[2]; |
| 886 | vk_pipeline pipeline_abs[2]; |
| 887 | vk_pipeline pipeline_softplus[2]; |
| 888 | vk_pipeline pipeline_step[2]; |
| 889 | vk_pipeline pipeline_round[2]; |
| 890 | vk_pipeline pipeline_ceil[2]; |
| 891 | vk_pipeline pipeline_floor[2]; |
| 892 | vk_pipeline pipeline_trunc[2]; |
| 893 | vk_pipeline pipeline_sgn[2]; |
| 894 | |
| 895 | // fused UNARY+MUL pipelines: [op][f16][norepeat][op_on_b] |
| 896 | vk_pipeline pipeline_unary_mul[4][2][2][2]; |
| 897 | |
| 898 | vk_pipeline pipeline_add1_f16_f16; |
| 899 | vk_pipeline pipeline_add1_f16_f32; |
| 900 | vk_pipeline pipeline_add1_f32_f32; |
| 901 | |
| 902 | vk_pipeline pipeline_arange_f32; |
| 903 | |
| 904 | vk_pipeline pipeline_fill_f32; |
| 905 | vk_pipeline pipeline_fill_f16; |
| 906 | |
| 907 | vk_pipeline pipeline_geglu[2]; |
| 908 | vk_pipeline pipeline_reglu[2]; |
| 909 | vk_pipeline pipeline_swiglu[2]; |
| 910 | vk_pipeline pipeline_swiglu_oai[2]; |
| 911 | vk_pipeline pipeline_swiglu_clamp[2]; |
| 912 | vk_pipeline pipeline_geglu_erf[2]; |
| 913 | vk_pipeline pipeline_geglu_quick[2]; |
| 914 | |
| 915 | vk_pipeline pipeline_leaky_relu[2]; |
| 916 | vk_pipeline pipeline_silu_back_f32; |
| 917 | vk_pipeline pipeline_diag_mask_inf_f32; |
| 918 | vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16; |
| 919 | vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512; |
| 920 | vk_pipeline pipeline_soft_max_back_f32; |
| 921 | |
| 922 | vk_pipeline pipeline_soft_max_large1_f32, pipeline_soft_max_large1_f32_f16; |
| 923 | vk_pipeline pipeline_soft_max_large2_f32, pipeline_soft_max_large2_f32_f16; |
| 924 | vk_pipeline pipeline_soft_max_large3_f32, pipeline_soft_max_large3_f32_f16; |
| 925 | |
| 926 | vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16, pipeline_rope_norm_f32_f16; |
| 927 | vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16, pipeline_rope_neox_f32_f16; |
| 928 | vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16, pipeline_rope_multi_f32_f16; |
| 929 | vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16; |
| 930 | vk_pipeline pipeline_argsort_f32[num_argsort_pipelines]; |
| 931 | vk_pipeline pipeline_argsort_large_f32[num_argsort_pipelines]; |
| 932 | vk_pipeline pipeline_topk_f32[num_topk_pipelines]; |
| 933 | vk_pipeline pipeline_topk_radix_f32; |
| 934 | vk_pipeline pipeline_topk_radix_qsa; // qwen4 QSA indexer fusion (f16 mask) |
| 935 | vk_pipeline pipeline_sum_rows_f32; |
| 936 | vk_pipeline pipeline_cross_entropy_loss_f32, pipeline_cross_entropy_loss_f32_wg512; |
| 937 | vk_pipeline pipeline_cross_entropy_loss_back_f32, pipeline_cross_entropy_loss_back_f32_wg512; |
| 938 | vk_pipeline pipeline_fwht_f32[4]; |
| 939 | vk_pipeline pipeline_cumsum_f32; |
| 940 | vk_pipeline pipeline_cumsum_small_f32; |
| 941 | vk_pipeline pipeline_cumsum_multipass1_f32; |
| 942 | vk_pipeline pipeline_cumsum_multipass2_f32; |
| 943 | vk_pipeline pipeline_argmax_f32; |
| 944 | vk_pipeline pipeline_count_equal_i32; |
| 945 | vk_pipeline pipeline_dsv4_hc_comb_f32; |
| 946 | vk_pipeline pipeline_dsv4_hc_pre_f32; |
| 947 | vk_pipeline pipeline_dsv4_hc_pre_gated_f32; |
| 948 | vk_pipeline pipeline_dsv4_hc_post_f32; |
| 949 | vk_pipeline pipeline_dsv4_hc_post_nocomb_f32; |
| 950 | std::map<vk_solve_tri_pipeline_state, vk_pipeline> pipeline_solve_tri_f32; |
| 951 | vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16; |
| 952 | vk_pipeline pipeline_im2col_3d_f32, pipeline_im2col_3d_f32_f16; |
| 953 | vk_pipeline pipeline_timestep_embedding_f32; |
| 954 | vk_pipeline pipeline_conv_transpose_1d_f32; |
| 955 | vk_pipeline pipeline_col2im_1d_f32; |
| 956 | vk_pipeline pipeline_col2im_1d_f16; |
| 957 | vk_pipeline pipeline_col2im_1d_bf16; |
| 958 | vk_pipeline pipeline_out_prod_f32; |
| 959 | vk_pipeline pipeline_snake_f32; |
| 960 | vk_pipeline pipeline_snake_f16; |
| 961 | vk_pipeline pipeline_snake_bf16; |
| 962 | vk_pipeline pipeline_pool1d_f32; |
| 963 | vk_pipeline pipeline_pool2d_f32; |
| 964 | vk_pipeline pipeline_pool2d_subgroup_f32; |
| 965 | vk_pipeline pipeline_rwkv_wkv6_f32; |
| 966 | vk_pipeline pipeline_rwkv_wkv7_f32; |
| 967 | vk_pipeline pipeline_gated_linear_attn_f32; |
| 968 | vk_pipeline pipeline_lightning_indexer_f32[GGML_TYPE_COUNT]; |
| 969 | // [size_idx][kda] where size_idx: 0=d16, 1=d32, 2=d64, 3=d128 |
| 970 | vk_pipeline pipeline_gated_delta_net[4][2]; |
| 971 | vk_pipeline pipeline_ssm_scan_f32_d128; |
| 972 | vk_pipeline pipeline_ssm_scan_f32_d256; |
| 973 | vk_pipeline pipeline_ssm_conv_f32; |
| 974 | vk_pipeline pipeline_ssm_conv_silu_f32; |
| 975 | vk_pipeline pipeline_ssm_conv_bias_silu_f32; |
| 976 | vk_pipeline pipeline_opt_step_adamw_f32; |
| 977 | vk_pipeline pipeline_opt_step_sgd_f32; |
| 978 | std::map<vk_conv2d_pipeline_state, vk_pipeline> pipeline_conv2d_f32[CONV_SHAPE_COUNT]; |
| 979 | std::map<vk_conv2d_pipeline_state, vk_pipeline> pipeline_conv2d_f16_f32[CONV_SHAPE_COUNT]; |
| 980 | std::map<vk_conv2d_pipeline_state, vk_pipeline> pipeline_conv_transpose_2d_f32[CONV_SHAPE_COUNT]; |
| 981 | std::map<vk_conv2d_pipeline_state, vk_pipeline> pipeline_conv_transpose_2d_f16_f32[CONV_SHAPE_COUNT]; |
| 982 | std::map<vk_conv3d_pipeline_state, vk_pipeline> pipeline_conv3d_f32[CONV_SHAPE_COUNT]; |
| 983 | std::map<vk_conv3d_pipeline_state, vk_pipeline> pipeline_conv3d_f16_f32[CONV_SHAPE_COUNT]; |
| 984 | vk_pipeline pipeline_conv2d_dw_whcn_f32, pipeline_conv2d_dw_whcn_f16_f32; |
| 985 | vk_pipeline pipeline_conv2d_dw_cwhn_f32, pipeline_conv2d_dw_cwhn_f16_f32; |
| 986 | |
| 987 | std::map<vk_fa_pipeline_state, vk_pipeline> pipeline_flash_attn_f32_f16; |
| 988 | |
| 989 | std::map<std::pair<uint32_t, uint32_t>, vk_pipeline> pipeline_fa_mask_opt; |
| 990 | |
| 991 | vk_pipeline pipeline_fa_sparse_compact; |
| 992 | vk_pipeline pipeline_fa_sparse_compact_subgroup; |
| 993 | bool fa_sparse_compact_use_subgroups; |
| 994 | |
| 995 | vk_pipeline pipeline_flash_attn_split_k_reduce; |
| 996 | std::map<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>, std::pair<vk_pipeline, vk_pipeline>> pipeline_xe_fa_decode_dual_phases; |
| 997 | vk_pipeline pipeline_count_experts; |
| 998 | |
| 999 | // [2] is for whether to take n_experts from spec constant (0) or push constant (1) |
| 1000 | vk_pipeline pipeline_topk_moe[num_topk_moe_pipelines][2]; |
| 1001 | |
| 1002 | std::vector<vk_pipeline_ref> all_pipelines; |
| 1003 | |
| 1004 | vk::Fence fence; |
| 1005 | vk_buffer sync_staging; |
| 1006 | |
| 1007 | ggml_backend_buffer_type buffer_type; |
| 1008 | |
| 1009 | bool disable_fusion; |
| 1010 | bool disable_descriptor_reuse; |
| 1011 | std::atomic<uint64_t> buffer_destroy_count {}; |
| 1012 | bool disable_host_visible_vidmem; |
| 1013 | bool allow_sysmem_fallback; |
| 1014 | bool disable_graph_optimize; |
| 1015 | |
| 1016 | std::unique_ptr<vk_memory_logger> memory_logger; |
| 1017 | |
| 1018 | ~vk_device_struct(); |
| 1019 | |
| 1020 | }; |
| 1021 | |
| 1022 | inline void vk_command_pool::init(vk_device& device, vk_queue *q_) { |
| 1023 | cmd_buffers.clear(); |
| 1024 | q = q_; |
| 1025 | |
| 1026 | vk::CommandPoolCreateInfo command_pool_create_info( |
| 1027 | vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT), |
| 1028 | q->queue_family_index); |
| 1029 | pool = device->device.createCommandPool(command_pool_create_info); |
| 1030 | } |
| 1031 | |
| 1032 | inline void vk_command_pool::destroy(vk::Device& device) { |
| 1033 | device.destroyCommandPool(pool); |
| 1034 | pool = nullptr; |
| 1035 | cmd_buffers.clear(); |
| 1036 | } |
| 1037 | |
| 1038 | struct vk_buffer_struct { |
| 1039 | vk::Buffer buffer = VK_NULL_HANDLE; |
| 1040 | vk::DeviceMemory device_memory = VK_NULL_HANDLE; |
| 1041 | vk::MemoryPropertyFlags memory_property_flags; |
| 1042 | void * ptr; |
| 1043 | size_t size = 0; |
| 1044 | vk::DeviceAddress bda_addr {}; |
| 1045 | |
| 1046 | vk_device device; |
| 1047 | |
| 1048 | ~vk_buffer_struct() { |
| 1049 | if (size == 0) { |
| 1050 | return; |
| 1051 | } |
| 1052 | VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")"); |
| 1053 | |
| 1054 | // bump before destroying, so a thread that sees the buffer gone also sees the new count |
| 1055 | device->buffer_destroy_count.fetch_add(1, std::memory_order_release); |
| 1056 | device->device.freeMemory(device_memory); |
| 1057 | device->device.destroyBuffer(buffer); |
| 1058 | } |
| 1059 | }; |
| 1060 | |
| 1061 | struct vk_subbuffer { |
| 1062 | vk_buffer buffer; |
| 1063 | uint64_t offset; |
| 1064 | uint64_t size; |
| 1065 | |
| 1066 | operator vk::DescriptorBufferInfo() const { |
| 1067 | return { buffer->buffer, offset, size }; |
| 1068 | } |
| 1069 | }; |
| 1070 | |
| 1071 | struct vk_semaphore { |
| 1072 | vk::Semaphore s; |
| 1073 | uint64_t value; |
| 1074 | }; |
| 1075 | |
| 1076 | struct vk_event { |
| 1077 | std::vector<vk::Event> events_free; // Events available for reuse |
| 1078 | std::vector<vk::Event> events_submitted; // Events that are fully submitted and can be reused on next synchronize |
| 1079 | vk::Event event; |
| 1080 | bool has_event; |
| 1081 | |
| 1082 | vk_semaphore tl_semaphore; |
| 1083 | vk_command_buffer* cmd_buffer = nullptr; |
| 1084 | uint64_t cmd_buffer_use_counter = 0; |
| 1085 | }; |
| 1086 | |
| 1087 | struct vk_submission { |
| 1088 | vk_command_buffer* buffer = nullptr; |
| 1089 | std::vector<vk_semaphore> wait_semaphores; |
| 1090 | std::vector<vk_semaphore> signal_semaphores; |
| 1091 | // submit waits until *ready >= ready_value |
| 1092 | const std::atomic<uint64_t> * ready = nullptr; |
| 1093 | uint64_t ready_value = 0; |
| 1094 | }; |
| 1095 | |
| 1096 | typedef std::vector<vk_submission> vk_sequence; |
| 1097 | |
| 1098 | #define MAT_VEC_FUSION_FLAGS_BIAS0 0x1 |
| 1099 | |
| 1100 | #define MAT_VEC_FUSION_FLAGS_BIAS1 0x2 |
| 1101 | |
| 1102 | #define MAT_VEC_FUSION_FLAGS_SCALE0 0x4 |
| 1103 | |
| 1104 | #define MAT_VEC_FUSION_FLAGS_SCALE1 0x8 |
| 1105 | |
| 1106 | struct vk_staging_memcpy { |
| 1107 | vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {} |
| 1108 | |
| 1109 | void * dst; |
| 1110 | const void * src; |
| 1111 | size_t n; |
| 1112 | }; |
| 1113 | |
| 1114 | struct vk_staging_memset { |
| 1115 | vk_staging_memset(void * _dst, uint32_t _val, size_t _n) : dst(_dst), val(_val), n(_n) {} |
| 1116 | |
| 1117 | void * dst; |
| 1118 | uint32_t val; |
| 1119 | size_t n; |
| 1120 | }; |
| 1121 | |
| 1122 | struct vk_context_struct { |
| 1123 | vk_submission * s; |
| 1124 | std::vector<vk_sequence> seqs; |
| 1125 | |
| 1126 | int exit_tensor_idx; |
| 1127 | |
| 1128 | std::vector<vk_staging_memcpy> in_memcpys; |
| 1129 | std::vector<vk_staging_memcpy> out_memcpys; |
| 1130 | std::vector<vk_staging_memset> memsets; |
| 1131 | |
| 1132 | std::vector<std::string> debug_labels; |
| 1133 | |
| 1134 | vk_command_pool * p {}; |
| 1135 | }; |
| 1136 | |
| 1137 | typedef std::shared_ptr<vk_context_struct> vk_context; |
| 1138 | |
| 1139 | typedef std::weak_ptr<vk_context_struct> vk_context_ref; |
| 1140 | |
| 1141 | struct ggml_vk_garbage_collector { |
| 1142 | std::vector<vk_semaphore> tl_semaphores; |
| 1143 | std::vector<vk_semaphore> semaphores; |
| 1144 | std::vector<vk::Event> events; |
| 1145 | std::vector<vk_context> contexts; |
| 1146 | }; |
| 1147 | |
| 1148 | #define VK_LOG_MEMORY(msg) if (vk_memory_logger_enabled) { std::cerr << "ggml_vulkan memory: " << msg << std::endl; } |
| 1149 | |
| 1150 | static std::string format_size(size_t size) { |
| 1151 | const size_t kib = 1024; |
| 1152 | const size_t mib = kib * 1024; |
| 1153 | const size_t gib = mib * 1024; |
| 1154 | |
| 1155 | std::ostringstream oss; |
| 1156 | oss << std::fixed << std::setprecision(2); |
| 1157 | |
| 1158 | if (size >= gib) { |
| 1159 | oss << static_cast<double>(size) / gib << " GiB"; |
| 1160 | } else if (size >= mib) { |
| 1161 | oss << static_cast<double>(size) / mib << " MiB"; |
| 1162 | } else if (size >= kib) { |
| 1163 | oss << static_cast<double>(size) / kib << " KiB"; |
| 1164 | } else { |
| 1165 | oss << size << " B"; |
| 1166 | } |
| 1167 | |
| 1168 | return oss.str(); |
| 1169 | } |
| 1170 | |
| 1171 | class vk_memory_logger { |
| 1172 | public: |
| 1173 | vk_memory_logger(): total_device(0), total_host(0) {} |
| 1174 | void log_allocation(vk_buffer_ref buf_ref, size_t size); |
| 1175 | void log_deallocation(vk_buffer_ref buf_ref); |
| 1176 | |
| 1177 | private: |
| 1178 | std::map<vk::Buffer, size_t> allocations; // Track allocations |
| 1179 | size_t total_device; |
| 1180 | size_t total_host; |
| 1181 | static std::mutex log_mutex; |
| 1182 | }; |
| 1183 | |
| 1184 | inline std::mutex vk_memory_logger::log_mutex; |
| 1185 | |
| 1186 | class vk_perf_logger { |
| 1187 | public: |
| 1188 | void print_timings(bool force = false); |
| 1189 | |
| 1190 | |
| 1191 | std::string get_node_fusion_name(const ggml_tensor * node, const char *fusion_name, uint64_t *n_flops); |
| 1192 | |
| 1193 | |
| 1194 | void log_timing(const ggml_tensor * node, const char *fusion_name, uint64_t time) { |
| 1195 | uint64_t n_flops; |
| 1196 | std::string name = get_node_fusion_name(node, fusion_name, &n_flops); |
| 1197 | if (n_flops) { |
| 1198 | flops[name].push_back(n_flops); |
| 1199 | } |
| 1200 | timings[name].push_back(time); |
| 1201 | } |
| 1202 | |
| 1203 | void log_timing(const std::vector<ggml_tensor *> &nodes, const std::vector<const char *> &names, uint64_t time) { |
| 1204 | uint64_t total_flops = 0; |
| 1205 | std::string name; |
| 1206 | for (size_t n = 0; n < nodes.size(); ++n) { |
| 1207 | uint64_t n_flops = 0; |
| 1208 | name += get_node_fusion_name(nodes[n], names[n], &n_flops); |
| 1209 | total_flops += n_flops; |
| 1210 | |
| 1211 | if (n != nodes.size() - 1) { |
| 1212 | name += ", "; |
| 1213 | } |
| 1214 | } |
| 1215 | if (total_flops) { |
| 1216 | flops[name].push_back(total_flops); |
| 1217 | } |
| 1218 | timings[name].push_back(time); |
| 1219 | } |
| 1220 | |
| 1221 | private: |
| 1222 | std::map<std::string, std::vector<uint64_t>> timings; |
| 1223 | std::map<std::string, std::vector<uint64_t>> flops; |
| 1224 | uint32_t print_count {}; |
| 1225 | }; |
| 1226 | |
| 1227 | struct ggml_backend_vk_context { |
| 1228 | std::string name; |
| 1229 | |
| 1230 | vk_device device; |
| 1231 | |
| 1232 | size_t semaphore_idx, binary_semaphore_idx, event_idx; |
| 1233 | ggml_vk_garbage_collector gc; |
| 1234 | size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k, prealloc_size_add_rms_partials, prealloc_size_add_rms_partials_offset; |
| 1235 | vk_buffer prealloc_x, prealloc_y, prealloc_split_k, prealloc_add_rms_partials, sync_staging; |
| 1236 | vk::Fence fence, almost_ready_fence; |
| 1237 | bool submit_pending {}; |
| 1238 | bool almost_ready_fence_pending {}; |
| 1239 | // Set before op_add and unset after op_rms_norm to indicate that the add should |
| 1240 | // write partial sums to accumulate the square of the vector components |
| 1241 | bool do_add_rms_partials_offset_calculation; |
| 1242 | bool do_add_rms_partials; |
| 1243 | |
| 1244 | uint64_t last_total_flops {UINT64_MAX}; |
| 1245 | |
| 1246 | // Cache most recent tensor that was converted into prealloc_y, and what pipeline it used to convert. |
| 1247 | vk_pipeline_struct * prealloc_y_last_pipeline_used {}; |
| 1248 | const ggml_tensor * prealloc_y_last_tensor_used {}; |
| 1249 | // True when the K dimension in prealloc_y is padded. |
| 1250 | bool prealloc_y_last_k_padded {}; |
| 1251 | |
| 1252 | // Track which nodes have been used since the last sync, and whether they were written to |
| 1253 | std::vector<const ggml_tensor *> unsynced_nodes_written; |
| 1254 | std::vector<const ggml_tensor *> unsynced_nodes_read; |
| 1255 | // Track which prealloc buffers have pending reads that need to be synchronized. |
| 1256 | // These are checked before writing to the buffer (and call ggml_vk_sync_buffers if set), |
| 1257 | // and set to true after the buffer contents are consumed. |
| 1258 | bool prealloc_x_need_sync, prealloc_y_need_sync, prealloc_split_k_need_sync; |
| 1259 | |
| 1260 | vk_context_ref compute_ctx; |
| 1261 | |
| 1262 | vk_context_ref transfer_ctx; |
| 1263 | vk_semaphore transfer_semaphore; |
| 1264 | uint64_t transfer_semaphore_last_submitted {}; |
| 1265 | |
| 1266 | std::vector<vk_context_ref> tensor_ctxs; |
| 1267 | |
| 1268 | std::vector<vk::DescriptorPool> descriptor_pools; |
| 1269 | std::vector<vk::DescriptorSet> descriptor_sets; |
| 1270 | // last bindings written to each set; descriptor_sets is append-only so an index always names the same set |
| 1271 | std::vector<std::vector<vk::DescriptorBufferInfo>> descriptor_set_bindings; |
| 1272 | uint64_t descriptor_set_bindings_destroy_count {}; |
| 1273 | uint32_t descriptor_set_idx {}; |
| 1274 | uint32_t pipeline_descriptor_set_requirements {}; |
| 1275 | |
| 1276 | vk_command_pool compute_cmd_pool; |
| 1277 | vk_command_pool transfer_cmd_pool; |
| 1278 | |
| 1279 | // number of additional consecutive nodes that are being fused with the |
| 1280 | // node currently being processed |
| 1281 | int num_additional_fused_ops {}; |
| 1282 | // Bitmask of which fused ops need to write an intermediate value to memory. |
| 1283 | // Bit 'i' means nodes[start_of_fusion + i] writes to memory. |
| 1284 | // If there's no fusion, bit 0 is still set. |
| 1285 | int fused_ops_write_mask {}; |
| 1286 | topk_moe_mode fused_topk_moe_mode {}; |
| 1287 | bool fused_topk_moe_scale {}; |
| 1288 | // QSA indexer gather+add+top_k fused into one radix-select |
| 1289 | bool fused_topk_qsa {}; |
| 1290 | bool fused_hc_post_gate {}; |
| 1291 | rms_norm_mode fused_rms_norm_mode {RMS_NORM_COUNT}; |
| 1292 | |
| 1293 | // for GGML_VK_PERF_LOGGER |
| 1294 | std::unique_ptr<vk_perf_logger> perf_logger; |
| 1295 | vk::QueryPool query_pool; |
| 1296 | std::vector<const char *> query_fusion_names; |
| 1297 | std::vector<int> query_fusion_node_count; |
| 1298 | std::vector<ggml_tensor *> query_nodes; |
| 1299 | std::vector<int> query_node_idx; |
| 1300 | int32_t num_queries {}; |
| 1301 | int32_t query_idx {}; |
| 1302 | }; |
| 1303 | |
| 1304 | struct ggml_backend_vk_buffer_context { |
| 1305 | vk_device_ref device; |
| 1306 | vk_buffer dev_buffer; |
| 1307 | std::string name; |
| 1308 | |
| 1309 | ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) : |
| 1310 | device(device), |
| 1311 | dev_buffer(dev_buffer), |
| 1312 | name(name) { |
| 1313 | } |
| 1314 | |
| 1315 | ~ggml_backend_vk_buffer_context(); |
| 1316 | |
| 1317 | }; |
| 1318 | |
| 1319 | struct vk_host_allocation { |
| 1320 | void * base = nullptr; |
| 1321 | size_t size = 0; |
| 1322 | vk_buffer views[GGML_VK_MAX_DEVICES]; // indexed by device->idx; null = not (yet) mapped here |
| 1323 | }; |
| 1324 | |
| 1325 | struct vk_instance_t { |
| 1326 | vk::Instance instance; |
| 1327 | |
| 1328 | bool debug_utils_support = false; // VK_EXT_debug_utils enabled |
| 1329 | PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {}; |
| 1330 | PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {}; |
| 1331 | PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {}; |
| 1332 | PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {}; |
| 1333 | PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {}; |
| 1334 | PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {}; |
| 1335 | |
| 1336 | std::vector<size_t> device_indices; |
| 1337 | std::vector<bool> device_supports_membudget; |
| 1338 | std::vector<bool> device_host_buffer_import; |
| 1339 | vk_device devices[GGML_VK_MAX_DEVICES]; |
| 1340 | |
| 1341 | std::vector<vk_host_allocation> host_allocations; |
| 1342 | std::shared_mutex host_allocations_mutex; |
| 1343 | |
| 1344 | size_t host_import_alignment = 0; |
| 1345 | uint32_t host_import_failed = 0; // bitmask by device->idx, guarded by host_allocations_mutex |
| 1346 | |
| 1347 | bool multi_device = false; |
| 1348 | bool allreduce_disabled = false; |
| 1349 | }; |
| 1350 | |
| 1351 | typedef void (*ggml_vk_func_t)(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst); |
| 1352 | |
| 1353 | static constexpr uint32_t kSpvOpCooperativeMatrixLoadTensorNV = 5367; |
| 1354 | |
| 1355 | static constexpr uint32_t kSpvCapabilityCooperativeMatrixDecodeVectorNV = 5447; |
| 1356 | |
| 1357 | static constexpr uint32_t kSpvTensorAddressingDecodeVectorFuncBit = 0x4; |
| 1358 | |
| 1359 | struct vk_fa_tuning_params { |
| 1360 | FaCodePath path; |
| 1361 | uint32_t workgroup_size; |
| 1362 | uint32_t subgroup_size; |
| 1363 | uint32_t block_rows; |
| 1364 | uint32_t block_cols; |
| 1365 | uint32_t d_split; |
| 1366 | uint32_t row_split; |
| 1367 | bool shmem_staging; |
| 1368 | bool disable_subgroups; |
| 1369 | uint32_t limit_occupancy_shmem; |
| 1370 | |
| 1371 | void print() const { |
| 1372 | std::cerr << "path=" << path << " workgroup_size=" << workgroup_size << " subgroup_size=" << subgroup_size << |
| 1373 | " block_rows=" << block_rows << " block_cols=" << block_cols << " d_split=" << d_split << |
| 1374 | " row_split=" << row_split << " shmem_staging=" << shmem_staging << " disable_subgroups=" << disable_subgroups << |
| 1375 | " limit_occupancy_shmem=" << limit_occupancy_shmem << std::endl; |
| 1376 | } |
| 1377 | }; |
| 1378 | |
| 1379 | struct GpuPipelineConfig { |
| 1380 | // GPU architecture identifier. |
| 1381 | // Example: vk_device_architecture::AMD_GCN |
| 1382 | vk_device_architecture arch; |
| 1383 | |
| 1384 | // Mapping of pipeline names to their specific subgroup sizes. |
| 1385 | // Example: {"soft_max_f32", 64} |
| 1386 | std::unordered_map<std::string, uint32_t> pipelines; |
| 1387 | |
| 1388 | // Default subgroup size for this GPU. |
| 1389 | // Defaults to 0 if not explicitly provided. |
| 1390 | uint32_t default_subgroup_size = 0; |
| 1391 | }; |
| 1392 | |
| 1393 | static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32; |
| 1394 | |
| 1395 | struct CompileTask { |
| 1396 | vk_pipeline pipeline; |
| 1397 | size_t spv_size; |
| 1398 | const void * spv_data; |
| 1399 | std::string entrypoint; |
| 1400 | uint32_t parameter_count; |
| 1401 | std::array<uint32_t, 3> wg_denoms; |
| 1402 | std::vector<uint32_t> specialization_constants; |
| 1403 | bool disable_robustness; |
| 1404 | bool require_full_subgroups; |
| 1405 | uint32_t required_subgroup_size; |
| 1406 | }; |
| 1407 | |
| 1408 | struct ggml_vk_debug_label { |
| 1409 | // at most one of these is set, depending on the scope the label was opened in |
| 1410 | vk_context_struct * subctx {}; |
| 1411 | vk_queue_handle * qhandle {}; |
| 1412 | |
| 1413 | // one region per dispatch, e.g. "matmul_q4_k_f32_f16acc_aligned_m (192,8,1)". |
| 1414 | // RGP cannot recover the pipeline name on its own, it only has the hash |
| 1415 | ggml_vk_debug_label(vk_context & ctx, const std::string & pipeline_name, uint32_t wg0, uint32_t wg1, uint32_t wg2); |
| 1416 | |
| 1417 | |
| 1418 | // one region per graph node |
| 1419 | // fused nodes are joined with '+', e.g. "RMS_NORM+MUL+ROPE Qcur-19" |
| 1420 | ggml_vk_debug_label(vk_context & ctx, const ggml_cgraph * cgraph, int node_idx, int n_fused); |
| 1421 | |
| 1422 | |
| 1423 | // one region per graph evaluation, opened on the queue instead of a command buffer |
| 1424 | // so it spans every submit the evaluation makes |
| 1425 | ggml_vk_debug_label(vk_queue_handle * handle, const char * name); |
| 1426 | |
| 1427 | |
| 1428 | // call before the command buffer can end, the destructor covers the rest |
| 1429 | void close(); |
| 1430 | |
| 1431 | |
| 1432 | ~ggml_vk_debug_label() { |
| 1433 | close(); |
| 1434 | } |
| 1435 | |
| 1436 | ggml_vk_debug_label(const ggml_vk_debug_label &) = delete; |
| 1437 | ggml_vk_debug_label & operator=(const ggml_vk_debug_label &) = delete; |
| 1438 | |
| 1439 | private: |
| 1440 | // the constructors check this too, so the name is not built when markers are off |
| 1441 | void begin(vk_context & ctx, const std::string & name); |
| 1442 | |
| 1443 | }; |
| 1444 | |
| 1445 | #define UNUSED GGML_UNUSED |
| 1446 | |
| 1447 | struct ggml_backend_vk_device_context { |
| 1448 | size_t device; |
| 1449 | std::string name; |
| 1450 | std::string description; |
| 1451 | bool is_integrated_gpu; |
| 1452 | std::string pci_bus_id; |
| 1453 | int op_offload_min_batch_size; |
| 1454 | }; |