Skip to content

Add optional DLPack/pybind11 interoperability layer for coupling IPPL with Python - #617

Open
nmamic wants to merge 16 commits into
IPPL-framework:masterfrom
nmamic:GSP_dev
Open

nmamic wants to merge 16 commits into
IPPL-framework:masterfrom
nmamic:GSP_dev

Conversation

@nmamic

@nmamic nmamic commented Oct 15, 2026

Copy link
Copy Markdown

Summary

Adds an optional interoperability layer (src/Interoperability, namespace
ippl::Interoperability) that lets IPPL share particle data with Python frameworks with no copying.

  • KokkosDLPack.h: exports a Kokkos::View as a DLPack capsule, zero-copy, in the View's memory space.
  • IpplDLPack.h: exports ParticleAttrib data.
  • InferenceBridge.h: loads a Python module through an embedded interpreter ( via pybind11), calls its functions with arbitrary arguments.
  • demos/alpine/LandauDampingNeurOp: LandauDamping where the electric field is inferred by a Python model every step instead of computed by the PIC field solver.

Build

It is opt-in with -DIPPL_ENABLE_PYTHON_BRIDGE=ON (default OFF). pybind11 is used if installed and fetched otherwise (-DPYBIND11_VERSION). DLPack (just a header, C ABI) is fetched (-DDLPACK_VERSION). -DPython_EXECUTABLE=<python> selects the Python environment to embed. The interoperability headers are not installed when the option is OFF.

Execution space, memory space and data movement

  • Exports alias the attribute's memory in its own memory space, so there are no host transfers and no copies.
  • Exports are rank-local (not collective!) and don't fence, so the caller fences before exporting, and the consumer synchronizes before IPPL uses the data again.
  • An exported tensor is valid until the next particle update/create/destroy/resize (since the DLPack capsule is just a description of the data, meaning that changing it makes the description stale).
  • Doxygen comments in the headers.

Physics / numerics impact

None on existing IPPL code: no existing kernels, floating-point behaviour or MPI/GPU
execution order are changed.

Testing

New unit test unit_tests/Interoperability/DLPackExport (GoogleTest, typed over all
execution spaces * float/double * Dim 1–3). Through InferenceBridge, it exports
ParticleAttribs to PyTorch and checks:

  • device, dtype, shape, strides, zero-copy data pointer and values as seen by torch,
  • that an in-place write from torch is visible on the IPPL side,
  • that torch tensors keep the allocation alive until Python releases them.

The test is only registered if the embedded Python can import torch.

Ran on JUWELS Booster after merging the current master: 36/36 tests passed on 2 MPI ranks.

mamic1 and others added 16 commits September 22, 2026 15:41
added files/directories:
- interop/: header-only:
	 KokkosDLPack.h (Kokkos::View -> DLPack capsule, zero-copy),
	 IpplDLPack.h (ippl ParticleAttrib adapter)
	 InferenceBridge.h (embedded python interpreter, sets the init/infer/finalize contract), exposed as INTERFACE target IPPL::python_bridge
- cmake:
	IPPL_ENABLE_PYTHON_BRIDGE option (default OFF); pybind11 and
  	DLPack resolved in Dependencies.cmake with overridable PYBIND11_VERSION / DLPACK_VERSION
- demos/alpine: LandauDampingNeurOp, numeric field solve with par2grid/grid2par replaced by surrogate inference in pytorch
- InferenceBridge: replaced fixed init/infer methods with
  call(fn, args...), a variadic template that perfect-forwards arguments to the Python function and returns result as py::object. This means the bridge doesn't mandate the python side API except finalize()

- guarded(): added catch for std::exception like from py::cas_error from unknown argument conversion (like a user defined class)
- demos/alpine: LandauDampingNeurOp is built with hidden symbol visibility
  (CXX_VISIBILITY_PRESET hidden, VISIBILITY_INLINES_HIDDEN), recommended
  recommended by pybind11 for embedding code; removes -Wattributes
  warning during compilation. Visibility attribute in the header is dropped, so its compiler-neutral.
@nmamic
nmamic requested a review from srikrrish October 15, 2026 23:35
@klappi-s

klappi-s commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

@srikrrish would this not somewhat best belong into the Stream directory? (so src/Stream/Interoperability).

Also to not overfill our main demo, do we want to consider again a separate demo folder for neursl operators (sth like demos/alpineNO.

@srikrrish

Copy link
Copy Markdown
Member

@srikrrish would this not somewhat best belong into the Stream directory? (so src/Stream/Interoperability).

Also to not overfill our main demo, do we want to consider again a separate demo folder for neursl operators (sth like demos/alpineNO.

Yeah agree to both. I haven't still reviewed this. Will do it soon and put these in the comments. Thanks

This branch has not been deployed

No deployments
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