Skip to content

Commit

Permalink
by comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kosuke Morimoto <[email protected]>
  • Loading branch information
kmrmt authored and actions-user committed Sep 24, 2020
1 parent 868e338 commit fd16f33
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 110 deletions.
32 changes: 17 additions & 15 deletions hack/benchmark/assets/x1b/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ var (
ErrUnsupportedFileType = errors.New("unsupported file type")
)

type X1b interface {
type BillionScaleVectors interface {
Load(i int) (interface{}, error)
Dimension() int
Size() int
Close() error
}

type Bvecs interface {
X1b
type Uint8Vectors interface {
BillionScaleVectors
LoadUint8(i int) ([]uint8, error)
}

type Fvecs interface {
X1b
type FloatVectors interface {
BillionScaleVectors
LoadFloat32(i int) ([]float32, error)
}

type Ivecs interface {
X1b
type Int32Vectors interface {
BillionScaleVectors
LoadInt32(i int) ([]int32, error)
}

Expand All @@ -78,7 +78,9 @@ func open(fname string, elementSize int) (f *file, err error) {
return nil, err
}
defer func() {
err = errors.Wrap(err, fp.Close().Error())
if e := fp.Close(); e != nil {
err = errors.Wrap(err, e.Error())
}
}()

fi, err := fp.Stat()
Expand Down Expand Up @@ -157,38 +159,38 @@ func (iv *ivecs) Load(i int) (interface{}, error) {
return iv.LoadInt32(i)
}

func NewBVecs(fname string) (Bvecs, error) {
func NewUint8Vectors(fname string) (Uint8Vectors, error) {
f, err := open(fname, 1)
if err != nil {
return nil, err
}
return &bvecs{f}, nil
}

func NewFVecs(fname string) (Fvecs, error) {
func NewFloatVectors(fname string) (FloatVectors, error) {
f, err := open(fname, 4)
if err != nil {
return nil, err
}
return &fvecs{f}, nil
}

func NewIVecs(fname string) (Ivecs, error) {
func NewInt32Vectors(fname string) (Int32Vectors, error) {
f, err := open(fname, 4)
if err != nil {
return nil, err
}
return &ivecs{f}, nil
}

func Open(fname string) (X1b, error) {
func Open(fname string) (BillionScaleVectors, error) {
switch filepath.Ext(fname) {
case ".bvecs":
return NewBVecs(fname)
return NewUint8Vectors(fname)
case ".fvecs":
return NewFVecs(fname)
return NewFloatVectors(fname)
case ".ivecs":
return NewIVecs(fname)
return NewInt32Vectors(fname)
default:
return nil, ErrUnsupportedFileType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
)

func BenchmarkBVecs(b *testing.B) {
bv, err := NewBVecs(bvecsFile)
bv, err := NewUint8Vectors(bvecsFile)
if err != nil {
b.Fatal(err)
}
Expand All @@ -40,29 +40,22 @@ func BenchmarkBVecs(b *testing.B) {
b.Run(bvecsFile, func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < b.N; n++ {
v, err := bv.Load(i)
for n := 0; n < bb.N; n++ {
_, err := bv.Load(i)
switch err {
case nil:
i++
continue
case ErrOutOfBounds:
if err := bv.Close(); err != nil {
bb.Fatal(err)
}
bv, err = NewBVecs(bvecsFile)
i = 0
}
if err != nil {
default:
bb.Fatal(err)
}
bb.Log(v)
}
})
}

func BenchmarkFVecs(b *testing.B) {
fv, err := NewFVecs(fvecsFile)
fv, err := NewFloatVectors(fvecsFile)
if err != nil {
b.Fatal(err)
}
Expand All @@ -76,29 +69,22 @@ func BenchmarkFVecs(b *testing.B) {
b.Run(fvecsFile, func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < b.N; n++ {
v, err := fv.Load(i)
for n := 0; n < bb.N; n++ {
_, err := fv.Load(i)
switch err {
case nil:
i++
continue
case ErrOutOfBounds:
if err := fv.Close(); err != nil {
bb.Fatal(err)
}
fv, err = NewFVecs(fvecsFile)
i = 0
}
if err != nil {
default:
bb.Fatal(err)
}
bb.Log(v)
}
})
}

func BenchmarkIVecs(b *testing.B) {
iv, err := NewIVecs(ivecsFile)
iv, err := NewInt32Vectors(ivecsFile)
if err != nil {
b.Fatal(err)
}
Expand All @@ -112,23 +98,16 @@ func BenchmarkIVecs(b *testing.B) {
b.Run(ivecsFile, func(bb *testing.B) {
bb.ReportAllocs()
bb.ResetTimer()
for n := 0; n < b.N; n++ {
v, err := iv.Load(i)
for n := 0; n < bb.N; n++ {
_, err := iv.Load(i)
switch err {
case nil:
i++
continue
case ErrOutOfBounds:
if err := iv.Close(); err != nil {
bb.Fatal(err)
}
iv, err = NewIVecs(ivecsFile)
i = 0
}
if err != nil {
default:
bb.Fatal(err)
}
bb.Log(v)
}
})
}
30 changes: 15 additions & 15 deletions hack/benchmark/assets/x1b/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,18 +992,18 @@ func TestNewBVecs(t *testing.T) {
fname string
}
type want struct {
want Bvecs
want Uint8Vectors
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, Bvecs, error) error
checkFunc func(want, Uint8Vectors, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got Bvecs, err error) error {
defaultCheckFunc := func(w want, got Uint8Vectors, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ func TestNewBVecs(t *testing.T) {
test.checkFunc = defaultCheckFunc
}

got, err := NewBVecs(test.args.fname)
got, err := NewUint8Vectors(test.args.fname)
if err := test.checkFunc(test.want, got, err); err != nil {
tt.Errorf("error = %v", err)
}
Expand All @@ -1067,18 +1067,18 @@ func TestNewFVecs(t *testing.T) {
fname string
}
type want struct {
want Fvecs
want FloatVectors
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, Fvecs, error) error
checkFunc func(want, FloatVectors, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got Fvecs, err error) error {
defaultCheckFunc := func(w want, got FloatVectors, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
Expand Down Expand Up @@ -1128,7 +1128,7 @@ func TestNewFVecs(t *testing.T) {
test.checkFunc = defaultCheckFunc
}

got, err := NewFVecs(test.args.fname)
got, err := NewFloatVectors(test.args.fname)
if err := test.checkFunc(test.want, got, err); err != nil {
tt.Errorf("error = %v", err)
}
Expand All @@ -1142,18 +1142,18 @@ func TestNewIVecs(t *testing.T) {
fname string
}
type want struct {
want Ivecs
want Int32Vectors
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, Ivecs, error) error
checkFunc func(want, Int32Vectors, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got Ivecs, err error) error {
defaultCheckFunc := func(w want, got Int32Vectors, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
Expand Down Expand Up @@ -1203,7 +1203,7 @@ func TestNewIVecs(t *testing.T) {
test.checkFunc = defaultCheckFunc
}

got, err := NewIVecs(test.args.fname)
got, err := NewInt32Vectors(test.args.fname)
if err := test.checkFunc(test.want, got, err); err != nil {
tt.Errorf("error = %v", err)
}
Expand All @@ -1217,18 +1217,18 @@ func TestOpen(t *testing.T) {
fname string
}
type want struct {
want X1b
want BillionScaleVectors
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, X1b, error) error
checkFunc func(want, BillionScaleVectors, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got X1b, err error) error {
defaultCheckFunc := func(w want, got BillionScaleVectors, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
Expand Down
22 changes: 12 additions & 10 deletions pkg/tools/cli/loadtest/assets/large_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ import (
"github.com/vdaas/vald/internal/errors"
)

const (
largeDatasetPath = "hack/benchmark/assets/dataset/large"
)

type largeDataset struct {
*dataset
train x1b.X1b
query x1b.X1b
train x1b.BillionScaleVectors
query x1b.BillionScaleVectors
groundTruth [][]int
distances x1b.Fvecs
distances x1b.FloatVectors
}

func loadLargeData(trainFileName, queryFileName, groundTruthFileName, distanceFileName, name, distanceType, objectType string) func() (Dataset, error) {
return func() (Dataset, error) {
dir, err := findDir("hack/benchmark/assets/dataset/large")
dir, err := findDir(largeDatasetPath)
if err != nil {
return nil, err
}
Expand All @@ -49,17 +53,15 @@ func loadLargeData(trainFileName, queryFileName, groundTruthFileName, distanceFi
if tdim != qdim {
return nil, errors.New("dimension must be same train and query.")
}
iv, err := x1b.NewIVecs(filepath.Join(dir, groundTruthFileName))
iv, err := x1b.NewInt32Vectors(filepath.Join(dir, groundTruthFileName))
if err != nil {
return nil, err
}
groundTruth := make([][]int, 0, iv.Size())
for i := 0; ; i++ {
gt32, err := iv.LoadInt32(i)
if err != nil {
if err == ErrOutOfBounds {
break
}
if err == ErrOutOfBounds {
break
}
gt := make([]int, 0, len(gt32))
for _, v := range gt32 {
Expand All @@ -68,7 +70,7 @@ func loadLargeData(trainFileName, queryFileName, groundTruthFileName, distanceFi
groundTruth = append(groundTruth, gt)
}

distances, err := x1b.NewFVecs(filepath.Join(dir, distanceFileName))
distances, err := x1b.NewFloatVectors(filepath.Join(dir, distanceFileName))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit fd16f33

Please sign in to comment.