ankane / torch.rb

Deep learning for Ruby, powered by LibTorch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Torch#zeros/ones_like methods lose the device affinity

orlando-labs opened this issue · comments

commented

Hi, @ankane.
As I found out recently, on the latest libtorch/torch.rb version (1.11.0/0.10.0) *_like methods lose the device affinity:

require 'torch-rb'; Torch.ones(5).to('cuda:0').then { |t| [t.device, Torch.zeros_like(t).device] }
=> ["cuda:0", "cpu"]

Meanwhile, the same version with python works well:

import torch; t = torch.ones(5).cuda(); [t.device, torch.zeros_like(t).device]                                                                                                                                       
[device(type='cuda', index=0), device(type='cuda', index=0)]

The only older version of torch.rb I'm able to test is 0.8.3 build on top on libtorch 1.9.0, and it produces the expected result:

require 'torch-rb'; Torch.ones(5).cuda.then { |t| [t.device, Torch.zeros_like(t).device] }
 => ["cuda:0", "cuda:0"]

Given misbehaviour, e. g., totally breaks optimizers when using GPUs.

Hey @orlando-labs, thanks for reporting! It looks like this was broken for like_* methods in 0.9.1 when they were moved to C++. Fixed in the commit above, and will push a new release shortly.