Skip to content

Commit

Permalink
Merge pull request #52 from dmitrichev/hnsw-m-could-be-zero
Browse files Browse the repository at this point in the history
The value of the HNSW configuration parameter M can be zero
  • Loading branch information
hkulekci authored May 31, 2024
2 parents 70f0a5a + e5f490d commit 72b2a4b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Models/Request/CollectionConfig/HnswConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function setPayloadM(?int $payloadM): HnswConfig
public function toArray(): array
{
$data = [];
if ($this->m) {
if ($this->m !== null) {
$data['m'] = $this->m;
}
if ($this->efConstruct) {
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Models/Request/CollectionConfig/HnswConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public function testWithM(): void
], $config->toArray());
}

public function testWithZeroM(): void
{
$config = (new HnswConfig())->setM(0);

$this->assertEquals([
'm' => 0
], $config->toArray());
}

public function testWithInvalidM(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Models/Request/CreateCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,31 @@ public function testCreateCollectionWithHnswConfig(): void
);
}

public function testCreateCollectionWithHnswConfigAndZeroM(): void
{
$collection = new CreateCollection();
$collection->addVector(new VectorParams(1024, VectorParams::DISTANCE_COSINE));
$diff = (new HnswConfig())
->setM(0)
->setPayloadM(1);

$collection->setHnswConfig($diff);

$this->assertEquals(
[
'vectors' => [
'size' => '1024',
'distance' => 'Cosine'
],
'hnsw_config' => [
'm' => 0,
'payload_m' => 1,
]
],
$collection->toArray()
);
}

public function testCreateCollectionWithWalConfig(): void
{
$collection = new CreateCollection();
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Models/Request/UpdateCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public function testUpdateCollectionWithHnswConfig(): void
);
}

public function testUpdateCollectionWithHnswConfigAndZeroM(): void
{
$collection = new UpdateCollection();
$collection->setHnswConfig((new HnswConfig())->setM(0)->setEfConstruct(5));

$this->assertEquals(
[
'hnsw_config' => [
'm' => 0,
'ef_construct' => 5
],
],
$collection->toArray()
);
}

public function testUpdateCollectionWithQuantizationConfig(): void
{
$collection = new UpdateCollection();
Expand Down

0 comments on commit 72b2a4b

Please sign in to comment.