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 support for unary negation operator #17560

Open
wants to merge 20 commits into
base: branch-25.02
Choose a base branch
from

Conversation

Matt711
Copy link
Contributor

@Matt711 Matt711 commented Dec 10, 2024

Description

Closes #17538. This PR adds support for unary negation operator in libcudf and plumbs the changes through cudf python and cudf polars.

Checklist

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

@Matt711 Matt711 added feature request New feature or request non-breaking Non-breaking change labels Dec 10, 2024
@Matt711 Matt711 requested review from a team as code owners December 10, 2024 00:46
@github-actions github-actions bot added libcudf Affects libcudf (C++/CUDA) code. Python Affects Python cuDF API. cudf.polars Issues specific to cudf.polars pylibcudf Issues specific to the pylibcudf package labels Dec 10, 2024
@mroeschke
Copy link
Contributor

Nice! If @bdice is OK with this approach could you also update cudf Python as well?

else -1 * col

@Matt711
Copy link
Contributor Author

Matt711 commented Dec 10, 2024

Nice! If @bdice is OK with this approach could you also update cudf Python as well?

else -1 * col

Ah yeah, thanks. Will do

@bdice
Copy link
Contributor

bdice commented Dec 10, 2024

Nice! If @bdice is OK with this approach could you also update cudf Python as well?

else -1 * col

Exactly — thanks @mroeschke. The negation behavior for bools and non-numerical types is the thing I want to watch carefully, to be sure we match the appropriate conventions across all the APIs we serve.

cpp/src/unary/math_ops.cu Show resolved Hide resolved
@Matt711 Matt711 added the 3 - Ready for Review Ready for review by team label Dec 10, 2024
Copy link
Contributor

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Could you add the cudf.polars test I added in #17556?

cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
python/pylibcudf/pylibcudf/libcudf/unary.pxd Show resolved Hide resolved
cpp/include/cudf/unary.hpp Outdated Show resolved Hide resolved
cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
cpp/include/cudf/unary.hpp Outdated Show resolved Hide resolved
cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
@Matt711 Matt711 removed the 3 - Ready for Review Ready for review by team label Dec 12, 2024
@Matt711 Matt711 requested review from wence- and bdice December 12, 2024 02:50
@Matt711 Matt711 added the 3 - Ready for Review Ready for review by team label Dec 12, 2024
Comment on lines +245 to +249
template <typename T, CUDF_ENABLE_IF(!std::is_signed_v<T> && !cudf::is_duration_t<T>::value)>
T __device__ operator()(T data)
{
return data;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am confused by this implementation. I thought we didn't want to provide an implementation for unsigned data types?

Copy link
Contributor

@bdice bdice Dec 12, 2024

Choose a reason for hiding this comment

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

I think that needs to happen at the dispatch layer, not SFINAE on the device operator itself. For example, we're not constraining types in other operators:

struct DeviceSqrt {
  template <typename T>
  __device__ T operator()(T data)
  {
    return std::sqrt(data);
  }
};

We only use SFINAE in this file when the implementation differs by type -- not to allow/disallow types.

Can we remove the SFINAE and just have this?

struct DeviceNegate {
  template <typename T>
  T __device__ operator()(T data)
  {
    return -data;
  }
};

Also related to https://github.com/rapidsai/cudf/pull/17560/files#r1878099060

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me post the compile error I got when I removed it.

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.

A few comments -- generally this is looking pretty good.

cpp/src/unary/math_ops.cu Outdated Show resolved Hide resolved
cpp/tests/unary/math_ops_test.cpp Outdated Show resolved Hide resolved
auto output = cudf::unary_operation(input, cudf::unary_operator::NEGATE);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}

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 another test that unsigned types, timestamps, strings, and other complex types fail.

cpp/tests/unary/math_ops_test.cpp Outdated Show resolved Hide resolved
cpp/tests/unary/unary_ops_test.cpp Outdated Show resolved Hide resolved
@@ -226,6 +228,17 @@ def as_numerical_column(
) -> "cudf.core.column.NumericalColumn":
return unary.cast(self, dtype) # type: ignore[return-value]

def unary_operator(self, unaryop: str) -> ColumnBase:
Copy link
Contributor

Choose a reason for hiding this comment

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

How did this work before? Could we use abs on a decimal column?

Ideally we should not have to hardcode the supported operators for each type in Python -- I'd rather catch an exception from C++ and re-raise it with a Python-friendly message.

python/cudf/cudf/tests/test_unaops.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 - Ready for Review Ready for review by team cudf.polars Issues specific to cudf.polars feature request New feature or request libcudf Affects libcudf (C++/CUDA) code. non-breaking Non-breaking change pylibcudf Issues specific to the pylibcudf package Python Affects Python cuDF API.
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

[FEA] Support Polars unary function negate.
5 participants