Skip to content

Conversation

@CascadingRadium
Copy link
Member

@CascadingRadium CascadingRadium commented Nov 4, 2025

  • Added GPU device query functions (NumGPUs(), FreeMemory())
  • Added bidirectional CPU/GPU index transfer (TransferToGPU(), TransferToCPU())
  • Introduced GPUIndexImpl with automatic resource cleanup

@abhinavdangeti abhinavdangeti changed the title Add GPU bindings MB-67052: Add GPU bindings Nov 11, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds GPU support to the FAISS Go bindings, enabling users to transfer indices between CPU and GPU memory for accelerated vector search operations.

  • Adds functions for GPU device management (NumGPUs, FreeMemory)
  • Implements GPU index wrapper (GPUIndexImpl) with proper resource cleanup
  • Provides bidirectional transfer functions between CPU and GPU memory (TransferToGPU, TransferToCPU)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +15 to +21

// NumGPUs returns the number of available GPU devices.
func NumGPUs() (int, error) {
var rv C.int
c := C.faiss_get_num_gpus(&rv)
if c != 0 {
return 0, errors.New("error getting number of GPUs")
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

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

Consider using getLastError() instead of a generic error message for consistency with the rest of the codebase. The underlying C API likely sets a detailed error message that can be retrieved via getLastError(), which would provide more helpful debugging information.

Suggested change
// NumGPUs returns the number of available GPU devices.
func NumGPUs() (int, error) {
var rv C.int
c := C.faiss_get_num_gpus(&rv)
if c != 0 {
return 0, errors.New("error getting number of GPUs")
func NumGPUs() (int, error) {
var rv C.int
c := C.faiss_get_num_gpus(&rv)
if c != 0 {
return 0, fmt.Errorf("error getting number of GPUs: %s", getLastError())

Copilot uses AI. Check for mistakes.
}
var gpuResource *C.FaissStandardGpuResources
if code := C.faiss_StandardGpuResources_new(&gpuResource); code != 0 {
return nil, fmt.Errorf("failed to initialize GPU resources: error code %d", code)
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

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

Consider using getLastError() for more detailed error information instead of only including the error code. This pattern is used consistently throughout the codebase (see ReadIndex, WriteIndex, etc.) and provides better debugging context.

Suggested change
return nil, fmt.Errorf("failed to initialize GPU resources: error code %d", code)
return nil, fmt.Errorf("failed to initialize GPU resources: error code %d, details: %s", code, C.GoString(C.faiss_get_last_error()))

Copilot uses AI. Check for mistakes.
)
if code != 0 {
C.faiss_StandardGpuResources_free(gpuResource)
return nil, fmt.Errorf("failed to transfer index to GPU device %d: error code %d", device, code)
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

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

Consider using getLastError() for more detailed error information instead of only including the error code. This pattern is used consistently throughout the codebase and provides better debugging context.

Copilot uses AI. Check for mistakes.
}
var cpuIndex *C.FaissIndex
if code := C.faiss_index_gpu_to_cpu(gpuIndex.cPtr(), &cpuIndex); code != 0 {
return nil, fmt.Errorf("failed to transfer index to CPU: error code %d", code)
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

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

Consider using getLastError() for more detailed error information instead of only including the error code. This pattern is used consistently throughout the codebase and provides better debugging context.

Copilot uses AI. Check for mistakes.
var freeBytes C.size_t
c := C.faiss_get_free_memory(C.int(device), &freeBytes)
if c != 0 {
return 0, fmt.Errorf("error getting free memory for device %d", device)
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

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

Consider using getLastError() for more detailed error information instead of a generic error message. This pattern is used consistently throughout the codebase and provides better debugging context.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants