torch::DeviceType device_type;
if (torch::cuda::is_available()) {
std::cout << "Cuda available, running on GPU" << std::endl;
device_type = torch::kCUDA;
} else {
std::cout << "Cuda NOT available, running on CPU" << std::endl;
device_type = torch::kCPU;
}
torch::Device device(device_type);
module->to(torch::Device(device));
I get the message: Cuda available, running on GPU. Nevertheless, when I execute: at::Tensor output = module->forward(inputs).toTensor();
The command consumes way to much time and I can see at the system monitor how all cores are being used and how no new GPU process is opened in the nvidia-smi tool.
I guess I am doing something wrong but I can’t find what. Any ideas?
I am using Cuda 9.0 and built the project from libcudnn7_7.4.2.24-1
I imported:
Sorry, I discovered that before tracing the model in python I didn’t move it to cuda. If I move it, trace it, and reload it in c++, it correctly runs on the GPU.
Still I can’t copy a cv::Mat into a torch::Tensor and move it to the GPU
at::Tensor output = torch::from_blob(img.data, {1, 3, img.rows, img.cols}, torch::kFloat32).clone();
output.to(torch::Device(torch::kCUDA, 0));
output.to(torch::kCUDA); //trying both ways just in case
assert(output.device().type() == torch::kCUDA); // Assertation failed
First I’d recommend using torch::Tensor instead of at::Tensor, as at::Tensor is now an implementation detail and torch::Tensor is the user-facing tensor class.
For the first example, we can try output = output.to(torch::Device(torch::kCUDA, 0)); to see if it works.
Is it normal that running the forward method on the GPU consumes more time than using the CPU? I wrote a simplified version of my code and this still occurs:
Hi Will, sorry for the late reply. Turns out that the first time I run forward it takes ~7s, but if I run it in a loop all the others take ~0.09s (still not as fast as running it from Python which is ~0.02s, but way more decent)
Any idea on what may be causing this? I guess I could just do a warm-up at the beginning and run it once but I am curious.