-
Notifications
You must be signed in to change notification settings - Fork 922
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
Conversation
There was a problem hiding this 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?
There was a problem hiding this 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.
Co-authored-by: Bradley Dice <[email protected]>
python/cudf/cudf/core/dataframe.py
Outdated
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__) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]>
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) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
Co-authored-by: Bradley Dice <[email protected]>
Co-authored-by: Bradley Dice <[email protected]>
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) |
There was a problem hiding this comment.
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
from cudf.pandas import isinstance_cudf_pandas | ||
|
||
obj = ... # Your object here | ||
if isinstance_cudf_pandas(obj, "Series"): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
responded here; #17629 (comment)
@@ -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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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__
.
There was a problem hiding this comment.
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. 👍
Co-authored-by: Bradley Dice <[email protected]>
@@ -1704,6 +1713,10 @@ def holiday_calendar_factory_wrapper(*args, **kwargs): | |||
) | |||
|
|||
|
|||
def isinstance_cudf_pandas(obj, type): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
def as_gpu_object(self): | ||
return self._fsproxy_slow_to_fast() |
There was a problem hiding this comment.
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?
There was a problem hiding this 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): |
There was a problem hiding this comment.
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.
/merge |
ed2f3c3
into
rapidsai:branch-25.02
Description
Fixes: #17524
Fixes: rapidsai/cuml#6232
This PR introduces methods to access the real underlying
cudf
andpandas
objects fromcudf.pandas
proxy objects. These methods ensure compatibility with libraries that arecudf
orpandas
aware.This PR also gives a performance boost to
cudf-pandas
workflows, speeds from the script posted in rapidsai/cuml#6232:branch-25.02
:This PR
:Changes:
get_gpu_object()
andget_cpu_object()
methods.Checklist