Skip to content

Commit

Permalink
make indices a buffer in AffineInputTransform (pytorch#1656)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#1656

see title. This is important for being able to set the device with `to()`

Differential Revision: https://internalfb.com/D43048429

fbshipit-source-id: 4f4150255c9856eb9a98e4f151577b68a1fd54fa
  • Loading branch information
sdaulton authored and facebook-github-bot committed Feb 6, 2023
1 parent 4fc59a4 commit 1ddfb53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion botorch/models/transforms/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def __init__(
raise ValueError("Elements of `indices` have to be smaller than `d`!")
if len(indices.unique()) != len(indices):
raise ValueError("Elements of `indices` tensor must be unique!")
self.indices = indices
self.register_buffer("indices", indices)
torch.broadcast_shapes(coefficient.shape, offset.shape)

self._d = d
Expand Down
20 changes: 17 additions & 3 deletions test/models/transforms/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ def test_normalize(self):
self.assertEqual(nlz.mins.shape, torch.Size([1, 1]))
self.assertEqual(nlz.ranges.shape, torch.Size([1, 1]))
self.assertEqual(len(nlz.indices), 1)
self.assertTrue((nlz.indices == torch.tensor([0], dtype=torch.long)).all())
nlz.to(device=self.device)
self.assertTrue(
(
nlz.indices
== torch.tensor([0], dtype=torch.long, device=self.device)
).all()
)

# test .to
other_dtype = torch.float if dtype == torch.double else torch.double
Expand Down Expand Up @@ -382,17 +388,25 @@ def test_standardize(self):
self.assertEqual(stdz.means.shape, torch.Size([1, 1]))
self.assertEqual(stdz.stds.shape, torch.Size([1, 1]))
self.assertEqual(len(stdz.indices), 1)
stdz.to(device=self.device)
self.assertTrue(
torch.equal(stdz.indices, torch.tensor([0], dtype=torch.long))
torch.equal(
stdz.indices,
torch.tensor([0], dtype=torch.long, device=self.device),
)
)
stdz = InputStandardize(d=2, indices=[0], batch_shape=torch.Size([3]))
stdz.to(device=self.device)
self.assertTrue(stdz.training)
self.assertEqual(stdz._d, 2)
self.assertEqual(stdz.means.shape, torch.Size([3, 1, 1]))
self.assertEqual(stdz.stds.shape, torch.Size([3, 1, 1]))
self.assertEqual(len(stdz.indices), 1)
self.assertTrue(
torch.equal(stdz.indices, torch.tensor([0], dtype=torch.long))
torch.equal(
stdz.indices,
torch.tensor([0], device=self.device, dtype=torch.long),
)
)

# test jitter
Expand Down

0 comments on commit 1ddfb53

Please sign in to comment.