question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scalar on GPU or setting config to Torch/GPU fails

See original GitHub issue

A first call to dsharp.config(backend=Backend.Torch, device=Device.GPU) fails with

System.InvalidOperationException: Torch device type CUDA did not initialise on the current machine.
   at TorchSharp.Torch.InitializeDeviceType(DeviceType deviceType)
   at TorchSharp.Tensor.TorchTensor.ToDevice(DeviceType deviceType, Int32 deviceIndex)
   ...

This is because https://github.com/DiffSharp/DiffSharp/blob/12137670a9aa347a0ac3199b45526c4118a821a7/src/DiffSharp.Core/DiffSharp.fs#L1116

Calls Float64Tensor.From(0.)

https://github.com/xamarin/TorchSharp/blob/bffe20f1221d5e0f2985cef573f393e3c7187238/src/TorchSharp/Tensor/TorchTensorTyped.generated.cs#L2190-L2196

Initializing torch to DeviceType.CPU and committing to the “torch_cpu” library.

A workaround is to make sure GPU is the first used device:

dsharp.tensor([1f], Dtype.Float32, Device.GPU, Backend.Torch)
dsharp.config(backend=Backend.Torch, device=Device.GPU)

Works fine because https://github.com/xamarin/TorchSharp/blob/bffe20f1221d5e0f2985cef573f393e3c7187238/src/TorchSharp/Tensor/TorchTensorTyped.tt#L259-L271

Does not first initialize device type.

Overall it looks like this was introduced in https://github.com/xamarin/TorchSharp/commit/7b3f3ca5c6233eb20010f231cab65178103eeae2

Where an initialization mask was swapped out for a single bool.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dsymecommented, Oct 10, 2021

Note that even after #383 the package restore will not yet unify native binaries into a single place that’s fully auto-loadable from F# interactive and notebooks. Hence there is still the workaround code in torchsharp to facilitate the loading. It works but feels fragile and is not under CI automated test, I wouldn’t be surprised if we end up back here.

0reactions
gbaydincommented, Oct 10, 2021

Also the recently merged #383 should significantly improve the reliability of loading native components as TorchSharp and libtorch native libraries are packaged together which helps them to be found together in runtime.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to pass too many CPU scalars to CUDA kernel!
Hello,. I have been trying to use either pytorch 1.6.0 rc or pytorch nightly (Currently torch-1.7.0.dev20200702+cu101 ) in order to get ...
Read more >
torch.where does not handle scalar type correctly #73298
I am expecting torch.where(ab, af, 3.0) to work fine, because PyTorch only support double scalar, not float scalar. It does not make any...
Read more >
RuntimeError: Input type (torch.FloatTensor) and weight ...
You get this error because your model is on the GPU, but your data is on the CPU. So, you need to send...
Read more >
Model is not properly moved to GPU memory with torch. ...
Hi everyone, I'm using OWLViTForObjectDetection model and I want to perform inference on the GPU. So what I'm doing is something like: model ......
Read more >
Developer Guide :: NVIDIA Deep Learning TensorRT ...
This NVIDIA TensorRT Developer Guide demonstrates how to use the C++ and Python APIs for implementing the most common deep learning layers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found