Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion linear_operator/operators/masked_linear_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ def to(
new_kwargs = {}
for arg in self._args:
if hasattr(arg, "to"):
if hasattr(arg, "dtype") and arg.dtype.is_floating_point == dtype.is_floating_point:
if (
dtype is not None
and hasattr(arg, "dtype")
and arg.dtype.is_floating_point == dtype.is_floating_point
):
new_args.append(arg.to(dtype=dtype, device=device))
else:
new_args.append(arg.to(device=device))
Expand Down
8 changes: 8 additions & 0 deletions test/operators/test_masked_linear_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def test_to_double(self):
self.assertEqual(linear_op.col_mask.dtype, torch.bool)
self.assertEqual(linear_op.row_mask.dtype, torch.bool)

def test_to_device(self):
# `.to(device=...)` with no dtype should not raise and should keep masks boolean.
linear_op = self.create_linear_op()
linear_op = linear_op.to(device=torch.device("cpu"))
self.assertEqual(linear_op.device.type, "cpu")
self.assertEqual(linear_op.col_mask.dtype, torch.bool)
self.assertEqual(linear_op.row_mask.dtype, torch.bool)


class TestMaskedLinearOperatorBatch(LinearOperatorTestCase, unittest.TestCase):
seed = 2023
Expand Down