-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use Arrow C Data Interface functions for Python interop #15904
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
Merged
rapids-bot
merged 32 commits into
NVIDIA:branch-24.08
from
vyasr:feat/from_arrow_c_interface_py
Jul 2, 2024
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
51b5964
First pass at adding an implementation for arrow streams
vyasr 2378bb7
Expose the new function to Python
vyasr 080542d
Update column overload in Python
vyasr 49305e3
Add missing default values
vyasr 37814c6
Formatting
vyasr 04666b0
Add basic handling for empty stream
vyasr d925a84
Properly support null columns, requires concatenate support too
vyasr 4de47ca
Handle nested types in empty table generation
vyasr 03c76c8
Some cleanup and improved use of algorithms
vyasr 6454a8a
Generalize check in concatenate
vyasr e5f0c35
Add concatenate tests for EMPTY dtype
vyasr b5dd6d8
Add main test for from_arrow_stream
vyasr 01c4de2
Add a test with an empty stream
vyasr 5bf1469
Reuse host table creation
vyasr 655ad9f
Address PR comments
vyasr dacd0d0
Merge remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr e878e92
Merge remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr cee9932
Merge remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr cb2c17c
Change from_arrow_stream to release the arrays
vyasr 3ef157c
Properly release in Python
vyasr d6f5609
Merge remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr 534741b
Merge remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr 63c2cd0
Merge remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr 93cb512
Initial attempts to get large strings working
vyasr e972907
Properly access string lengths
vyasr 3d227dd
Drop the exact equivalence comparison since libcudf will compress lar…
vyasr c03fc2a
Merge remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr 6d255d5
Block from_arrow_device on large strings
vyasr 1abe072
Remove unnecessary code specialization for chars buffer
vyasr 26fcaaf
Address reviews
vyasr cc023d7
Mergh remote-tracking branch 'upstream/branch-24.08' into feat/from_a…
vyasr e1cc728
Merge branch 'branch-24.08' into feat/from_arrow_c_interface_py
vyasr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
| * Copyright (c) 2024, NVIDIA CORPORATION. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "arrow_utilities.hpp" | ||
|
|
||
| #include <cudf/column/column_factories.hpp> | ||
| #include <cudf/detail/concatenate.hpp> | ||
| #include <cudf/detail/nvtx/ranges.hpp> | ||
| #include <cudf/interop.hpp> | ||
| #include <cudf/table/table.hpp> | ||
|
|
||
| #include <rmm/cuda_stream_view.hpp> | ||
| #include <rmm/mr/device/device_memory_resource.hpp> | ||
| #include <rmm/mr/device/per_device_resource.hpp> | ||
|
|
||
| #include <nanoarrow/nanoarrow.h> | ||
| #include <nanoarrow/nanoarrow.hpp> | ||
|
|
||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace cudf { | ||
| namespace detail { | ||
|
|
||
| namespace { | ||
|
|
||
| std::unique_ptr<column> make_empty_column_from_schema(ArrowSchema const* schema, | ||
| rmm::cuda_stream_view stream, | ||
| rmm::mr::device_memory_resource* mr) | ||
| { | ||
| ArrowSchemaView schema_view; | ||
| NANOARROW_THROW_NOT_OK(ArrowSchemaViewInit(&schema_view, schema, nullptr)); | ||
|
|
||
| auto const type{arrow_to_cudf_type(&schema_view)}; | ||
| switch (type.id()) { | ||
| case type_id::EMPTY: { | ||
| return std::make_unique<column>( | ||
| data_type(type_id::EMPTY), 0, rmm::device_buffer{}, rmm::device_buffer{}, 0); | ||
| } | ||
| case type_id::LIST: { | ||
| return cudf::make_lists_column(0, | ||
| cudf::make_empty_column(data_type{type_id::INT32}), | ||
| make_empty_column_from_schema(schema->children[0], stream, mr), | ||
| 0, | ||
| {}, | ||
| stream, | ||
| mr); | ||
| } | ||
| case type_id::STRUCT: { | ||
| std::vector<std::unique_ptr<column>> child_columns; | ||
| child_columns.reserve(schema->n_children); | ||
| std::transform( | ||
| schema->children, | ||
| schema->children + schema->n_children, | ||
| std::back_inserter(child_columns), | ||
| [&](auto const& child) { return make_empty_column_from_schema(child, stream, mr); }); | ||
| return cudf::make_structs_column(0, std::move(child_columns), 0, {}, stream, mr); | ||
| } | ||
| default: { | ||
| return cudf::make_empty_column(type); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::unique_ptr<table> from_arrow_stream(ArrowArrayStream* input, | ||
| rmm::cuda_stream_view stream, | ||
| rmm::mr::device_memory_resource* mr) | ||
| { | ||
| CUDF_EXPECTS(input != nullptr, "input ArrowArrayStream must not be NULL", std::invalid_argument); | ||
|
|
||
| // Potential future optimization: Since the from_arrow API accepts an | ||
| // ArrowSchema we're allocating one here instead of using a view, which we | ||
| // could avoid with a different underlying implementation. | ||
| ArrowSchema schema; | ||
| NANOARROW_THROW_NOT_OK(ArrowArrayStreamGetSchema(input, &schema, nullptr)); | ||
|
|
||
| std::vector<std::unique_ptr<cudf::table>> chunks; | ||
| ArrowArray chunk; | ||
| while (true) { | ||
| NANOARROW_THROW_NOT_OK(ArrowArrayStreamGetNext(input, &chunk, nullptr)); | ||
| if (chunk.release == nullptr) { break; } | ||
| chunks.push_back(from_arrow(&schema, &chunk, stream, mr)); | ||
| chunk.release(&chunk); | ||
| } | ||
| input->release(input); | ||
|
|
||
| if (chunks.empty()) { | ||
| if (schema.n_children == 0) { | ||
| schema.release(&schema); | ||
| return std::make_unique<cudf::table>(); | ||
| } | ||
|
|
||
| // If there are no chunks but the schema has children, we need to construct a suitable empty | ||
| // table. | ||
| std::vector<std::unique_ptr<cudf::column>> columns; | ||
| columns.reserve(chunks.size()); | ||
| std::transform( | ||
| schema.children, | ||
| schema.children + schema.n_children, | ||
| std::back_inserter(columns), | ||
| [&](auto const& child) { return make_empty_column_from_schema(child, stream, mr); }); | ||
| schema.release(&schema); | ||
| return std::make_unique<cudf::table>(std::move(columns)); | ||
| } | ||
|
|
||
| schema.release(&schema); | ||
|
|
||
| auto chunk_views = std::vector<table_view>{}; | ||
| chunk_views.reserve(chunks.size()); | ||
| std::transform( | ||
| chunks.begin(), chunks.end(), std::back_inserter(chunk_views), [](auto const& chunk) { | ||
| return chunk->view(); | ||
| }); | ||
| return cudf::detail::concatenate(chunk_views, stream, mr); | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| std::unique_ptr<table> from_arrow_stream(ArrowArrayStream* input, | ||
| rmm::cuda_stream_view stream, | ||
| rmm::mr::device_memory_resource* mr) | ||
| { | ||
| CUDF_FUNC_RANGE(); | ||
| return detail::from_arrow_stream(input, stream, mr); | ||
| } | ||
| } // namespace cudf | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.