-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Report a clear error instead of crashing on a device-planned copy #21960
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
Merged
Changes from all commits
Commits
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
88 changes: 88 additions & 0 deletions
88
runtime/core/exec_aten/util/test/copy_tensor_data_device_test.cpp
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,88 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include <executorch/runtime/core/exec_aten/exec_aten.h> | ||
| #include <executorch/runtime/core/exec_aten/util/tensor_util.h> | ||
| #include <executorch/runtime/platform/runtime.h> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <array> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| using executorch::runtime::Error; | ||
| using executorch::runtime::etensor::DeviceType; | ||
| using executorch::runtime::etensor::ScalarType; | ||
| using executorch::runtime::etensor::Tensor; | ||
| using executorch::runtime::etensor::TensorImpl; | ||
| using executorch::runtime::internal::copy_tensor_data; | ||
|
|
||
| // Copying into a memory-planned tensor uses a host memcpy. A tensor that lives | ||
| // on an accelerator cannot be filled that way, and doing it anyway crashed the | ||
| // process with no message, so the runtime reports it instead. | ||
| class CopyTensorDataDeviceTest : public ::testing::Test { | ||
| protected: | ||
| void SetUp() override { | ||
| executorch::runtime::runtime_init(); | ||
| } | ||
|
|
||
| // Each tensor gets its own storage, so a host-to-host copy has a distinct | ||
| // destination to land in and the data can be checked afterwards. Sharing one | ||
| // buffer would make the copy a self-memcpy, which is undefined and which no | ||
| // assertion could distinguish from the copy being skipped. | ||
| Tensor make( | ||
| DeviceType device, | ||
| std::array<float, 4> values = {0.0f, 0.0f, 0.0f, 0.0f}) { | ||
| storages_.push_back(std::make_unique<std::array<float, 4>>(values)); | ||
| impls_.push_back(std::make_unique<TensorImpl>( | ||
| ScalarType::Float, | ||
| static_cast<ssize_t>(sizes_.size()), | ||
| sizes_.data(), | ||
| storages_.back()->data(), | ||
| dim_order_.data(), | ||
| strides_.data(), | ||
| executorch::runtime::TensorShapeDynamism::STATIC, | ||
| device)); | ||
| return Tensor(impls_.back().get()); | ||
| } | ||
|
|
||
| std::array<executorch::aten::SizesType, 1> sizes_{4}; | ||
| std::array<executorch::aten::DimOrderType, 1> dim_order_{0}; | ||
| std::array<executorch::aten::StridesType, 1> strides_{1}; | ||
| std::vector<std::unique_ptr<std::array<float, 4>>> storages_; | ||
| std::vector<std::unique_ptr<TensorImpl>> impls_; | ||
| }; | ||
|
|
||
| TEST_F(CopyTensorDataDeviceTest, HostToHostIsCopied) { | ||
| Tensor destination = make(DeviceType::CPU); | ||
| Tensor source = make(DeviceType::CPU, {1.0f, 2.0f, 3.0f, 4.0f}); | ||
| EXPECT_EQ(copy_tensor_data(destination, source), Error::Ok); | ||
| // Assert the data actually moved. Checking only the returned Error would | ||
| // still pass if the copy were removed entirely. | ||
| const float* copied = destination.const_data_ptr<float>(); | ||
| ASSERT_NE(copied, nullptr); | ||
| EXPECT_EQ(copied[0], 1.0f); | ||
| EXPECT_EQ(copied[1], 2.0f); | ||
| EXPECT_EQ(copied[2], 3.0f); | ||
| EXPECT_EQ(copied[3], 4.0f); | ||
| } | ||
|
|
||
| TEST_F(CopyTensorDataDeviceTest, ADeviceDestinationIsRefused) { | ||
| // This is the case that used to crash: a planned buffer on an accelerator, | ||
| // filled by a host memcpy. | ||
| Tensor destination = make(DeviceType::CUDA); | ||
| Tensor source = make(DeviceType::CPU); | ||
| EXPECT_EQ(copy_tensor_data(destination, source), Error::NotSupported); | ||
| } | ||
|
|
||
| TEST_F(CopyTensorDataDeviceTest, ADeviceSourceIsRefused) { | ||
| Tensor destination = make(DeviceType::CPU); | ||
| Tensor source = make(DeviceType::CUDA); | ||
| EXPECT_EQ(copy_tensor_data(destination, source), Error::NotSupported); | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @Gasoonjia how are we supposed to copy back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outputs never reach this code: the only caller is
set_input(method.cpp:1250), and a memory-planned output is refused earlier byset_output_data_ptrwithInvalidState. For a planned input that lives on a device we could do a real host to device copy, sinceDeviceAllocator::copy_host_to_deviceexists anddevice_memory_buffer.cpp:18already resolves an allocator from runtime core, but that adds hidden stream and sync behaviour, so I would rather keep the error here and add the copy as its own change if you want it.