Skip to content
This repository was archived by the owner on Sep 18, 2025. It is now read-only.

[MERGE] Fixes wrong shape and stack allocation in BufferRegion#144

Merged
quasiben merged 9 commits intorapidsai:develfrom
madsbk:fix_memoryview
Aug 19, 2019
Merged

[MERGE] Fixes wrong shape and stack allocation in BufferRegion#144
quasiben merged 9 commits intorapidsai:develfrom
madsbk:fix_memoryview

Conversation

@madsbk
Copy link
Copy Markdown
Member

@madsbk madsbk commented Aug 15, 2019

len(memoryview.shape) might not equal memoryview.ndim

All tests in tests/test_buffer_region.py now passes on my installation

Update: the returned shape and strides in __buffer__ is now allocated on the heap.

@madsbk madsbk changed the title Fixes BufferRegion.populate_ptr() Fixes wrong shape in BufferRegion.populate_ptr() Aug 15, 2019
@madsbk madsbk changed the title Fixes wrong shape in BufferRegion.populate_ptr() [REVIEW]Fixes wrong shape in BufferRegion.populate_ptr() Aug 15, 2019
@jakirkham
Copy link
Copy Markdown
Member

len(memoryview.shape) might not equal memoryview.ndim

Very interesting! When is that not true?

@madsbk
Copy link
Copy Markdown
Member Author

madsbk commented Aug 15, 2019

len(memoryview.shape) might not equal memoryview.ndim

Very interesting! When is that not true?

On my installation :P
I don't know why, but pyobj.shape[i] was [2,0,0,0,0,0,0] in one of the test in tests/test_buffer_region.py

Copy link
Copy Markdown
Member

@quasiben quasiben left a comment

Choose a reason for hiding this comment

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

@madsbk Thanks for taking a look at this!

@quasiben
Copy link
Copy Markdown
Member

@madsbk is it possible to add something which can explicitly test this ? Or are the tests in buffer_region.py enough ?

@jakirkham
Copy link
Copy Markdown
Member

jakirkham commented Aug 15, 2019

len(memoryview.shape) might not equal memoryview.ndim

Very interesting! When is that not true?

On my installation :P
I don't know why, but pyobj.shape[i] was [2,0,0,0,0,0,0] in one of the test in tests/test_buffer_region.py

Ah right! Yeah sometimes C code will allocate a buffer larger than the needed shape. Good catch. Sorry forgot about this.

@quasiben
Copy link
Copy Markdown
Member

These tests pass for me -- @Akshay-Venkatesh should we merge ?

@Akshay-Venkatesh
Copy link
Copy Markdown
Contributor

Also, can someone ELI5 what was the problem exactly and what this PR is doing?

@quasiben
Copy link
Copy Markdown
Member

@Akshay-Venkatesh I can try but I am not as familiar with buffer protocols as @jakirkham / @madsbk @kkraus14 . On my machine (and @madsbk as well) pyobj, in populate_ptr looks like the following:

[2, 0, 0, 0, 0, 0, 0, 0]

@jakirkham suggested that, " C code will allocate a buffer larger than the needed shape" . The only reference I could find about this is in the numpy docs (look at the shape definition). The cython docs recommend simply handling the 1st dimension of shape:

self.shape = pyobj.shape[0]

For reference in a python session we see the following:

In [1]: memoryview(b'hi').shape                                                                                                                                                       
Out[1]: (2,)

In [2]: memoryview(b'hi').ndim                                                                                                                                                        
Out[2]: 1

I confirmed the large buffer size by putting a print statement in the cpdef _populate_ptr definition.

@quasiben
Copy link
Copy Markdown
Member

If there are no objects can we/I merge this tomorrow ?

@madsbk madsbk changed the title [REVIEW]Fixes wrong shape in BufferRegion.populate_ptr() [MERGE] Fixes wrong shape in BufferRegion.populate_ptr() Aug 17, 2019
@madsbk
Copy link
Copy Markdown
Member Author

madsbk commented Aug 17, 2019

The shape is now a tuple instead of a list.
@quasiben, I think it is ready for merging now.

@quasiben
Copy link
Copy Markdown
Member

Apologies for hastily rushing this along. I tested this PR with test_custom_send_recv and got some errors:

>   self.shape[0] = len
E   TypeError: 'tuple' object does not support item assignment

ucp/_libs/ucp_py_buffer_helper.pyx:233: TypeError

In various functions inside the buffer helper, ucx-py is assigning shape and this fails if shape is a tuple.
For example:

def alloc_host(self, Py_ssize_t len):
self.buf = allocate_host_buffer(len)
self._is_cuda = 0
self._mem_allocated = 1
self.shape[0] = len
def alloc_cuda(self, len):
cuda_check()
self.buf = allocate_cuda_buffer(len)
self._is_cuda = 1
self._mem_allocated = 1
self.shape[0] = len

@madsbk @Akshay-Venkatesh what are you're thoughts here ? Should this be fixed in a follow on PR or continued to be worked in this PR ?

@madsbk
Copy link
Copy Markdown
Member Author

madsbk commented Aug 18, 2019

@quasiben, It should be fixed now; can you confirm?

@madsbk madsbk changed the title [MERGE] Fixes wrong shape in BufferRegion.populate_ptr() [MERGE] Fixes wrong shape and stack allocation in BufferRegion Aug 18, 2019
@quasiben
Copy link
Copy Markdown
Member

Thank you @madsbk ! The tests for buffer and send_recv are passing now. I'm going to merge and continue cleaning up tests

@madsbk madsbk deleted the fix_memoryview branch August 20, 2019 08:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants