Skip to content

Commit 3c69a89

Browse files
committed
RDBC-999 Test VectorQuantizer
1 parent d97a9f7 commit 3c69a89

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

ravendb/tests/documents_tests/query_tests/test_vector_search.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,128 @@
1+
import unittest
2+
from ravendb.documents.queries.vector import VectorQuantizer
13
from ravendb.tests.dotnet_migrated_tests.test_ravenDB_22076 import Dto
24
from ravendb.tests.test_base import TestBase
35

46

7+
class TestVectorQuantizer(unittest.TestCase):
8+
"""Unit tests for VectorQuantizer - no server required."""
9+
10+
def test_to_int8_basic_quantization(self):
11+
"""Test basic Int8 quantization matches C# output."""
12+
# Test case from C# example: VectorQuantizer.ToInt8(new float[] { 0.1f, 0.2f })
13+
# Expected output: [64, 127, -51, -52, 76, 62]
14+
# Note: C# output shows [64, 127, ...] but our analysis showed it should be [63, 127, ...]
15+
result = VectorQuantizer.to_int8([0.1, 0.2])
16+
17+
# Should have 2 quantized values + 4 bytes for max_component float
18+
self.assertEqual(6, len(result))
19+
20+
# All values should be signed integers in range [-128, 127]
21+
for val in result:
22+
self.assertIsInstance(val, int)
23+
self.assertGreaterEqual(val, -128)
24+
self.assertLessEqual(val, 127)
25+
26+
# First value: 0.1 scaled by (127 / 0.2) = 63.5 -> 63
27+
self.assertEqual(63, result[0])
28+
29+
# Second value: 0.2 scaled by (127 / 0.2) = 127
30+
self.assertEqual(127, result[1])
31+
32+
# Last 4 bytes represent max_component (0.2) as little-endian float
33+
# 0.2f in IEEE 754 little-endian: 0x3E4CCCCD = [-51, -52, 76, 62] as signed bytes
34+
self.assertEqual([-51, -52, 76, 62], result[2:6])
35+
36+
def test_to_int8_second_example(self):
37+
"""Test second example from C# code."""
38+
# VectorQuantizer.ToInt8(new float[] { 0.3f, 0.4f })
39+
result = VectorQuantizer.to_int8([0.3, 0.4])
40+
41+
self.assertEqual(6, len(result))
42+
43+
# First value: 0.3 scaled by (127 / 0.4) = 95.25 -> 95
44+
self.assertEqual(95, result[0])
45+
46+
# Second value: 0.4 scaled by (127 / 0.4) = 127
47+
self.assertEqual(127, result[1])
48+
49+
# Last 4 bytes represent max_component (0.4) as little-endian float
50+
# 0.4f in IEEE 754 little-endian: 0x3ECCCCCD = [-51, -52, -52, 62] as signed bytes
51+
self.assertEqual([-51, -52, -52, 62], result[2:6])
52+
53+
def test_to_int8_negative_values(self):
54+
"""Test Int8 quantization with negative values."""
55+
result = VectorQuantizer.to_int8([-0.5, 0.5])
56+
57+
self.assertEqual(6, len(result))
58+
59+
# First value: -0.5 scaled by (127 / 0.5) = -127
60+
self.assertEqual(-127, result[0])
61+
62+
# Second value: 0.5 scaled by (127 / 0.5) = 127
63+
self.assertEqual(127, result[1])
64+
65+
def test_to_int8_all_zeros(self):
66+
"""Test Int8 quantization with all zeros."""
67+
result = VectorQuantizer.to_int8([0.0, 0.0, 0.0])
68+
69+
self.assertEqual(7, len(result)) # 3 quantized + 4 float bytes
70+
71+
# All quantized values should be 0
72+
self.assertEqual([0, 0, 0], result[:3])
73+
74+
# max_component is 0.0, which is all zeros in IEEE 754
75+
self.assertEqual([0, 0, 0, 0], result[3:7])
76+
77+
def test_to_int8_empty_list(self):
78+
"""Test Int8 quantization with empty list."""
79+
result = VectorQuantizer.to_int8([])
80+
self.assertEqual([], result)
81+
82+
def test_to_int1_basic_quantization(self):
83+
"""Test basic Int1 (binary) quantization."""
84+
# Positive values -> 1, negative values -> 0
85+
result = VectorQuantizer.to_int1([0.5, -0.3, 0.8, -0.1, 0.2])
86+
87+
self.assertEqual(5, len(result))
88+
self.assertEqual([1, 0, 1, 0, 1], result)
89+
90+
def test_to_int1_zero_is_positive(self):
91+
"""Test that zero is treated as non-negative (1)."""
92+
result = VectorQuantizer.to_int1([0.0, -0.0, 1.0, -1.0])
93+
94+
self.assertEqual(4, len(result))
95+
# 0.0 and -0.0 are both >= 0, so they should be 1
96+
self.assertEqual([1, 1, 1, 0], result)
97+
98+
def test_to_int1_all_positive(self):
99+
"""Test Int1 quantization with all positive values."""
100+
result = VectorQuantizer.to_int1([0.1, 0.2, 0.3, 0.4, 0.5])
101+
102+
self.assertEqual(5, len(result))
103+
self.assertEqual([1, 1, 1, 1, 1], result)
104+
105+
def test_to_int1_all_negative(self):
106+
"""Test Int1 quantization with all negative values."""
107+
result = VectorQuantizer.to_int1([-0.1, -0.2, -0.3, -0.4, -0.5])
108+
109+
self.assertEqual(5, len(result))
110+
self.assertEqual([0, 0, 0, 0, 0], result)
111+
112+
def test_to_int1_empty_list(self):
113+
"""Test Int1 quantization with empty list."""
114+
result = VectorQuantizer.to_int1([])
115+
self.assertEqual([], result)
116+
117+
def test_to_int1_padding_trimmed(self):
118+
"""Test that Int1 quantization trims padding bits correctly."""
119+
# 9 values should pack into 2 bytes (16 bits), but only return 9 values
120+
result = VectorQuantizer.to_int1([1.0] * 9)
121+
122+
self.assertEqual(9, len(result))
123+
self.assertEqual([1] * 9, result)
124+
125+
5126
class TestVectorSearch(TestBase):
6127
def test_should_generate_rql_with_text_field_using_named_ai_task(self):
7128
with self.store.open_session() as session:

0 commit comments

Comments
 (0)