Skip to content

Improve pickling support - #2406

Open
ndgrigorian wants to merge 5 commits into
masterfrom
feature/improve-pickling-support
Open

ndgrigorian wants to merge 5 commits into
masterfrom
feature/improve-pickling-support

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

This PR improves the pickling support in dpctl by making SyclContext, SyclDevice, and SyclQueue picklable, with the intention of making downstream projects (i.e., dpnp) that implement objects that use these objects picklable, as well

The device is restored by its filter string, the context by one of the devices, and the queue is restored by the mix of context, device, and properties. If the properties are default properties, the cached queue is used.

Note that for queues, only queues on a root device with the default context can be pickled

MemoryUSM* memory classes now also carry the queue information when pickled, and will fail to pickle with PicklingError for an unpicklable queue

Closes #1716

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?
  • If this PR is a work in progress, are you opening the PR as a draft?

Root devices pickle by filter string and name; unpickling checks the
name. Queues pickle by device and properties and unpickle into the
device's default context; queues on non-default contexts and on
sub-devices raise PicklingError. copy and deepcopy return the same
object.

Closes #1716
MemoryUSM* objects now pickle their queue and unpickle onto it instead
of the default device. Memory whose queue can not be pickled raises
PicklingError. Pickles without a queue still load on the default device.
This makes SyclQueue pickling also pickle the context. For non-picklable (i.e., non-default) contexts, the queue will throw a PicklingError
SyclQueue objects with default properties now unpickle as the cached queue of their device, so unpickled USM memory lands on one queue across separate loads. Queues with properties still unpickle as new queues. Document the pickling limitations on SyclQueue.
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

@coveralls

coveralls commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 74.893% (-0.1%) from 74.989% — feature/improve-pickling-support into master

@antonwolfy antonwolfy left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Built the PR head (96ece36) with DPC++ 2026.2 on a Panther Lake host (Arc B390, devices opencl:gpu:0, opencl:cpu:0, level_zero:gpu:0).

I checked by hand:

  • context, device, queue and USM memory round-trip on every device, and a context survives a second round-trip;
  • a default-property queue is restored as get_device_cached_queue(dev); an in_order queue shared by two objects in one dumps is restored as a single object; USM memory keeps in_order through pickling;
  • old two-argument _to_memory pickles still load, onto the default device;
  • pickle protocols 0-5 all work;
  • loading in a separate process with no ONEAPI_DEVICE_SELECTOR restores the same filter strings.

Two small points are inline: the error type when the device is missing at load time, and the error message for a queue on a sub-device.

One question, which I could not test with a single GPU: the filter string's relative id is counted among the devices visible under the current ONEAPI_DEVICE_SELECTOR. With two identical GPUs, a pickle made with level_zero:1 and loaded without a selector would land on the other GPU, and the name check would not catch it, since the names match. Is that acceptable for now, maybe with a note in the docstring?

Comment thread dpctl/_sycl_device.pyx Resolved
Comment thread dpctl/_sycl_queue.pyx Resolved
Raise UnpicklingError when the pickled device is not available, and name
the queue in PicklingError for sub-device and non-default-context queues.
Document that filter string ids depend on ONEAPI_DEVICE_SELECTOR.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The test allocates every USM kind on every device without checking support, causing USMAllocationError on valid configurations before pickling is exercised.

1 open issue
What changed in this PR

Adds pickling support for SYCL devices, default contexts, and eligible queues, enabling downstream objects that hold them to be serialized.

Changes:

  • Adds reconstruction logic and identity-preserving copy behavior for SYCL objects.
  • Preserves queue information in pickled USM memory, reusing cached queues for default properties.
  • Adds round-trip, rejection, subprocess, and backward-compatibility tests.
File Description
dpctl/​tests/​test_sycl_usm.py Tests queue preservation and legacy memory reconstruction.
dpctl/​tests/​test_sycl_queue.py Tests queue pickling, caching, restrictions, and copying.
dpctl/​tests/​test_sycl_device.py Tests device reconstruction, validation, and copying.
dpctl/​tests/​test_sycl_context.py Tests context pickling restrictions and copying.
dpctl/​tests/​helper/​_helper.py Adds a helper for finding partitionable devices.
dpctl/​tests/​helper/​__init__.py Exports the sub-device helper.
dpctl/​memory/​_memory.pyx Includes queue information in memory serialization.
dpctl/​_sycl_queue.pyx Adds queue serialization and documents reconstruction semantics.
dpctl/​_sycl_device.pyx Adds device serialization with filter and name validation.
dpctl/​_sycl_context.pyx Adds serialization for default root-device contexts.
CHANGELOG.md Documents pickling and copy behavior changes.

Review effort: Balanced

pytest.skip("No SYCL devices available")
for dev in devs:
q = dpctl.SyclQueue(dev, property="in_order")
mobj = memory_ctor(64, queue=q)

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.

Feature request: make SyclQueue picklable

4 participants