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

Use a CUDAGuard when running Torch models #340

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

VivekPanyam
Copy link
Collaborator

This PR ensures that we're running on the correct device even if something else calls cudaSetDevice before running inference.

This fixes a class of issues where another piece of code changes the current device for the current thread. For example, this can happen if TF and Torch run together on the same threadpool. TF will call cudaSetDevice and cause torch to break if it runs on the same thread in the future.

This can cause some obscure cuDNN errors and generally hard-to-debug issues.

@@ -291,6 +295,16 @@ std::unique_ptr<NeuropodValueMap> TorchNeuropodBackend::infer_internal(const Neu
{
torch::NoGradGuard guard;

#ifndef __APPLE__
// Make sure we're running on the correct device
std::unique_ptr<at::cuda::CUDAGuard> device_guard;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <memory>

const auto model_device = get_torch_device(DeviceType::GPU);
if (model_device.is_cuda())
{
device_guard = stdx::make_unique<at::cuda::CUDAGuard>(model_device);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can use std:: here not stdx:: because Neuropod became C++14 recently, right?

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