Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support numeric labels in neighbor algorithms #740

Closed
wants to merge 19 commits into from
Closed
71 changes: 71 additions & 0 deletions base/hnsw/bruteforce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2023 gorse Project Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hnsw

import (
"context"

"github.com/chewxy/math32"
"github.com/zhenghaoz/gorse/base/heap"
)

type Bruteforce struct {
vectors []Vector
distFn Distance
}

func NewBruteforce(dist Distance) *Bruteforce {
return &Bruteforce{
distFn: dist,
}
}

func (b *Bruteforce) Add(ctx context.Context, vectors ...Vector) {
b.vectors = append(b.vectors, vectors...)
}

func (b *Bruteforce) Evaluate(n int) float64 {
return 1
}

// Search returns the nearest neighbors of q.
func (b *Bruteforce) Search(q Vector, n int) []Result {
if q == nil {
return nil
}
pq := heap.NewPriorityQueue(true)
for i, vec := range b.vectors {
if vec != nil && vec != q {
d := b.distFn(q, vec)
if math32.IsInf(d, 1) {
continue
}
pq.Push(int32(i), d)
if pq.Len() > n {
pq.Pop()
}
}
}
pq = pq.Reverse()
var results []Result
for pq.Len() > 0 {
value, score := pq.Pop()
results = append(results, Result{
Index: value,
Distance: score,
})
}
return results
}
62 changes: 62 additions & 0 deletions base/hnsw/bruteforce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 gorse Project Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hnsw

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBruteforce(t *testing.T) {
bf := NewBruteforce(Euclidean)
bf.Add(context.Background(), NewDenseVector([]float32{0, 0, 0, 0, 0, 1}))
bf.Add(context.Background(), NewDenseVector([]float32{0, 0, 0, 0, 1, 1}))
bf.Add(context.Background(), NewDenseVector([]float32{0, 0, 0, 1, 1, 1}))
bf.Add(context.Background(), NewDenseVector([]float32{0, 0, 1, 1, 1, 1}))
bf.Add(context.Background(), NewDenseVector([]float32{0, 1, 1, 1, 1, 1}))
bf.Add(context.Background(), NewDenseVector([]float32{1, 1, 1, 1, 1, 1}))
results := bf.Search(NewDenseVector([]float32{0, 0, 0, 0, 0, 0}), 3)
assert.Equal(t, []Result{
{Index: 0, Distance: 1},
{Index: 1, Distance: 2},
{Index: 2, Distance: 3},
}, results)

bf.distFn = Dot
results = bf.Search(NewDenseVector([]float32{1, 1, 1, 1, 1, 1}), 3)
assert.Equal(t, []Result{
{Index: 5, Distance: -6},
{Index: 4, Distance: -5},
{Index: 3, Distance: -4},
}, results)
}

func TestBruteforce_HasNil(t *testing.T) {
vectors := []Vector{nil, nil, NewDenseVector([]float32{0, 0, 0, 0, 0, 1}), nil}
bf := NewBruteforce(Euclidean)
bf.Add(context.Background(), vectors...)
results := bf.Search(NewDenseVector([]float32{0, 0, 0, 0, 0, 1}), 3)
assert.Equal(t, []Result{{Index: 2, Distance: 0}}, results)
}

func TestBruteforce_HasInf(t *testing.T) {
bf := NewBruteforce(Euclidean)
bf.Add(context.Background(), NewSparseVector([]int32{1, 3, 5}, []float32{1, 2, 3}))
bf.Add(context.Background(), NewSparseVector([]int32{2, 4, 6}, []float32{1, 2, 3}))
results := bf.Search(NewSparseVector([]int32{1, 3, 5}, []float32{1, 2, 3}), 3)
assert.Equal(t, []Result{{Index: 0, Distance: 0}}, results)
}
Loading
Loading