-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: kpango <[email protected]>
- Loading branch information
Showing
39 changed files
with
2,334 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
// | ||
// Copyright (C) 2019-2023 vdaas.org vald team <[email protected]> | ||
// | ||
// 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 | ||
// | ||
// https://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 ngt provides implementation of Go API for https://github.com/yahoojapan/NGT | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"runtime" | ||
"sync" | ||
"time" | ||
|
||
"github.com/vdaas/vald/internal/conv" | ||
"github.com/vdaas/vald/internal/core/algorithm/ngt" | ||
"github.com/vdaas/vald/internal/log" | ||
"github.com/vdaas/vald/internal/net/http/metrics" | ||
"github.com/vdaas/vald/internal/strings" | ||
"gonum.org/v1/hdf5" | ||
) | ||
|
||
func main() { | ||
pfile := fmt.Sprintf("/proc/%d/status", os.Getpid()) | ||
output := func(header string) { | ||
buf, err := os.ReadFile(pfile) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var vmpeak, vmrss, vmhwm string | ||
for _, line := range strings.Split(conv.Btoa(buf), "\n") { | ||
switch { | ||
case strings.HasPrefix(line, "VmPeak"): | ||
vmpeak = strings.Fields(line)[1] | ||
case strings.HasPrefix(line, "VmHWM"): | ||
vmhwm = strings.Fields(line)[1] | ||
case strings.HasPrefix(line, "VmRSS"): | ||
vmrss = strings.Fields(line)[1] | ||
} | ||
} | ||
|
||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
log.Infof("%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v", header, vmpeak, vmhwm, vmrss, m.Alloc/1024, m.TotalAlloc/1024, m.HeapAlloc/1024, m.HeapSys/1024, m.HeapInuse/1024) | ||
} | ||
defer output(" end") | ||
|
||
run(time.Hour*2+time.Minute*30, output) | ||
|
||
end := time.NewTicker(10 * time.Minute) | ||
defer end.Stop() | ||
|
||
gc := time.NewTicker(4 * time.Minute) | ||
defer gc.Stop() | ||
ticker := time.NewTicker(5 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for range ticker.C { | ||
select { | ||
case <-end.C: | ||
return | ||
case <-gc.C: | ||
runtime.GC() | ||
output(" gc") | ||
default: | ||
output(" finalizing") | ||
} | ||
} | ||
} | ||
|
||
func run(dur time.Duration, output func(header string)) { | ||
vectors, _, _ := load(os.Getenv("DATA_PATH")) | ||
n, _ := ngt.New( | ||
ngt.WithDimension(len(vectors[0])), | ||
ngt.WithDefaultPoolSize(8), | ||
ngt.WithObjectType(ngt.Float), | ||
ngt.WithDistanceType(ngt.L2), | ||
) | ||
|
||
log.Infof("# of vectors: %v", len(vectors)) | ||
log.Info(" operation\tVmPeak\tVmHWM\tVmRSS\tAlloc\tTotalAlloc\tHeapAlloc\tHeapSys\tHeapInuse") | ||
output(" start") | ||
ctx, cancel := context.WithTimeout(context.Background(), dur) | ||
defer cancel() | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
wg.Done() | ||
srv := &http.Server{ | ||
Addr: "0.0.0.0:6060", | ||
Handler: metrics.NewPProfHandler(), | ||
} | ||
go srv.ListenAndServe() | ||
<-ctx.Done() | ||
srv.Shutdown(context.Background()) | ||
}() | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
ids := make([]uint, len(vectors)) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
ids = ids[:0:0] | ||
ids = nil | ||
for _, vector := range vectors { | ||
_, err := n.Insert(vector) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
output(" insert") | ||
if err := n.CreateIndex(8); err != nil { | ||
log.Fatal(err) | ||
} | ||
output("create index") | ||
return | ||
default: | ||
for i, vector := range vectors { | ||
id, err := n.Insert(vector) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ids[i] = id | ||
} | ||
output(" insert") | ||
|
||
if err := n.CreateIndex(8); err != nil { | ||
log.Fatal(err) | ||
} | ||
output("create index") | ||
|
||
for _, id := range ids { | ||
if err := n.Remove(id); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
output(" remove") | ||
} | ||
} | ||
}() | ||
|
||
wg.Wait() | ||
end := time.NewTicker(10 * time.Minute) | ||
defer end.Stop() | ||
ticker := time.NewTicker(5 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for range ticker.C { | ||
select { | ||
case <-end.C: | ||
return | ||
default: | ||
output(" waiting") | ||
} | ||
} | ||
vectors = vectors[:0:0] | ||
vectors = nil | ||
} | ||
|
||
// load function loads training and test vector from hdf file. The size of ids is same to the number of training data. | ||
// Each id, which is an element of ids, will be set a random number. | ||
func load(path string) (train, test [][]float32, err error) { | ||
var f *hdf5.File | ||
f, err = hdf5.OpenFile(path, hdf5.F_ACC_RDONLY) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer f.Close() | ||
|
||
// readFn function reads vectors of the hierarchy with the given the name. | ||
readFn := func(name string) ([][]float32, error) { | ||
// Opens and returns a named Dataset. | ||
// The returned dataset must be closed by the user when it is no longer needed. | ||
d, err := f.OpenDataset(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer d.Close() | ||
|
||
// Space returns an identifier for a copy of the dataspace for a dataset. | ||
sp := d.Space() | ||
defer sp.Close() | ||
|
||
// SimpleExtentDims returns dataspace dimension size and maximum size. | ||
dims, _, _ := sp.SimpleExtentDims() | ||
row, dim := int(dims[0]), int(dims[1]) | ||
|
||
// Gets the stored vector. All are represented as one-dimensional arrays. | ||
// The type of the slice depends on your dataset. | ||
// For fashion-mnist-784-euclidean.hdf5, the datatype is float32. | ||
vec := make([]float32, sp.SimpleExtentNPoints()) | ||
if err := d.Read(&vec); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Converts a one-dimensional array to a two-dimensional array. | ||
// Use the `dim` variable as a separator. | ||
vecs := make([][]float32, row) | ||
for i := 0; i < row; i++ { | ||
vecs[i] = make([]float32, dim) | ||
for j := 0; j < dim; j++ { | ||
vecs[i][j] = float32(vec[i*dim+j]) | ||
} | ||
} | ||
|
||
return vecs, nil | ||
} | ||
|
||
// Gets vector of `train` hierarchy. | ||
train, err = readFn("train") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// Gets vector of `test` hierarchy. | ||
test, err = readFn("test") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.