Repository navigation
Closed as not planned
Closed as not planned
Copy link
Labels
Description
Describe the issue:
On Linux x86-64 Python 3.14.0 (installed from uv python, but I haven't tested other Python installations), I find that round-tripping an empty array through pickle is causing it to come back with a pointer that's unaligned.
Reproduce the code example:
import numpy
import pickle
empty = numpy.zeros((0,), dtype='u4')
assert empty.ctypes.data % 4 == 0 # Passes.
roundtrip = pickle.loads(pickle.dumps(empty))
print(f"{roundtrip.ctypes.data:#x}")
assert roundtrip.flags["ALIGNED"] # Passes.
assert roundtrip.ctypes.data % 4 == 0 # Typically fails.Error message:
The roundtripped pointer came back as (e.g.) 0x17fd2a1, and then the final assertion fails.
Python and NumPy Versions:
Python: 3.14.0 from uv
Numpy: 2.3.4 (just pip install)
Runtime Environment:
[{'numpy_version': '2.3.4',
'python': '3.14.0 (main, Oct 14 2025, 21:27:55) [Clang 20.1.4 ]',
'uname': uname_result(system='Linux', node='guinea-pig', release='6.8.0-85-generic', version='#85-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 18 15:26:59 UTC 2025', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3', 'SSE41', 'POPCNT', 'SSE42'],
'not_found': ['AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL',
'AVX512_SPR']}},
{'architecture': 'Nehalem',
'filepath': '/home/jake/code/qiskit/venv314/lib/python3.14/site-packages/numpy.libs/libscipy_openblas64_-8fb3d286.so',
'internal_api': 'openblas',
'num_threads': 4,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.30'}]
Context for the issue:
A relatively minor problem for me: when interoperating with Rust, this causes us to need extra care before taking out Rust slices, because Rust slices require their backing pointer to be non-null and aligned, even for an empty slice.
Activity
jakelishman commentedon Oct 23, 2025
This is specifically related to pickle protocol 5, where Numpy uses the out-of-band
PickleBufferform - I can reproduce the bug with older Pythons (but still only on Linux, it seems) while forcing up the version ofpickleprotocol used.jakelishman commentedon Oct 23, 2025
Ok, so I think the reason that this happens is because the empty array pickles at protocol 5 to cause an empty
bytesarray()to be loaded and used as the backing data buffer of the array, and that empty array's buffer is a singleton fixed by Python during initialisation. On my Linux x86-64 box, that buffer is reliably at an odd-number address for both Python 3.13 and 3.14. On my Intel Mac, it's reliably aligned on an 8.From a bit of playing around, it seems like on both macOS and Linux, any
bytearraythat actually triggers a backing allocation is always aligned to at least the highest required alignment of the platform. It seems to be only the emptybytearray()buffer that may be not aligned for every data type (not to mention its pointer is clearly into a different memory region on Linux), which matches my expectation of memory allocators.For example, given this (hacky - sorry) setup:
On the Linux box I get (Python 3.13 and 3.14) something like
On the macOS machine I get
I'm not sure what a nice path forwards here might be. I can raise a discussion against CPython to see if the empty
bytearraybuffer could be guaranteed to be aligned for all data types, as a convenience for interop with other languages with stricter guarantees about alignment of empty buffers (e.g. Rust, in my case). For Numpy: given how the protocol-5 pickling is currently being done to returnnumpy._core.numeric._frombufferas the reconstructor which will be unreliable for alignment of empty arrays, would you accept a PR that returns a different constructor from__reduce_ex__(5)for pickling empty arrays?ngoldbaum commentedon Oct 24, 2025
Maybe this is a python-build-standalone bug? uv uses a python-build-standalone Python build by default and it does some unusual things that other Python builds don't do. https://github.com/astral-sh/python-build-standalone
jakelishman commentedon Oct 24, 2025
I was able to reproduce with CPython built from source, and I have a CPython patch that fixes it that I'll post over there shortly. Just quickly: the shared buffer of empty
bytearrayis just achar buf[] = "";, which is 1-byte aligned. My patch just forcibly ups it to matchmax_align_t, so it's consistent with any heap pointer you might get frommalloc.I think there's a couple of places in
rust-numpyand PyO3 that might currently be falling afoul of alignment problems too - I'm just checking those out on the side.jakelishman commentedon Oct 24, 2025
I've opened python/cpython#140557 and python/cpython#140559 to propose aligning these objects by default.
Would it still be worth, in Numpy, organising the v5 pickle of empty arrays to always produce aligned arrays too, or would the additional complexity within the pickle impl not be necessary? The downstream places I have in mind need to have special handling to ensure safety anyway - I'm just not sure how many places in the stack should care about this.
ngoldbaum commentedon Oct 24, 2025
Nah, hopefully this will only be an issue on 3.14.0. I'll close this, thanks for all your checking across the ecosystem!
jakelishman commentedon Oct 24, 2025
It's not just 3.14.0 - it's extant in at least the 3.13 series as well, it's just that 3.14 sets the default pickle version to 5, which is the point I detected it.
For example, using Python 3.13.9 on the same Linux box as the issue, but forcibly setting the Pickle protocol:
That said, I don't feel strongly about whether this should be considered a Numpy bug or not - it's arguably not even a CPython bug, just a nice-to-have.
ngoldbaum commentedon Oct 24, 2025
Either way, I'd prefer to rely on CPython having sensible behavior. IMO we should fix it there rather than adding overhead and complexity to NumPy's pickle implementation.