Skip to content

Commit

Permalink
Fixed index method for Device when no index [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 3, 2024
1 parent 5752d8f commit 6b3f01b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Improved `inspect` for `Device`
- Fixed equality for `Device`
- Fixed `index` method for `Device` when no index

## 0.18.0 (2024-10-22)

Expand Down
2 changes: 1 addition & 1 deletion ext/torch/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void init_device(Rice::Module& m) {
Rice::define_class_under<torch::Device>(m, "Device")
.define_constructor(Rice::Constructor<torch::Device, const std::string&>())
.define_method(
"index",
"_index",
[](torch::Device& self) {
return self.index();
})
Expand Down
6 changes: 5 additions & 1 deletion lib/torch/device.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Torch
class Device
def index
index? ? _index : nil
end

def inspect
extra = ", index: #{index.inspect}" if index != -1
extra = ", index: #{index.inspect}" if index?
"device(type: #{type.inspect}#{extra})"
end
alias_method :to_s, :inspect
Expand Down
9 changes: 9 additions & 0 deletions test/device_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require_relative "test_helper"

class DeviceTest < Minitest::Test
def test_type
assert_equal Torch.device("cpu:0").type, "cpu"
end

def test_index
assert_nil Torch.device("cpu").index
assert_equal 0, Torch.device("cpu:0").index
end

def test_equal
a = Torch.device("cpu")
b = Torch.device("cpu")
Expand Down

0 comments on commit 6b3f01b

Please sign in to comment.