Skip to content

Generate group labels from offsets - #10945

Merged
rapids-bot[bot] merged 43 commits into
NVIDIA:branch-22.08from
ttnghia:list_label
Jun 1, 2022
Merged

rapids-bot[bot] merged 43 commits into
NVIDIA:branch-22.08from
ttnghia:list_label

Conversation

@ttnghia

@ttnghia ttnghia commented May 24, 2022 •

Copy link
Copy Markdown
Contributor

This PR adds a small (detail) API that generates group labels from a given offset array offsets. The output will be an array containing consecutive groups of identical labels, the number of elements in each group i is defined by offsets[i+1] - offsets[i].

Examples:

offsets = [ 0, 4, 6, 10 ]
output  = [ 0, 0, 0, 0, 1, 1, 2, 2, 2, 2 ]

offsets = [ 5, 10, 12 ]
output  = [ 0, 0, 0, 0, 0, 1, 1 ]

Note that the label values always start from 0. We can in fact add a parameter to allow specifying any starting value but we don't need it in now.

Several places in cudf have been updated to adopt the new API immediately. These places have been tested extensively thus no unit tests for the new API is needed. In addition, I ran a benchmark for groupby aggregations and found no performance difference after adopting this.

Closes #10905 and unblocks #10409.

@ttnghia ttnghia added feature request New feature or request 3 - Ready for Review Ready for review by team libcudf blocker libcudf Affects libcudf (C++/CUDA) code. Spark Functionality that helps Spark RAPIDS non-breaking Non-breaking change labels May 24, 2022
@ttnghia
ttnghia requested a review from a team as a code owner May 24, 2022 05:01
@ttnghia ttnghia self-assigned this May 24, 2022
@ttnghia
ttnghia requested review from davidwendt and devavret May 24, 2022 05:01
@codecov

This comment was marked as off-topic.

Comment thread cpp/src/groupby/sort/sort_helper.cu Resolved
Comment thread cpp/src/lists/drop_list_duplicates.cu Resolved
Comment thread cpp/src/lists/drop_list_duplicates.cu Resolved
Comment thread cpp/include/cudf/detail/fill.cuh Resolved
Comment thread cpp/include/cudf/detail/fill.cuh Resolved
Comment thread cpp/include/cudf/detail/fill.cuh Resolved
Comment thread cpp/include/cudf/detail/fill.cuh Resolved
Comment thread cpp/include/cudf/detail/fill.cuh Resolved
@ttnghia

ttnghia commented May 25, 2022 •

Copy link
Copy Markdown
Contributor Author

Here is the new benchmark, which does not have overhead but just measures the run time of the labeling methods. Sorry that I can't run compare.py on my computer today thus you have to compare by your eyes:

Old method (scatter + inclusive_scan)

--------------------------------------------------------------------------------------
Benchmark                                            Time             CPU   Iterations
--------------------------------------------------------------------------------------
Labeling/LabelingOldWay/1000/manual_time         0.028 ms        0.046 ms        22443
Labeling/LabelingOldWay/1024/manual_time         0.028 ms        0.045 ms        25207
Labeling/LabelingOldWay/4096/manual_time         0.062 ms        0.080 ms        10629
Labeling/LabelingOldWay/16384/manual_time        0.211 ms        0.234 ms         3451
Labeling/LabelingOldWay/65536/manual_time        0.754 ms        0.773 ms          862
Labeling/LabelingOldWay/262144/manual_time        2.93 ms         2.95 ms          238
Labeling/LabelingOldWay/1048576/manual_time       11.7 ms         11.7 ms           58
Labeling/LabelingOldWay/4194304/manual_time       50.4 ms         50.4 ms           15
Labeling/LabelingOldWay/4200000/manual_time       51.0 ms         51.0 ms           13

New method (in this PR):

--------------------------------------------------------------------------------------
Benchmark                                            Time             CPU   Iterations
--------------------------------------------------------------------------------------
Labeling/LabelingNewWay/1000/manual_time         0.019 ms        0.037 ms        37149
Labeling/LabelingNewWay/1024/manual_time         0.018 ms        0.037 ms        36759
Labeling/LabelingNewWay/4096/manual_time         0.057 ms        0.075 ms        11696
Labeling/LabelingNewWay/16384/manual_time        0.248 ms        0.266 ms         2764
Labeling/LabelingNewWay/65536/manual_time         1.09 ms         1.11 ms          627
Labeling/LabelingNewWay/262144/manual_time        4.87 ms         4.89 ms          133
Labeling/LabelingNewWay/1048576/manual_time       21.4 ms         21.5 ms           32
Labeling/LabelingNewWay/4194304/manual_time       94.1 ms         94.2 ms            7
Labeling/LabelingNewWay/4200000/manual_time       94.4 ms         94.4 ms            7

PS: The maximum size of each group is 1000.

Code to generate benchmark

#include <benchmarks/common/generate_input.hpp>
#include <benchmarks/fixture/benchmark_fixture.hpp>
#include <benchmarks/synchronization/synchronization.hpp>

#include <cudf/types.hpp>

#include <thrust/binary_search.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/iterator_traits.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/transform_output_iterator.h>
#include <thrust/scan.h>
#include <thrust/scatter.h>
#include <thrust/uninitialized_fill.h>

#include <rmm/device_uvector.hpp>
#include <rmm/exec_policy.hpp>

//==================================================================================================
auto create_offsets(cudf::size_type n_groups, rmm::cuda_stream_view stream)
{
// This is the maximum size of each group.
constexpr cudf::size_type max_int = 1000;

auto table_profile = data_profile{};
table_profile.set_distribution_params(cudf::type_id::INT32, distribution_id::UNIFORM, 0, max_int);
auto sizes =
std::move(create_random_table({cudf::type_id::INT32}, row_count{n_groups}, table_profile)
->release()
.front());
auto const sizes_view = sizes->mutable_view();

thrust::exclusive_scan(rmm::exec_policy(),
sizes_view.template begincudf::size_type(),
sizes_view.template endcudf::size_type(),
sizes_view.template begincudf::size_type());

cudf::size_type n_elements;
CUDF_CUDA_TRY(cudaMemcpyAsync(&n_elements,
sizes_view.template endcudf::size_type() - 1,
sizeof(cudf::size_type),
cudaMemcpyDeviceToHost,
stream.value()));
stream.synchronize();

return std::pair{std::move(sizes), n_elements};
}

//==================================================================================================
template <typename InputIterator, typename OutputIterator>
void old_way(InputIterator offsets_begin,
InputIterator offsets_end,
OutputIterator out_begin,
OutputIterator out_end,
rmm::cuda_stream_view stream)
{
thrust::uninitialized_fill(rmm::exec_policy(stream), out_begin, out_end, cudf::size_type{0});
thrust::scatter(
rmm::exec_policy(stream),
thrust::make_constant_iterator(1, 1),
thrust::make_constant_iterator(
1, static_castcudf::size_type(thrust::distance(offsets_begin, offsets_end)) - 1),
offsets_begin + 1,
out_begin);
thrust::inclusive_scan(rmm::exec_policy(stream), out_begin, out_end, out_begin);
}

//==================================================================================================
template <typename InputIterator, typename OutputIterator>
void new_way(InputIterator offsets_begin,
InputIterator offsets_end,
OutputIterator out_begin,
OutputIterator out_end,
rmm::cuda_stream_view stream)
{
auto const zero_normalized_offsets = thrust::make_transform_iterator(
offsets_begin, [offsets_begin] device(auto const idx) { return idx - *offsets_begin; });

// The output labels from upper_bound will start from 1.
// This will shift the result values back to start from 0.
using OutputType = typename thrust::iterator_value::type;
auto const output = thrust::make_transform_output_iterator(
out_begin, [] device(auto const idx) { return idx - OutputType{1}; });

thrust::upper_bound(rmm::exec_policy(stream),
zero_normalized_offsets,
zero_normalized_offsets + thrust::distance(offsets_begin, offsets_end),
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(
static_cast(thrust::distance(out_begin, out_end))),
output);
}

//==================================================================================================
template
void BM_labeling(benchmark::State& state)
{
auto const n_groups = static_castcudf::size_type(state.range(0));
auto const stream = rmm::cuda_stream_default;

auto const [offsets, n_labels] = create_offsets(n_groups, stream);
auto const offsets_view = offsets->view();
auto labels = rmm::device_uvectorcudf::size_type(n_labels, stream);

for (auto _ : state) {
[[maybe_unused]] auto const timer = cuda_event_timer(state, true);
if constexpr (use_old) {
old_way(offsets_view.template begincudf::size_type(),
offsets_view.template endcudf::size_type(),
labels.begin(),
labels.end(),
stream);
} else {
new_way(offsets_view.template begincudf::size_type(),
offsets_view.template endcudf::size_type(),
labels.begin(),
labels.end(),
stream);
}
}
}

//==================================================================================================
class Labeling : public cudf::benchmark {
};

#define MIN_RANGE 1'000
#define MAX_RANGE 4'200'000

#define REGISTER_BENCHMARK(name, use_old)
BENCHMARK_DEFINE_F(Labeling, name)(::benchmark::State & state) { BM_labeling<use_old>(state); }
BENCHMARK_REGISTER_F(Labeling, name)
->UseManualTime()
->Unit(benchmark::kMillisecond)
->RangeMultiplier(4)
->Ranges({{MIN_RANGE, MAX_RANGE}});

// REGISTER_BENCHMARK(LabelingOldWay, true)
REGISTER_BENCHMARK(LabelingNewWay, false)

@ttnghia

ttnghia commented May 25, 2022 •

Copy link
Copy Markdown
Contributor Author

So starting from around 10'000 rows, the new method performs slower. This makes me think of using the old method for labeling in this PR.

@ttnghia

ttnghia commented May 29, 2022

Copy link
Copy Markdown
Contributor Author

Rerun tests.

Signed-off-by: Nghia Truong <[email protected]>
@ttnghia
ttnghia marked this pull request as draft May 29, 2022 13:20
@ttnghia

ttnghia commented May 29, 2022

Copy link
Copy Markdown
Contributor Author

Rerun tests.

@ttnghia
ttnghia marked this pull request as ready for review May 30, 2022 14:13
ttnghia added 2 commits May 30, 2022 08:02
Signed-off-by: Nghia Truong <[email protected]>
Signed-off-by: Nghia Truong <[email protected]>
ttnghia added 2 commits May 31, 2022 14:22
Signed-off-by: Nghia Truong <[email protected]>
Signed-off-by: Nghia Truong <[email protected]>
@ttnghia

ttnghia commented Jun 1, 2022

Copy link
Copy Markdown
Contributor Author

@gpucibot merge

@rapids-bot
rapids-bot Bot merged commit 7f359e0 into NVIDIA:branch-22.08 Jun 1, 2022
@ttnghia
ttnghia deleted the list_label branch June 1, 2022 17:57
rapids-bot Bot pushed a commit that referenced this pull request Jun 3, 2022
Given an array of integer values which may be the labels of some list elements, we want to generate an array of offsets so we can create a lists column from these offsets.

For example:
```
input_labels = [ 0, 0, 0, 0, 1, 1, 4, 4, 4, 4 ]
output = [ 0, 4, 6, 6, 6, 10 ]
```

This is basically the reverse operation of #10945.

Closes #10955.

Authors:
  - Nghia Truong (https://github.com/ttnghia)

Approvers:
  - Devavret Makkar (https://github.com/devavret)
  - Jake Hemstad (https://github.com/jrhemstad)

URL: #11017
rapids-bot Bot pushed a commit that referenced this pull request Jul 26, 2022
This PR adds the following APIs for set operations:
 * `lists::have_overlap`
 * `lists::intersect_distinct`
 * `lists::union_distinct`
 * `lists::difference_distinct`

### Name Convention
Except for the first API (`lists::have_overlap`) that returns a boolean column, the suffix `_distinct` of the rest APIs denotes that their results will be lists columns in which all list rows have been post-processed to remove duplicates. As such, their results are actually "set" columns in which each row is a "set" of distinct elements.

---

Depends on:
 * #10945
 * #11017
 * NVIDIA/cuCollections#175
 * #11052
 * #11118
 * #11100
 * #11149

Closes #10409.

Authors:
  - Nghia Truong (https://github.com/ttnghia)
  - Yunsong Wang (https://github.com/PointKernel)

Approvers:
  - Michael Wang (https://github.com/isVoid)
  - AJ Schmidt (https://github.com/ajschmidt8)
  - Bradley Dice (https://github.com/bdice)
  - Yunsong Wang (https://github.com/PointKernel)

URL: #11043
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 feature request New feature or request libcudf Affects libcudf (C++/CUDA) code. non-breaking Non-breaking change Spark Functionality that helps Spark RAPIDS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEA] Generate element labels from offsets

3 participants