Skip to content
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

Support for runtime CPU/CUDA selection #3060

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kSkip
Copy link
Contributor

@kSkip kSkip commented Feb 26, 2025

This PR adds support for selecting either the CPU or CUDA implementation of the dnn module at runtime. As discussed in #1852, the #ifdef DLIB_USE_CUDA directives in tensor_tools.cpp just need to be replaced with a runtime branch like

if (use_cuda())
    cuda::function();
else
    cpu::function();

The thread local variable returned by use_cuda() is initialized to be consistent with the current behavior. If DLIB_USE_CUDA=True the function, use_cuda() will return true by default.

I found while making these changes that is was necessary to keep the preprocessor directives because the cuda namespace cannot be referenced when CUDA is not linked. Additionally, the pooling and convolution classes in tensor_tools.h have instances of both implementations as members. I couldn't find a cleaner way (with limited time) to handle those cases.

@davisking
Copy link
Owner

Nice, let me test this a bit and look it over.

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