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

Go API - [WIP] #212

Open
wants to merge 59 commits into
base: branch-25.02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
7961798
wip
ajit283 Jun 14, 2024
764dc1e
cleanup
ajit283 Jun 14, 2024
b0fd0b1
Added support for different data types and device allocation in NewMa…
ajit283 Jun 19, 2024
b47cf5b
bruteforce works
ajit283 Jun 19, 2024
1ec0c8c
refactor
ajit283 Jun 19, 2024
d5c0dc3
improve resources
ajit283 Jun 19, 2024
cc6319e
add ivf_flat, modules not working yet
ajit283 Jun 25, 2024
01af7c9
sync
ajit283 Jun 29, 2024
ed9bf47
cleanup
ajit283 Jul 2, 2024
4e79d8e
Merge branch 'rapidsai:branch-24.08' into go
ajit283 Jul 2, 2024
d278abc
rename
ajit283 Jul 2, 2024
b91721d
sync
ajit283 Jul 2, 2024
6b90861
.
ajit283 Jul 2, 2024
773fd94
Merge branch 'rapidsai:branch-24.08' into go
ajit283 Jul 3, 2024
7cbc1f9
sync
ajit283 Jul 10, 2024
46ec2f7
ivf_pq + cagra
ajit283 Jul 10, 2024
9dcffbd
Merge branch 'branch-24.08' into go
ajit283 Jul 10, 2024
f82216c
CI/CD attempt
ajit283 Jul 10, 2024
486d40a
add distance
ajit283 Jul 10, 2024
6f5c5a6
replace string arguments
ajit283 Jul 21, 2024
bfdf3be
make library easier to use
ajit283 Jul 21, 2024
de3cea0
graph degrees
ajit283 Jul 23, 2024
57e8dc0
fix top-level tests
ajit283 Jul 24, 2024
ab173dc
Merge branch 'branch-24.08' into go
cjnolet Jul 24, 2024
2d5fb95
renaming
ajit283 Aug 1, 2024
505d8dc
Merge branch 'go' of https://github.com/ajit283/cuvs into go
ajit283 Aug 1, 2024
c3360ee
change package name
ajit283 Aug 5, 2024
e261c8a
package name change (rapidsai)
ajit283 Aug 5, 2024
a4890ed
dlpack add expand
ajit283 Aug 7, 2024
f2bac2d
cagra: expose some types
ajit283 Aug 11, 2024
a684b01
Merge branch 'branch-24.08' into go
ajit283 Aug 11, 2024
7d45e24
extend
ajit283 Aug 22, 2024
a7084c2
add extend with return (wip, test missing)
ajit283 Aug 22, 2024
3af1b73
Merge branch 'branch-24.10' into go
cjnolet Aug 22, 2024
44d9e58
fix brute_force, add pool
ajit283 Aug 26, 2024
9447f63
update, add search_width
ajit283 Oct 14, 2024
04b6532
add memory resource test
ajit283 Oct 16, 2024
a082f67
thread_local fix
ajit283 Oct 17, 2024
a84e764
change pool memory
ajit283 Oct 28, 2024
853d538
Merge branch 'branch-24.10' into go
cjnolet Nov 4, 2024
4021229
Merge branch 'branch-24.12' into go
cjnolet Nov 4, 2024
6dd2044
simplify memory_resource
ajit283 Nov 5, 2024
3e33692
Merge branch 'go' of https://github.com/ajit283/cuvs into go
ajit283 Nov 5, 2024
b1a0476
Merge branch 'branch-24.12' into go
cjnolet Nov 15, 2024
dddb165
Merge branch 'branch-24.12' into go
ajit283 Nov 21, 2024
e05782a
cleanup top-level packages
ajit283 Nov 21, 2024
a315632
cleanup neighbors
ajit283 Nov 22, 2024
3c97864
.
ajit283 Nov 22, 2024
bd2dd76
build
ajit283 Nov 22, 2024
bd76cf8
ci
ajit283 Nov 22, 2024
1e5a756
ci
ajit283 Nov 22, 2024
927f2be
fix pointer pinning issues
ajit283 Nov 26, 2024
f7fac35
add docstrings
ajit283 Dec 4, 2024
d814e37
Merge branch 'branch-24.12' into go
ajit283 Dec 4, 2024
2708bc0
Merge branch 'branch-25.02' into go
ajit283 Dec 10, 2024
e634821
filter improv. + tests
ajit283 Dec 27, 2024
8c9105a
add doc. for cagra filter
ajit283 Jan 4, 2025
4a80cf7
Merge branch 'branch-25.02' into go
cjnolet Jan 8, 2025
f5b8e72
fix dependencies.yml
ajit283 Jan 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ jobs:
node_type: "gpu-v100-latest-1"
run_script: "ci/build_rust.sh"
sha: ${{ inputs.sha }}
go-build:
needs: cpp-build
secrets: inherit
uses: rapidsai/shared-workflows/.github/workflows/[email protected]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am I doing this correctly?

with:
build_type: ${{ inputs.build_type || 'branch' }}
branch: ${{ inputs.branch }}
arch: "amd64"
date: ${{ inputs.date }}
container_image: "rapidsai/ci-conda:latest"
node_type: "gpu-v100-latest-1"
run_script: "ci/build_go.sh"
sha: ${{ inputs.sha }}
python-build:
needs: [cpp-build]
secrets: inherit
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- conda-python-tests
- docs-build
- rust-build
- go-build
- wheel-build-cuvs
- wheel-tests-cuvs
- devcontainer
Expand All @@ -44,13 +45,15 @@ jobs:
- '!notebooks/**'
- '!python/**'
- '!rust/**'
- '!go/**'
- '!thirdparty/LICENSES/**'
test_notebooks:
- '**'
- '!.devcontainer/**'
- '!.pre-commit-config.yaml'
- '!README.md'
- '!rust/**'
- '!go/**'
- '!thirdparty/LICENSES/**'
test_python:
- '**'
Expand All @@ -61,6 +64,7 @@ jobs:
- '!img/**'
- '!notebooks/**'
- '!rust/**'
- '!go/**'
- '!thirdparty/LICENSES/**'
checks:
secrets: inherit
Expand Down Expand Up @@ -122,6 +126,16 @@ jobs:
arch: "amd64"
container_image: "rapidsai/ci-conda:latest"
run_script: "ci/build_rust.sh"
go-build:
needs: conda-cpp-build
secrets: inherit
uses: rapidsai/shared-workflows/.github/workflows/[email protected]
with:
build_type: pull-request
node_type: "gpu-v100-latest-1"
arch: "amd64"
container_image: "rapidsai/ci-conda:latest"
run_script: "ci/build_go.sh"
wheel-build-cuvs:
needs: checks
secrets: inherit
Expand Down
10 changes: 9 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ ARGS=$*
# scripts, and that this script resides in the repo dir!
REPODIR=$(cd $(dirname $0); pwd)

VALIDARGS="clean libcuvs python rust docs tests bench-ann examples --uninstall -v -g -n --compile-static-lib --allgpuarch --no-mg --no-cpu --cpu-only --no-shared-libs --no-nvtx --show_depr_warn --incl-cache-stats --time -h"
VALIDARGS="clean libcuvs python rust go docs tests bench-ann examples --uninstall -v -g -n --compile-static-lib --allgpuarch --no-mg --no-cpu --cpu-only --no-shared-libs --no-nvtx --show_depr_warn --incl-cache-stats --time -h"
HELP="$0 [<target> ...] [<flag> ...] [--cmake-args=\"<args>\"] [--cache-tool=<tool>] [--limit-tests=<targets>] [--limit-bench-ann=<targets>] [--build-metrics=<filename>]
where <target> is:
clean - remove all existing build artifacts and configuration (start over)
libcuvs - build the cuvs C++ code only. Also builds the C-wrapper library
around the C++ code.
python - build the cuvs Python package
rust - build the cuvs Rust bindings
go - build the cuvs Go bindings
docs - build the documentation
tests - build the tests
bench-ann - build end-to-end ann benchmarks
Expand Down Expand Up @@ -459,6 +460,13 @@ if (( ${NUMARGS} == 0 )) || hasArg rust; then
cargo test
fi

# Build the cuvs Go bindings
if (( ${NUMARGS} == 0 )) || hasArg go; then
cd ${REPODIR}/go
go build ./...
go test ./...
fi

export RAPIDS_VERSION="$(sed -E -e 's/^([0-9]{2})\.([0-9]{2})\.([0-9]{2}).*$/\1.\2.\3/' "${REPODIR}/VERSION")"
export RAPIDS_VERSION_MAJOR_MINOR="$(sed -E -e 's/^([0-9]{2})\.([0-9]{2})\.([0-9]{2}).*$/\1.\2/' "${REPODIR}/VERSION")"

Expand Down
40 changes: 40 additions & 0 deletions ci/build_go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
# Copyright (c) 2024, NVIDIA CORPORATION.

set -euo pipefail

rapids-logger "Create test conda environment"
. /opt/conda/etc/profile.d/conda.sh

RAPIDS_VERSION="$(rapids-version)"

rapids-dependency-file-generator \
--output conda \
--file-key go \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know how I could add this file key to rapids-dependency-file-generator?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @bdice @vyasr @AyodeAwe any suggestions here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a key under files: in dependencies.yaml, then add dependency lists with the conda packages you need. Here’s the rust one for comparison:

rust:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More explanation in the README here: https://github.com/rapidsai/dependency-file-generator

--matrix "cuda=${RAPIDS_CUDA_VERSION%.*};arch=$(arch);py=${RAPIDS_PY_VERSION}" | tee env.yaml

rapids-mamba-retry env create --yes -f env.yaml -n go

# seeing failures on activating the environment here on unbound locals
# apply workaround from https://github.com/conda/conda/issues/8186#issuecomment-532874667
set +eu
conda activate go
set -eu

rapids-print-env

export CGO_CFLAGS="-I/usr/local/cuda/include -I/home/ajit/miniforge3/envs/cuvs/include"
export CGO_LDFLAGS="-L/usr/local/cuda/lib64 -L/home/ajit/miniforge3/envs/cuvs/lib -lcudart -lcuvs -lcuvs_c"
export CC=clang

rapids-logger "Downloading artifacts from previous jobs"
CPP_CHANNEL=$(rapids-download-conda-from-s3 cpp)

# installing libcuvs/libraft will speed up the rust build substantially
rapids-mamba-retry install \
--channel "${CPP_CHANNEL}" \
libcuvs \
libraft \
cuvs

bash ./build.sh go
17 changes: 17 additions & 0 deletions dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ files:
- test_py_cuvs
- cupy
- rust
- go
bench_ann:
output: conda
matrix:
Expand Down Expand Up @@ -74,6 +75,13 @@ files:
- rapids_build
- cuda
- rust
go:
output: none
includes:
- cuda_version
- rapids_build
- cuda
- rust
py_build_cuvs:
output: pyproject
pyproject_dir: python/cuvs
Expand Down Expand Up @@ -411,6 +419,15 @@ dependencies:
# clang/liblclang only needed for bindgen support
- clang
- libclang
go:
common:
- output_types: [conda]
packages:
- make
- go
# clang/liblclang only needed for bindgen support
- clang
- libclang
build_wheels:
common:
- output_types: [requirements, pyproject]
Expand Down
2 changes: 2 additions & 0 deletions examples/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

cmake_minimum_required(VERSION 3.26.4 FATAL_ERROR)

set(CMAKE_CUDA_ARCHITECTURES 89)
ajit283 marked this conversation as resolved.
Show resolved Hide resolved

# ------------- configure rapids-cmake --------------#

include(../cmake/thirdparty/fetch_rapids.cmake)
Expand Down
64 changes: 64 additions & 0 deletions go/brute_force/brute_force.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package brute_force

// #include <cuvs/neighbors/brute_force.h>
import "C"

import (
"errors"
"unsafe"

cuvs "github.com/rapidsai/cuvs/go"
)

type bruteForceIndex struct {
index C.cuvsBruteForceIndex_t
trained bool
}

func CreateIndex() (*bruteForceIndex, error) {
var index C.cuvsBruteForceIndex_t

err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsBruteForceIndexCreate(&index)))
if err != nil {
return nil, err
}

return &bruteForceIndex{index: index, trained: false}, nil
}

func (index *bruteForceIndex) Close() error {
err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsBruteForceIndexDestroy(index.index)))
if err != nil {
return err
}
return nil
}

func BuildIndex[T any](Resources cuvs.Resource, Dataset *cuvs.Tensor[T], metric cuvs.Distance, metric_arg float32, index *bruteForceIndex) error {
CMetric, exists := cuvs.CDistances[metric]

if !exists {
return errors.New("cuvs: invalid distance metric")
}

err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsBruteForceBuild(C.cuvsResources_t(Resources.Resource), (*C.DLManagedTensor)(unsafe.Pointer(Dataset.C_tensor)), C.cuvsDistanceType(CMetric), C.float(metric_arg), index.index)))
if err != nil {
return err
}
index.trained = true

return nil
}

func SearchIndex[T any](resources cuvs.Resource, index bruteForceIndex, queries *cuvs.Tensor[T], neighbors *cuvs.Tensor[int64], distances *cuvs.Tensor[T]) error {
if !index.trained {
return errors.New("index needs to be built before calling search")
}

prefilter := C.cuvsFilter{
addr: 0,
_type: C.NO_FILTER,
}

return cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsBruteForceSearch(C.ulong(resources.Resource), index.index, (*C.DLManagedTensor)(unsafe.Pointer(queries.C_tensor)), (*C.DLManagedTensor)(unsafe.Pointer(neighbors.C_tensor)), (*C.DLManagedTensor)(unsafe.Pointer(distances.C_tensor)), prefilter)))
}
110 changes: 110 additions & 0 deletions go/brute_force/brute_force_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package brute_force

import (
"math/rand/v2"
"testing"

cuvs "github.com/rapidsai/cuvs/go"
)

func TestCagra(t *testing.T) {
const (
nDataPoints = 1024
nFeatures = 16
nQueries = 4
k = 4
epsilon = 0.001
)

resource, _ := cuvs.NewResource(nil)
defer resource.Close()

testDataset := make([][]float32, nDataPoints)
for i := range testDataset {
testDataset[i] = make([]float32, nFeatures)
for j := range testDataset[i] {
testDataset[i][j] = rand.Float32()
}
}

dataset, err := cuvs.NewTensor(testDataset)
if err != nil {
t.Fatalf("error creating dataset tensor: %v", err)
}
defer dataset.Close()

index, _ := CreateIndex()
defer index.Close()

// use the first 4 points from the dataset as queries : will test that we get them back
// as their own nearest neighbor
queries, _ := cuvs.NewTensor(testDataset[:nQueries])
defer queries.Close()

neighbors, err := cuvs.NewTensorOnDevice[int64](&resource, []int64{int64(nQueries), int64(k)})
if err != nil {
t.Fatalf("error creating neighbors tensor: %v", err)
}
defer neighbors.Close()

distances, err := cuvs.NewTensorOnDevice[float32](&resource, []int64{int64(nQueries), int64(k)})
if err != nil {
t.Fatalf("error creating distances tensor: %v", err)
}
defer distances.Close()

if _, err := dataset.ToDevice(&resource); err != nil {
t.Fatalf("error moving dataset to device: %v", err)
}

if err := BuildIndex(resource, &dataset, cuvs.DistanceL2, 2.0, index); err != nil {
t.Fatalf("error building index: %v", err)
}

if err := resource.Sync(); err != nil {
t.Fatalf("error syncing resource: %v", err)
}

if _, err := queries.ToDevice(&resource); err != nil {
t.Fatalf("error moving queries to device: %v", err)
}

err = SearchIndex(resource, *index, &queries, &neighbors, &distances)
if err != nil {
t.Fatalf("error searching index: %v", err)
}

if _, err := neighbors.ToHost(&resource); err != nil {
t.Fatalf("error moving neighbors to host: %v", err)
}

if _, err := distances.ToHost(&resource); err != nil {
t.Fatalf("error moving distances to host: %v", err)
}

if err := resource.Sync(); err != nil {
t.Fatalf("error syncing resource: %v", err)
}

neighborsSlice, err := neighbors.Slice()
if err != nil {
t.Fatalf("error getting neighbors slice: %v", err)
}

for i := range neighborsSlice {
if neighborsSlice[i][0] != int64(i) {
t.Error("wrong neighbor, expected", i, "got", neighborsSlice[i][0])
}
}

distancesSlice, err := distances.Slice()
if err != nil {
t.Fatalf("error getting distances slice: %v", err)
}

for i := range distancesSlice {
if distancesSlice[i][0] >= epsilon || distancesSlice[i][0] <= -epsilon {
t.Error("distance should be close to 0, got", distancesSlice[i][0])
}
}
}
Loading
Loading