Skip to content

Commit

Permalink
fixed reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
harshil-goel committed Jul 25, 2024
1 parent 3cce9dc commit 1f98928
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 84 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
run:
skip-dirs:
skip-files:
- '_test\.go$'

linters-settings:
lll:
Expand Down
16 changes: 8 additions & 8 deletions dgraphtest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ func NewClusterConfig() ClusterConfig {
}
}

func newClusterConfigFrom(cc ClusterConfig) ClusterConfig {
prefix := fmt.Sprintf("dgraphtest-%d", rand.NewSource(time.Now().UnixNano()).Int63()%1000000)
defaultBackupVol := fmt.Sprintf("%v_backup", prefix)
defaultExportVol := fmt.Sprintf("%v_export", prefix)
cc.prefix = prefix
cc.volumes = map[string]string{DefaultBackupDir: defaultBackupVol, DefaultExportDir: defaultExportVol}
return cc
}
//func newClusterConfigFrom(cc ClusterConfig) ClusterConfig {
// prefix := fmt.Sprintf("dgraphtest-%d", rand.NewSource(time.Now().UnixNano()).Int63()%1000000)
// defaultBackupVol := fmt.Sprintf("%v_backup", prefix)
// defaultExportVol := fmt.Sprintf("%v_export", prefix)
// cc.prefix = prefix
// cc.volumes = map[string]string{DefaultBackupDir: defaultBackupVol, DefaultExportDir: defaultExportVol}
// return cc
//}

// WithNAlphas sets the number of alphas in the cluster
func (cc ClusterConfig) WithNumAlphas(n int) ClusterConfig {
Expand Down
15 changes: 5 additions & 10 deletions dgraphtest/local_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,6 @@ func (c *LocalCluster) Cleanup(verbose bool) {
return
}

defer func() {
c.alphas = c.alphas[:0]
c.zeros = c.zeros[:0]
}()

if verbose {
if err := c.printAllLogs(); err != nil {
log.Printf("[WARNING] error printing container logs: %v", err)
Expand Down Expand Up @@ -409,7 +404,7 @@ func (c *LocalCluster) Cleanup(verbose bool) {
}
}

func (c *LocalCluster) DeletePrevious() error {
func (c *LocalCluster) cleanupDocker() error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
// Prune containers
Expand Down Expand Up @@ -464,10 +459,10 @@ func (c *LocalCluster) Start() error {
log.Printf("[WARNING] error while stopping :%v", err1)
}
c.Cleanup(true)
// No need to do this anymore now that tests are passing. We can try it later if that changes.
//if err := c.DeletePrevious(); err != nil {
// log.Printf("[ERROR] while deleting old dockers %v", err)
//}

if err := c.cleanupDocker(); err != nil {
log.Printf("[ERROR] while cleaning old dockers %v", err)
}

c.conf.prefix = fmt.Sprintf("dgraphtest-%d", rand.NewSource(time.Now().UnixNano()).Int63()%1000000)
if err := c.init(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ require (
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.24.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/mod v0.18.0
golang.org/x/net v0.26.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.22.0
Expand Down Expand Up @@ -144,7 +145,6 @@ require (
github.com/xdg/stringprep v1.0.3 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/api v0.122.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
3 changes: 1 addition & 2 deletions posting/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,7 @@ func (r *rebuilder) RunWithoutTemp(ctx context.Context) error {
}
}

_, err = r.fn(pk.Uid, l, txn)
if err != nil {
if _, err := r.fn(pk.Uid, l, txn); err != nil {
return nil, err
}
return nil, nil
Expand Down
7 changes: 0 additions & 7 deletions systest/license/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package main

import (
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -156,12 +155,6 @@ func assertLicenseNotEnabled(t *testing.T, hcli *dgraphapi.HTTPClient, user stri
response, err := hcli.GetZeroState()
require.NoError(t, err)

if len(user) != len(response.Extensions["user"].(string)) {
time.Sleep(5 * time.Second)
response, err = hcli.GetZeroState()
require.NoError(t, err)
}

require.Equal(t, response.Extensions["user"], user)
require.Equal(t, response.Extensions["enabled"], false)
}
55 changes: 17 additions & 38 deletions tok/hnsw/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ func (s *SearchResult) GetExtraMetrics() map[string]uint64 {
return s.extraMetrics
}

// This needs to implement signature of SimilarityType[T].distanceScore
// function, hence it takes in a floatBits parameter,
// but doesn't actually use it.
func dotProduct[T c.Float](a, b []T, floatBits int) (T, error) {
func applyDistanceFunction[T c.Float](a, b []T, floatBits int,
applyFn32 func(a, b []float32) float32, applyFn64 func(a, b []float64) float64) (T, error) {
if len(a) != len(b) {
err := errors.New("can not compute dot product on vectors of different lengths")
return T(0), err
Expand All @@ -75,57 +73,36 @@ func dotProduct[T c.Float](a, b []T, floatBits int) (T, error) {
var a1, b1 []float32
a1 = *(*[]float32)(unsafe.Pointer(&a))
b1 = *(*[]float32)(unsafe.Pointer(&b))
return T(vek32.Dot(a1, b1)), nil
return T(applyFn32(a1, b1)), nil
} else if floatBits == 64 {
var a1, b1 []float64
a1 = *(*[]float64)(unsafe.Pointer(&a))
b1 = *(*[]float64)(unsafe.Pointer(&b))
return T(vek.Dot(a1, b1)), nil
return T(applyFn64(a1, b1)), nil
}

panic("invalid")
panic("While applying function on two floats, found an invalid number of float bits")

}

// This needs to implement signature of SimilarityType[T].distanceScore
// function, hence it takes in a floatBits parameter,
// but doesn't actually use it.
func dotProduct[T c.Float](a, b []T, floatBits int) (T, error) {
return applyDistanceFunction(a, b, floatBits, vek32.Dot, vek.Dot)
}

// This needs to implement signature of SimilarityType[T].distanceScore
// function, hence it takes in a floatBits parameter.
func cosineSimilarity[T c.Float](a, b []T, floatBits int) (T, error) {
if len(a) != len(b) {
err := errors.New("can not compute dot product on vectors of different lengths")
return T(0), err
}
if floatBits == 32 {
var a1, b1 []float32
a1 = *(*[]float32)(unsafe.Pointer(&a))
b1 = *(*[]float32)(unsafe.Pointer(&b))
return T(vek32.CosineSimilarity(a1, b1)), nil
} else if floatBits == 64 {
var a1, b1 []float64
a1 = *(*[]float64)(unsafe.Pointer(&a))
b1 = *(*[]float64)(unsafe.Pointer(&b))
return T(vek.CosineSimilarity(a1, b1)), nil
}
panic("Invalid")
return applyDistanceFunction(a, b, floatBits, vek32.CosineSimilarity, vek.CosineSimilarity)
}

// This needs to implement signature of SimilarityType[T].distanceScore
// function, hence it takes in a floatBits parameter,
// but doesn't actually use it.
func euclidianDistanceSq[T c.Float](a, b []T, floatBits int) (T, error) {
if len(a) != len(b) {
return 0, errors.New("can not subtract vectors of different lengths")
}
if floatBits == 32 {
var a1, b1 []float32
a1 = *(*[]float32)(unsafe.Pointer(&a))
b1 = *(*[]float32)(unsafe.Pointer(&b))
return T(vek32.Distance(a1, b1)), nil
} else if floatBits == 64 {
var a1, b1 []float64
a1 = *(*[]float64)(unsafe.Pointer(&a))
b1 = *(*[]float64)(unsafe.Pointer(&b))
return T(vek.Distance(a1, b1)), nil
}
panic("Invalid")
return applyDistanceFunction(a, b, floatBits, vek32.Distance, vek.Distance)
}

// Used for distance, since shorter distance is better
Expand Down Expand Up @@ -435,6 +412,8 @@ func (ph *persistentHNSW[T]) createEntryAndStartNodes(
return entry, edges, nil
}

// Converts the matrix into linear array that looks like
// [0: Number of rows 1: Length of row1 2-n: Data of row1 3: Length of row2 ..]
func encodeUint64MatrixUnsafe(matrix [][]uint64) []byte {
if len(matrix) == 0 {
return nil
Expand Down
27 changes: 10 additions & 17 deletions tok/index/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/binary"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"testing"
"unsafe"
Expand Down Expand Up @@ -156,8 +155,8 @@ func encodeUint64MatrixUnsafe(matrix [][]uint64) []byte {
rowLen := uint64(len(row))
copy(data[offset:offset+8], (*[8]byte)(unsafe.Pointer(&rowLen))[:])
offset += 8
for _, value := range row {
copy(data[offset:offset+8], (*[8]byte)(unsafe.Pointer(&value))[:])
for i := range row {
copy(data[offset:offset+8], (*[8]byte)(unsafe.Pointer(&row[i]))[:])
offset += 8
}
}
Expand All @@ -172,16 +171,14 @@ func decodeUint64MatrixUnsafe(data []byte) ([][]uint64, error) {

offset := 0
// Read number of rows
var rows uint64
rows = *(*uint64)(unsafe.Pointer(&data[offset]))
rows := *(*uint64)(unsafe.Pointer(&data[offset]))
offset += 8

matrix := make([][]uint64, rows)

for i := 0; i < int(rows); i++ {
// Read row length
var rowLen uint64
rowLen = *(*uint64)(unsafe.Pointer(&data[offset]))
rowLen := *(*uint64)(unsafe.Pointer(&data[offset]))
offset += 8

matrix[i] = make([]uint64, rowLen)
Expand Down Expand Up @@ -248,32 +245,28 @@ func BenchmarkEncodeDecodeUint64Matrix(b *testing.B) {
})
}

func dotProductT[T c.Float](a, b []T, floatBits int) (T, error) {
func dotProductT[T c.Float](a, b []T, floatBits int) {
var dotProduct T
if len(a) != len(b) {
err := errors.New("can not compute dot product on vectors of different lengths")
return dotProduct, err
return
}
for i := 0; i < len(a); i++ {
dotProduct += a[i] * b[i]
}
return dotProduct, nil
}

func dotProduct(a, b []float32) (float32, error) {
func dotProduct(a, b []float32) {
if len(a) != len(b) {
err := errors.New("can not compute dot product on vectors of different lengths")
return 0, err
return
}
sum := int8(0)
for i := 0; i < len(a); i += 2 {
sum += *(*int8)(unsafe.Pointer(&a[i]))**(*int8)(unsafe.Pointer(&b[i])) + *(*int8)(unsafe.Pointer(&a[i+1]))**(*int8)(unsafe.Pointer(&b[i+1]))
sum += *(*int8)(unsafe.Pointer(&a[i]))**(*int8)(unsafe.Pointer(&b[i])) +
*(*int8)(unsafe.Pointer(&a[i+1]))**(*int8)(unsafe.Pointer(&b[i+1]))
}
return float32(sum), nil
}

func BenchmarkDotProduct(b *testing.B) {

num := 1500
data := make([]byte, 64*num)
_, err := rand.Read(data)
Expand Down

0 comments on commit 1f98928

Please sign in to comment.