Skip to content

Commit

Permalink
Add support for pylibcudf.DataType serialization (#17352)
Browse files Browse the repository at this point in the history
For the multi-GPU cudf-polars work serializing `pylibcudf.DataType` will be required. This change implements the `__reduce__` method and add appropriate tests.

Authors:
  - Peter Andreas Entschev (https://github.com/pentschev)

Approvers:
  - Matthew Murray (https://github.com/Matt711)
  - Lawrence Mitchell (https://github.com/wence-)

URL: #17352
  • Loading branch information
pentschev authored Nov 28, 2024
1 parent 9b88794 commit 891a865
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions python/pylibcudf/pylibcudf/tests/test_serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
import pickle

import pytest

from pylibcudf import DataType
from pylibcudf.types import TypeId


@pytest.fixture(params=list(TypeId))
def dtype(request):
tid = request.param
if tid in {TypeId.DECIMAL32, TypeId.DECIMAL64, TypeId.DECIMAL128}:
scale = 5
else:
scale = 0
return DataType(tid, scale)


def test_reduce(dtype):
(typ, (tid, scale)) = dtype.__reduce__()
assert typ is DataType
assert tid == dtype.id()
assert scale == dtype.scale()


def test_pickle(dtype):
assert dtype == pickle.loads(pickle.dumps(dtype))
3 changes: 3 additions & 0 deletions python/pylibcudf/pylibcudf/types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ cdef class DataType:
def __hash__(self):
return hash((self.c_obj.id(), self.c_obj.scale()))

def __reduce__(self):
return (type(self), (self.c_obj.id(), self.c_obj.scale()))

@staticmethod
cdef DataType from_libcudf(data_type dt):
"""Create a DataType from a libcudf data_type.
Expand Down

0 comments on commit 891a865

Please sign in to comment.