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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions source/neuropod/backends/torchscript/torch_backend.cc
Original file line number Diff line number Diff line change
@@ -7,6 +7,10 @@
#include "neuropod/backends/torchscript/type_utils.hh"
#include "neuropod/internal/tensor_types.hh"

#ifndef __APPLE__
#include <c10/cuda/CUDAGuard.h>
#endif

#include <caffe2/core/macros.h>

#include <iostream>
@@ -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?

}
#endif

// Get inference schema
const auto &method = model_->get_method("forward");
const auto &schema = SCHEMA(method);