FastEmbed on GPU
As of version 0.2.7 FastEmbed supports GPU acceleration.
This notebook covers the installation process and usage of fastembed on GPU.
Installation
Fastembed depends on onnxruntime and inherits its scheme of GPU support.
In order to use GPU with onnx models, you would need to have onnxruntime-gpu package, which substitutes all the onnxruntime functionality.
Fastembed mimics this behavior and requires fastembed-gpu package to be installed.
!pip install fastembed-gpu
NOTE: onnxruntime-gpu and onnxruntime can't be installed in the same environment. If you have onnxruntime installed, you would need to uninstall it before installing onnxruntime-gpu. Same is true for fastembed and fastembed-gpu.
CUDA and cuDNN
onnxruntime-gpu needs CUDA and cuDNN 9, and each release is built for one CUDA version. Check which CUDA version your NVIDIA driver supports with nvidia-smi:
| onnxruntime-gpu | CUDA | NVIDIA driver |
|---|---|---|
| 1.27 and newer | 13.x | 580 or newer |
| 1.19 to 1.26 | 12.x | 525 or newer |
With cuDNN from pip, both need a Turing GPU (compute capability 7.5) or newer: cuDNN 9.11 and CUDA 13 dropped older GPUs.
The cuda and cudnn extras of onnxruntime-gpu install both libraries from pip, and FastEmbed loads them before it creates a CUDA session, so you don't need a CUDA toolkit or LD_LIBRARY_PATH. With a driver of 580 or newer:
!pip install -U fastembed-gpu "onnxruntime-gpu[cuda,cudnn]" -qqq
With an older driver, stay on CUDA 12 by keeping onnxruntime-gpu below 1.27. Use 1.24.4 or newer, which runs several models faster on the GPU (embeddinggemma-300m 4 to 7 times).
!pip install fastembed-gpu "onnxruntime-gpu[cuda,cudnn]>=1.24.4,<1.27" -qqq
CUDA 11.x support
onnxruntime-gpu supports CUDA 11 up to 1.20.1, which is available from the CUDA 11 package feed:
!pip install onnxruntime-gpu -i https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-11/pypi/simple/ -qq
NOTE: Older FastEmbed versions suggested pip install onnxruntime-gpu --extra-index-url with the CUDA 12 package feed. pip installs the newest version from any index, so that command now installs the CUDA 13 build from PyPI.
CUDA drivers
FastEmbed does not include NVIDIA drivers. With the pip extras above, the driver is all you need. Without them, install CUDA and cuDNN 9 yourself, as listed in the CUDA Execution Provider requirements, and make sure their libraries are on LD_LIBRARY_PATH.
Setting up fastembed-gpu on GCP
CUDA drivers
Without the pip extras, the CUDA toolkit for your onnxruntime-gpu (CUDA 12.x or 13.x) has to be installed if it hasn't yet been set up.
Example of setting up CUDA 12.x on Ubuntu 22.04
Make sure to download an archive which has been created for your particular platform, CPU architecture and OS distribution.
For Ubuntu 22.04 with x86_64 CPU architecture the following archive has to be downloaded.
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda
NOTE: When installing CUDA, the environment variable might not be set by default. Make sure to add the following line to your environment variables:
LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
CuDNN 9.x
CuDNN 9.x library can be installed via the following archive.
Example of setting up CuDNN 9.x on Ubuntu 22.04
CuDNN 9.x for Ubuntu 22.04 x86_64 archive can be downloaded and installed in the following way:
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cudnn
Common issues
The following are some common issues that may arise while using fastembed-gpu if not installed properly:
CUDA library is not installed:
FAIL : Failed to load library libonnxruntime_providers_cuda.so with error: libcublasLt.so.x: cannot open shared object file: No such file or directory
CuDNN library is not installed:
FAIL : Failed to load library libonnxruntime_providers_cuda.so with error: libcudnn.so.x: cannot open shared object file: No such file or directory
CUDA library path is not set:
FAIL : Failed to load library libonnxruntime_providers_cuda.so with error: libcufft.so.x: failed to map segment from shared object
Make sure to add the following line to your environment variables:
LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
The NVIDIA driver is older than the CUDA version of onnxruntime-gpu, for example onnxruntime-gpu 1.27 or newer (CUDA 13) with a driver older than 580. onnxruntime-gpu then falls back to the CPU:
CUDA failure 35: CUDA driver version is insufficient for CUDA runtime version
nvidia-smi shows the newest CUDA version the driver supports, and FastEmbed's warning names the CUDA version onnxruntime-gpu is built for. Install the CUDA 12 build, pip install "onnxruntime-gpu[cuda,cudnn]>=1.21,<1.27", or update the driver. Then restart the kernel and create the model again.
Usage
import numpy as np
from fastembed import TextEmbedding
embedding_model_gpu = TextEmbedding(
model_name="BAAI/bge-small-en-v1.5", providers=["CUDAExecutionProvider"]
)
embedding_model_gpu.model.model.get_providers()
documents: list[str] = list(np.repeat("Demonstrating GPU acceleration in fastembed", 500))
%%timeit
list(embedding_model_gpu.embed(documents))
embedding_model_cpu = TextEmbedding(model_name="BAAI/bge-small-en-v1.5")
embedding_model_cpu.model.model.get_providers()
%%timeit
list(embedding_model_cpu.embed(documents))