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 63 commits into
base: branch-25.02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
63 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
7b93510
fix style checker
benfred Jan 15, 2025
f738da4
Merge branch 'branch-25.02' into go
benfred Jan 15, 2025
51b5747
update build branch
ajit283 Jan 17, 2025
ff36031
Merge branch 'branch-25.02' into go
ajit283 Jan 17, 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
Prev Previous commit
Next Next commit
add docstrings
  • Loading branch information
ajit283 committed Dec 4, 2024
commit f7fac3586ab9d37564141061236a27fa7ffc2f51
1 change: 0 additions & 1 deletion examples/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

cmake_minimum_required(VERSION 3.26.4 FATAL_ERROR)

set(CMAKE_CUDA_ARCHITECTURES 89)

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

Expand Down
31 changes: 25 additions & 6 deletions go/brute_force/brute_force.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,42 @@ import (
cuvs "github.com/rapidsai/cuvs/go"
)

type bruteForceIndex struct {
// Brute Force KNN Index
type BruteForceIndex struct {
index C.cuvsBruteForceIndex_t
trained bool
}

func CreateIndex() (*bruteForceIndex, error) {
// Creates a new empty Brute Force KNN Index
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
return &BruteForceIndex{index: index, trained: false}, nil
}

func (index *bruteForceIndex) Close() error {
// Destroys the Brute Force KNN Index
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 {
// Builds a new Brute Force KNN Index from the dataset for efficient search.
//
// # Arguments
//
// * `Resources` - Resources to use
// * `Dataset` - A row-major matrix on either the host or device to index
// * `metric` - Distance type to use for building the index
// * `metric_arg` - Value of `p` for Minkowski distances - set to 2.0 if not applicable
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 {
Expand All @@ -50,7 +61,15 @@ func BuildIndex[T any](Resources cuvs.Resource, Dataset *cuvs.Tensor[T], metric
return nil
}

func SearchIndex[T any](resources cuvs.Resource, index bruteForceIndex, queries *cuvs.Tensor[T], neighbors *cuvs.Tensor[int64], distances *cuvs.Tensor[T]) error {
// Perform a Nearest Neighbors search on the Index
//
// # Arguments
//
// * `Resources` - Resources to use
// * `queries` - Tensor in device memory to query for
// * `neighbors` - Tensor in device memory that receives the indices of the nearest neighbors
// * `distances` - Tensor in device memory that receives the distances of the nearest neighbors
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")
}
Expand Down
29 changes: 29 additions & 0 deletions go/cagra/cagra.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
cuvs "github.com/rapidsai/cuvs/go"
)

// Cagra ANN Index
type CagraIndex struct {
index C.cuvsCagraIndex_t
trained bool
}

// Creates a new empty Cagra Index
func CreateIndex() (*CagraIndex, error) {
var index C.cuvsCagraIndex_t
err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsCagraIndexCreate(&index)))
Expand All @@ -25,6 +27,14 @@ func CreateIndex() (*CagraIndex, error) {
return &CagraIndex{index: index}, nil
}

// Builds a new Index from the dataset for efficient search.
//
// # Arguments
//
// * `Resources` - Resources to use
// * `params` - Parameters for building the index
// * `dataset` - A row-major Tensor on either the host or device to index
// * `index` - CagraIndex to build
func BuildIndex[T any](Resources cuvs.Resource, params *IndexParams, dataset *cuvs.Tensor[T], index *CagraIndex) error {
err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsCagraBuild(C.ulong(Resources.Resource), params.params, (*C.DLManagedTensor)(unsafe.Pointer(dataset.C_tensor)), index.index)))
if err != nil {
Expand All @@ -34,6 +44,15 @@ func BuildIndex[T any](Resources cuvs.Resource, params *IndexParams, dataset *cu
return nil
}

// Extends the index with additional data
//
// # Arguments
//
// * `Resources` - Resources to use
// * `params` - Parameters for extending the index
// * `additional_dataset` - A row-major Tensor on the device to extend the index with
// * `return_dataset` - A row-major Tensor on the device that will receive the extended dataset
// * `index` - CagraIndex to extend
func ExtendIndex[T any](Resources cuvs.Resource, params *ExtendParams, additional_dataset *cuvs.Tensor[T], return_dataset *cuvs.Tensor[T], index *CagraIndex) error {
if !index.trained {
return errors.New("index needs to be built before calling extend")
Expand All @@ -45,6 +64,7 @@ func ExtendIndex[T any](Resources cuvs.Resource, params *ExtendParams, additiona
return nil
}

// Destroys the Cagra Index
func (index *CagraIndex) Close() error {
err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsCagraIndexDestroy(index.index)))
if err != nil {
Expand All @@ -53,6 +73,15 @@ func (index *CagraIndex) Close() error {
return nil
}

// Perform a Approximate Nearest Neighbors search on the Index
//
// # Arguments
//
// * `Resources` - Resources to use
// * `params` - Parameters to use in searching the index
// * `queries` - A tensor in device memory to query for
// * `neighbors` - Tensor in device memory that receives the indices of the nearest neighbors
// * `distances` - Tensor in device memory that receives the distances of the nearest neighbors
func SearchIndex[T any](Resources cuvs.Resource, params *SearchParams, index *CagraIndex, queries *cuvs.Tensor[T], neighbors *cuvs.Tensor[uint32], distances *cuvs.Tensor[T]) error {
if !index.trained {
return errors.New("index needs to be built before calling search")
Expand Down
8 changes: 8 additions & 0 deletions go/cagra/extend_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
cuvs "github.com/rapidsai/cuvs/go"
)

// Parameters to extend CAGRA Index
type ExtendParams struct {
params C.cuvsCagraExtendParams_t
}

// Creates a new ExtendParams
func CreateExtendParams() (*ExtendParams, error) {
var params C.cuvsCagraExtendParams_t

Expand All @@ -24,6 +26,12 @@ func CreateExtendParams() (*ExtendParams, error) {
return ExtendParams, nil
}

// The additional dataset is divided into chunks and added to the graph.
// This is the knob to adjust the tradeoff between the recall and operation throughput.
// Large chunk sizes can result in high throughput, but use more
// working memory (O(max_chunk_size*degree^2)).
// This can also degrade recall because no edges are added between the nodes in the same chunk.
// Auto select when 0.
func (p *ExtendParams) SetMaxChunkSize(max_chunk_size uint32) (*ExtendParams, error) {
p.params.max_chunk_size = C.uint32_t(max_chunk_size)
return p, nil
Expand Down
20 changes: 20 additions & 0 deletions go/cagra/index_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type IndexParams struct {
params C.cuvsCagraIndexParams_t
}

// Supplemental parameters to build CAGRA Index
type CompressionParams struct {
params C.cuvsCagraCompressionParams_t
}
Expand All @@ -31,6 +32,7 @@ var cBuildAlgos = map[BuildAlgo]int{
AutoSelect: C.AUTO_SELECT,
}

// Creates a new CompressionParams
func CreateCompressionParams() (*CompressionParams, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we please document all of these functions? Does Go have a common convention or tooling for generating standardized documentation?

var params C.cuvsCagraCompressionParams_t

Expand All @@ -46,42 +48,54 @@ func CreateCompressionParams() (*CompressionParams, error) {
return &CompressionParams{params: params}, nil
}

// The bit length of the vector element after compression by PQ.
func (p *CompressionParams) SetPQBits(pq_bits uint32) (*CompressionParams, error) {
p.params.pq_bits = C.uint32_t(pq_bits)

return p, nil
}

// The dimensionality of the vector after compression by PQ. When zero,
// an optimal value is selected using a heuristic.
func (p *CompressionParams) SetPQDim(pq_dim uint32) (*CompressionParams, error) {
p.params.pq_dim = C.uint32_t(pq_dim)

return p, nil
}

// Vector Quantization (VQ) codebook size - number of "coarse cluster
// centers". When zero, an optimal value is selected using a heuristic.
func (p *CompressionParams) SetVQNCenters(vq_n_centers uint32) (*CompressionParams, error) {
p.params.vq_n_centers = C.uint32_t(vq_n_centers)

return p, nil
}

// The number of iterations searching for kmeans centers (both VQ & PQ
// phases).
func (p *CompressionParams) SetKMeansNIters(kmeans_n_iters uint32) (*CompressionParams, error) {
p.params.kmeans_n_iters = C.uint32_t(kmeans_n_iters)

return p, nil
}

// The fraction of data to use during iterative kmeans building (VQ
// phase). When zero, an optimal value is selected using a heuristic.
func (p *CompressionParams) SetVQKMeansTrainsetFraction(vq_kmeans_trainset_fraction float64) (*CompressionParams, error) {
p.params.vq_kmeans_trainset_fraction = C.double(vq_kmeans_trainset_fraction)

return p, nil
}

// The fraction of data to use during iterative kmeans building (PQ
// phase). When zero, an optimal value is selected using a heuristic.
func (p *CompressionParams) SetPQKMeansTrainsetFraction(pq_kmeans_trainset_fraction float64) (*CompressionParams, error) {
p.params.pq_kmeans_trainset_fraction = C.double(pq_kmeans_trainset_fraction)

return p, nil
}

// Creates a new IndexParams
func CreateIndexParams() (*IndexParams, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Are these function names namespaced in some way? For example, will this conflict with an ivfpq CreateIndexParams or does the Go compiler know that this is different because it's nested in a cagra directory? I wonder if we should mirror the C nameing convention (i.e. cuvsCagraIndexParamsCreate, prefix functions with cuvs, followed by the subject and put the verb at the end).

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, I see the package cagra at the top of the file so I'm assuming everything is nested inside. I think I'd prefer to fully qualify these packages, though, for consistency with the other cuVS APIs: cuvs.neighbors.cagra.

var params C.cuvsCagraIndexParams_t

Expand All @@ -95,17 +109,20 @@ func CreateIndexParams() (*IndexParams, error) {
return IndexParams, nil
}

// Degree of input graph for pruning
func (p *IndexParams) SetIntermediateGraphDegree(intermediate_graph_degree uintptr) (*IndexParams, error) {
p.params.intermediate_graph_degree = C.size_t(intermediate_graph_degree)
return p, nil
}

// Degree of output graph
func (p *IndexParams) SetGraphDegree(intermediate_graph_degree uintptr) (*IndexParams, error) {
p.params.graph_degree = C.size_t(intermediate_graph_degree)

return p, nil
}

// ANN algorithm to build knn graph
func (p *IndexParams) SetBuildAlgo(build_algo BuildAlgo) (*IndexParams, error) {
CBuildAlgo, exists := cBuildAlgos[build_algo]

Expand All @@ -117,18 +134,21 @@ func (p *IndexParams) SetBuildAlgo(build_algo BuildAlgo) (*IndexParams, error) {
return p, nil
}

// Number of iterations to run if building with NN_DESCENT
func (p *IndexParams) SetNNDescentNiter(nn_descent_niter uint32) (*IndexParams, error) {
p.params.nn_descent_niter = C.ulong(nn_descent_niter)

return p, nil
}

// Compression parameters
func (p *IndexParams) SetCompression(compression *CompressionParams) (*IndexParams, error) {
p.params.compression = C.cuvsCagraCompressionParams_t(compression.params)

return p, nil
}

// Destroys IndexParams
func (p *IndexParams) Close() error {
err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsCagraIndexParamsDestroy(p.params)))
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions go/cagra/search_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cuvs "github.com/rapidsai/cuvs/go"
)

// Supplemental parameters to search CAGRA Index
type SearchParams struct {
params C.cuvsCagraSearchParams_t
}
Expand All @@ -30,6 +31,7 @@ const (
HashmapModeAuto
)

// Creates a new SearchParams
func CreateSearchParams() (*SearchParams, error) {
var params C.cuvsCagraSearchParams_t

Expand All @@ -41,21 +43,27 @@ func CreateSearchParams() (*SearchParams, error) {
return &SearchParams{params: params}, nil
}

// Maximum number of queries to search at the same time (batch size). Auto select when 0
func (p *SearchParams) SetMaxQueries(max_queries uintptr) (*SearchParams, error) {
p.params.max_queries = C.size_t(max_queries)
return p, nil
}

// Number of intermediate search results retained during the search.
// This is the main knob to adjust trade off between accuracy and search speed.
// Higher values improve the search accuracy
func (p *SearchParams) SetItopkSize(itopk_size uintptr) (*SearchParams, error) {
p.params.itopk_size = C.size_t(itopk_size)
return p, nil
}

// Upper limit of search iterations. Auto select when 0.
func (p *SearchParams) SetMaxIterations(max_iterations uintptr) (*SearchParams, error) {
p.params.max_iterations = C.size_t(max_iterations)
return p, nil
}

// Which search implementation to use.
func (p *SearchParams) SetAlgo(algo SearchAlgo) (*SearchParams, error) {
CAlgo := C.SINGLE_CTA

Expand All @@ -77,26 +85,31 @@ func (p *SearchParams) SetAlgo(algo SearchAlgo) (*SearchParams, error) {
return p, nil
}

// Number of threads used to calculate a single distance. 4, 8, 16, or 32.
func (p *SearchParams) SetTeamSize(team_size uintptr) (*SearchParams, error) {
p.params.team_size = C.size_t(team_size)
return p, nil
}

// Lower limit of search iterations.
func (p *SearchParams) SetMinIterations(min_iterations uintptr) (*SearchParams, error) {
p.params.min_iterations = C.size_t(min_iterations)
return p, nil
}

// How many nodes to search at once. Auto select when 0.
func (p *SearchParams) SetSearchWidth(search_width uintptr) (*SearchParams, error) {
p.params.search_width = C.size_t(search_width)
return p, nil
}

// Thread block size. 0, 64, 128, 256, 512, 1024. Auto selection when 0.
func (p *SearchParams) SetThreadBlockSize(thread_block_size uintptr) (*SearchParams, error) {
p.params.thread_block_size = C.size_t(thread_block_size)
return p, nil
}

// Hashmap type. Auto selection when AUTO.
func (p *SearchParams) SetHashmapMode(hashmap_mode HashmapMode) (*SearchParams, error) {
CHashMode := C.AUTO_HASH

Expand All @@ -116,26 +129,31 @@ func (p *SearchParams) SetHashmapMode(hashmap_mode HashmapMode) (*SearchParams,
return p, nil
}

// Lower limit of hashmap bit length. More than 8.
func (p *SearchParams) SetHashmapMinBitlen(hashmap_min_bitlen uintptr) (*SearchParams, error) {
p.params.hashmap_min_bitlen = C.size_t(hashmap_min_bitlen)
return p, nil
}

// Upper limit of hashmap fill rate. More than 0.1, less than 0.9.
func (p *SearchParams) SetHashmapMaxFillRate(hashmap_max_fill_rate float32) (*SearchParams, error) {
p.params.hashmap_max_fill_rate = C.float(hashmap_max_fill_rate)
return p, nil
}

// Number of iterations of initial random seed node selection. 1 or more.
func (p *SearchParams) SetNumRandomSamplings(num_random_samplings uint32) (*SearchParams, error) {
p.params.num_random_samplings = C.uint32_t(num_random_samplings)
return p, nil
}

// Bit mask used for initial random seed node selection.
func (p *SearchParams) SetRandXorMask(rand_xor_mask uint64) (*SearchParams, error) {
p.params.rand_xor_mask = C.uint64_t(rand_xor_mask)
return p, nil
}

// Destroys SearchParams
func (p *SearchParams) Close() error {
err := cuvs.CheckCuvs(cuvs.CuvsError(C.cuvsCagraSearchParamsDestroy(p.params)))
if err != nil {
Expand Down
Loading
Loading