Skip to content

JSON Column creation in GPU - #11714

Merged
rapids-bot[bot] merged 412 commits into
NVIDIA:branch-22.10from
karthikeyann:fea-json-column-gpu
Sep 27, 2022
Merged

rapids-bot[bot] merged 412 commits into
NVIDIA:branch-22.10from
karthikeyann:fea-json-column-gpu

Conversation

@karthikeyann

@karthikeyann karthikeyann commented Sep 19, 2022 •

Copy link
Copy Markdown
Contributor

Description

This PR generates json column creation from the traversed json tree. It has following parts

  1. reduce_to_column_tree - Reduce node tree into column tree by aggregating each property of each column and number of rows in each column.
  2. make_json_column2 - creates the GPU json column tree structure from tree and column info
  3. json_column_to_cudf_column2 - converts this GPU json column to cudf column.
  4. parse_nested_json2 - combines all json tokenizer, json tree generation, traversal, json column creation, cudf column conversion together. All steps run on device.

Depends on PR #11518 #11610
For code-review, use PR karthikeyann#5 which contains only this tree changes.

Overview

JSON has 5 categories of nodes. STRUCT, LIST, FIELD, VALUE, STRING,
STRUCT, LIST are nested types.
FIELD nodes are struct columns' keys.
VALUE node is similar to STRING column but without double quotes. Actual datatype conversion happens in json_column_to_cudf_column2

Tree Representation tree_meta_t has 4 data members.

  1. node categories
  2. node parents' id
  3. node level
  4. node's string range {begin, end} (as 2 vectors)

Currently supported JSON formats are records orient, and JSON lines.

This PR - Detailed explanation

This PR has 3 steps.

  1. reduce_to_column_tree
    • Required to compute total number of columns, column type, nested column structure, and number of rows in each column.
    • Generates tree_meta_t data members for column.
      • Sort node tree by col_id (stable sort)
      • reduce_by_key custom_op on node_categories, collapses to column category
      • unique_by_key_copy by col_id, copies first parent_node_id, string_ranges. This parent_node_id will be transformed to parent_column_id.
      • reduce_by_key max on row_offsets gives maximum row offset in each column, Propagate list column children's max row offset to their children because sometime structs may miss entries, so parent list gives correct count.
  2. make_json_column2
    • Converts nodes to GPU json columns in tree structure
      • get column tree, transfer column names to host.
      • Create d_json_column for non-field columns.
      • if 2 columns occurs on same path, and one of them is nested and other is string column, discard the string column.
      • For STRUCT, LIST, VALUE, STRING nodes, set the validity bits, and copy string {begin, end} range to string_offsets and string length.
      • Compute list offset
      • Perform scan max operation on offsets. (to fill 0's with previous offset value).
    • Now the d_json_column is nested, and contains offsets, validity bits, unparsed unconverted string information.
  3. json_column_to_cudf_column2 - converts this GPU json column to cudf column.
    • Recursively goes over each d_json_column and converts to cudf::column by inferring the type, parsing the string to type, and setting validity bits further.
  4. parse_nested_json2 - combines all json tokenizer, json tree generation, traversal, json column creation, cudf column conversion together. All steps run on device.

Checklist

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

elstehle and others added 30 commits September 1, 2022 10:13

@upsj upsj left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

flushing my first comments, only json_column_to_cudf_colum2 and make_json_column2 to go. Looks really good so far

Comment thread cpp/src/io/json/json_tree.cu Resolved
Comment thread cpp/src/io/json/nested_json.hpp Resolved
Comment thread cpp/src/io/json/nested_json.hpp Outdated
// Map of child columns, if applicable.
// Following "element" as the default child column's name of a list column
// Using the struct's field names
std::map<std::string, d_json_column> child_columns;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you keep the order in column_order:

Suggested change
std::map<std::string, d_json_column> child_columns;
std::unordered_map<std::string, d_json_column> child_columns;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I followed the same types used in json_column. will optimize this in a follow up PR.

Comment on lines +126 to +136
thrust::reduce_by_key(rmm::exec_policy(stream),
col_ids.begin(),
col_ids.end(),
row_offsets.begin(),
unique_col_ids.begin(),
max_row_offsets.begin(),
thrust::equal_to<size_type>(),
thrust::maximum<size_type>());
// 3. reduce_by_key {col_id}, {node_categories} - custom opp (*+v=*, v+v=v, *+#=E)
rmm::device_uvector<NodeT> column_categories(num_columns, stream);
thrust::reduce_by_key(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These two could be fused, though I'm not sure it's worth the effort

static_cast<size_type>(thrust::get<1>(offsets) - thrust::get<0>(offsets)));
});
auto d_column_names = cudf::make_strings_column(string_views, stream);
auto to_host = [](auto const& col) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: This (almost) IIFE seems redundant, if you return its result from the function again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is mostly for readability, and modularity.

Comment thread cpp/src/io/json/json_column.cu Resolved
// build std::string vector from chars and offsets
std::vector<std::string> host_data;
host_data.reserve(col.size());
std::transform(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nice! I wasn't aware of the two-range version of this algorithm

Comment thread cpp/src/io/json/json_column.cu Resolved

@upsj upsj left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

there are some details of the columnar layout I'm not familiar enough with to evaluate them, but the rest (algorithms, code) looks really good!

parent_col_ids = parent_col_ids.begin(),
max_row_offsets = max_row_offsets.begin()] __device__(size_type col_id) {
auto parent_col_id = parent_col_ids[col_id];
while (parent_col_id != parent_node_sentinel and

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If this ever became a performance issue for complex structs, you could consider caching the struct roots with a parallel path compression-like approach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

won't it be susceptible to RAW, WAR race conditions? (In this case, it may not matter since the path will end up correct anyway.)
Also, will do it in another PR for optimizations.

@upsj upsj Sep 26, 2022 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, you'd probably need atomic loads/stores to do it safely. But this may not be worth the effort anyways, just a random idea :)

Comment thread cpp/src/io/json/json_column.cu Resolved
d_columns_data[original_col_ids[parent_node_id]]
.child_offsets[row_offsets[parent_node_id]] = ordered_row_offsets[i];
}
// TODO: verify if this code is right. check with more test cases.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This should be correct if the ordered_parent_node_ids are also grouped together (not necessarily sorted, but all equal elements in the same range). I'd assume this is the case, but maybe you can confirm.

@github-actions github-actions Bot added the Python Affects Python cuDF API. label Sep 26, 2022
@karthikeyann
karthikeyann requested a review from vuule September 26, 2022 19:37
@galipremsagar

Copy link
Copy Markdown
Contributor

rerun tests

1 similar comment
@karthikeyann

Copy link
Copy Markdown
Contributor Author

rerun tests

@vuule vuule left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great stuff 🔥
I have one major suggestion (fallback to CPU code) and a bunch of non-blocking nitpicks.

auto data = host_span<char const>(reinterpret_cast<char const*>(buffer.data()), buffer.size());

return cudf::io::json::detail::parse_nested_json(data, reader_opts, stream, mr);
return cudf::io::json::detail::parse_nested_json2(data, reader_opts, stream, mr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would something like this make sense?

Suggested change
return cudf::io::json::detail::parse_nested_json2(data, reader_opts, stream, mr);
try {
return cudf::io::json::detail::parse_nested_json2(data, reader_opts, stream, mr);
} catch (cudf::logic_error const& err) {
return cudf::io::json::detail::parse_nested_json(data, reader_opts, stream, mr);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added a print to stdout about fallback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated.

Comment thread cpp/tests/io/nested_json_test.cpp Resolved
Comment thread cpp/src/io/json/nested_json.hpp Resolved
Comment thread cpp/src/io/json/nested_json.hpp Resolved
Comment thread cpp/src/io/json/json_column.cu Resolved
Comment thread cpp/src/io/json/json_column.cu Resolved
Comment thread cpp/src/io/json/json_column.cu Resolved
Comment thread cpp/src/io/json/json_column.cu Resolved
Comment thread cpp/src/io/json/json_column.cu Resolved
Comment thread cpp/src/io/json/json_column.cu Resolved
@karthikeyann
karthikeyann requested a review from vuule September 26, 2022 22:15
@karthikeyann
karthikeyann requested a review from vuule September 27, 2022 04:34

@vuule vuule left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome PR!

@karthikeyann

Copy link
Copy Markdown
Contributor Author

@gpucibot merge

@rapids-bot
rapids-bot Bot merged commit c5d555a into NVIDIA:branch-22.10 Sep 27, 2022
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 4 - Needs Review Waiting for reviewer to review or respond CMake CMake build issue cuIO cuIO issue feature request New feature or request libcudf Affects libcudf (C++/CUDA) code. non-breaking Non-breaking change Python Affects Python cuDF API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants