facebookresearch / theseus

A library for differentiable nonlinear optimization
MIT License
1.78k stars 128 forks source link

`TheseusLayer.to` improvements: Return itself and support `cuda` #622

Closed tvercaut closed 1 year ago

tvercaut commented 1 year ago

🚀 Feature

It would be great to be able to call

theseus_optim = th.TheseusLayer(optimizer).to(torch.device('cuda'))

as well as

theseus_optim = th.TheseusLayer(optimizer, device=torch.device('cuda'))

Currently this fails for two reasons:

Motivation

This would make the use of Theseus more convenient.

Pitch

See above

Alternatives

See above

Additional context

https://github.com/facebookresearch/theseus/blob/9a117fd02867c5007c6686e342630f110e488c65/theseus/theseus_layer.py#L137-L140

Other issues with to have been discussed in:

luisenp commented 1 year ago

This is a good suggestion, thanks for bringing it up! One question is about the expected behavior when passing torch.device("cuda) . Should we automatically pass to "cuda:0" on our side? As you say, this seems to be the case for torch tensors, but for some reason torch.device() doesn't do this automatically. Not sure if this is an oversight on Pytorch's side or a deliberate choice.

luisenp commented 1 year ago

Added feature in #623.