Skip to content

Support pickle and copy.deepcopy of dpnp.ndarray - #3142

Draft
antonwolfy wants to merge 3 commits into
masterfrom
support-pickle-ndarray
Draft

antonwolfy wants to merge 3 commits into
masterfrom
support-pickle-ndarray

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

pickle.dumps(a) and copy.deepcopy(a) failed on any dpnp.ndarray with TypeError: no default __reduce__ due to non-trivial __cinit__. The default pickling tries to serialize the underlying usm_ndarray together with its SYCL queue, and a SYCL queue couldn't be pickled.

Closes #2602.

import copy, pickle
import dpnp

a = dpnp.arange(6.0, device="gpu").reshape(2, 3)
b = pickle.loads(pickle.dumps(a))  # TypeError before, now on the same device
c = copy.deepcopy(a)               # TypeError before, works now

How pickling works (__reduce__):

  • The data is copied to the host with asnumpy(order="A") and pickled together with the array type, the USM type and the SYCL queue. dpctl can pickle a SyclQueue from 0.24.0dev1 on, so the dpctl dev pins are raised to 0.24.0dev1.
  • Unpickling allocates the array with the original USM type on the queue as dpctl restores it. A queue with default properties comes back as the cached queue of its device. Arrays pickled in one pickle.dumps call that share an "in_order" or "enable_profiling" queue come back on one new queue, so they can still be computed together.
  • As with dpctl's USM memory objects, pickling an array whose queue dpctl can't pickle (the device is a sub-device, or the context isn't the device's default context) raises pickle.PicklingError. Unpickling raises pickle.UnpicklingError if the device isn't available.
  • The dtype is kept: unpickling raises ValueError on a device that doesn't support it instead of silently downcasting float64 to float32 as dpnp.asarray would.
  • The memory layout follows NumPy: an F-contiguous array stays F-contiguous, and anything else becomes C-contiguous.
  • Subclasses of dpnp.ndarray are preserved.

copy.deepcopy (__deepcopy__) behaves like copy.copy: it returns a.copy(order="K") on the array's own queue, with no host round trip. It has to be defined explicitly; otherwise copy.deepcopy would go through __reduce__ and a host round trip.

Docs: ndarray.__reduce__ is now listed in doc/reference/ndarray.rst.

  • 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?

Both failed with "no default __reduce__ due to non-trivial __cinit__"
because a SYCL queue can't be pickled.

`__reduce__` copies the data to the host and pickles it with the array
type and the USM type; unpickling allocates the array on the default
device, keeping the USM type, the dtype (raising on a device that
doesn't support it), the C/F layout and the subclass.

`__deepcopy__` copies on the array's own queue, like `__copy__`, so a
deep copy doesn't go through `__reduce__` to the default device.

Enable the matching CuPy tests and update `test_pickle` in `test_npz.py`
to its current upstream version.
@antonwolfy antonwolfy added this to the 0.22.0 release milestone Nov 1, 2026
@antonwolfy antonwolfy self-assigned this Nov 1, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3142/index.html

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev0=np2py314h8d9cdd5_7 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 82.64% (+0.004%) from 82.636% — support-pickle-ndarray into master

@ndgrigorian

Copy link
Copy Markdown
Collaborator

@antonwolfy
I am working to make SyclQueue picklable under the right conditions. In the future, when that PR is open and then merged to dpctl, we can use this as a fallback for unpicklable arrays (cases where non-default context is used, etc.)

@antonwolfy

Copy link
Copy Markdown
Contributor Author

@ndgrigorian
That sounds great! What do you think we need to wait with adding pickle support to dpnp?
Or it sounds reasonable to add a limited one with the default device/context now and to extend that by later PR, once SyclQueue is pickable?

Comparing to CuPy, they seem also supporting only the default device.

@ndgrigorian

ndgrigorian commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Or it sounds reasonable to add a limited one with the default device/context now and to extend that by later PR, once SyclQueue is pickable?

I think this sounds reasonable, if the dpctl PR ends up open soon (which it may be) we can rework this one slightly

@ndgrigorian

Copy link
Copy Markdown
Collaborator

IntelPython/dpctl#2406 is now open

# Conflicts:
#	CHANGELOG.md
#	dpnp/tests/test_sycl_queue.py
`__reduce__` now pickles the array's queue, which `dpctl` can do from 0.24.0dev1 on, so an unpickled array comes back on its own device instead of the default one. As with `dpctl`'s USM memory, pickling an array whose queue `dpctl` can't pickle (a sub-device, or a context that isn't the device's default one) raises `pickle.PicklingError`.

Raise the `dpctl` dev pins to 0.24.0dev1.
@antonwolfy antonwolfy changed the title Support pickle and copy.deepcopy of dpnp.ndarray Support pickle and copy.deepcopy of dpnp.ndarray Dec 5, 2026

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.

Cannot serialize arrays

3 participants