Skip to content

Instantly share code, notes, and snippets.

@nguyenhoan1988
Created October 19, 2021 22:05
Show Gist options
  • Select an option

  • Save nguyenhoan1988/ed92d58054b985a1b45a521fcf8fa781 to your computer and use it in GitHub Desktop.

Select an option

Save nguyenhoan1988/ed92d58054b985a1b45a521fcf8fa781 to your computer and use it in GitHub Desktop.
Installing dlib using conda with CUDA enabled

Installing dlib using conda with CUDA enabled

Prerequisite: conda and/or miniconda are already installed

  1. Create a conda environment.
$ conda create -n dlib python=3.8 cmake ipython
  1. Activate the environment.
$ conda activate dlib
  1. Install CUDA and cuDNN with conda using nvidia channel
$ conda install cuda cudnn -c nvidia

Then find the path to the nvcc of this environment. We will use this path for the build step below

$which nvcc
/path/to/your/miniconda3/envs/dlib/bin/
  1. Install dlib. Clone and build dlib from source
$ git clone https://github.com/davisking/dlib.git
$ cd dlib
$ mkdir build
$ cd build
$ cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1 -DCUDAToolkit_ROOT=/path/to/your/miniconda3/envs/dlib/bin/
$ cmake --build .
$ cd ..
$ python setup.py install --set DLIB_USE_CUDA=1
  1. Test dlib
(dlib) $ ipython
Python 3.8.12 (default, Oct 12 2021, 13:49:34)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.27.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import dlib

In [2]: dlib.DLIB_USE_CUDA
Out[2]: True

In [3]: print(dlib.cuda.get_num_devices())
1
@semajbaker
Copy link

semajbaker commented Sep 23, 2024 via email

@giampierobartolomei
Copy link

Ok thanks, now I have another question. I successfully installed all my python libraries. now when i try to run a cnn_face_detector (based on dlib) i find this error: dets = self.loader.cnn_face_detector(self.frame, 1)
RuntimeError: Error while calling cudaOccupancyMaxPotentialBlockSize(&num_blocks,&num_threads,K) in file /home/chiara/Desktop/gpu_4070/dlib/dlib/cuda/cuda_utils.h:164. code: 222, reason: the provided PTX was compiled with an unsupported toolchain.
Aborted (core dumped). I found that this error may be due to the cuda compatibilities with my rtx geforce 4070. I checked and it needs cuda >11.8. So I installed dlib with cuda like before and checked and DLIB_USE_CUDA=True.

This on my conda list:
cuda 11.8.0 0 nvidia
cuda-cccl_linux-64 12.6.37 0 nvidia
cuda-command-line-tools 12.6.1 0 nvidia
cuda-compiler 12.6.0 0 nvidia
cuda-cudart 12.6.68 0 nvidia
cuda-cudart-dev 12.6.68 0 nvidia
cuda-cudart-dev_linux-64 12.6.68 0 nvidia
cuda-cudart-static 12.6.68 0 nvidia
cuda-cudart-static_linux-64 12.6.68 0 nvidia
cuda-cudart_linux-64 12.6.68 0 nvidia
cuda-cuobjdump 12.6.68 0 nvidia
cuda-cupti 12.6.68 0 nvidia
cuda-cupti-dev 12.6.68 0 nvidia
cuda-cuxxfilt 12.6.68 0 nvidia
cuda-demo-suite 12.4.127 0 nvidia
cuda-documentation 12.4.127 0 nvidia
cuda-driver-dev 12.6.68 0 nvidia
cuda-driver-dev_linux-64 12.6.68 0 nvidia
cuda-gdb 12.6.68 0 nvidia
cuda-libraries 12.6.1 0 nvidia
cuda-libraries-dev 12.6.1 0 nvidia
cuda-libraries-static 12.6.1 0 nvidia
cuda-nsight 12.6.68 0 nvidia
cuda-nvcc 12.4.131 0 nvidia
cuda-nvdisasm 12.6.68 0 nvidia
cuda-nvml-dev 12.6.68 2 nvidia
cuda-nvprof 12.6.68 0 nvidia
cuda-nvprune 12.6.68 0 nvidia
cuda-nvrtc 12.6.68 0 nvidia
cuda-nvrtc-dev 12.6.68 0 nvidia
cuda-nvrtc-static 12.6.68 0 nvidia
cuda-nvtx 12.6.68 0 nvidia
cuda-nvvp 12.6.68 0 nvidia
cuda-opencl 12.6.68 0 nvidia
cuda-opencl-dev 12.6.68 0 nvidia
cuda-profiler-api 12.6.68 0 nvidia
cuda-runtime 12.6.1 0 nvidia
cuda-sanitizer-api 12.6.68 0 nvidia
cuda-toolkit 12.4.1 0 nvidia
cuda-tools 12.6.1 0 nvidia
cuda-version 12.6 3 nvidia
cuda-visual-tools 12.6.1 0 nvidia
cudnn 8.9.7.29 h092f7fd_3 conda-forge

This my nvidia-smi:
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.183.01 Driver Version: 535.183.01 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 4070 ... Off | 00000000:01:00.0 On | N/A |
| N/A 38C P8 4W / 55W | 17MiB / 8188MiB | 0% Default |

So even if i installed dlib with the right CUDA VERSION, i cannot use the model with the gpu.
Do you have any suggestion?

@semajbaker
Copy link

semajbaker commented Sep 23, 2024 via email

@giampierobartolomei
Copy link

@semajbaker
Copy link

semajbaker commented Sep 24, 2024 via email

@giampierobartolomei
Copy link

Thank you for your help, now it works! I just solved by installing latest NVIDIA driver after instaling cuda and cudnn on my local machine.

@semajbaker
Copy link

semajbaker commented Sep 24, 2024 via email

@YBachmann
Copy link

Nice guide! In my case I had to set export CUDAHOSTCXX=/usr/bin/g++ because otherwise I got the following error:

-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- *****************************************************************************************************************
-- *** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA. 
-- *** The output of the failed CUDA test compile is shown below: 
-- *** 
-- ***   Change Dir: /home/yannic/dlib/build/temp.linux-x86_64-3.10/dlib_build/cuda_test_build
   ***   
   ***   Run Build Command(s):/usr/bin/gmake -f Makefile && [ 50%] Building NVCC (Device) object CMakeFiles/cuda_test.dir/cuda_test_generated_cuda_test.cu.o
   ***   nvcc warning : Support for offline compilation for architectures prior to '<compute/sm/lto>_75' will be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
   ***   cc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory
   ***   compilation terminated.
   ***   nvcc fatal   : Failed to preprocess host compiler properties.
   ***   CMake Error at cuda_test_generated_cuda_test.cu.o.cmake:216 (message):
   ***     Error generating
   ***     /home/yannic/dlib/build/temp.linux-x86_64-3.10/dlib_build/cuda_test_build/CMakeFiles/cuda_test.dir//./cuda_test_generated_cuda_test.cu.o
   ***   
   ***   
   ***   gmake[2]: *** [CMakeFiles/cuda_test.dir/build.make:77: CMakeFiles/cuda_test.dir/cuda_test_generated_cuda_test.cu.o] Error 1
   ***   gmake[1]: *** [CMakeFiles/Makefile2:82: CMakeFiles/cuda_test.dir/all] Error 2
   ***   gmake: *** [Makefile:91: all] Error 2
   ***   
   ***   
-- *****************************************************************************************************************
-- Disabling CUDA support for dlib.  DLIB WILL NOT USE CUDA

@MichaelTarasov
Copy link

The BEST manual!
It's works!
Thank You very much!
Win 11.

@sickybee
Copy link

i've added some tweaks in my build
-install cuda 12.8
-install vs_BuildTools ->install Clang
-install intel onemkl
-install OpenBLAS

  • cmake .. -DDLIB_USE_MKL_WITH_TBB=1 -DDLIB_USE_BLAS=1 -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1 -DUSE_SS2_INSTRUCTIONS=1 -DUSE_SS4_INSTRUCTIONS=1 -DCUDAToolkit_ROOT=E:\Source_Codes\WhoIsShe.conda\Library\bin\

  • activate conda environment

  • pip uninstall dlib #uninstall previous dlib

  • python setup.py install --set DLIB_USE_CUDA=1

DLIB CUDA is available
DLIB BLAS is available
DLIB LAPACK is available
DLIB AVX is available

... and now face_recognition webcam face blur works flawlessly in my GTX 970

@Rapcole12
Copy link

Rapcole12 commented Jul 9, 2025

for windows user who met the import dlib Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\oceanx\AppData\Local\Programs\Python\Python310\lib\site-packages\dlib-19.24.99-py3.10-win-amd64.egg\dlib\__init__.py", line 19, in <module> from _dlib_pybind11 import * ImportError: DLL load failed while importing _dlib_pybind11: The specified module could not be found. check your __init__.py , make sure its "on"=="on".

Thank you so much! I was wondering what was going on. Also make sure for anyone getting an error for not being able to find specific header files to ensure that your file paths are easily readable by C++ compilers. For example: If i have a apostrophe in my file path ex. C:\Go'Up, dlib will say it cannot find some header files when compiling for CUDA. But if i have ex. C:Go_Up, the problem is solves

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment