Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Fix quantize: false silently enabling Q8_0 quantization in the GGML backend (#1236)

Amir Fathiebe13ae304bd
diff --git a/src/python/txtai/ann/dense/ggml.py b/src/python/txtai/ann/dense/ggml.py
index 9e34e74b..8d4c71df 100644
--- a/src/python/txtai/ann/dense/ggml.py
+++ b/src/python/txtai/ann/dense/ggml.py
@@ -519,7 +519,11 @@ class GGMLTensors:
# Read tensor type
tensortype = self.quantize
- tensortype = "Q8_0" if isinstance(tensortype, bool) else f"Q{int(tensortype)}_0" if isinstance(tensortype, int) else tensortype
+ tensortype = (
+ "Q8_0"
+ if isinstance(tensortype, bool) and tensortype
+ else f"Q{int(tensortype)}_0" if isinstance(tensortype, int) and not isinstance(tensortype, bool) else tensortype
+ )
tensortype = tensortype.upper() if tensortype else "F32"
# Validate tensor type
diff --git a/test/python/testann/testdense.py b/test/python/testann/testdense.py
index 5561af43..2185856d 100644
--- a/test/python/testann/testdense.py
+++ b/test/python/testann/testdense.py
@@ -10,12 +10,14 @@ import unittest
from unittest.mock import patch
+import ggml
import numpy as np
from sqlalchemy.dialects.postgresql import BIT
from sqlalchemy.ext.compiler import compiles
from txtai.ann import ANNFactory, ANN
+from txtai.ann.dense.ggml import GGMLTensors
from txtai.serialize import SerializeFactory
@@ -223,6 +225,17 @@ class TestDense(unittest.TestCase):
ann.load(index)
self.assertEqual(ann.count(), 99)
+ def testGGMLQuantizeDisabled(self):
+ """
+ Test that quantize: false disables GGML tensor quantization
+ """
+
+ data = np.random.rand(4, 256).astype(np.float32)
+ for quantize, expected in [(False, ggml.GGML_TYPE_F32), (True, ggml.GGML_TYPE_Q8_0), (4, ggml.GGML_TYPE_Q4_0)]:
+ with self.subTest(quantize=quantize):
+ tensors = GGMLTensors(False, 64, quantize)
+ self.assertEqual(tensors.tensortype(data), expected)
+
def testGGMLEmpty(self):
"""
Test GGML backend with an empty index