Open
Description
The following F# program crashes:
open TorchSharp
open type torch
open type torch.nn
torch.set_default_device(torch.CUDA)
let embedding = Embedding(2, 3)
let tensor = tensor([|1|])
tensor --> embedding // boom
The error message is: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0!
. This occurs because the embedding is created on the CPU, even though CUDA is set as the default device.
It looks like all modules created in this way ignore the default device, although this doesn't cause a crash for other module types. I'm not sure if this lack of a crash is the expected behavior or not, but it surprised me. For example, the following code works, even though the linear module is created on the CPU:
torch.set_default_device(torch.CUDA)
let linear = Linear(2, 3)
let tensor = tensor([|1.0f; 2.0f|])
tensor --> linear
The output tensor in this case is on the CUDA device, even though the linear module is running on the CPU.