-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(cudf): Fix UCX output buffer lifetime after async cuDF concat #17533
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
Open
kjmph
wants to merge
7
commits into
facebookincubator:main
Choose a base branch
from
kjmph:fix/experimental-cudf/use-after-free
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+345
−18
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7bdb36a
Fix UCX output buffer lifetime after async cuDF concat
kjmph a891a35
Use spans for cuDF stream-ordering helpers
kjmph 30936d8
fix(cudf): Rebind packed table buffers to target stream
kjmph 66f43cb
fix(cudf): Reuse TLS event for deallocation fallback
kjmph 9bdae55
Update velox/experimental/cudf/vector/CudfVector.h
kjmph 5342968
Update velox/experimental/cudf/tests/CudfVectorTest.cpp
kjmph 4fb8b97
Test cuDF vector rebind with async memory resource
kjmph 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * 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 "velox/experimental/cudf/vector/CudfVector.h" | ||
|
|
||
| #include "velox/common/base/Exceptions.h" | ||
| #include "velox/vector/tests/utils/VectorTestBase.h" | ||
|
|
||
| #include <cudf/contiguous_split.hpp> | ||
| #include <cudf/utilities/error.hpp> | ||
|
|
||
| #include <rmm/device_buffer.hpp> | ||
| #include <rmm/mr/cuda_memory_resource.hpp> | ||
| #include <rmm/resource_ref.hpp> | ||
|
|
||
| #include <cuda/memory_resource> | ||
| #include <cuda/stream_ref> | ||
| #include <cuda_runtime_api.h> | ||
|
|
||
| #include <array> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| using namespace facebook::velox; | ||
| using namespace facebook::velox::cudf_velox; | ||
| using namespace facebook::velox::test; | ||
|
|
||
| namespace { | ||
|
|
||
| class TestCudaStream { | ||
| public: | ||
| TestCudaStream() { | ||
| VELOX_CHECK_EQ( | ||
| cudaStreamCreateWithFlags(&stream_, cudaStreamNonBlocking), | ||
| cudaSuccess); | ||
| } | ||
|
|
||
| ~TestCudaStream() { | ||
| if (stream_ != nullptr) { | ||
| cudaStreamDestroy(stream_); | ||
| } | ||
| } | ||
|
|
||
| rmm::cuda_stream_view view() const { | ||
| return rmm::cuda_stream_view{stream_}; | ||
| } | ||
|
|
||
| cudaStream_t value() const { | ||
| return stream_; | ||
| } | ||
|
|
||
| private: | ||
| cudaStream_t stream_{nullptr}; | ||
| }; | ||
|
|
||
| struct RecordingState { | ||
| cudaStream_t lastDeallocationStream{nullptr}; | ||
| int deallocationCount{0}; | ||
|
kjmph marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| class RecordingDeviceResource { | ||
| public: | ||
| void* allocate( | ||
| cuda::stream_ref stream, | ||
| std::size_t bytes, | ||
| std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) { | ||
| return upstream_.allocate(stream, bytes, alignment); | ||
| } | ||
|
|
||
| void deallocate( | ||
| cuda::stream_ref stream, | ||
| void* ptr, | ||
| std::size_t bytes, | ||
| std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept { | ||
| state_->lastDeallocationStream = stream.get(); | ||
| ++state_->deallocationCount; | ||
| upstream_.deallocate(stream, ptr, bytes, alignment); | ||
| } | ||
|
|
||
| void* allocate_sync( | ||
| std::size_t bytes, | ||
| std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) { | ||
| return upstream_.allocate_sync(bytes, alignment); | ||
| } | ||
|
|
||
| void deallocate_sync( | ||
| void* ptr, | ||
| std::size_t bytes, | ||
| std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept { | ||
| upstream_.deallocate_sync(ptr, bytes, alignment); | ||
| } | ||
|
|
||
| void reset() { | ||
| state_->lastDeallocationStream = nullptr; | ||
| state_->deallocationCount = 0; | ||
| } | ||
|
|
||
| int deallocationCount() const { | ||
| return state_->deallocationCount; | ||
| } | ||
|
|
||
| cudaStream_t lastDeallocationStream() const { | ||
| return state_->lastDeallocationStream; | ||
| } | ||
|
|
||
| bool operator==(const RecordingDeviceResource& other) const noexcept { | ||
| return state_ == other.state_; | ||
| } | ||
|
|
||
| private: | ||
| std::shared_ptr<RecordingState> state_{std::make_shared<RecordingState>()}; | ||
| rmm::mr::cuda_memory_resource upstream_; | ||
|
kjmph marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| void get_property( | ||
| const RecordingDeviceResource&, | ||
| cuda::mr::device_accessible) noexcept {} | ||
|
|
||
| std::unique_ptr<cudf::table> makeTable( | ||
| rmm::cuda_stream_view stream, | ||
| rmm::device_async_resource_ref mr) { | ||
| std::array<int32_t, 4> values{{1, 2, 3, 4}}; | ||
| rmm::device_buffer data(values.size() * sizeof(int32_t), stream, mr); | ||
| CUDF_CUDA_TRY(cudaMemcpyAsync( | ||
| data.data(), | ||
| values.data(), | ||
| values.size() * sizeof(int32_t), | ||
| cudaMemcpyHostToDevice, | ||
| stream.value())); | ||
|
|
||
| std::vector<std::unique_ptr<cudf::column>> columns; | ||
| columns.push_back(std::make_unique<cudf::column>( | ||
| cudf::data_type{cudf::type_id::INT32}, | ||
| static_cast<cudf::size_type>(values.size()), | ||
| std::move(data), | ||
| rmm::device_buffer{}, | ||
| 0)); | ||
| return std::make_unique<cudf::table>(std::move(columns)); | ||
| } | ||
|
|
||
| class CudfVectorTest : public ::testing::Test, public VectorTestBase { | ||
| protected: | ||
| static void SetUpTestCase() { | ||
| memory::MemoryManager::testingSetInstance(memory::MemoryManager::Options{}); | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(CudfVectorTest, rebindOwnedTableDeallocationStream) { | ||
| TestCudaStream allocationStream; | ||
| TestCudaStream targetStream; | ||
| RecordingDeviceResource resource; | ||
|
|
||
| auto table = makeTable( | ||
| allocationStream.view(), | ||
| rmm::to_device_async_resource_ref_checked(&resource)); | ||
| allocationStream.view().synchronize(); | ||
|
|
||
| auto vector = std::make_shared<CudfVector>( | ||
| pool_.get(), | ||
| ROW({"c0"}, {INTEGER()}), | ||
| table->num_rows(), | ||
| std::move(table), | ||
| allocationStream.view()); | ||
| resource.reset(); | ||
|
|
||
| ASSERT_TRUE(vector->rebindStream(targetStream.view())); | ||
| vector.reset(); | ||
|
|
||
| EXPECT_GT(resource.deallocationCount(), 0); | ||
| EXPECT_EQ(resource.lastDeallocationStream(), targetStream.value()); | ||
| } | ||
|
|
||
| TEST_F(CudfVectorTest, rebindPackedTableDeallocationStream) { | ||
| TestCudaStream allocationStream; | ||
| TestCudaStream targetStream; | ||
| RecordingDeviceResource resource; | ||
|
|
||
| auto table = makeTable( | ||
| allocationStream.view(), cudf::get_current_device_resource_ref()); | ||
| auto packedColumns = cudf::pack( | ||
| table->view(), | ||
| allocationStream.view(), | ||
| rmm::to_device_async_resource_ref_checked(&resource)); | ||
| allocationStream.view().synchronize(); | ||
| auto tableView = cudf::unpack(packedColumns); | ||
| auto packedTable = std::make_unique<cudf::packed_table>( | ||
| cudf::packed_table{tableView, std::move(packedColumns)}); | ||
|
|
||
| // Model the intra-node UCX path: the packed buffer was allocated on | ||
| // allocationStream, but downstream work is associated with targetStream. | ||
| // The CudfVector logical stream is already targetStream, but the packed | ||
| // buffer's deallocation stream is still allocationStream. rebindStream must | ||
| // update the packed buffer even when stream_ already matches targetStream. | ||
| auto vector = std::make_shared<CudfVector>( | ||
| pool_.get(), | ||
| ROW({"c0"}, {INTEGER()}), | ||
| packedTable->table.num_rows(), | ||
| std::move(packedTable), | ||
| targetStream.view()); | ||
| resource.reset(); | ||
|
|
||
| ASSERT_TRUE(vector->rebindStream(targetStream.view())); | ||
| vector.reset(); | ||
|
|
||
| EXPECT_GT(resource.deallocationCount(), 0); | ||
| EXPECT_EQ(resource.lastDeallocationStream(), targetStream.value()); | ||
| } | ||
|
|
||
| } // namespace | ||
Oops, something went wrong.
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.