Skip to content

Create a System class in Python - #325

Open
EricBoittier wants to merge 3 commits into
metatensor:metatomic-corefrom
EricBoittier:python-wrap-system
Open

EricBoittier wants to merge 3 commits into
metatensor:metatomic-corefrom
EricBoittier:python-wrap-system

Conversation

@EricBoittier

@EricBoittier EricBoittier commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

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?

@EricBoittier
EricBoittier changed the base branch from main to metatomic-core September 15, 2026 07:32
@Luthaf Luthaf changed the title Add an empty metatomic-core python package, re-exporting metatomic-torch Create a System class in Python Sep 15, 2026
@EricBoittier
EricBoittier marked this pull request as ready for review September 15, 2026 11:15
Comment thread docs/src/core/reference/python/system.rst Outdated
Comment thread metatomic-core/CHANGELOG.md Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/tests/system.py Outdated

@EricBoittier EricBoittier left a comment

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.

👀

@EricBoittier
EricBoittier requested a review from Luthaf September 15, 2026 13:23

@Luthaf Luthaf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you check for compatibility with torch.compile/jax.jit?

Comment thread python/metatomic_core/tests/system.py
Comment thread python/metatomic_core/tests/system.py
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/tests/system.py Outdated
Comment thread python/metatomic_core/tests/system.py Outdated
Comment thread python/metatomic_core/tests/system.py Outdated
@EricBoittier

Copy link
Copy Markdown
Contributor Author

Did you check for compatibility with torch.compile/jax.jit?

I generated a notebook to answer these questions (attached as PDF, sorry for LLM-braining
system-compile-jit.pdf
)

It shows:

  • numpy / torch / jax construction and getters work

  • extracted arrays work with torch.compile (fullgraph=True) and jax.jit

  • constructing System inside torch.compile(..., fullgraph=True) fails on ctypes POINTER.new and ctypes graph-breaks to eager if fullgraph!=True

  • jax.jit rejects System; constructing inside jit fails (dlpack) since metatomic.System is a host-side ctypes wrapper around mta_system_t, not a traced model type.

@Luthaf

Luthaf commented Sep 17, 2026

Copy link
Copy Markdown
Member

I'd say it's fine for now if we can not construct a system inside a torch.compile/jax.jit bit of code, but it would be nice to be able to compile a function like squared_sum_positions(system): return torch.sum(system.positions)**2. Looks like this works for torch, how hard would it be to make it work for jax? We don't have to do it in this PR, just make sure we are not painting ourself in a corner!

@EricBoittier
EricBoittier requested a review from Luthaf September 18, 2026 09:48

@Luthaf Luthaf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not understand the new requirement of releasing all arrays views before adding pairs/custom data. Can you explain?

Comment thread docs/src/core/reference/python/system.rst
Comment thread metatomic-core/include/metatomic.h Outdated
Comment thread metatomic-core/src/c_api/system.rs
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread python/metatomic_core/src/metatomic/_system.py Outdated
Comment thread tox.ini Outdated
@EricBoittier

Copy link
Copy Markdown
Contributor Author

I do not understand the new requirement of releasing all arrays views before adding pairs/custom data. Can you explain?

The two APIs do not share buffers.
add_pairs only does system_mut.pairs.insert(...) (system.rs 371).
They're coupled because mta_system_get_data clones the Arc into manager_ctx (c_api/system.rs 191–209). add_pairs / add_custom_data go through Arc::get_mut (281–285 / 408–412), so any live getter view makes strong_count > 1, so mutation fails

We can keep this, or allow adds with live views (interior mutability on pairs / custom_data only). Which do you want?

On the topic of mutability, jax arrays strictly immutable. In torch, arrays may still look writable depending on version; the C comment on get_data is still “do not modify” (c_api/system.rs 168–170).
I would go for immutable, even if pytorch doesn't strictly require it.

@Luthaf

Luthaf commented Sep 23, 2026

Copy link
Copy Markdown
Member

Ah, right there is the Arc::get_mut thing. Ok, makes sense then!

On the topic of mutability, jax arrays strictly immutable. [...]

This should already be handled by DLPack, which has a flag for immutable data. So unless we gain something by specifying it ourself, I'd leave read-only considerations to DLPack and not mention it ourself.

Comment thread metatomic-core/tests/system.cpp Outdated
Comment on lines +324 to +327
if module.startswith("torch") and name == "Tensor":
return "torch"
if module.startswith("jax") or module.startswith("jaxlib"):
return "jax"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Still relevant

Comment thread python/metatomic_core/src/metatomic/_system.py
raise ValueError(f"Unknown arrays backend: {backend}")


def _pair_options_json(options: PairListOptions) -> bytes:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

not worth a helper IMO

Comment on lines +607 to +608
The returned array uses the configured :py:attr:`arrays_backend` and is
a read-only view.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
The returned array uses the configured :py:attr:`arrays_backend` and is
a read-only view.
The returned array uses the configured :py:attr:`arrays_backend`.

and same below

Comment thread python/metatomic_core/tests/system.py Outdated
Comment thread python/metatomic_core/tests/system.py
Comment thread python/metatomic_core/tests/system.py Outdated
Comment thread python/metatomic_core/tests/system.py Outdated
Comment thread tox.ini Outdated
@Luthaf
Luthaf force-pushed the metatomic-core branch 2 times, most recently from 9b44483 to 15fd62d Compare September 29, 2026 15:12
@Luthaf
Luthaf force-pushed the metatomic-core branch 2 times, most recently from 5ec31c2 to 09b9cf2 Compare October 1, 2026 10:54
@EricBoittier
EricBoittier requested a review from Luthaf October 1, 2026 13:24
@Luthaf
Luthaf force-pushed the python-wrap-system branch 2 times, most recently from 38c8afa to 68614de Compare October 1, 2026 13:45
@Luthaf
Luthaf force-pushed the python-wrap-system branch from 68614de to 33fd484 Compare October 1, 2026 13:51
@Luthaf

Luthaf commented Oct 1, 2026

Copy link
Copy Markdown
Member

I rebased and squashed all commits into a single one, but there are still some unresolved review comments here.

to pull the rebased changes:

git checkout python-wrap-system
git fetch
git reset --hard origin/python-wrap-system # (assuming the remote it called origin)

EricBoittier and others added 2 commits October 1, 2026 16:36
Co-authored-by: Cursor <cursoragent@cursor.com>
Added comments to clarify the use of `ManuallyDrop` in `mta_system_add_pairs` and `mta_system_add_custom_data` functions, ensuring that the caller's system remains valid even when these functions fail. Introduced a new test case to verify that the system is not dropped on failure during pair and custom data additions.
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