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
Next Next commit
wip
  • Loading branch information
ajit283 committed Jun 14, 2024
commit 7961798a868b1775e7763a3dbe4e640322e4596d
67 changes: 67 additions & 0 deletions go/cuvs/bruteforce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package common

// #cgo CFLAGS: -I/usr/local/cuda/include -I/home/ajit/miniforge3/include
// #cgo LDFLAGS: -L/usr/local/cuda/lib64 -L/home/ajit/miniforge3/lib -lcudart -lcuvs -lcuvs_c
// #include <cuda_runtime_api.h>
// #include <cuvs/core/c_api.h>
// #include <cuvs/distance/pairwise_distance.h>
// #include <cuvs/neighbors/brute_force.h>
// #include <cuvs/neighbors/ivf_flat.h>
// #include <cuvs/neighbors/cagra.h>
// #include <cuvs/neighbors/ivf_pq.h>
import "C"
import (
"unsafe"
)

type Index struct {
index C.cuvsBruteForceIndex_t
trained bool
}

func CreateIndex() *Index {

index := (C.cuvsBruteForceIndex_t)(C.malloc(C.size_t(unsafe.Sizeof(C.cuvsBruteForceIndex{}))))

err := C.cuvsBruteForceIndexCreate(&index)

CheckCuvs(err)

return &Index{index: index, trained: false}

}

func DestroyIndex(index Index) {
err := C.cuvsBruteForceIndexDestroy(index.index)
CheckCuvs(err)

}

func BuildIndex(Resources C.cuvsResources_t, Dataset *C.DLManagedTensor, metric string, metric_arg float32, index C.cuvsBruteForceIndex_t) {

// Data := unsafe.Pointer(Dataset)

// C.cuvsRMMAlloc(Resources, &Data, 24)

CMetric := C.cuvsDistanceType(0)

switch metric {
case "L2Expanded":
CMetric = C.L2Expanded
default:
panic("Unsupported metric")
}

CheckCuvs(C.cuvsBruteForceBuild(Resources, Dataset, CMetric, C.float(metric_arg), index))

}

func SearchIndex(resources C.cuvsResources_t, index Index, queries *ManagedTensor, neighbors *ManagedTensor, distances *ManagedTensor) {

if !index.trained {
panic("Index needs to be built before calling search.")
}

CheckCuvs(C.cuvsBruteForceSearch(resources, index.index, queries, neighbors, distances))

}
33 changes: 33 additions & 0 deletions go/cuvs/bruteforce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package common

import (
"testing"
)

func TestBruteForce(t *testing.T) {

resource := NewResource(nil)

dataset := NewManagedTensor(true, []int{1, 1}, []float32{1, 2})

println(dataset.dl_tensor.dtype.code)

index := CreateIndex()

BuildIndex(resource.resource, dataset, "L2Expanded", 2.0, index.index)

index.trained = true

queries := NewManagedTensor(true, []int{1, 1}, []float32{1, 2})

neighbors := NewManagedTensor(true, []int{1, 1}, []float32{3, 3})

distances := NewManagedTensor(true, []int{1, 1}, []float32{1, 4})

SearchIndex(resource.resource, *index, queries, neighbors, distances)

p := (*float32)(neighbors.dl_tensor.data)

println(*p)

}
64 changes: 64 additions & 0 deletions go/cuvs/dlpack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package common

// #include <stdio.h>
// #include <stdlib.h>
// #include <dlpack/dlpack.h> // Replace with the actual header file containing DLManagedTensor
// void delete_tensor(DLManagedTensor *tensor){
// free(tensor->dl_tensor.shape);
// tensor->manager_ctx = NULL;
// free(tensor);
// }
import "C"
import (
"unsafe"
)

type ManagedTensor = C.DLManagedTensor

func NewManagedTensor(from_cai bool, shape []int, data []float32) *C.DLManagedTensor {

if len(shape) < 2 {
panic("shape must be atleast 2")
}

dlm := (*C.DLManagedTensor)(C.malloc(C.size_t(unsafe.Sizeof(C.DLManagedTensor{}))))

if dlm == nil {
panic("memory allocation failed")
}

var devicetype C.DLDeviceType

if from_cai {
devicetype = C.kDLCUDA
} else {
devicetype = C.kDLCPU
}

device := C.DLDevice{
device_type: devicetype,
device_id: 0,
}

dtype := C.DLDataType{
bits: C.uchar(32),
lanes: C.ushort(1),
code: C.kDLFloat,
}

dlm.dl_tensor.data = unsafe.Pointer(&data[0])

dlm.dl_tensor.device = device

dlm.dl_tensor.dtype = dtype
dlm.dl_tensor.ndim = C.int(len(shape))
dlm.dl_tensor.shape = (*C.int64_t)(unsafe.Pointer(&shape[0]))
dlm.dl_tensor.strides = nil
dlm.dl_tensor.byte_offset = 0

dlm.manager_ctx = nil
dlm.deleter = (*[0]byte)(C.delete_tensor)

return dlm

}
11 changes: 11 additions & 0 deletions go/cuvs/dlpack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common

import (
"testing"
)

func TestCreateNewTensor(t *testing.T) {

// tensor := NewManagedTensor(false, []int{1}, []int32{1})

}
17 changes: 17 additions & 0 deletions go/cuvs/exceptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package common

// #cgo CFLAGS: -I/usr/local/cuda/include -I/home/ajit/miniforge3/include
// #cgo LDFLAGS: -L/usr/local/cuda/lib64 -L/home/ajit/miniforge3/lib -lcudart -lcudart -lcuvs
// #include <cuvs/core/c_api.h>
// #include <cuvs/distance/pairwise_distance.h>
// #include <cuvs/neighbors/brute_force.h>
// #include <cuvs/neighbors/ivf_flat.h>
// #include <cuvs/neighbors/cagra.h>
// #include <cuvs/neighbors/ivf_pq.h>
import "C"

func CheckCuvs(error C.cuvsError_t) {
if error == C.CUVS_ERROR {
panic(C.GoString(C.cuvsGetLastErrorText()))
}
}
34 changes: 34 additions & 0 deletions go/cuvs/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package common

// #cgo CFLAGS: -I/usr/local/cuda/include -I/home/ajit/miniforge3/include
// #cgo LDFLAGS: -L/usr/local/cuda/lib64 -L/home/ajit/miniforge3/lib -lcudart -lcudart -lcuvs
// #include <cuda_runtime_api.h>
// #include <cuvs/core/c_api.h>
// #include <cuvs/distance/pairwise_distance.h>
// #include <cuvs/neighbors/brute_force.h>
// #include <cuvs/neighbors/ivf_flat.h>
// #include <cuvs/neighbors/cagra.h>
// #include <cuvs/neighbors/ivf_pq.h>
import "C"

type Resource struct {
resource C.cuvsResources_t
}

// func NewResource() *Resource {
func NewResource(stream C.cudaStream_t) Resource {

res := C.cuvsResources_t(0)

CheckCuvs(C.cuvsResourcesCreate(&res))

if stream != nil {
CheckCuvs(C.cuvsStreamSet(res, stream))
}

return Resource{resource: res}
}

func Sync(r C.cuvsResources_t) {
CheckCuvs(C.cuvsStreamSync(r))
}
3 changes: 3 additions & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module rapidsai/cuvs

go 1.22.4