Skip to content

Commit

Permalink
Merge branch 'branch-25.02' into mukernels_strings
Browse files Browse the repository at this point in the history
  • Loading branch information
pmattione-nvidia authored Jan 13, 2025
2 parents 8712b53 + 4ec389b commit d360200
Show file tree
Hide file tree
Showing 59 changed files with 1,375 additions and 960 deletions.
4 changes: 2 additions & 2 deletions cpp/include/cudf/aggregation.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -601,7 +601,7 @@ std::unique_ptr<Base> make_udf_aggregation(udf_type type,
data_type output_type);

// Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
struct host_udf_base;
class host_udf_base;

/**
* @brief Factory to create a HOST_UDF aggregation.
Expand Down
478 changes: 301 additions & 177 deletions cpp/include/cudf/aggregation/host_udf.hpp

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions cpp/include/cudf/detail/aggregation/aggregation.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -967,7 +967,9 @@ class udf_aggregation final : public rolling_aggregation {
/**
* @brief Derived class for specifying host-based UDF aggregation.
*/
class host_udf_aggregation final : public groupby_aggregation {
class host_udf_aggregation final : public groupby_aggregation,
public reduce_aggregation,
public segmented_reduce_aggregation {
public:
std::unique_ptr<host_udf_base> udf_ptr;

Expand Down
12 changes: 9 additions & 3 deletions cpp/include/cudf/detail/utilities/integer_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2019 BlazingDB, Inc.
* Copyright 2019 Eyal Rozenberg <[email protected]>
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
* Copyright (c) 2020-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,8 @@
*/

#include <cudf/fixed_point/temporary.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/error.hpp>

#include <cmath>
#include <cstdlib>
Expand All @@ -44,13 +46,17 @@ namespace util {
* `modulus` is positive. The safety is in regard to rollover.
*/
template <typename S>
constexpr S round_up_safe(S number_to_round, S modulus)
CUDF_HOST_DEVICE constexpr S round_up_safe(S number_to_round, S modulus)
{
auto remainder = number_to_round % modulus;
if (remainder == 0) { return number_to_round; }
auto rounded_up = number_to_round - remainder + modulus;
if (rounded_up < number_to_round) {
throw std::invalid_argument("Attempt to round up beyond the type's maximum value");
#ifndef __CUDA_ARCH__
CUDF_FAIL("Attempt to round up beyond the type's maximum value", cudf::data_type_error);
#else
CUDF_UNREACHABLE("Attempt to round up beyond the type's maximum value");
#endif
}
return rounded_up;
}
Expand Down
40 changes: 24 additions & 16 deletions cpp/include/cudf/utilities/span.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
* Copyright (c) 2020-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -197,11 +197,16 @@ struct host_span : public cudf::detail::span_base<T, Extent, host_span<T, Extent

constexpr host_span() noexcept : base() {} // required to compile on centos

/// Constructor from pointer and size
/// @param data Pointer to the first element in the span
/// @param size The number of elements in the span
/// @param is_device_accessible Whether the data is device accessible (e.g. pinned memory)
constexpr host_span(T* data, std::size_t size, bool is_device_accessible)
/**
* @brief Constructor from pointer and size
*
* @note This needs to be host-device , as it's used by a host-device function in base_2dspan
*
* @param data Pointer to the first element in the span
* @param size The number of elements in the span
* @param is_device_accessible Whether the data is device accessible (e.g. pinned memory)
*/
CUDF_HOST_DEVICE constexpr host_span(T* data, std::size_t size, bool is_device_accessible)
: base(data, size), _is_device_accessible{is_device_accessible}
{
}
Expand Down Expand Up @@ -311,8 +316,8 @@ struct host_span : public cudf::detail::span_base<T, Extent, host_span<T, Extent
* @param count The number of elements in the subspan
* @return A subspan of the sequence, of requested count and offset
*/
[[nodiscard]] constexpr host_span subspan(typename base::size_type offset,
typename base::size_type count) const noexcept
[[nodiscard]] CUDF_HOST_DEVICE constexpr host_span subspan(
typename base::size_type offset, typename base::size_type count) const noexcept
{
return host_span{this->data() + offset, count, _is_device_accessible};
}
Expand Down Expand Up @@ -434,8 +439,8 @@ struct device_span : public cudf::detail::span_base<T, Extent, device_span<T, Ex
* @param count The number of elements in the subspan
* @return A subspan of the sequence, of requested count and offset
*/
[[nodiscard]] constexpr device_span subspan(typename base::size_type offset,
typename base::size_type count) const noexcept
[[nodiscard]] CUDF_HOST_DEVICE constexpr device_span subspan(
typename base::size_type offset, typename base::size_type count) const noexcept
{
return device_span{this->data() + offset, count};
}
Expand Down Expand Up @@ -475,28 +480,28 @@ class base_2dspan {
*
* @return A pointer to the first element of the span
*/
[[nodiscard]] constexpr auto data() const noexcept { return _flat.data(); }
[[nodiscard]] CUDF_HOST_DEVICE constexpr auto data() const noexcept { return _flat.data(); }

/**
* @brief Returns the size in the span as pair.
*
* @return pair representing rows and columns size of the span
*/
[[nodiscard]] constexpr auto size() const noexcept { return _size; }
[[nodiscard]] CUDF_HOST_DEVICE constexpr auto size() const noexcept { return _size; }

/**
* @brief Returns the number of elements in the span.
*
* @return Number of elements in the span
*/
[[nodiscard]] constexpr auto count() const noexcept { return _flat.size(); }
[[nodiscard]] CUDF_HOST_DEVICE constexpr auto count() const noexcept { return _flat.size(); }

/**
* @brief Checks if the span is empty.
*
* @return True if the span is empty, false otherwise
*/
[[nodiscard]] constexpr bool is_empty() const noexcept { return count() == 0; }
[[nodiscard]] CUDF_HOST_DEVICE constexpr bool is_empty() const noexcept { return count() == 0; }

/**
* @brief Returns a reference to the row-th element of the sequence.
Expand All @@ -507,7 +512,7 @@ class base_2dspan {
* @param row the index of the element to access
* @return A reference to the row-th element of the sequence, i.e., `data()[row]`
*/
constexpr RowType<T, dynamic_extent> operator[](size_t row) const
CUDF_HOST_DEVICE constexpr RowType<T, dynamic_extent> operator[](size_t row) const
{
return _flat.subspan(row * _size.second, _size.second);
}
Expand All @@ -517,7 +522,10 @@ class base_2dspan {
*
* @return A flattened span of the 2D span
*/
[[nodiscard]] constexpr RowType<T, dynamic_extent> flat_view() const { return _flat; }
[[nodiscard]] CUDF_HOST_DEVICE constexpr RowType<T, dynamic_extent> flat_view() const
{
return _flat;
}

/**
* @brief Construct a 2D span from another 2D span of convertible type
Expand Down
39 changes: 0 additions & 39 deletions cpp/include/nvtext/detail/generate_ngrams.hpp

This file was deleted.

4 changes: 3 additions & 1 deletion cpp/include/nvtext/generate_ngrams.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
* Copyright (c) 2020-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -117,13 +117,15 @@ std::unique_ptr<cudf::column> generate_character_ngrams(
*
* @param input Strings column to produce ngrams from
* @param ngrams The ngram number to generate. Default is 5.
* @param seed The seed value to use with the hash algorithm. Default is 0.
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned column's device memory.
* @return A lists column of hash values
*/
std::unique_ptr<cudf::column> hash_character_ngrams(
cudf::strings_column_view const& input,
cudf::size_type ngrams = 5,
uint32_t seed = 0,
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::device_async_resource_ref mr = cudf::get_current_device_resource_ref());

Expand Down
9 changes: 6 additions & 3 deletions cpp/src/groupby/groupby.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -145,8 +145,11 @@ struct empty_column_constructor {
}

if constexpr (k == aggregation::Kind::HOST_UDF) {
auto const& udf_ptr = dynamic_cast<cudf::detail::host_udf_aggregation const&>(agg).udf_ptr;
return std::get<std::unique_ptr<column>>(udf_ptr->get_empty_output(std::nullopt, stream, mr));
auto const& udf_base_ptr =
dynamic_cast<cudf::detail::host_udf_aggregation const&>(agg).udf_ptr;
auto const udf_ptr = dynamic_cast<groupby_host_udf const*>(udf_base_ptr.get());
CUDF_EXPECTS(udf_ptr != nullptr, "Invalid HOST_UDF instance for groupby aggregation.");
return udf_ptr->get_empty_output(stream, mr);
}

return make_empty_column(target_type(values.type(), k));
Expand Down
81 changes: 32 additions & 49 deletions cpp/src/groupby/sort/aggregate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -795,58 +795,41 @@ void aggregate_result_functor::operator()<aggregation::HOST_UDF>(aggregation con
{
if (cache.has_result(values, agg)) { return; }

auto const& udf_ptr = dynamic_cast<cudf::detail::host_udf_aggregation const&>(agg).udf_ptr;
auto const data_attrs = [&]() -> host_udf_base::data_attribute_set_t {
if (auto tmp = udf_ptr->get_required_data(); !tmp.empty()) { return tmp; }
// Empty attribute set means everything.
return {host_udf_base::groupby_data_attribute::INPUT_VALUES,
host_udf_base::groupby_data_attribute::GROUPED_VALUES,
host_udf_base::groupby_data_attribute::SORTED_GROUPED_VALUES,
host_udf_base::groupby_data_attribute::NUM_GROUPS,
host_udf_base::groupby_data_attribute::GROUP_OFFSETS,
host_udf_base::groupby_data_attribute::GROUP_LABELS};
}();
auto const& udf_base_ptr = dynamic_cast<cudf::detail::host_udf_aggregation const&>(agg).udf_ptr;
auto const udf_ptr = dynamic_cast<groupby_host_udf*>(udf_base_ptr.get());
CUDF_EXPECTS(udf_ptr != nullptr, "Invalid HOST_UDF instance for groupby aggregation.");

// Do not cache udf_input, as the actual input data may change from run to run.
host_udf_base::input_map_t udf_input;
for (auto const& attr : data_attrs) {
CUDF_EXPECTS(std::holds_alternative<host_udf_base::groupby_data_attribute>(attr.value) ||
std::holds_alternative<std::unique_ptr<aggregation>>(attr.value),
"Invalid input data attribute for HOST_UDF groupby aggregation.");
if (std::holds_alternative<host_udf_base::groupby_data_attribute>(attr.value)) {
switch (std::get<host_udf_base::groupby_data_attribute>(attr.value)) {
case host_udf_base::groupby_data_attribute::INPUT_VALUES:
udf_input.emplace(attr, values);
break;
case host_udf_base::groupby_data_attribute::GROUPED_VALUES:
udf_input.emplace(attr, get_grouped_values());
break;
case host_udf_base::groupby_data_attribute::SORTED_GROUPED_VALUES:
udf_input.emplace(attr, get_sorted_values());
break;
case host_udf_base::groupby_data_attribute::NUM_GROUPS:
udf_input.emplace(attr, helper.num_groups(stream));
break;
case host_udf_base::groupby_data_attribute::GROUP_OFFSETS:
udf_input.emplace(attr, helper.group_offsets(stream));
break;
case host_udf_base::groupby_data_attribute::GROUP_LABELS:
udf_input.emplace(attr, helper.group_labels(stream));
break;
default: CUDF_UNREACHABLE("Invalid input data attribute for HOST_UDF groupby aggregation.");
}
} else { // data is result from another aggregation
auto other_agg = std::get<std::unique_ptr<aggregation>>(attr.value)->clone();
if (!udf_ptr->callback_input_values) {
udf_ptr->callback_input_values = [&]() -> column_view { return values; };
}
if (!udf_ptr->callback_grouped_values) {
udf_ptr->callback_grouped_values = [&]() -> column_view { return get_grouped_values(); };
}
if (!udf_ptr->callback_sorted_grouped_values) {
udf_ptr->callback_sorted_grouped_values = [&]() -> column_view { return get_sorted_values(); };
}
if (!udf_ptr->callback_num_groups) {
udf_ptr->callback_num_groups = [&]() -> size_type { return helper.num_groups(stream); };
}
if (!udf_ptr->callback_group_offsets) {
udf_ptr->callback_group_offsets = [&]() -> device_span<size_type const> {
return helper.group_offsets(stream);
};
}
if (!udf_ptr->callback_group_labels) {
udf_ptr->callback_group_labels = [&]() -> device_span<size_type const> {
return helper.group_labels(stream);
};
}
if (!udf_ptr->callback_compute_aggregation) {
udf_ptr->callback_compute_aggregation =
[&](std::unique_ptr<aggregation> other_agg) -> column_view {
cudf::detail::aggregation_dispatcher(other_agg->kind, *this, *other_agg);
auto result = cache.get_result(values, *other_agg);
udf_input.emplace(std::move(other_agg), std::move(result));
}
return cache.get_result(values, *other_agg);
};
}

auto output = (*udf_ptr)(udf_input, stream, mr);
CUDF_EXPECTS(std::holds_alternative<std::unique_ptr<column>>(output),
"Invalid output type from HOST_UDF groupby aggregation.");
cache.add_result(values, agg, std::get<std::unique_ptr<column>>(std::move(output)));
cache.add_result(values, agg, (*udf_ptr)(stream, mr));
}

} // namespace detail
Expand Down
Loading

0 comments on commit d360200

Please sign in to comment.