-
Notifications
You must be signed in to change notification settings - Fork 6
MB-67052: Add GPU bindings #46
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
|
|
||
| // 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") |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
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.
| // 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()) |
| } | ||
| 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) |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
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.
| 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())) |
| ) | ||
| 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) |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
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.
| } | ||
| 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) |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
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.
| 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) |
Copilot
AI
Nov 13, 2025
There was a problem hiding this comment.
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.
NumGPUs(),FreeMemory())TransferToGPU(),TransferToCPU())GPUIndexImplwith automatic resource cleanup