Documentation
¶
Overview ¶
Package cuda provides NVIDIA GPU acceleration using CUDA. This is a stub implementation for systems without CUDA support. It includes runtime detection via nvidia-smi for informational purposes.
Package cuda provides NVIDIA GPU acceleration for vector operations using CUDA.
This package requires:
- NVIDIA GPU with CUDA Compute Capability 3.5+
- CUDA Toolkit 11.0+ installed
- cuBLAS library available
The package provides GPU-accelerated:
- Vector normalization
- Cosine similarity computation
- Top-K selection
Build Requirements:
On Linux:
- Install CUDA Toolkit: https://developer.nvidia.com/cuda-downloads
- Set environment: export CUDA_HOME=/usr/local/cuda
- Ensure libcublas.so is in LD_LIBRARY_PATH
On Windows:
- Install CUDA Toolkit from NVIDIA
- Visual Studio with C++ build tools
- CUDA_PATH environment variable set
Build tags:
- Build with: go build -tags cuda
- Without CUDA: builds with stub implementations
Example usage:
if cuda.IsAvailable() {
device, err := cuda.NewDevice(0)
if err != nil {
log.Fatal(err)
}
defer device.Release()
// Create buffer with embeddings
buf, _ := device.NewBuffer(embeddings, cuda.MemoryDevice)
// Perform similarity search
results, _ := device.Search(buf, query, n, dims, k)
}
Index ¶
- Variables
- func DeviceCount() int
- func GPUMemoryMB() int
- func GPUName() string
- func HasGPUHardware() bool
- func IsAvailable() bool
- func IsCUDACapable() bool
- type Buffer
- type Device
- func (d *Device) ComputeCapability() (int, int)
- func (d *Device) CosineSimilarity(embeddings, query, scores *Buffer, n, dimensions uint32, normalized bool) error
- func (d *Device) ID() int
- func (d *Device) MemoryBytes() uint64
- func (d *Device) MemoryMB() int
- func (d *Device) Name() string
- func (d *Device) NewBuffer(data []float32, memType MemoryType) (*Buffer, error)
- func (d *Device) NewEmptyBuffer(count uint64, memType MemoryType) (*Buffer, error)
- func (d *Device) NormalizeVectors(vectors *Buffer, n, dimensions uint32) error
- func (d *Device) Release()
- func (d *Device) Search(embeddings *Buffer, query []float32, n, dimensions uint32, k int, ...) ([]SearchResult, error)
- func (d *Device) TopK(scores *Buffer, n, k uint32) ([]uint32, []float32, error)
- type MemoryType
- type SearchResult
Constants ¶
This section is empty.
Variables ¶
var ( ErrCUDANotAvailable = errors.New("cuda: CUDA is not available (build without cuda tag or unsupported platform)") ErrDeviceCreation = errors.New("cuda: failed to create CUDA device") ErrBufferCreation = errors.New("cuda: failed to create buffer") ErrKernelExecution = errors.New("cuda: kernel execution failed") ErrInvalidBuffer = errors.New("cuda: invalid buffer") )
Errors
Functions ¶
func DeviceCount ¶
func DeviceCount() int
DeviceCount returns 1 if GPU detected, 0 otherwise. Note: In stub mode, device can't actually be used for CUDA operations.
func GPUMemoryMB ¶
func GPUMemoryMB() int
GPUMemoryMB returns the detected GPU memory in MB, or 0 if none.
func GPUName ¶
func GPUName() string
GPUName returns the detected GPU name, or empty string if none.
func HasGPUHardware ¶
func HasGPUHardware() bool
HasGPUHardware returns true if NVIDIA GPU hardware is detected. This is separate from CUDA availability - GPU may be present but CUDA not usable.
func IsAvailable ¶
func IsAvailable() bool
IsAvailable checks if CUDA/GPU is available. In stub mode, we detect GPU via nvidia-smi but can't use it for acceleration. This allows informative logging about GPU presence.
func IsCUDACapable ¶
func IsCUDACapable() bool
IsCUDACapable returns false in stub mode - GPU is detected but CUDA ops unavailable.
Types ¶
type Buffer ¶
type Buffer struct{}
Buffer represents a CUDA memory buffer (stub).
func (*Buffer) ReadFloat32 ¶
ReadFloat32 returns nil.
type Device ¶
type Device struct{}
Device represents a CUDA GPU device (stub).
func NewDevice ¶
NewDevice returns an error on systems without CUDA. Even with GPU detected, CUDA operations require the cuda build tag.
func (*Device) ComputeCapability ¶
ComputeCapability returns 0, 0.
func (*Device) CosineSimilarity ¶
func (d *Device) CosineSimilarity(embeddings, query, scores *Buffer, n, dimensions uint32, normalized bool) error
CosineSimilarity returns an error.
func (*Device) NewBuffer ¶
func (d *Device) NewBuffer(data []float32, memType MemoryType) (*Buffer, error)
NewBuffer returns an error.
func (*Device) NewEmptyBuffer ¶
func (d *Device) NewEmptyBuffer(count uint64, memType MemoryType) (*Buffer, error)
NewEmptyBuffer returns an error.
func (*Device) NormalizeVectors ¶
NormalizeVectors returns an error.
type MemoryType ¶
type MemoryType int
MemoryType defines how buffer memory is managed.
const ( MemoryDevice MemoryType = 0 MemoryPinned MemoryType = 1 )
type SearchResult ¶
SearchResult holds a similarity search result.