OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GGUF FE] Add Q1_0 (binary, 1-bit) quantization support (#38026)
Michal Lukaszewskiaa82f310ac76
diff --git a/src/frontends/gguf/docs/supported_models.md b/src/frontends/gguf/docs/supported_models.mdindex 75cd494e879..f7eca3d0c4f 100644--- a/src/frontends/gguf/docs/supported_models.md+++ b/src/frontends/gguf/docs/supported_models.md@@ -79,8 +79,8 @@ time. External definitions and custom-family catalog entries extend the same reg| `olmoe` | full-width QK-norm and MoE || `phi3` | fused QKV || `qwen2` | qwen2 / qwen2.5 |-| `qwen3` | QK-norm before RoPE |-| `qwen35` | hybrid GatedDeltaNet + full attention, interleaved M-RoPE; greedy / batch 1 only |+| `qwen3` | QK-norm before RoPE; also covers Bonsai-8B (Q1_0) |+| `qwen35` | hybrid GatedDeltaNet + full attention, interleaved M-RoPE; greedy / batch 1 only; also covers Bonsai-27B (Q1_0) and Ternary-Bonsai-27B (Q2_0) || `qwen3moe` | NEOX RoPE, per-head QK-norm and normalized expert weights || `smollm3` | NORMAL RoPE, skipped on every fourth layer |diff --git a/src/frontends/gguf/src/quant/gguf.cpp b/src/frontends/gguf/src/quant/gguf.cppindex c6f1d2a2b17..b8878b106f8 100644--- a/src/frontends/gguf/src/quant/gguf.cpp+++ b/src/frontends/gguf/src/quant/gguf.cpp@@ -50,6 +50,8 @@ TypeTraits type_traits(uint32_t type) {return {1, 2};case GGUF_TYPE_Q4_0:return {32, 18};+ case GGUF_TYPE_Q1_0:+ return {128, 18};case GGUF_TYPE_Q4_1:return {32, 20};case GGUF_TYPE_Q5_0:@@ -111,6 +113,8 @@ std::optional<QuantLayout> quant_layout(uint32_t type) {switch (type) {case GGUF_TYPE_Q4_0:return QuantLayout{ov::element::i4, 1, ov::element::f16, 32, symmetric};+ case GGUF_TYPE_Q1_0:+ return QuantLayout{ov::element::i4, 1, ov::element::f16, 128, symmetric};case GGUF_TYPE_Q3_K:return QuantLayout{ov::element::i4, 1, ov::element::f16, 16, symmetric};case GGUF_TYPE_Q5_0:diff --git a/src/frontends/gguf/src/quant/gguf.hpp b/src/frontends/gguf/src/quant/gguf.hppindex 4cd33f7843a..2f78bcd5de2 100644--- a/src/frontends/gguf/src/quant/gguf.hpp+++ b/src/frontends/gguf/src/quant/gguf.hpp@@ -42,6 +42,7 @@ enum GgufTensorType {GGUF_TYPE_F64 = 28,GGUF_TYPE_BF16 = 30,GGUF_TYPE_MXFP4 = 39, // 4-bit microscaling (gpt-oss): 1-byte E8M0 scale + 32x E2M1+ GGUF_TYPE_Q1_0 = 41, // binary: f16 scale + 128x 1-bit codes, value = bit ? +scale : -scaleGGUF_TYPE_Q2_0 = 42, // ternary: f16 scale + 64x 2-bit codes, value = (code - 1) * scaleGGUF_TYPE_COUNT,};diff --git a/src/frontends/gguf/src/quant/gguf_quants.cpp b/src/frontends/gguf/src/quant/gguf_quants.cppindex c0997113da5..45ff3b55521 100644--- a/src/frontends/gguf/src/quant/gguf_quants.cpp+++ b/src/frontends/gguf/src/quant/gguf_quants.cpp@@ -733,6 +733,34 @@ static void fill_q4_0(const GgufTensor& tensor, ov::Tensor& weights_arr, ov::Ten});}+// Q1_0 block: 18 bytes = f16 scale d + 128 packed 1-bit codes (bit ? +d : -d).+// We re-expand each bit to an i4 nibble (0 -> 0xF, 1 -> 0x1), packed two-per-byte+// low-nibble-first - same layout OpenVINO already uses for Q4_0.+// So Q1_0 reuses the existing SYMMETRIC_I4 path unchanged.+static void fill_q1_0(const GgufTensor& tensor, ov::Tensor& weights_arr, ov::Tensor& scales_arr) {+ const uint64_t bytes_per_block = 18;+ const uint64_t out_bytes_per_block = 64; // 128 elements as i4, 2 per byte+ auto data = static_cast<const uint8_t*>(tensor.weights_data);+ auto weights = static_cast<uint8_t*>(weights_arr.data());+ auto scales = scales_arr.data<ov::element_type_traits<ov::element::f16>::value_type>();++ ov::parallel_for(scales_arr.get_size(), [&](size_t i) {+ const uint8_t* block = data + i * bytes_per_block;+ uint16_t scale_bits;+ std::memcpy(&scale_bits, block, sizeof(scale_bits));+ scales[i] = ov::float16::from_bits(scale_bits);+ const uint8_t* qs = block + 2;+ uint8_t* out = weights + i * out_bytes_per_block;+ for (int j = 0; j < 128; j += 2) {+ const int bit0 = (qs[j / 8] >> (j % 8)) & 1;+ const int bit1 = (qs[(j + 1) / 8] >> ((j + 1) % 8)) & 1;+ const uint8_t nib0 = bit0 ? 0x1 : 0xF;+ const uint8_t nib1 = bit1 ? 0x1 : 0xF;+ out[j / 2] = static_cast<uint8_t>(nib0 | (nib1 << 4));+ }+ });+}+// Q8_K symmetric: block = |f32 d|i8 qs[256]|i16 bsums[16]| (292 bytes/block).// bsums are partial sums for dot-product acceleration; unused in dequant-then-multiply.void fill_q8_k(const GgufTensor& tensor, ov::Tensor& weights_arr, ov::Tensor& scales_arr) {@@ -773,10 +801,13 @@ void gguf_fill_q2_0(const GgufTensor& tensor, ov::Tensor& weights_arr, ov::Tenso});}-// Symmetric types (Q4_0, Q8_0, Q5_0, Q6_K, Q3_K): fill weights + scales (f16), no zero-point.-// Q8_K uses f32 scales and is handled by a separate overload dispatched on tensor.type.+// Symmetric types (Q1_0, Q4_0, Q8_0, Q5_0, Q6_K, Q3_K): fill weights + scales (f16), no+// zero-point. Q8_K uses f32 scales and is handled by a separate overload dispatched on+// tensor.type.void gguf_fill_sym(const GgufTensor& tensor, ov::Tensor& weights, ov::Tensor& scales) {switch (tensor.type) {+ case GGUF_TYPE_Q1_0:+ return fill_q1_0(tensor, weights, scales);case GGUF_TYPE_Q4_0:return fill_q4_0(tensor, weights, scales);case GGUF_TYPE_Q8_0:diff --git a/src/frontends/gguf/src/quant/weights.cpp b/src/frontends/gguf/src/quant/weights.cppindex fecb89aca27..cc51f252c49 100644--- a/src/frontends/gguf/src/quant/weights.cpp+++ b/src/frontends/gguf/src/quant/weights.cpp@@ -65,6 +65,8 @@ WeightFormat get_weight_format(GgufTensorType qtype) {switch (qtype) {case GGUF_TYPE_MXFP4:return {FillKind::MXFP4, 32, ov::element::f4e2m1};+ case GGUF_TYPE_Q1_0:+ return {FillKind::SYMMETRIC, 128, ov::element::i4};case GGUF_TYPE_Q4_0:return {FillKind::SYMMETRIC, 32, ov::element::i4};case GGUF_TYPE_Q3_K:@@ -445,6 +447,7 @@ GgufTensorType gguf_type_from_name(const std::string& quant_type) {{"Q6_K", GGUF_TYPE_Q6_K},{"Q8_K", GGUF_TYPE_Q8_K},{"MXFP4", GGUF_TYPE_MXFP4},+ {"Q1_0", GGUF_TYPE_Q1_0},{"Q2_0", GGUF_TYPE_Q2_0}};// Accept ggml's lowercase type names ("q4_0", "q6_K", "f16", ...) as well as the// canonical uppercase form.diff --git a/src/frontends/gguf/tests/test_data/q1_0_deq.npy b/src/frontends/gguf/tests/test_data/q1_0_deq.npynew file mode 100644index 00000000000..4d240104dcdBinary files /dev/null and b/src/frontends/gguf/tests/test_data/q1_0_deq.npy differdiff --git a/src/frontends/gguf/tests/test_data/q1_0_qbytes.npy b/src/frontends/gguf/tests/test_data/q1_0_qbytes.npynew file mode 100644index 00000000000..c9016258863Binary files /dev/null and b/src/frontends/gguf/tests/test_data/q1_0_qbytes.npy differdiff --git a/src/frontends/gguf/tests/test_dequant_vs_ggml.cpp b/src/frontends/gguf/tests/test_dequant_vs_ggml.cppindex 84c948fe507..8059f1d47ce 100644--- a/src/frontends/gguf/tests/test_dequant_vs_ggml.cpp+++ b/src/frontends/gguf/tests/test_dequant_vs_ggml.cpp@@ -75,6 +75,8 @@ const char* type_name(uint32_t type) {return "Q6_K";case GGUF_TYPE_Q2_0:return "Q2_0";+ case GGUF_TYPE_Q1_0:+ return "Q1_0";default:return "";}@@ -220,7 +222,10 @@ INSTANTIATE_TEST_SUITE_P(AllQuantTypes,// Q2_0 is bit-exact: both sides compute (code - 1) * d// from the same f16 scale, and the u8 zero-point of 1 is// represented exactly, so no dequant noise is introduced.- DeqCase{"q2_0", GGUF_TYPE_Q2_0, kTolExact}),+ DeqCase{"q2_0", GGUF_TYPE_Q2_0, kTolExact},+ // Q1_0 is bit-exact: both sides compute bit ? +d : -d from+ // the same f16 scale, with no zero-point rounding at all.+ DeqCase{"q1_0", GGUF_TYPE_Q1_0, kTolExact}),[](const ::testing::TestParamInfo<DeqCase>& i) {return std::string(i.param.stem);});diff --git a/src/frontends/gguf/tests/test_weights.cpp b/src/frontends/gguf/tests/test_weights.cppindex a9f075c3865..f0532b26948 100644--- a/src/frontends/gguf/tests/test_weights.cpp+++ b/src/frontends/gguf/tests/test_weights.cpp@@ -13,6 +13,7 @@#include <cmath>#include <cstring>+#include <vector>#include "op_test_utils.hpp"#include "quant/weights.hpp"@@ -86,7 +87,8 @@ INSTANTIATE_TEST_SUITE_P(AllQuantTypes,WeightCase{"q4_k", "Q4_K", kTolU4Requant},WeightCase{"q5_k", "Q5_K", kTolRequant},WeightCase{"q6_k", "Q6_K", kTolRequant},- WeightCase{"q2_0", "Q2_0", kTolFaithful}),+ WeightCase{"q2_0", "Q2_0", kTolFaithful},+ WeightCase{"q1_0", "Q1_0", kTolFaithful}),[](const ::testing::TestParamInfo<WeightCase>& i) {return std::string(i.param.stem);});diff --git a/tests/model_hub_tests/gguf/gguf_models_nightly b/tests/model_hub_tests/gguf/gguf_models_nightlyindex f218800e274..4859f54c915 100644--- a/tests/model_hub_tests/gguf/gguf_models_nightly+++ b/tests/model_hub_tests/gguf/gguf_models_nightly@@ -16,6 +16,8 @@# resident memory recorded in that document's memory table (the largest entry kept here,# muse-glimmer, peaks around 37 GiB) -- hence the 16-core/64 GiB nightly runner.olmoe,allenai/OLMoE-1B-7B-0924-Instruct-GGUF,olmoe-1b-7b-0924-instruct-q4_0.gguf+qwen3-q1_0,prism-ml/Bonsai-8B-gguf,Bonsai-8B-Q1_0.gguf+qwen35-bonsai-q1_0,prism-ml/Bonsai-27B-gguf,Bonsai-27B-Q1_0.ggufmuse-glimmer,unsloth/Muse-Glimmer-30B-GGUF,Muse-Glimmer-30B-UD-Q4_K_XL.ggufqwen35-bonsai,prism-ml/Ternary-Bonsai-27B-gguf,Ternary-Bonsai-27B-Q2_g64.ggufernie4_5-moe,bartowski/baidu_ERNIE-4.5-21B-A3B-PT-GGUF,baidu_ERNIE-4.5-21B-A3B-PT-Q4_K_M.gguf