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 57 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
57 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
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
bruteforce works
  • Loading branch information
ajit283 committed Jun 19, 2024
commit b47cf5bc98eb4b03955f1d357ef3a93431fc9ad4
4 changes: 4 additions & 0 deletions go/cuvs/bruteforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func DestroyIndex(index Index) {

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

if Dataset.dl_tensor.device.device_type != C.kDLCUDA {
panic("Dataset must be on GPU")
}

// Data := unsafe.Pointer(Dataset)

// CheckCuvs(C.cuvsRMMAlloc(Resources, &Data, 2400))
Expand Down
63 changes: 35 additions & 28 deletions go/cuvs/bruteforce_test.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,63 @@
package common

import (
"math/rand"
"testing"
"time"
"unsafe"
)

func TestBruteForce(t *testing.T) {

resource := NewResource(nil)

dataset := NewManagedTensor(true, []int{1, 2}, []float32{1, 2}, false)
bytes := GetBytes(dataset)
println("size:")
println(bytes)
println(dataset.dl_tensor.dtype.code)
rand.Seed(time.Now().UnixNano())

index := CreateIndex()

BuildIndex(resource.resource, dataset, "L2Expanded", 2.0, index)
println("got here")

println("index created")

index.trained = true
NDataPoints := 16
NFeatures := 8

queries := NewManagedTensor(true, []int{1, 2}, []float32{1, 2}, false)
TestDataset := make([]float32, NDataPoints*NFeatures)
for index := range TestDataset {
TestDataset[index] = rand.Float32()
// TestDataset[index] = float32(index)
}

neighbors := NewManagedTensor(true, []int{1, 1}, []int64{3, 3}, true)
dataset := NewManagedTensor(true, []int{NDataPoints, NFeatures}, TestDataset, false)

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

println("tensors created")
index := CreateIndex()
// use the first 4 points from the dataset as queries : will test that we get them back
// as their own nearest neighbor

println(queries.dl_tensor.data)
NQueries := 4
K := 4
queries := NewManagedTensor(true, []int{NQueries, NFeatures}, TestDataset[:(NQueries*NFeatures)], false)
neighbors := NewManagedTensor(true, []int{NQueries, K}, make([]int64, NQueries*K), true)
distances := NewManagedTensor(true, []int{NQueries, K}, make([]float32, NQueries*K), false)

queries_device := ToDevice(queries, &resource)
ToDevice(neighbors, &resource)
ToDevice(distances, &resource)
ToDevice(dataset, &resource)

println("queries tensor transferred")
BuildIndex(resource.resource, dataset, "L2Expanded", 2.0, index)
Sync(resource.resource)

// neighbors_device := ToDevice(neighbors, &resource)
ToDevice(queries, &resource)

// distances_device := ToDevice(distances, &resource)
index.trained = true

println(queries_device.dl_tensor.data)
SearchIndex(resource.resource, *index, queries, neighbors, distances)

println("tensors on device")
ToHost(neighbors, &resource)
ToHost(distances, &resource)

// SearchIndex(resource.resource, *index, queries_device, neighbors_device, distances_device)
Sync(resource.resource)

println("search done")
p := (*int64)(unsafe.Pointer(uintptr(neighbors.dl_tensor.data) + uintptr(K*8*3)))

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

println(*p)

println(*d)

}
82 changes: 71 additions & 11 deletions go/cuvs/dlpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func NewManagedTensor[T any](from_cai bool, shape []int, data []T, use_int64 boo

var devicetype C.DLDeviceType

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

device := C.DLDevice{
device_type: devicetype,
Expand Down Expand Up @@ -94,7 +94,7 @@ func GetBytes(t *C.DLManagedTensor) int {
return bytes
}

func ToDevice(t *C.DLManagedTensor, res *Resource) *C.DLManagedTensor {
func ToDevice(t *C.DLManagedTensor, res *Resource) {
bytes := GetBytes(t)

// device_data := &C.void{}
Expand All @@ -112,8 +112,8 @@ func ToDevice(t *C.DLManagedTensor, res *Resource) *C.DLManagedTensor {
// println("new host data location:")
// println(t.dl_tensor.data)

// CheckCuvs(C.cuvsRMMAlloc(res.resource, &DeviceDataPointer, C.size_t(bytes)))
CheckCuda(C.cudaMalloc(&DeviceDataPointer, C.size_t(bytes)))
CheckCuvs(C.cuvsRMMAlloc(res.resource, &DeviceDataPointer, C.size_t(bytes)))
// CheckCuda(C.cudaMalloc(&DeviceDataPointer, C.size_t(bytes)))

println("device data pointer:")
println(DeviceDataPointer)
Expand All @@ -134,9 +134,11 @@ func ToDevice(t *C.DLManagedTensor, res *Resource) *C.DLManagedTensor {

CheckCuda(
C.cudaMemcpy(
// DeviceDataPointer,
DeviceDataPointer,
// t.dl_tensor.data,
unsafe.Pointer(&hostData[0]),
// unsafe.Pointer(&hostData[0]),
t.dl_tensor.data,
// unsafe.Pointer(&hostData[0]),
// DeviceDataPointer,
// C.size_t(bytes),
C.size_t(bytes),
Expand All @@ -149,7 +151,65 @@ func ToDevice(t *C.DLManagedTensor, res *Resource) *C.DLManagedTensor {
println("done")

t.dl_tensor.data = DeviceDataPointer
println("normal transfer done")
// return t

}

func ToHost(t *C.DLManagedTensor, res *Resource) {
bytes := GetBytes(t)

// var DeviceDataPointer unsafe.Pointer

addr := (C.malloc(C.size_t(bytes)))

// hostData := make([]float32, 2)

CheckCuda(
C.cudaMemcpy(
// DeviceDataPointer,
addr,
// unsafe.Pointer(&hostData[0]),
t.dl_tensor.data,
// unsafe.Pointer(&hostData[0]),
// DeviceDataPointer,
// C.size_t(bytes),
C.size_t(bytes),
C.cudaMemcpyDeviceToHost,

// res.get_cuda_stream(),
// GetCudaStream(res.resource),
))

t.dl_tensor.data = addr
}

func TestCuda() {

var DeviceDataPointer unsafe.Pointer
CheckCuda(C.cudaMalloc(&DeviceDataPointer, C.size_t(8)))

array := make([]float32, 2)

for i := range array {
array[i] = float32(i)
}

CheckCuda(
C.cudaMemcpy(
// DeviceDataPointer,
DeviceDataPointer,
unsafe.Pointer(&array[0]),
// unsafe.Pointer(&hostData[0]),
// DeviceDataPointer,
// C.size_t(bytes),
C.size_t(8),
C.cudaMemcpyHostToDevice,

// res.get_cuda_stream(),
// GetCudaStream(res.resource),
))

return t
println("cuda test done")

}