question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValueError continuous memory view and 0-size numpy array

See original GitHub issue

A ValueError is raised if one gives a numpy array with shape (2, 0, 1) to such Cython functions with a 3d memoryview as argument:

cimport numpy as np
import numpy as np
np.import_array()

cpdef myfunc3d(np.float64_t[:, :, ::1] arr):
    if arr.size > 0:
        print(arr[0, 0, 0])
    else:
        print('size == 0')

The code to get the error:

import numpy as np
arr = np.ones((2, 0, 1))
print(arr.flags)
myfunc3d(arr)

which gives:

C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-81a397764d36> in <module>()
      1 arr = np.ones((2, 0, 1))
      2 print(arr.flags)
----> 3 myfunc3d(arr)

_cython_magic_5dfab2d7f2a0e837d79c56566c9147b4.pyx in _cython_magic_5dfab2d7f2a0e837d79c56566c9147b4.myfunc3d()

ValueError: Buffer and memoryview are not contiguous in the same dimension.
In [ ]:

Strangely, no error is raised for a shape (1, 0, 1)!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
c-f-hcommented, Jul 6, 2020

I ran into this too. Here’s another minimal example to reproduce this:

cdef void foo(double[:, :, ::1] X):
    pass

import numpy as np
X = np.zeros((4,4,4))

# this works
foo(np.ascontiguousarray(X[:, 2:3, :]))

# this fails with 'ValueError: Buffer and memoryview are not contiguous in the
# same dimension.'
foo(np.ascontiguousarray(X[:, 2:2, :]))

The problem is that __Pyx_ValidateAndInit_memviewslice does not correctly realize that a 0-size array is always C-contiguous.

This crops up in the wild, too. A search for the error message reveals many instances where people inadvertently passed 0-size arrays to cython-implemented functions and got confusing error messages. E.g.: https://stackoverflow.com/questions/46470492/buffer-and-memoryview-are-not-contiguous-in-the-same-dimension-in-python

0reactions
scodercommented, Jul 6, 2020

Yes, that’s a bug. It should behave like NumPy. PR welcome. Tests can go into tests/memoryview/memslice.pyx, there are several contiguity tests already.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError: When changing to a larger dtype, its size must be ...
You are trying to create a view on an array with an incompatible memory layout, where the output dtype has an itemsize that...
Read more >
Look Ma, No For-Loops: Array Programming With NumPy
Technical Detail: The smaller-sized array or scalar is not literally stretched in memory: it is the computation itself that is repeated. This extends...
Read more >
numpy.ascontiguousarray — NumPy v1.24 Manual
Return a contiguous array (ndim >= 1) in memory (C order). Parameters: ... Reference object to allow the creation of arrays which are...
Read more >
Typed Memoryviews — Cython 3.0.0a11 documentation
Typed Memoryviews¶. Typed memoryviews allow efficient access to memory buffers, such as those underlying NumPy arrays, without incurring any Python overhead ...
Read more >
Arithmetic on Cython Typed Memoryviews - Google Groups
But converting the memory view back to an np.array is slower than tortoises ... ValueError: Buffer and memoryview are not contiguous in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found