Skip to content

chore(devicePicker): handle more and return device type#113

Merged
Luthaf merged 5 commits intometatensor:mainfrom
HaoZeke:tdevpicker
Nov 14, 2025
Merged

chore(devicePicker): handle more and return device type#113
Luthaf merged 5 commits intometatensor:mainfrom
HaoZeke:tdevpicker

Conversation

@HaoZeke
Copy link
Copy Markdown
Member

@HaoZeke HaoZeke commented Nov 3, 2025

I really like #90 however I realized in TheochemUI/eOn#262 that it might be more ergonomic to pass around the devicetype instead of the string representation. The Python side should still get the str of course.

Plus this adds the rest of the devices supported by Torch, except the privateuse thing which sounds kinda scammy.

Contributor (creator of pull-request) checklist

  • Tests updated (for new features and bugfixes)?
  • Documentation updated (for new features)?
  • Issue referenced (for PRs that solve an issue)?

Reviewer checklist

  • CHANGELOG updated with public API or any other important changes?

@HaoZeke HaoZeke force-pushed the tdevpicker branch 3 times, most recently from 77c7978 to b23efc2 Compare November 3, 2025 02:34
@HaoZeke HaoZeke requested a review from PicoCentauri November 3, 2025 02:41
Copy link
Copy Markdown
Contributor

@PicoCentauri PicoCentauri left a comment

Choose a reason for hiding this comment

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

Thanks! I am fine with having a different API for C and Python.

Maybe we also ask for a comment by @Luthaf.

Comment thread metatomic-torch/src/register.cpp Outdated
@Luthaf
Copy link
Copy Markdown
Member

Luthaf commented Nov 3, 2025

I'm happy to add support for all other device types in the function checking for availability, but I don't see a point returning a DeviceType instead of a string. This would be a breaking change requiring new releases of everything that depends on metatomic, and downstream code can always create the torch Device themself.

Comment thread metatomic-torch/src/register.cpp Outdated
Comment thread metatomic-torch/src/misc.cpp
Copy link
Copy Markdown
Contributor

@PicoCentauri PicoCentauri left a comment

Choose a reason for hiding this comment

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

Nice. Tests are not happy though :-(

Comment thread metatomic-torch/src/register.cpp Outdated
@HaoZeke HaoZeke force-pushed the tdevpicker branch 4 times, most recently from e0f39ea to d444ce1 Compare November 12, 2025 07:26
@HaoZeke HaoZeke requested a review from PicoCentauri November 12, 2025 07:41
Copy link
Copy Markdown
Contributor

@PicoCentauri PicoCentauri left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

Copy link
Copy Markdown
Member

@Luthaf Luthaf left a comment

Choose a reason for hiding this comment

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

What's the plan for backward compatibility? This is only breaking the C++ API, which should only be used in LAMMPS.

Comment thread metatomic-torch/src/misc.cpp Outdated
Comment thread metatomic-torch/src/misc.cpp Outdated
Comment thread metatomic-torch/src/misc.cpp Outdated
Comment thread metatomic-torch/src/misc.cpp Outdated
Comment thread metatomic-torch/src/register.cpp Outdated
Comment thread python/metatomic_torch/tests/test_pick_device.py Outdated
@PicoCentauri
Copy link
Copy Markdown
Contributor

PicoCentauri commented Nov 12, 2025

Yes, I will fix it asap in lammps.

It should also not break lammps as the version there is pinned

@HaoZeke
Copy link
Copy Markdown
Member Author

HaoZeke commented Nov 12, 2025

What's the plan for backward compatibility? This is only breaking the C++ API, which should only be used in LAMMPS.

PLUMED/EON/GROMACS?

(too much screamcase)

@PicoCentauri
Copy link
Copy Markdown
Contributor

PLUMED not using it
EON - does it
GROMACS - we are not using yet

@HaoZeke HaoZeke force-pushed the tdevpicker branch 2 times, most recently from c821e13 to 9d4126f Compare November 12, 2025 16:30
@HaoZeke HaoZeke requested a review from Luthaf November 12, 2025 16:34
Refactors the C++  function to perform more robust
device detection and normalization.

- Changes the return type from std::string to c10::DeviceType.
- Adds helper functions for string normalization, device availability
  checks (is_known_device, available_device), and mapping to
  the DeviceType enum.
- Adds warnings for unknown or unavailable devices.
- Throws an error on fallback instead of defaulting to CPU.
Moves the Python binding logic for  out of a
complex lambda and into a standalone
function.

- This wrapper handles the conversion from the C++ c10::DeviceType
  to the Python-facing string.
- It strips the device index (e.g., 'cpu:0' -> 'cpu') for
  backwards compatibility.
- Includes robust try/catch blocks to convert c10::Error
  into std::runtime_error for Python.
Copy link
Copy Markdown
Member

@Luthaf Luthaf left a comment

Choose a reason for hiding this comment

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

One final suggestion and then we can merge!

Comment thread metatomic-torch/src/register.cpp Outdated
HaoZeke and others added 2 commits November 14, 2025 15:23
Co-authored-by: Guillaume Fraux <luthaf@luthaf.fr>
- Updates C++ tests in 'misc.cpp' to align with the new
  c10::DeviceType return value.
- Adds a new Python test suite 'test_pick_device.py'.
- Tests basic selection, requested device selection, error
  handling for unavailable devices, and warnings for
  unrecognized devices.
@HaoZeke
Copy link
Copy Markdown
Member Author

HaoZeke commented Nov 14, 2025

Will merge when green ^_^

@Luthaf Luthaf merged commit b317a54 into metatensor:main Nov 14, 2025
7 checks passed
@HaoZeke HaoZeke deleted the tdevpicker branch November 14, 2025 15:26
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.

3 participants