Skip to content

Modules should be created on the default device #1438

Open
@brianberns

Description

@brianberns

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions