Skip to content

Replace use_cuda with device parameter in AnnLoader#2360

Open
LiudengZhang wants to merge 5 commits into
scverse:mainfrom
LiudengZhang:feat/device-parameter
Open

Replace use_cuda with device parameter in AnnLoader#2360
LiudengZhang wants to merge 5 commits into
scverse:mainfrom
LiudengZhang:feat/device-parameter

Conversation

@LiudengZhang
Copy link
Copy Markdown
Contributor

Summary

  • Replace use_cuda: bool with device: str = "cpu" in default_converter and AnnLoader.__init__, addressing the TODO comment in the code (# maybe replace use_cuda with explicit device option).
  • use_cuda is kept for backward compatibility but emits a FutureWarning when explicitly passed. Passing both use_cuda and device raises a ValueError.
  • pin_memory logic is now correctly guarded to only apply on CPU (it is a CPU-only operation).

Motivation

The existing use_cuda boolean is limiting — it only supports CPU or CUDA. The new device parameter accepts any PyTorch device string (e.g., "cpu", "cuda", "cuda:1", "mps"), making the API more flexible and consistent with PyTorch conventions.

Test plan

  • test_annloader_default_device — default device="cpu" produces CPU tensors
  • test_annloader_explicit_cpu_device — explicit device="cpu" works
  • test_annloader_use_cuda_deprecation_warninguse_cuda emits FutureWarning
  • test_annloader_use_cuda_and_device_conflict — both params raises ValueError

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 9, 2026

Codecov Report

❌ Patch coverage is 21.42857% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.35%. Comparing base (a0c4283) to head (a520fbc).
⚠️ Report is 27 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/anndata/experimental/pytorch/_annloader.py 21.42% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2360      +/-   ##
==========================================
- Coverage   87.36%   85.35%   -2.01%     
==========================================
  Files          49       49              
  Lines        7785     7790       +5     
==========================================
- Hits         6801     6649     -152     
- Misses        984     1141     +157     
Files with missing lines Coverage Δ
src/anndata/experimental/pytorch/_annloader.py 23.14% <21.42%> (-0.16%) ⬇️

... and 8 files with indirect coverage changes

Copy link
Copy Markdown
Member

@flying-sheep flying-sheep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! Some small notes from me

use_cuda: bool = _UNSET,
**kwargs,
):
if use_cuda is not _UNSET:
Copy link
Copy Markdown
Member

@flying-sheep flying-sheep Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a standard Empty.TOKEN object for this:

class Empty(Enum):
TOKEN = auto()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — switched to Empty.TOKEN in 6f01e74.

shuffle: bool = False,
use_default_converter: bool = True,
use_cuda: bool = False,
device: str = "cpu",
Copy link
Copy Markdown
Member

@flying-sheep flying-sheep Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use a Literal type enumerating all valid values. (maybe imported from torch if it exists there)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — used Literal["cpu", "cuda", "mps"] since torch doesn't export a device string literal. Fixed in 6f01e74.

@LiudengZhang
Copy link
Copy Markdown
Contributor Author

Hi @flying-sheep, I've addressed both points (switched to Empty.TOKEN and used Literal type) in 6f01e74. Let me know if there's anything else!

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.

2 participants