Skip to content

Commit 979bccd

Browse files
authored
Merge branch 'ggml-org:master' into master
2 parents cd98872 + aa0c461 commit 979bccd

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

ggml/src/ggml-vulkan/ggml-vulkan.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
#include "ggml-cpu.h"
66
#endif
77

8+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
9+
#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
10+
811
#include <vulkan/vulkan.hpp>
912

13+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
14+
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
15+
1016
#include <algorithm>
1117
#include <cmath>
1218
#include <iomanip>
@@ -121,6 +127,8 @@ struct vk_pipeline_struct {
121127
bool needed {};
122128
// set to true when the shader has been compiled
123129
bool compiled {};
130+
// number of registers used, extracted from pipeline executable properties
131+
uint32_t register_count {};
124132
};
125133

126134
typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
@@ -429,6 +437,8 @@ struct vk_device_struct {
429437

430438
bool coopmat2;
431439

440+
bool pipeline_executable_properties_support {};
441+
432442
size_t idx;
433443

434444
bool mul_mat_l[GGML_TYPE_COUNT];
@@ -1603,6 +1613,20 @@ static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipelin
16031613
vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
16041614
}
16051615

1616+
if (device->pipeline_executable_properties_support) {
1617+
vk::PipelineExecutableInfoKHR executableInfo;
1618+
executableInfo.pipeline = pipeline->pipeline;
1619+
1620+
auto statistics = device->device.getPipelineExecutableStatisticsKHR(executableInfo);
1621+
for (auto & s : statistics) {
1622+
// "Register Count" is reported by NVIDIA drivers.
1623+
if (strcmp(s.name, "Register Count") == 0) {
1624+
VK_LOG_DEBUG(pipeline->name << " " << s.name << ": " << s.value.u64 << " registers");
1625+
pipeline->register_count = (uint32_t)s.value.u64;
1626+
}
1627+
}
1628+
}
1629+
16061630
{
16071631
std::lock_guard<std::recursive_mutex> guard(device->mutex);
16081632
device->all_pipelines.push_back(pipeline);
@@ -3610,6 +3634,7 @@ static vk_device ggml_vk_get_device(size_t idx) {
36103634
bool amd_shader_core_properties2 = false;
36113635
bool pipeline_robustness = false;
36123636
bool coopmat2_support = false;
3637+
bool pipeline_executable_properties_support = false;
36133638
device->coopmat_support = false;
36143639
device->integer_dot_product = false;
36153640
bool bfloat16_support = false;
@@ -3652,6 +3677,8 @@ static vk_device ggml_vk_get_device(size_t idx) {
36523677
!getenv("GGML_VK_DISABLE_BFLOAT16")) {
36533678
bfloat16_support = true;
36543679
#endif
3680+
} else if (strcmp("VK_KHR_pipeline_executable_properties", properties.extensionName) == 0) {
3681+
pipeline_executable_properties_support = true;
36553682
}
36563683
}
36573684

@@ -3878,8 +3905,18 @@ static vk_device ggml_vk_get_device(size_t idx) {
38783905
device_extensions.push_back("VK_KHR_shader_integer_dot_product");
38793906
}
38803907

3908+
VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR pep_features {};
3909+
pep_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR;
3910+
if (pipeline_executable_properties_support) {
3911+
last_struct->pNext = (VkBaseOutStructure *)&pep_features;
3912+
last_struct = (VkBaseOutStructure *)&pep_features;
3913+
device_extensions.push_back("VK_KHR_pipeline_executable_properties");
3914+
}
3915+
38813916
vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
38823917

3918+
device->pipeline_executable_properties_support = pipeline_executable_properties_support;
3919+
38833920
device->fp16 = device->fp16 && vk12_features.shaderFloat16;
38843921

38853922
#if defined(VK_KHR_shader_bfloat16)
@@ -4395,6 +4432,9 @@ static void ggml_vk_instance_init() {
43954432
}
43964433
VK_LOG_DEBUG("ggml_vk_instance_init()");
43974434

4435+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
4436+
VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);
4437+
43984438
uint32_t api_version = vk::enumerateInstanceVersion();
43994439

44004440
if (api_version < VK_API_VERSION_1_2) {
@@ -4462,6 +4502,9 @@ static void ggml_vk_instance_init() {
44624502

44634503
vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
44644504

4505+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
4506+
VULKAN_HPP_DEFAULT_DISPATCHER.init(vk_instance.instance);
4507+
44654508
std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
44664509

44674510
// Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan

ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_s.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void main() {
2929
uint qs = data_a[ib].qs[4 * ib32 + l];
3030
const uint8_t sign = data_a[ib].qs[QUANT_K / 8 + 4 * ib32 + l];
3131
qs |= (qh << (8 - 2 * l)) & 0x300;
32-
const uvec2 grid = iq2s_grid[qs & 511];
32+
const uvec2 grid = iq2s_grid[qs];
3333
const u8vec4 grid0 = unpack8(grid.x);
3434
const u8vec4 grid1 = unpack8(grid.y);
3535
data_b[b_idx + 8 * l + 0] = D_TYPE(db[l/2] * grid0.x * ((sign & 1) != 0 ? -1.0 : 1.0));

ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xxs.comp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ void main() {
3333
[[unroll]] for (uint l = 0; l < 4; ++l) {
3434
const uint sign7 = bitfieldExtract(signscale, 7 * int(l), 7);
3535
const uint sign8 = sign7 | (bitCount(sign7) << 7); // parity bit
36-
const uvec2 grid = iq2xxs_grid[data_a[ib].qs[8 * is + l]];
36+
const uint qs = data_a[ib].qs[8 * is + l];
37+
const uvec2 grid = iq2xxs_grid[qs];
3738
const u8vec4 grid0 = unpack8(grid.x);
3839
const u8vec4 grid1 = unpack8(grid.y);
3940
data_b[b_idx + 8 * l + 0] = D_TYPE(db * grid0.x * ((sign8 & 1) != 0 ? -1.0 : 1.0));

ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_s.comp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ void main() {
2222
const uint b_idx = 256 * ib + 32 * is;
2323

2424
const float d = float(data_a[ib].d);
25-
const float db = d * (1 + 2 * ((data_a[ib].scales[is] >> (4 * (is % 2))) & 0xf));
25+
const float db = d * (1 + 2 * ((data_a[ib].scales[is / 2] >> (4 * (is % 2))) & 0xf));
2626

2727
// We must produce 32 values using 4 sign bytes, 1 qh byte, 8 qs bytes.
2828
uint qh = data_a[ib].qh[is];
2929
[[unroll]] for (uint l = 0; l < 8; ++l) {
30-
uint qs = data_a[ib].qs[8 * is + l];
31-
uint gidx = qs | ((qh << (8 - l)) & 256);
32-
uint8_t signs = data_a[ib].signs[8 * is + l / 2] >> (4 * (l & 1));
33-
u8vec4 grid = unpack8(iq3s_grid[gidx]);
30+
const uint iqs = 8 * is + l;
31+
const uint qs = data_a[ib].qs[iqs];
32+
const uint gidx = qs | ((qh << (8 - l)) & 256);
33+
const uint8_t signs = data_a[ib].signs[iqs / 2] >> (4 * (l & 1));
34+
const u8vec4 grid = unpack8(iq3s_grid[gidx]);
3435
data_b[b_idx + 4 * l + 0] = D_TYPE(db * grid.x * ((signs & 1) != 0 ? -1.0 : 1.0));
3536
data_b[b_idx + 4 * l + 1] = D_TYPE(db * grid.y * ((signs & 2) != 0 ? -1.0 : 1.0));
3637
data_b[b_idx + 4 * l + 2] = D_TYPE(db * grid.z * ((signs & 4) != 0 ? -1.0 : 1.0));

ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_xxs.comp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ void main() {
3535
const uint sign7 = bitfieldExtract(signscale, 7 * int(l), 7);
3636
// Restore parity bit.
3737
const uint sign8 = sign7 | (bitCount(sign7) << 7);
38-
const u8vec4 grid0 = unpack8(iq3xxs_grid[data_a[ib].qs[8 * is + 2 * l]]);
39-
const u8vec4 grid1 = unpack8(iq3xxs_grid[data_a[ib].qs[8 * is + 2 * l + 1]]);
38+
const uint qs0 = data_a[ib].qs[8 * is + 2 * l];
39+
const uint qs1 = data_a[ib].qs[8 * is + 2 * l + 1];
40+
const u8vec4 grid0 = unpack8(iq3xxs_grid[qs0]);
41+
const u8vec4 grid1 = unpack8(iq3xxs_grid[qs1]);
4042
data_b[b_idx + 8 * l + 0] = D_TYPE(db * grid0.x * ((sign8 & 1) != 0 ? -1.0 : 1.0));
4143
data_b[b_idx + 8 * l + 1] = D_TYPE(db * grid0.y * ((sign8 & 2) != 0 ? -1.0 : 1.0));
4244
data_b[b_idx + 8 * l + 2] = D_TYPE(db * grid0.z * ((sign8 & 4) != 0 ? -1.0 : 1.0));

0 commit comments

Comments
 (0)