From 8698364a28de0eeb1bac5d6dc82cc71c1fe3f873 Mon Sep 17 00:00:00 2001 From: "Sabnis, Omkar" Date: Sat, 28 Nov 2026 15:27:56 -0800 Subject: [PATCH] ggml-openvino: keep the Q5_0 layout instead of converting it to Q4_0_64 --- docs/backend/OPENVINO.md | 5 ++--- .../src/ggml-openvino/ggml-openvino-extra.cpp | 22 ++++++++++--------- ggml/src/ggml-openvino/model-cache.cpp | 3 ++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/backend/OPENVINO.md b/docs/backend/OPENVINO.md index f645f25d3f05..3def163ba1c2 100644 --- a/docs/backend/OPENVINO.md +++ b/docs/backend/OPENVINO.md @@ -86,7 +86,7 @@ Although OpenVINO supports a wide range of [Intel hardware](https://docs.openvin - `Q4_0` - `Q4_1` - `Q4_K` -- `Q5_0` (converted at runtime, see the details below) +- `Q5_0` - `Q5_1` - `Q5_K` (converted at runtime, see the details below) - `Q6_K` (converted at runtime, see the details below) @@ -101,9 +101,8 @@ Although OpenVINO supports a wide range of [Intel hardware](https://docs.openvin - A quantized token embedding (`token_embd.weight`) and output weight (`output.weight`) are converted to `Q8_0_C`. **CPU and GPU:** -- `Q4_0`, `Q4_1`, `Q4_K`, `Q5_1` and `Q8_0` tensors keep their layout: weights, scales and zero points are extracted directly. +- `Q4_0`, `Q4_1`, `Q4_K`, `Q5_0`, `Q5_1` and `Q8_0` tensors keep their layout: weights, scales and zero points are extracted directly. - `Q5_K` and `Q6_K` tensors are converted to `Q8_0_C`. `GGML_OPENVINO_REQUANT_KQUANT` selects a 4-bit layout instead. -- `Q5_0` tensors are converted to `Q4_0_64`. **GPU:** - `Q4_K`, `Q5_K` and `Q6_K` MoE expert tensors are converted to `Q4_1_64`. This lets the GPU plugin use its fused MoE op, which is much faster for prompt processing. `GGML_OPENVINO_REQUANT_KQUANT` does not change this. `GGML_OPENVINO_MOE_OP=0` turns the fused MoE op off. diff --git a/ggml/src/ggml-openvino/ggml-openvino-extra.cpp b/ggml/src/ggml-openvino/ggml-openvino-extra.cpp index 7b254e3454b5..349cdef5bdde 100644 --- a/ggml/src/ggml-openvino/ggml-openvino-extra.cpp +++ b/ggml/src/ggml-openvino/ggml-openvino-extra.cpp @@ -973,6 +973,11 @@ clEnqueueMemcpyINTEL_fn ggml_openvino_get_clEnqueueMemcpyINTEL() { return ggml_openvino_get_device_config().cl_mem_cpy_fn; } +// Q5_0, Q5_1 and Q8_0 keep their weights as 8-bit values and follow the same rules. +static bool is_8bit_layout(ggml_type type) { + return type == GGML_TYPE_Q5_0 || type == GGML_TYPE_Q5_1 || type == GGML_TYPE_Q8_0; +} + // Get requantization type for a tensor type (returns nullopt if no requant needed) std::optional ggml_openvino_get_requant_type(const ggml_tensor * tensor, bool no_requant) { if (no_requant) { @@ -1001,12 +1006,12 @@ std::optional ggml_openvino_get_requant_type(const ggml_tensor * // GGML_OPENVINO_REQUANT_KQUANT selects a 4-bit target instead. Names are // q4_[_all]: says whether a per-group zero point is kept, // is the group size. The _all suffix includes Q4_K; q4_asym64_all also includes - // ordinary Q8_0/Q5_1 linear weights, matching the INT4_ASYM/group-64 primary precision + // ordinary Q8_0/Q5_1/Q5_0 linear weights, matching the INT4_ASYM/group-64 primary precision // used by NNCF with ratio=1.0. Embedding/output retain the channel-wise INT8 overrides above: // q4_sym128 Q6_K/Q5_K -> Q4_0_128 (u4, group 128, symmetric) // q4_sym128_all and Q4_K too -- drops Q4_K's per-32 zero point, which costs some accuracy // q4_asym64 Q6_K/Q5_K -> Q4_1_64 (u4, group 64, asymmetric) - // q4_asym64_all Q6_K/Q5_K/Q4_K/Q8_0/Q5_1 -> Q4_1_64 (u4, group 64, asymmetric) + // q4_asym64_all Q6_K/Q5_K/Q4_K/Q8_0/Q5_1/Q5_0 -> Q4_1_64 (u4, group 64, asymmetric) // native no requantization at all (keep Q6_K/Q5_K as they are) // // q4_asym64 leaves Q4_K at its native group 32. Use q4_asym64_all to keep the group size uniform. @@ -1038,22 +1043,19 @@ std::optional ggml_openvino_get_requant_type(const ggml_tensor * return ExtraQuantType::Q4_1_64; } } - // Include dense Q8_0/Q5_1 in the explicit all-asymmetric policy. Keep the default GPU + // Include dense Q8_0/Q5_1/Q5_0 in the explicit all-asymmetric policy. Keep the default GPU // workaround below restricted to MoE experts: ordinary grouped INT8 MatMul is supported. - if (asym64_all && (tensor->type == GGML_TYPE_Q5_1 || tensor->type == GGML_TYPE_Q8_0)) { + if (asym64_all && is_8bit_layout(tensor->type)) { // A group must fit within each row. Use backup channel-wise INT8 for incompatible - // widths (Q8_0/Q5_1 allow rows of 32 weights); every affected Qwen3.6 row is divisible by 64. + // widths (Q8_0/Q5_1/Q5_0 allow rows of 32 weights); every affected Qwen3.6 row is divisible by 64. return tensor->ne[0] % 64 == 0 ? ExtraQuantType::Q4_1_64 : ExtraQuantType::Q8_0_C; } - if (tensor->type == GGML_TYPE_Q5_0) { - return group64(ExtraQuantType::Q4_0_64); - } - // MoE expert weights (3D, ne[2] = n_expert) stored as Q5_1/Q8_0 are the expert-side + // MoE expert weights (3D, ne[2] = n_expert) stored as Q5_1/Q8_0/Q5_0 are the expert-side // equivalent of Q6_K/Q5_K: kept at 8 bits by default while the rest of the model is at 4 // (gemma-4 26B-A4B keeps its down projection there). Send them to 4 bits under the same // option, at group 64 rather than 128: the down expert has k=704, which 64 divides // (704/64 = 11) and 128 does not. - if (tensor->ne[2] > 1 && (tensor->type == GGML_TYPE_Q5_1 || tensor->type == GGML_TYPE_Q8_0)) { + if (tensor->ne[2] > 1 && is_8bit_layout(tensor->type)) { if (sym128 || sym128_all) { return group64(ExtraQuantType::Q4_0_64); } diff --git a/ggml/src/ggml-openvino/model-cache.cpp b/ggml/src/ggml-openvino/model-cache.cpp index 74ba8a1cecba..423012cbfe10 100644 --- a/ggml/src/ggml-openvino/model-cache.cpp +++ b/ggml/src/ggml-openvino/model-cache.cpp @@ -334,7 +334,8 @@ uint64_t ggml_openvino_model_fingerprint(const ggml_cgraph * cgraph, // Version 9 hashes GGML_OPENVINO_ROPE_F32_ANGLE, which became opt-in. // Version 10 hashes only whether GGML_OPENVINO_PROFILING is 2 or more (it then compiles with enable_profiling), // and no longer GGML_OPENVINO_DISABLE_REMOTE_OUTPUTS, which only changes the output bindings. - h = fnv1a_u64(h, 10); + // Version 11 keeps the Q5_0 layout (was Q4_0_64). + h = fnv1a_u64(h, 11); h = fnv1a(h, graph_signature.data(), graph_signature.size()); for (const char * name : cache_settings) { const char * value = ggml_openvino_getenv_str(name, "");