You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "anjakefala (via GitHub)" <gi...@apache.org> on 2024/08/06 17:45:03 UTC

[PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

anjakefala opened a new pull request, #43590:
URL: https://github.com/apache/arrow/pull/43590

   WIP
   
   - [ ] Within Copy, in addition to accepting a MemoryManager as argument to the copy function, we could also accept a Device (see review comments here: https://github.com/apache/arrow/pull/42223)
   - [ ]  add a test in test_io.py where Buffer is being tested, so there is also coverage of this new function outside of the CUDA tests
   - [ ] add CopyNonOwned
   - [ ] add View
   - [ ] add ViewOrCopy
   
   <!--
   Thanks for opening a pull request!
   If this is your first pull request you can find detailed information on how 
   to contribute here:
     * [New Contributor's Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
     * [Contributing Overview](https://arrow.apache.org/docs/dev/developers/overview.html)
   
   
   If this is not a [minor PR](https://github.com/apache/arrow/blob/main/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose
   
   Opening GitHub issues ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
   
   Then could you also rename the pull request title in the following format?
   
       GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   or
   
       MINOR: [${COMPONENT}] ${SUMMARY}
   
   In the case of PARQUET issues on JIRA the title also supports:
   
       PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   -->
   
   ### Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ### What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ### Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ### Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please uncomment the line below and explain which changes are breaking.
   -->
   <!-- **This PR includes breaking changes to public APIs.** -->
   
   <!--
   Please uncomment the line below (and provide explanation) if the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld). We use this to highlight fixes to issues that may affect users without their knowledge. For this reason, fixing bugs that cause errors don't count, since those are usually obvious.
   -->
   <!-- **This PR contains a "Critical Fix".** -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2362161158

   Revision: 931954349a0a558d983958915a7aaa87cb00186d
   
   Submitted crossbow builds: [ursacomputing/crossbow @ actions-9f9dcb6f39](https://github.com/ursacomputing/crossbow/branches/all?query=actions-9f9dcb6f39)
   
   |Task|Status|
   |----|------|
   |test-cuda-python-ubuntu-22.04-cuda-11.7.1|[![GitHub Actions](https://github.com/ursacomputing/crossbow/actions/workflows/crossbow.yml/badge.svg?branch=actions-9f9dcb6f39-github-test-cuda-python-ubuntu-22.04-cuda-11.7.1)](https://github.com/ursacomputing/crossbow/actions/runs/10948764335/job/30400522690)|


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1775334692


##########
python/pyarrow/io.pxi:
##########
@@ -1446,6 +1446,41 @@ cdef class Buffer(_Weakrefable):
         """
         return _wrap_device_allocation_type(self.buffer.get().device_type())
 
+    def copy(self, destination):
+        """
+        Copy the buffer to the destination device.
+
+        The buffer contents will be copied into a new buffer allocated onto
+        the destination MemoryManager or Device. It will return the new Buffer.
+        This function supports cross-device copies.
+
+        Parameters
+        ----------
+        destination : pyarrow.MemoryManager or pyarrow.Device

Review Comment:
   We don't have a way to map a given MemoryPool to a device manager (AFAIK), so in that case you mean to use a different API under the hood to copy to the memory pool? (`Buffer::CopySlice` seems to allow that, or first explicitly allocate a new Buffer of the correct size using the memory pool and then copying the data to it?)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2367632482

   @github-actions crossbow submit test*cuda*py*


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1767463395


##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -119,6 +119,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        assert buf2.device_type == pa.DeviceAllocationType.CUDA

Review Comment:
   AH, sorry :-)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1768909590


##########
python/pyarrow/tests/test_io.py:
##########
@@ -710,6 +710,15 @@ def test_non_cpu_buffer(pickle_module):
     # Sliced buffers with same address
     assert buf_on_gpu_sliced.equals(cuda_buf[2:4])
 
+    # Testing copying buffer
+    mm = ctx.memory_manager
+    for dest in [mm, mm.device]:
+        buf2 = cuda_buf.copy(dest)
+        cuda_buf2 = cuda.CudaBuffer.from_buffer(buf2)
+        assert cuda_buf2.size == cuda_buf.size
+        assert cuda_buf2.copy_to_host()[:] == b'testing'
+        assert cuda_buf2.device == mm.device

Review Comment:
   ```suggestion
           assert buf2.device == mm.device
   ```
   
   To test the actual resulting Buffer of the `copy` method?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1764717499


##########
python/pyarrow/tests/test_io.py:
##########
@@ -710,6 +710,15 @@ def test_non_cpu_buffer(pickle_module):
     # Sliced buffers with same address
     assert buf_on_gpu_sliced.equals(cuda_buf[2:4])
 
+    # Testing copying buffer
+    mm = ctx.memory_manager
+    for dest in [mm, mm.device]:
+        buf2 = cuda_buf.copy(dest)
+        cuda_buf2 = cuda.CudaBuffer.from_buffer(buf2)
+        cuda_buf2.size == cuda_buf.size
+        cuda_buf2.copy_to_host()[:] == b'testing'

Review Comment:
   Are you missing `assert`s here?



##########
python/pyarrow/io.pxi:
##########
@@ -1446,6 +1446,40 @@ cdef class Buffer(_Weakrefable):
         """
         return _wrap_device_allocation_type(self.buffer.get().device_type())
 
+    def copy(self, destination):
+        """
+        Copy the buffer to the destination device.
+
+        The buffer contents will be copied into a new buffer allocated onto
+        the destination MemoryManager or Device. It will return the new Buffer.
+        This function supports cross-device copies.
+
+        Parameters
+        ----------
+        destination : pyarrow.MemoryManager or pyarrow.Device
+            Used to allocate the new buffer.
+
+        Returns
+        -------
+        Buffer
+        """
+        cdef:
+            shared_ptr[CBuffer] c_buffer
+            shared_ptr[CMemoryManager] c_memory_manager
+
+        if isinstance(destination, Device):
+            c_memory_manager = (<Device>destination).unwrap().get().default_memory_manager()
+        elif isinstance(destination, MemoryManager):
+            c_memory_manager = (<MemoryManager>destination).unwrap()
+        else:
+            raise TypeError(
+                "Argument 'destination' is incorrect type (expected pyarrow.Device or "
+                f"pyarrow.MemoryManager, got {type(destination)})"
+            )
+
+        c_buffer = GetResultValue(CBuffer.Copy(self.buffer, c_memory_manager))

Review Comment:
   This should release the GIL since copying is an expensive operation.



##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -119,6 +119,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        assert buf2.device_type == pa.DeviceAllocationType.CUDA

Review Comment:
   Perhaps also try copying directly to the CPU?
   ```suggestion
           assert buf2.device_type == pa.DeviceAllocationType.CUDA
           assert buf2.copy(pa.default_memory_manager()).equals(buf)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1747585024


##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -119,6 +119,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        assert buf2.device_type == pa.DeviceAllocationType.CUDA
+        cudabuf2 = cuda.CudaBuffer.from_buffer(buf2)
+        cudabuf2.size == cudabuf.size

Review Comment:
   You're right, it is missing an `assert`!
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1756867166


##########
python/pyarrow/io.pxi:
##########
@@ -1446,6 +1446,38 @@ cdef class Buffer(_Weakrefable):
         """
         return _wrap_device_allocation_type(self.buffer.get().device_type())
 
+    def copy(self, dest):
+        """
+        The buffer contents will be copied into a new buffer allocated onto

Review Comment:
   ```suggestion
           Copy the buffer to the destination device.
   
           The buffer contents will be copied into a new buffer allocated onto
   ```
   
   To follow the standard docstring format of first having a 1-line summary, and then optionally a multi-line extended explanation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #43590: GH-43589: [Python] Add bindings for Buffer copy() method to other device
URL: https://github.com/apache/arrow/pull/43590


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1747633580


##########
python/pyarrow/includes/libarrow.pxd:
##########
@@ -366,6 +366,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
         shared_ptr[CDevice] device()
         const shared_ptr[CMemoryManager] memory_manager()
         CDeviceAllocationType device_type()
+        CResult[shared_ptr[CBuffer]] Copy(shared_ptr[CBuffer] source, const shared_ptr[CMemoryManager]& to) const

Review Comment:
   Oh good catch. How do I declare static? Is it correct that I declare it by adding a `@staticmethod` header above line 369? @jorisvandenbossche 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1756836879


##########
python/pyarrow/includes/libarrow.pxd:
##########
@@ -366,6 +366,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
         shared_ptr[CDevice] device()
         const shared_ptr[CMemoryManager] memory_manager()
         CDeviceAllocationType device_type()
+        CResult[shared_ptr[CBuffer]] Copy(shared_ptr[CBuffer] source, const shared_ptr[CMemoryManager]& to) const

Review Comment:
   Ah, good catch. It does seem to work right now as well, but so how we do it for other static methods is to decorate it here with `@staticmethod` and then in the code use it as such (`CBuffer.Copy(self.buffer, ...)`)
   
   See for example `CTable` `Make` method in this file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1774843887


##########
python/pyarrow/io.pxi:
##########
@@ -1446,6 +1446,41 @@ cdef class Buffer(_Weakrefable):
         """
         return _wrap_device_allocation_type(self.buffer.get().device_type())
 
+    def copy(self, destination):
+        """
+        Copy the buffer to the destination device.
+
+        The buffer contents will be copied into a new buffer allocated onto
+        the destination MemoryManager or Device. It will return the new Buffer.
+        This function supports cross-device copies.
+
+        Parameters
+        ----------
+        destination : pyarrow.MemoryManager or pyarrow.Device

Review Comment:
   Not required, but it may be nice to also allow a MemoryPool here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1775497621


##########
python/pyarrow/io.pxi:
##########
@@ -1446,6 +1446,41 @@ cdef class Buffer(_Weakrefable):
         """
         return _wrap_device_allocation_type(self.buffer.get().device_type())
 
+    def copy(self, destination):
+        """
+        Copy the buffer to the destination device.
+
+        The buffer contents will be copied into a new buffer allocated onto
+        the destination MemoryManager or Device. It will return the new Buffer.
+        This function supports cross-device copies.
+
+        Parameters
+        ----------
+        destination : pyarrow.MemoryManager or pyarrow.Device

Review Comment:
   You can use https://github.com/apache/arrow/blob/c557fe51b3763b9492392f48c5ebcae5a1dd0b42/cpp/src/arrow/device.h#L295-L298



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1767459756


##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -119,6 +119,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        assert buf2.device_type == pa.DeviceAllocationType.CUDA

Review Comment:
   Ah, so the `pa.default_memory_manager()` is the memory manager for CPU.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2361956716

   @github-actions crossbow submit test-cuda-python


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2364134853

   @anjakefala added a few more comments on the tests, but for the rest looks good!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-3547050976

   Thank you for your contribution. Unfortunately, this pull request has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this PR will be closed in 14 days.  Feel free to re-open this if it has been closed in error. If you do not have repository permissions to reopen the PR, please tag a maintainer.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1742031315


##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -116,6 +116,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)

Review Comment:
   ```suggestion
           buf2 = cudabuf.copy(dest)
           assert buf2.device_type == pa.DeviceAllocationType.CUDA
   ```
   
   This should also already work before creating a CudaBuffer from it? Might be good to explicitly verify that



##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -116,6 +116,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        cudabuf2 = cuda.CudaBuffer.from_buffer(buf2)
+        cudabuf2.size == cudabuf.size
+        assert not cudabuf2.is_cpu
+        assert cudabuf2.device_type == pa.DeviceAllocationType.CUDA
+
+        arr2 = np.frombuffer(cudabuf2.copy_to_host(), dtype=np.uint8)
+        np.testing.assert_equal(arr, arr2)

Review Comment:
   Could simplify a little bit by checking equality of the buffers directly? Something like (untested) `assert cudabuf2.copy_to_host().equals(buf)`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1756868643


##########
python/pyarrow/io.pxi:
##########
@@ -1446,6 +1446,38 @@ cdef class Buffer(_Weakrefable):
         """
         return _wrap_device_allocation_type(self.buffer.get().device_type())
 
+    def copy(self, dest):

Review Comment:
   ```suggestion
       def copy(self, destination):
   ```
   
   Just to be consistent, but for `Array.copy_to` method I spelled it in full, so would propose to follow that here as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2350454623

   Revision: 2e1090558d5d399b060a7051eb9d05caa2174821
   
   Submitted crossbow builds: [ursacomputing/crossbow @ actions-6f97bc8a50](https://github.com/ursacomputing/crossbow/branches/all?query=actions-6f97bc8a50)
   
   |Task|Status|
   |----|------|
   |test-cuda-python|[![GitHub Actions](https://github.com/ursacomputing/crossbow/actions/workflows/crossbow.yml/badge.svg?branch=actions-6f97bc8a50-github-test-cuda-python)](https://github.com/ursacomputing/crossbow/actions/runs/10856767909/job/30132050660)|


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2372246562

   Is anything else needed before merging? The failed CI seems unrelated.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2367638413

   Revision: 230abf2e114abc01b90089ab042c3076b258b5b3
   
   Submitted crossbow builds: [ursacomputing/crossbow @ actions-b9e1188e21](https://github.com/ursacomputing/crossbow/branches/all?query=actions-b9e1188e21)
   
   |Task|Status|
   |----|------|
   |test-cuda-python-ubuntu-22.04-cuda-11.7.1|[![GitHub Actions](https://github.com/ursacomputing/crossbow/actions/workflows/crossbow.yml/badge.svg?branch=actions-b9e1188e21-github-test-cuda-python-ubuntu-22.04-cuda-11.7.1)](https://github.com/ursacomputing/crossbow/actions/runs/10991174630/job/30512988686)|


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1747523388


##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -116,6 +116,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        cudabuf2 = cuda.CudaBuffer.from_buffer(buf2)
+        cudabuf2.size == cudabuf.size
+        assert not cudabuf2.is_cpu
+        assert cudabuf2.device_type == pa.DeviceAllocationType.CUDA
+
+        arr2 = np.frombuffer(cudabuf2.copy_to_host(), dtype=np.uint8)
+        np.testing.assert_equal(arr, arr2)

Review Comment:
   Oh wow, that works! Thank you! That's great.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2350445100

   @danepitkin We decided to move those to a separate PR!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1767462977


##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -119,6 +119,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        assert buf2.device_type == pa.DeviceAllocationType.CUDA

Review Comment:
   It is `default_cpu_memory_manager`* =)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2361958574

   ```
   Unable to match any tasks for `test-cuda-python`
   The Archery job run can be found at: https://github.com/apache/arrow/actions/runs/10947293941
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2362153274

   @github-actions crossbow submit test*cuda*py*


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1769300357


##########
python/pyarrow/tests/test_io.py:
##########
@@ -710,6 +710,15 @@ def test_non_cpu_buffer(pickle_module):
     # Sliced buffers with same address
     assert buf_on_gpu_sliced.equals(cuda_buf[2:4])
 
+    # Testing copying buffer
+    mm = ctx.memory_manager
+    for dest in [mm, mm.device]:
+        buf2 = cuda_buf.copy(dest)
+        cuda_buf2 = cuda.CudaBuffer.from_buffer(buf2)
+        assert cuda_buf2.size == cuda_buf.size
+        assert cuda_buf2.copy_to_host()[:] == b'testing'
+        assert cuda_buf2.device == mm.device

Review Comment:
   @jorisvandenbossche Ah yes, because regular buffers also have a device property. I used the converted CudaBuffer because I was confused on that front.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2334570136

   @github-actions crossbow submit test-cuda-python


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2271821018

   :warning: GitHub issue #43589 **has been automatically assigned in GitHub** to PR creator.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2362123644

   ```
   Unable to match any tasks for `test-cuda-python`
   The Archery job run can be found at: https://github.com/apache/arrow/actions/runs/10948481926
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "danepitkin (via GitHub)" <gi...@apache.org>.
danepitkin commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2362121918

   @github-actions crossbow submit test-cuda-python


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1768909175


##########
python/pyarrow/tests/test_io.py:
##########
@@ -710,6 +710,15 @@ def test_non_cpu_buffer(pickle_module):
     # Sliced buffers with same address
     assert buf_on_gpu_sliced.equals(cuda_buf[2:4])
 
+    # Testing copying buffer
+    mm = ctx.memory_manager
+    for dest in [mm, mm.device]:
+        buf2 = cuda_buf.copy(dest)
+        cuda_buf2 = cuda.CudaBuffer.from_buffer(buf2)
+        assert cuda_buf2.size == cuda_buf.size
+        assert cuda_buf2.copy_to_host()[:] == b'testing'

Review Comment:
   ```suggestion
           assert cuda_buf2..to_pybytes() == b'testing'
   ```
   
   (shouldn't matter in practice, but just to use the same method for verifying the result as above)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] Add bindings for Buffer copy() method to other device [arrow]

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1768908101


##########
python/pyarrow/tests/test_io.py:
##########
@@ -710,6 +710,15 @@ def test_non_cpu_buffer(pickle_module):
     # Sliced buffers with same address
     assert buf_on_gpu_sliced.equals(cuda_buf[2:4])
 
+    # Testing copying buffer
+    mm = ctx.memory_manager
+    for dest in [mm, mm.device]:
+        buf2 = cuda_buf.copy(dest)

Review Comment:
   ```suggestion
           buf2 = buf_on_gpu.copy(dest)
   ```
   
   This test is generally testing with the `buf_on_gpu` object, because it is testing the base class `Buffer` class instances with a non-cpu device (in `test_cuda.py` we are testing `CudaBuffer`).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2334573723

   Revision: a64d54dfd1c1a50fc34ea85bfae7d0529e7be26a
   
   Submitted crossbow builds: [ursacomputing/crossbow @ actions-b9914c094c](https://github.com/ursacomputing/crossbow/branches/all?query=actions-b9914c094c)
   
   |Task|Status|
   |----|------|
   |test-cuda-python|[![GitHub Actions](https://github.com/ursacomputing/crossbow/actions/workflows/crossbow.yml/badge.svg?branch=actions-b9914c094c-github-test-cuda-python)](https://github.com/ursacomputing/crossbow/actions/runs/10743076668/job/29797063185)|


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "danepitkin (via GitHub)" <gi...@apache.org>.
danepitkin commented on code in PR #43590:
URL: https://github.com/apache/arrow/pull/43590#discussion_r1747565122


##########
python/pyarrow/tests/test_cuda.py:
##########
@@ -119,6 +119,24 @@ def make_random_buffer(size, target='host'):
     raise ValueError('invalid target value')
 
 
+def test_copy_from_buffer():
+    arr, buf = make_random_buffer(128)
+    cudabuf = global_context.buffer_from_data(buf)
+
+    mm2 = global_context1.memory_manager
+    for dest in [mm2, mm2.device]:
+        buf2 = cudabuf.copy(dest)
+        assert buf2.device_type == pa.DeviceAllocationType.CUDA
+        cudabuf2 = cuda.CudaBuffer.from_buffer(buf2)
+        cudabuf2.size == cudabuf.size

Review Comment:
   should this be a test assertion or should it be an assignment `=` vs `==`?



##########
python/pyarrow/includes/libarrow.pxd:
##########
@@ -366,6 +366,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
         shared_ptr[CDevice] device()
         const shared_ptr[CMemoryManager] memory_manager()
         CDeviceAllocationType device_type()
+        CResult[shared_ptr[CBuffer]] Copy(shared_ptr[CBuffer] source, const shared_ptr[CMemoryManager]& to) const

Review Comment:
   In C++, this method is `static` instead of `const`. Any idea if Cython needs to reflect this to match exactly?
   
   https://github.com/apache/arrow/blob/ab0a40ee34217070f14027776682074c55d0b507/cpp/src/arrow/buffer.h#L331



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-43589: [Python] add bindings for additional Buffer class non-CPU methods [arrow]

Posted by "anjakefala (via GitHub)" <gi...@apache.org>.
anjakefala commented on PR #43590:
URL: https://github.com/apache/arrow/pull/43590#issuecomment-2350445820

   @github-actions crossbow submit test-cuda-python


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org