Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ env:
WEAVIATE_138: 1.38.22
WEAVIATE_139: 1.39.13
WEAVIATE_140: 1.40.2
WEAVIATE_141: 1.41.0-dev-483b145

jobs:
lint-and-format:
Expand Down Expand Up @@ -387,7 +388,8 @@ jobs:
$WEAVIATE_137,
$WEAVIATE_138,
$WEAVIATE_139,
$WEAVIATE_140
$WEAVIATE_140,
$WEAVIATE_141
]
steps:
- name: Checkout
Expand Down
46 changes: 45 additions & 1 deletion integration/test_collection_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import time
from typing import Any, Dict, Generator, List, Optional, Union
from typing import Any, Callable, Dict, Generator, List, Optional, Union

import pytest as pytest
from _pytest.fixtures import SubRequest
Expand Down Expand Up @@ -706,6 +706,50 @@ def test_hnsw_with_rq4c(collection_factory: CollectionFactory) -> None:
assert config.vector_index_config.quantizer.training_limit == 10000


@pytest.mark.parametrize(
"configure,reconfigure",
[
(Configure.VectorIndex.Quantizer.pq, Reconfigure.VectorIndex.Quantizer.pq),
(Configure.VectorIndex.Quantizer.bq, Reconfigure.VectorIndex.Quantizer.bq),
(Configure.VectorIndex.Quantizer.sq, Reconfigure.VectorIndex.Quantizer.sq),
(Configure.VectorIndex.Quantizer.rq, Reconfigure.VectorIndex.Quantizer.rq),
],
ids=["pq", "bq", "sq", "rq"],
)
def test_hnsw_quantizer_rescore_factor(
collection_factory: CollectionFactory,
configure: Callable[..., Any],
reconfigure: Callable[..., Any],
) -> None:
dummy = collection_factory("dummy")
if dummy._connection._weaviate_version.is_lower_than(1, 41, 0):
pytest.skip("rescoreFactor is not supported in Weaviate versions lower than 1.41.0")

collection = collection_factory(
vector_index_config=Configure.VectorIndex.hnsw(
quantizer=configure(rescore_limit=30, rescore_factor=2.5),
),
)

config = collection.config.get()
assert isinstance(config.vector_index_config, _VectorIndexConfigHNSW)
assert config.vector_index_config.quantizer is not None
assert config.vector_index_config.quantizer.rescore_limit == 30
assert config.vector_index_config.quantizer.rescore_factor == 2.5

collection.config.update(
vector_index_config=Reconfigure.VectorIndex.hnsw(
quantizer=reconfigure(rescore_factor=4),
),
)

config = collection.config.get()
assert isinstance(config.vector_index_config, _VectorIndexConfigHNSW)
assert config.vector_index_config.quantizer is not None
assert config.vector_index_config.quantizer.rescore_limit == 30
assert config.vector_index_config.quantizer.rescore_factor == 4


def test_hnsw_with_pathseer_filter_strategy(collection_factory: CollectionFactory) -> None:
dummy = collection_factory("dummy")
if dummy._connection._weaviate_version.is_lower_than(1, 40, 0):
Expand Down
25 changes: 24 additions & 1 deletion test/collection/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from typing import List, Union
from typing import Any, List, Union

import pytest
from pydantic import ValidationError
Expand Down Expand Up @@ -3569,3 +3569,26 @@ def test_reconfigure_inverted_index_without_stopword_presets_leaves_existing(sel
}
merged = rc.merge_with_existing(existing)
assert merged["stopwordPresets"] == {"fr": ["le", "la"]}


@pytest.mark.parametrize(
"quantizer,name",
[
(Configure.VectorIndex.Quantizer.pq(rescore_limit=123, rescore_factor=2.5), "pq"),
(Configure.VectorIndex.Quantizer.bq(rescore_limit=123, rescore_factor=2.5), "bq"),
(Configure.VectorIndex.Quantizer.sq(rescore_limit=123, rescore_factor=2.5), "sq"),
(Configure.VectorIndex.Quantizer.rq(rescore_limit=123, rescore_factor=2.5), "rq"),
],
)
def test_vector_config_hnsw_quantizer_rescore_factor(quantizer: Any, name: str) -> None:
vi_dict = Configure.VectorIndex.hnsw(quantizer=quantizer)._to_dict()

assert vi_dict[name]["rescoreLimit"] == 123
assert vi_dict[name]["rescoreFactor"] == 2.5


def test_vector_config_hnsw_quantizer_rescore_factor_omitted_by_default() -> None:
vi_dict = Configure.VectorIndex.hnsw(quantizer=Configure.VectorIndex.Quantizer.pq())._to_dict()

assert "rescoreLimit" not in vi_dict["pq"]
assert "rescoreFactor" not in vi_dict["pq"]
26 changes: 26 additions & 0 deletions test/collection/test_config_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,29 @@ def test_collection_config_stopword_presets_absent() -> None:
schema = _full_schema("TestNoStopwordPresets")
full = _collection_config_from_json(schema)
assert full.inverted_index_config.stopword_presets is None


@pytest.mark.parametrize("rescore_factor", [None, -1, 2.5])
def test_collection_config_from_json_quantizer_rescore_factor(
rescore_factor: Optional[float],
) -> None:
index_config = dict(HFRESH_CONFIG)
if rescore_factor is not None:
index_config["rq"] = {**HFRESH_CONFIG["rq"], "rescoreFactor": rescore_factor}
schema = _schema_with_vector_config(
{
"vec": {
"vectorizer": {"none": {}},
"vectorIndexType": "hfresh",
"vectorIndexConfig": index_config,
}
}
)

config = _collection_config_from_json(schema)

assert config.vector_config is not None
quantizer = config.vector_config["vec"].vector_index_config.quantizer
assert quantizer is not None
assert quantizer.rescore_limit == 350
assert quantizer.rescore_factor == rescore_factor
18 changes: 18 additions & 0 deletions test/collection/test_config_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,21 @@ def test_updating_vector_when_none_left() -> None:

with pytest.raises(WeaviateInvalidInputError, match="does not exist"):
update.merge_with_existing({"class": "Test", "properties": []})


def test_updating_rq_rescore_factor() -> None:
schema = _hfresh_schema()
update = _CollectionConfigUpdate(
vector_config=Reconfigure.Vectors.update(
name="boi",
vector_index_config=Reconfigure.VectorIndex.hfresh(
quantizer=Reconfigure.VectorIndex.Quantizer.rq(rescore_factor=3.5)
),
)
)

new_schema = update.merge_with_existing(schema)

rq = new_schema["vectorConfig"]["boi"]["vectorIndexConfig"]["rq"]
assert rq["rescoreFactor"] == 3.5
assert rq["rescoreLimit"] == 20
27 changes: 24 additions & 3 deletions weaviate/collections/classes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,8 @@ class _PQConfig(_ConfigBase):
centroids: int
training_limit: int
encoder: PQEncoderConfig
rescore_limit: Optional[int]
rescore_factor: Optional[float]

@property
def bit_compression(self) -> bool:
Expand All @@ -2080,11 +2082,13 @@ def bit_compression(self) -> bool:
class _BQConfig(_ConfigBase):
cache: Optional[bool]
rescore_limit: int
rescore_factor: Optional[float]


@dataclass
class _SQConfig(_ConfigBase):
rescore_limit: int
rescore_factor: Optional[float]
training_limit: int


Expand All @@ -2093,6 +2097,7 @@ class _RQConfig(_ConfigBase):
cache: Optional[bool]
bits: Optional[int]
rescore_limit: int
rescore_factor: Optional[float]
centering: Optional[bool]
training_limit: Optional[int]

Expand Down Expand Up @@ -3106,6 +3111,8 @@ def pq(
segments: Optional[int] = None,
training_limit: Optional[int] = None,
enabled: bool = True,
rescore_limit: Optional[int] = None,
rescore_factor: Optional[float] = None,
) -> _PQConfigUpdate:
"""Create a `_PQConfigUpdate` object to be used when updating the product quantization (PQ) configuration of Weaviate.

Expand All @@ -3127,24 +3134,33 @@ def pq(
if encoder_type is not None or encoder_distribution is not None
else None
),
rescoreLimit=rescore_limit,
rescoreFactor=rescore_factor,
)

@staticmethod
def bq(rescore_limit: Optional[int] = None, enabled: bool = True) -> _BQConfigUpdate:
def bq(
rescore_limit: Optional[int] = None,
enabled: bool = True,
rescore_factor: Optional[float] = None,
) -> _BQConfigUpdate:
"""Create a `_BQConfigUpdate` object to be used when updating the binary quantization (BQ) configuration of Weaviate.

Use this method when defining the `quantizer` argument in the `vector_index` configuration in `collection.update()`.

Args:
See [the docs](https://weaviate.io/developers/weaviate/concepts/vector-index#hnsw-with-compression) for a more detailed view!
""" # noqa: D417 (missing argument descriptions in the docstring)
return _BQConfigUpdate(rescoreLimit=rescore_limit, enabled=enabled)
return _BQConfigUpdate(
rescoreLimit=rescore_limit, rescoreFactor=rescore_factor, enabled=enabled
)

@staticmethod
def sq(
rescore_limit: Optional[int] = None,
training_limit: Optional[int] = None,
enabled: bool = True,
rescore_factor: Optional[float] = None,
) -> _SQConfigUpdate:
"""Create a `_SQConfigUpdate` object to be used when updating the scalar quantization (SQ) configuration of Weaviate.

Expand All @@ -3154,7 +3170,10 @@ def sq(
See [the docs](https://weaviate.io/developers/weaviate/concepts/vector-index#hnsw-with-compression) for a more detailed view!
""" # noqa: D417 (missing argument descriptions in the docstring)
return _SQConfigUpdate(
enabled=enabled, rescoreLimit=rescore_limit, trainingLimit=training_limit
enabled=enabled,
rescoreLimit=rescore_limit,
rescoreFactor=rescore_factor,
trainingLimit=training_limit,
)

@staticmethod
Expand All @@ -3164,6 +3183,7 @@ def rq(
bits: Optional[int] = None,
centering: Optional[bool] = None,
training_limit: Optional[int] = None,
rescore_factor: Optional[float] = None,
) -> _RQConfigUpdate:
"""Create a `_RQConfigUpdate` object to be used when updating the Rotational quantization (RQ) configuration of Weaviate.

Expand All @@ -3175,6 +3195,7 @@ def rq(
return _RQConfigUpdate(
enabled=enabled,
rescoreLimit=rescore_limit,
rescoreFactor=rescore_factor,
bits=bits,
centering=centering,
trainingLimit=training_limit,
Expand Down
5 changes: 5 additions & 0 deletions weaviate/collections/classes/config_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ def __get_quantizer_config(
quantizer = _BQConfig(
cache=config["bq"].get("cache"),
rescore_limit=config["bq"].get("rescoreLimit"),
rescore_factor=config["bq"].get("rescoreFactor"),
)
elif "sq" in config and config["sq"]["enabled"]:
# values are not present for bq+hnsw
quantizer = _SQConfig(
rescore_limit=config["sq"].get("rescoreLimit"),
rescore_factor=config["sq"].get("rescoreFactor"),
training_limit=config["sq"].get("trainingLimit"),
)
elif "pq" in config and config["pq"].get("enabled"):
Expand All @@ -153,12 +155,15 @@ def __get_quantizer_config(
config["pq"].get("encoder", {}).get("distribution")
),
),
rescore_limit=config["pq"].get("rescoreLimit"),
rescore_factor=config["pq"].get("rescoreFactor"),
)
elif "rq" in config and config["rq"].get("enabled"):
quantizer = _RQConfig(
cache=config["rq"].get("cache"),
bits=config["rq"].get("bits"),
rescore_limit=config["rq"].get("rescoreLimit"),
rescore_factor=config["rq"].get("rescoreFactor"),
centering=config["rq"].get("centering"),
training_limit=config["rq"].get("trainingLimit"),
)
Expand Down
Loading
Loading