Skip to content

Keep the dtype and device in to() if only one of them is given - #143

Open
M-Colley wants to merge 1 commit into
cornellius-gp:mainfrom
M-Colley:fix/to-without-dtype
Open

M-Colley wants to merge 1 commit into
cornellius-gp:mainfrom
M-Colley:fix/to-without-dtype

Conversation

@M-Colley

Copy link
Copy Markdown

Fixes #141.

Problem

_to_helper returns None for the dtype (device) if only a device (dtype) is passed to to(), but several to() overrides did not handle that:

  • InterpolatedLinearOperator.to and MaskedLinearOperator.to raised AttributeError: 'NoneType' object has no attribute 'is_floating_point' for e.g. op.to(device="cpu"). This also broke indexing a CatLinearOperator of such operators, which calls .to(output_device) on the selected operator (Fix scalarize_posterior for interpolated covariance blocks meta-pytorch/botorch#3408).
  • IdentityLinearOperator.to set the dtype (device) to None, so op.to(device) evaluated in the default dtype instead of op.dtype, and op.to(dtype) moved a CUDA operator to the CPU.
  • CatLinearOperator.to set output_device to None if only a dtype was given.

Change

Only move the device if no dtype is given (as #133 proposed for MaskedLinearOperator), and keep the current dtype, device or output device if it is not given.

Tests

  • LinearOperatorTestCase.test_to_device_or_dtype_only calls to() with only a device (positional and keyword) and with only a dtype, and checks dtype, device and values. On main it fails for all 12 Interpolated, Masked, Identity and Cat test classes (75 classes run it).
  • TestInterpolatedLinearOperator.test_getitem_cat indexes a CatLinearOperator of InterpolatedLinearOperators.
  • The Interpolated and Masked overrides also check that index and mask tensors keep their dtypes.

Full suite: 5122 tests pass, except test_psd_safe_cholesky_psd, which also fails on main locally (torch 2.13 on Windows, unrelated). I also checked CUDA round trips (.to(cuda), .to(torch.float32), .to(device="cpu")) for the four operators manually. flake8 passes. Tested with Python 3.13, PyTorch 2.13.

🤖 Generated with Claude Code

_to_helper returns None for the dtype (device) if only a device (dtype)
is passed to to(), but several to() overrides did not handle that:

- InterpolatedLinearOperator.to and MaskedLinearOperator.to evaluated
  dtype.is_floating_point and raised "AttributeError: 'NoneType' object
  has no attribute 'is_floating_point'" for e.g. op.to(device="cpu").
  This also broke indexing a CatLinearOperator of such operators, which
  calls .to(output_device) on the selected operator.
- IdentityLinearOperator.to set the dtype (device) to None, so
  op.to(device) evaluated in the default dtype instead of op.dtype, and
  op.to(dtype) moved a CUDA operator to the CPU.
- CatLinearOperator.to set output_device to None if only a dtype is
  given.

Only move the device if no dtype is given, and keep the current dtype,
device or output device if it is not given.

Add a test to LinearOperatorTestCase that calls to() with only a device
and only a dtype, and a test that indexes a CatLinearOperator of
InterpolatedLinearOperators.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] to() with only a device (or only a dtype) fails or drops the dtype/device for several operators

1 participant