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 public APIs to Access Underlying cudf and pandas Objects from cudf.pandas Proxy Objects #17629

Merged
merged 27 commits into from
Jan 29, 2025

Conversation

galipremsagar
Copy link
Contributor

@galipremsagar galipremsagar commented Dec 19, 2024

Description

Fixes: #17524
Fixes: rapidsai/cuml#6232
This PR introduces methods to access the real underlying cudf and pandas objects from cudf.pandas proxy objects. These methods ensure compatibility with libraries that are cudf or pandas aware.

This PR also gives a performance boost to cudf-pandas workflows, speeds from the script posted in rapidsai/cuml#6232:

branch-25.02:

cuML Label Encoder with cuDF-Pandas took 2.00794 seconds

This PR:

cuML Label Encoder with cuDF-Pandas took 0.09284 seconds

Changes:

  • Added get_gpu_object() and get_cpu_object() methods.
  • Updated faq.md with a section explaining how to use these methods.

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@galipremsagar galipremsagar added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels Dec 19, 2024
@galipremsagar galipremsagar self-assigned this Dec 19, 2024
@galipremsagar galipremsagar requested a review from a team as a code owner December 19, 2024 09:27
@github-actions github-actions bot added Python Affects Python cuDF API. cudf.pandas Issues specific to cudf.pandas labels Dec 19, 2024
Copy link
Contributor

@Matt711 Matt711 left a comment

Choose a reason for hiding this comment

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

Overall, this looks good to me. But I think we should add a bullet to the "Are there any limitations?" section of this faq.md . And it should describe the implications for users of cudf.pandas and third-party libraries that are "cudf aware." For example, they could get a cupy array (not a numpy array) when working with xgboost. What do you think?

@galipremsagar
Copy link
Contributor Author

@vyasr @bdice @Matt711 This is ready for another round of reviews.

@galipremsagar galipremsagar added the 3 - Ready for Review Ready for review by team label Jan 25, 2025
Copy link
Contributor

@bdice bdice left a comment

Choose a reason for hiding this comment

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

How we document this for users is probably the most important aspect of this PR. I gave some suggestions, let me know what you think.

docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
docs/cudf/source/cudf_pandas/faq.md Show resolved Hide resolved
and (index is None or index_extracted)
and (columns is None or columns_extracted)
) and (dtype is None and copy is None):
self.__dict__.update(data.__dict__)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use _mimic_inplace instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel this is much light weight that using _mimic_inplace and also copies cached attributes.

Co-authored-by: Matthew Roeschke <[email protected]>
docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
python/cudf/cudf/pandas/_wrappers/numpy.py Outdated Show resolved Hide resolved
Comment on lines 1720 to 1729
def is_cudf_pandas_dataframe(obj):
return is_proxy_object(obj) and isinstance(obj, DataFrame)


def is_cudf_pandas_series(obj):
return is_proxy_object(obj) and isinstance(obj, Series)


def is_cudf_pandas_index(obj):
return is_proxy_object(obj) and isinstance(obj, Index)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these functions actually useful? It seems like it would be better to just tell the user how to check this themselves with is_proxy_object + isinstance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we could do that but my intention here was to just have single API that informs the users. It will end up being a verbose pattern in libraries that operate cudf/cudf.pandas aware.

Copy link
Contributor

Choose a reason for hiding this comment

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

My suggestion in a thread above was to instead implement a single method (bikeshed the name as much as you wish, I'm mostly concerned with suggesting an implementation)

def isinstance_cudf_pandas(obj, type):
    return is_proxy_object(obj) and isinstance(obj, type)

so that they don't need to be aware of is_proxy_object and it "feels like" they're using isinstance.

docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
python/cudf/cudf_pandas_tests/test_cudf_pandas.py Outdated Show resolved Hide resolved
python/cudf/cudf_pandas_tests/test_cudf_pandas.py Outdated Show resolved Hide resolved
python/cudf/cudf_pandas_tests/test_cudf_pandas.py Outdated Show resolved Hide resolved
docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
docs/cudf/source/cudf_pandas/faq.md Show resolved Hide resolved
python/cudf/cudf/core/dataframe.py Outdated Show resolved Hide resolved
Comment on lines 1720 to 1729
def is_cudf_pandas_dataframe(obj):
return is_proxy_object(obj) and isinstance(obj, DataFrame)


def is_cudf_pandas_series(obj):
return is_proxy_object(obj) and isinstance(obj, Series)


def is_cudf_pandas_index(obj):
return is_proxy_object(obj) and isinstance(obj, Index)
Copy link
Contributor

Choose a reason for hiding this comment

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

My suggestion in a thread above was to instead implement a single method (bikeshed the name as much as you wish, I'm mostly concerned with suggesting an implementation)

def isinstance_cudf_pandas(obj, type):
    return is_proxy_object(obj) and isinstance(obj, type)

so that they don't need to be aware of is_proxy_object and it "feels like" they're using isinstance.

@galipremsagar galipremsagar requested review from vyasr and bdice January 28, 2025 15:22
from cudf.pandas import isinstance_cudf_pandas

obj = ... # Your object here
if isinstance_cudf_pandas(obj, "Series"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Normally isinstance takes a class, not a string. Is that an option here, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

responded here; #17629 (comment)

docs/cudf/source/cudf_pandas/faq.md Outdated Show resolved Hide resolved
@@ -176,3 +176,7 @@ def ndarray__array_ufunc__(self, ufunc, method, *inputs, **kwargs):
cupy._core.flags.Flags,
_numpy_flagsobj,
)


def is_cudf_pandas_ndarray(obj):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed.

@@ -1704,6 +1713,10 @@ def holiday_calendar_factory_wrapper(*args, **kwargs):
)


def isinstance_cudf_pandas(obj, type_name):
return is_proxy_object(obj) and obj.__class__.__name__ == type_name
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the second half of this sufficiently precise? Can we rely on names alone? I would like to use some kind of "real" check like isinstance if that is allowable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think allowing real types will make this API more complex and confusing. We don't want to allow isinstance_cudf_pandas(obj, pd.DataFrame/cudf.DataFrame) as users might get confused to what they are actually checking when cudf.pandas is enabled and disabled.

xgboost has this string based checks: https://github.com/dmlc/xgboost/pull/11014/files#diff-bf11fe0b3133c5b20253ea67b82c3e576513c8079f3be355fc323e3e903d989cR852

Copy link
Contributor

@bdice bdice Jan 28, 2025

Choose a reason for hiding this comment

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

Typically string-based checks are used as a way to avoid requiring an import of that package. We could still accept a class in the API and do a string-based check of obj.__class__.__name__ == type.__name__.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the api to use this approach. 👍

@@ -1704,6 +1713,10 @@ def holiday_calendar_factory_wrapper(*args, **kwargs):
)


def isinstance_cudf_pandas(obj, type):
Copy link
Contributor

Choose a reason for hiding this comment

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

We need API docs for public APIs.

Copy link
Contributor Author

@galipremsagar galipremsagar Jan 28, 2025

Choose a reason for hiding this comment

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

Can I add that in a followup PR to save some CI time? I'd like to get this PR in and fix cuml ASAP.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine. Please file an issue or draft PR to ensure this isn't lost.

Copy link
Contributor

@Matt711 Matt711 left a comment

Choose a reason for hiding this comment

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

Thanks! I had some non-blocking questions.

python/cudf/cudf/core/column/column.py Show resolved Hide resolved
Comment on lines +207 to +208
def as_gpu_object(self):
return self._fsproxy_slow_to_fast()
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to call isinstance_cudf_pandas inside this function and raise a friendly error message on False?

python/cudf/cudf/core/series.py Outdated Show resolved Hide resolved
Copy link
Contributor

@vyasr vyasr left a comment

Choose a reason for hiding this comment

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

Can we revert the changes to dataframe.py, index.py, series.py, and pandas/init.py, ? Those are now just holdovers of earlier commits. I'm preapproving so that you can merge when ready. Thanks Prem!

from cudf.pandas import isinstance_cudf_pandas

obj = ... # Your object here
if isinstance_cudf_pandas(obj, pd.Series):
Copy link
Contributor

Choose a reason for hiding this comment

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

We should document if this is doing to work with cudf.Series if cudf is directly imported. It's fine if the answer is no, or if the answer is UB and we don't promise anything, we should just mention it either way.

@galipremsagar galipremsagar added 5 - Ready to Merge Testing and reviews complete, ready to merge and removed 3 - Ready for Review Ready for review by team labels Jan 28, 2025
@galipremsagar
Copy link
Contributor Author

/merge

@rapids-bot rapids-bot bot merged commit ed2f3c3 into rapidsai:branch-25.02 Jan 29, 2025
107 of 108 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 - Ready to Merge Testing and reviews complete, ready to merge cudf.pandas Issues specific to cudf.pandas improvement Improvement / enhancement to an existing function non-breaking Non-breaking change Python Affects Python cuDF API.
Projects
Status: Done
5 participants