From 1af9e2ee77f44269e4d5a3dffbc611f84da201fb Mon Sep 17 00:00:00 2001 From: lovedheart <6277001+lovedheart@users.noreply.github.com> Date: Sat, 16 Aug 2025 22:30:53 +0200 Subject: [PATCH 1/4] Update types.comp Modified code (branchless) for e8m0_to_fp32 --- .../src/ggml-vulkan/vulkan-shaders/types.comp | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp index a36c33e26764b..85194caaa9fe7 100644 --- a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +++ b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp @@ -1413,16 +1413,20 @@ float bf16_to_fp32(uint32_t u) } float e8m0_to_fp32(uint8_t x) { - uint32_t bits; - - if (x == 0) { - bits = 0x00400000; - } else { - bits = x; - bits = bits << 23; - } - - return uintBitsToFloat(bits); +// uint32_t bits; + +// if (x == 0) { +// bits = 0x00400000; +// } else { +// bits = x; +// bits = bits << 23; +// } + +// branchless optimization + uint ux = uint(x); + uint is_zero = uint(x == 0); + uint result = (ux << 23) * (1 - is_zero) + (0x00400000u * is_zero); + return uintBitsToFloat(result); } #endif // !defined(GGML_TYPES_COMP) From 640e2078df8d21a999ecb383c8dc1ef2c8bc730c Mon Sep 17 00:00:00 2001 From: lovedheart <6277001+lovedheart@users.noreply.github.com> Date: Sun, 17 Aug 2025 06:38:06 +0200 Subject: [PATCH 2/4] Update types.comp --- ggml/src/ggml-vulkan/vulkan-shaders/types.comp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp index 85194caaa9fe7..1ba1594f313da 100644 --- a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +++ b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp @@ -1423,9 +1423,8 @@ float e8m0_to_fp32(uint8_t x) { // } // branchless optimization - uint ux = uint(x); - uint is_zero = uint(x == 0); - uint result = (ux << 23) * (1 - is_zero) + (0x00400000u * is_zero); + uint is_zero = uint(x == 0); + uint result = (uint(x) << 23) | (is_zero << 22); return uintBitsToFloat(result); } From 31efd0c12baa99741fe851973972c431508eb93e Mon Sep 17 00:00:00 2001 From: lovedheart <6277001+lovedheart@users.noreply.github.com> Date: Tue, 19 Aug 2025 11:23:33 +0200 Subject: [PATCH 3/4] Update types.comp --- ggml/src/ggml-vulkan/vulkan-shaders/types.comp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp index 1ba1594f313da..7bcca52fa72a2 100644 --- a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +++ b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp @@ -1413,16 +1413,7 @@ float bf16_to_fp32(uint32_t u) } float e8m0_to_fp32(uint8_t x) { -// uint32_t bits; - -// if (x == 0) { -// bits = 0x00400000; -// } else { -// bits = x; -// bits = bits << 23; -// } - -// branchless optimization +// branchless implementation uint is_zero = uint(x == 0); uint result = (uint(x) << 23) | (is_zero << 22); return uintBitsToFloat(result); From 076bd2b90c4a5bd0ea5ad3ae9df90abdf62488b7 Mon Sep 17 00:00:00 2001 From: lovedheart <6277001+lovedheart@users.noreply.github.com> Date: Tue, 19 Aug 2025 22:38:24 +0200 Subject: [PATCH 4/4] Update types.comp --- ggml/src/ggml-vulkan/vulkan-shaders/types.comp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp index 7bcca52fa72a2..74342d3f9a703 100644 --- a/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +++ b/ggml/src/ggml-vulkan/vulkan-shaders/types.comp @@ -1413,8 +1413,8 @@ float bf16_to_fp32(uint32_t u) } float e8m0_to_fp32(uint8_t x) { -// branchless implementation - uint is_zero = uint(x == 0); +// branchless implementation + uint is_zero = uint(x == 0); uint result = (uint(x) << 23) | (is_zero << 22); return uintBitsToFloat(result); }