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

Add assert_column_memory_* #9882

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions python/cudf/cudf/testing/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,22 @@ def does_not_raise():

def xfail_param(param, **kwargs):
return pytest.param(param, marks=pytest.mark.xfail(**kwargs))


def assert_column_memory_eq(
lhs: cudf.core.column.ColumnBase, rhs: cudf.core.column.ColumnBase
):
assert lhs.base_data_ptr == rhs.base_data_ptr
assert lhs.base_mask_ptr == rhs.base_mask_ptr
for lhs_child, rhs_child in zip(lhs.base_children, rhs.base_children):
assert_column_memory_eq(lhs_child, rhs_child)
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't this also return True for columns that have the same base pointers but different sizes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right.. Is adding check for size sufficient?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes - an additional check for size should be enough.



def assert_column_memory_ne(
lhs: cudf.core.column.ColumnBase, rhs: cudf.core.column.ColumnBase
):
try:
assert_column_memory_eq(lhs, rhs)
except AssertionError:
return
raise AssertionError("lhs and rhs holds the same memory.")
60 changes: 6 additions & 54 deletions python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
SIGNED_INTEGER_TYPES,
SIGNED_TYPES,
UNSIGNED_TYPES,
assert_column_memory_eq,
assert_column_memory_ne,
assert_eq,
assert_exceptions_equal,
)
Expand Down Expand Up @@ -391,62 +393,12 @@ def test_index_copy_category(name, dtype, deep=True):
],
)
def test_index_copy_deep(idx, deep):
"""Test if deep copy creates a new instance for device data.
The general criterion is to compare `Buffer.ptr` between two data objects.
Specifically for:
- CategoricalIndex, this applies to both `.codes` and `.categories`
- StringIndex, to every element in `._base_children`
- Others, to `.base_data`
No test is defined for RangeIndex.
"""
"""Test if deep copy creates a new instance for device data."""
idx_copy = idx.copy(deep=deep)
same_ref = not deep
if isinstance(idx, cudf.CategoricalIndex):
assert (
idx._values.codes.base_data.ptr
== idx_copy._values.codes.base_data.ptr
) == same_ref
if isinstance(
idx._values.categories, cudf.core.column.string.StringColumn
):
children = idx._values.categories._base_children
copy_children = idx_copy._values.categories._base_children
assert all(
[
(
children[i].base_data.ptr
== copy_children[i].base_data.ptr
)
== same_ref
for i in range(len(children))
]
)
elif isinstance(
idx._values.categories, cudf.core.column.numerical.NumericalColumn
):
assert (
idx._values.categories.base_data.ptr
== idx_copy._values.categories.base_data.ptr
) == same_ref
elif isinstance(idx, cudf.StringIndex):
children = idx._values._base_children
copy_children = idx_copy._values._base_children
assert all(
[
(
(
children[i].base_data.ptr
== copy_children[i].base_data.ptr
)
== same_ref
)
for i in range(len(children))
]
)
if not deep:
assert_column_memory_eq(idx._values, idx_copy._values)
else:
assert (
idx._values.base_data.ptr == idx_copy._values.base_data.ptr
) == same_ref
assert_column_memory_ne(idx._values, idx_copy._values)


@pytest.mark.parametrize("idx", [[1, None, 3, None, 5]])
Expand Down