Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-41664: [C++][Python] PrettyPrint non-cpu data by copying to default CPU device #42010

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cpp/src/arrow/pretty_print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,14 @@ class ArrayPrinter : public PrettyPrinter {
}

Status Print(const Array& array) {
RETURN_NOT_OK(VisitArrayInline(array, this));
if (array.device_type() != DeviceAllocationType::kCPU) {
// GH-43055: ideally we only copy start/end slices from non-CPU memory
// based on the window size that is being printed
ARROW_ASSIGN_OR_RAISE(auto array_cpu, array.CopyTo(default_cpu_memory_manager()));
RETURN_NOT_OK(VisitArrayInline(*array_cpu, this));
} else {
RETURN_NOT_OK(VisitArrayInline(array, this));
}
Flush();
return Status::OK();
}
Expand Down
5 changes: 3 additions & 2 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,9 @@ cdef class Array(_PandasConvertible):
"""
Render a "pretty-printed" string representation of the Array.

Note: for data on a non-CPU device, the full array is copied to CPU
memory.

Parameters
----------
indent : int, default 2
Expand All @@ -1261,8 +1264,6 @@ cdef class Array(_PandasConvertible):
If the array should be rendered as a single line of text
or if each element should be on its own line.
"""
self._assert_cpu()

cdef:
c_string result
PrettyPrintOptions options
Expand Down
6 changes: 2 additions & 4 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4035,6 +4035,8 @@ def test_non_cpu_array():
assert arr.is_cpu is False
assert len(arr) == 4
assert arr.slice(2, 2).offset == 2
assert repr(arr)
assert str(arr)

# TODO support DLPack for CUDA
with pytest.raises(NotImplementedError):
Expand Down Expand Up @@ -4065,10 +4067,6 @@ def test_non_cpu_array():
arr.get_total_buffer_size()
with pytest.raises(NotImplementedError):
[i for i in iter(arr)]
with pytest.raises(NotImplementedError):
repr(arr)
with pytest.raises(NotImplementedError):
str(arr)
with pytest.raises(NotImplementedError):
arr == arr2
with pytest.raises(NotImplementedError):
Expand Down
54 changes: 54 additions & 0 deletions python/pyarrow/tests/test_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,3 +944,57 @@ def test_device_interface_batch_array():
with pytest.raises(ValueError,
match="ArrowSchema describes non-struct type"):
pa.RecordBatch._import_from_c_device(ptr_array, ptr_schema)


def test_print_array():
batch = make_recordbatch(10)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
arr = batch["f0"]
carr = cbatch["f0"]
assert str(carr) == str(arr)

batch = make_recordbatch(100)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
arr = batch["f0"]
carr = cbatch["f0"]
assert str(carr) == str(arr)


def make_chunked_array(n_elements_per_chunk, n_chunks):
arrs = []
carrs = []
for _ in range(n_chunks):
batch = make_recordbatch(n_elements_per_chunk)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
arrs.append(batch["f0"])
carrs.append(cbatch["f0"])

return pa.chunked_array(arrs), pa.chunked_array(carrs)
Comment on lines +965 to +975
Copy link
Member Author

Choose a reason for hiding this comment

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

This is still a bit verbose, but can be simplified once the "copy to device" functionality is exposed (should maybe do that first now)



def test_print_chunked_array():
arr, carr = make_chunked_array(10, 3)
assert str(carr) == str(arr)

arr, carr = make_chunked_array(100, 20)
assert str(carr) == str(arr)


def test_print_record_batch():
batch = make_recordbatch(10)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
assert str(cbatch) == str(batch)

batch = make_recordbatch(100)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
assert str(cbatch) == str(batch)


def test_print_table():
_, table, _, ctable = make_table_cuda()
assert str(ctable) == str(table)
Loading