Spaces:
Running
Running
File size: 5,428 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
//go:build !race
package hnsw
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate/adapters/repos/db/vector/common"
"github.com/weaviate/weaviate/adapters/repos/db/vector/compressionhelpers"
"github.com/weaviate/weaviate/adapters/repos/db/vector/hnsw/distancer"
"github.com/weaviate/weaviate/adapters/repos/db/vector/testinghelpers"
"github.com/weaviate/weaviate/entities/cyclemanager"
"github.com/weaviate/weaviate/entities/storobj"
ent "github.com/weaviate/weaviate/entities/vectorindex/hnsw"
)
func Test_NoRaceCompressDoesNotCrash(t *testing.T) {
efConstruction := 64
ef := 32
maxNeighbors := 32
dimensions := 20
vectors_size := 10000
queries_size := 100
k := 100
delete_indices := make([]uint64, 0, 1000)
for i := 0; i < 1000; i++ {
delete_indices = append(delete_indices, uint64(i+10))
}
delete_indices = append(delete_indices, uint64(1))
vectors, queries := testinghelpers.RandomVecs(vectors_size, queries_size, dimensions)
distancer := distancer.NewL2SquaredProvider()
uc := ent.UserConfig{}
uc.MaxConnections = maxNeighbors
uc.EFConstruction = efConstruction
uc.EF = ef
uc.VectorCacheMaxObjects = 10e12
uc.PQ = ent.PQConfig{Enabled: true, Encoder: ent.PQEncoder{Type: "title", Distribution: "normal"}}
index, _ := New(Config{
RootPath: t.TempDir(),
ID: "recallbenchmark",
MakeCommitLoggerThunk: MakeNoopCommitLogger,
DistanceProvider: distancer,
VectorForIDThunk: func(ctx context.Context, id uint64) ([]float32, error) {
if int(id) >= len(vectors) {
return nil, storobj.NewErrNotFoundf(id, "out of range")
}
return vectors[int(id)], nil
},
TempVectorForIDThunk: func(ctx context.Context, id uint64, container *common.VectorSlice) ([]float32, error) {
copy(container.Slice, vectors[int(id)])
return container.Slice, nil
},
}, uc, cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(),
cyclemanager.NewCallbackGroupNoop(), testinghelpers.NewDummyStore(t))
defer index.Shutdown(context.Background())
compressionhelpers.Concurrently(uint64(len(vectors)), func(id uint64) {
index.Add(uint64(id), vectors[id])
})
index.Delete(delete_indices...)
cfg := ent.PQConfig{
Enabled: true,
Encoder: ent.PQEncoder{
Type: ent.PQEncoderTypeKMeans,
Distribution: ent.PQEncoderDistributionLogNormal,
},
Segments: dimensions,
Centroids: 256,
}
uc.PQ = cfg
index.compress(uc)
for _, v := range queries {
_, _, err := index.SearchByVector(v, k, nil)
assert.Nil(t, err)
}
}
func TestHnswPqNilVectors(t *testing.T) {
dimensions := 20
vectors_size := 10_000
queries_size := 10
vectors, _ := testinghelpers.RandomVecs(vectors_size, queries_size, dimensions)
// set some vectors to nil
for i := range vectors {
if i == 500 {
vectors[i] = nil
}
}
userConfig := ent.UserConfig{
MaxConnections: 30,
EFConstruction: 64,
EF: 32,
// The actual size does not matter for this test, but if it defaults to
// zero it will constantly think it's full and needs to be deleted - even
// after just being deleted, so make sure to use a positive number here.
VectorCacheMaxObjects: 1000000,
}
rootPath := "doesnt-matter-as-committlogger-is-mocked-out"
defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
fmt.Println(err)
}
}(rootPath)
index, err := New(Config{
RootPath: rootPath,
ID: "nil-vector-test",
MakeCommitLoggerThunk: MakeNoopCommitLogger,
DistanceProvider: distancer.NewCosineDistanceProvider(),
VectorForIDThunk: func(ctx context.Context, id uint64) ([]float32, error) {
vec := vectors[int(id)]
if vec == nil {
return nil, storobj.NewErrNotFoundf(id, "nil vec")
}
return vec, nil
},
TempVectorForIDThunk: TempVectorForIDThunk(vectors),
}, userConfig, cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(), cyclemanager.NewCallbackGroupNoop(), testinghelpers.NewDummyStore(t))
require.NoError(t, err)
compressionhelpers.Concurrently(uint64(len(vectors)/2), func(id uint64) {
if vectors[id] == nil {
return
}
err := index.Add(uint64(id), vectors[id])
require.Nil(t, err)
})
userConfig.PQ = ent.PQConfig{
Enabled: true,
Encoder: ent.PQEncoder{
Type: ent.PQEncoderTypeTile,
Distribution: ent.PQEncoderDistributionLogNormal,
},
BitCompression: false,
Segments: dimensions,
Centroids: 256,
}
ch := make(chan error)
err = index.UpdateUserConfig(userConfig, func() {
close(ch)
})
require.NoError(t, err)
<-ch
start := uint64(len(vectors) / 2)
compressionhelpers.Concurrently(uint64(len(vectors)/2), func(id uint64) {
if vectors[id+start] == nil {
return
}
err = index.Add(uint64(id)+start, vectors[id+start])
require.Nil(t, err)
})
}
|