|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 10 | +#include <executorch/runtime/core/exec_aten/util/tensor_util.h> |
| 11 | +#include <executorch/runtime/platform/runtime.h> |
| 12 | + |
| 13 | +#include <gtest/gtest.h> |
| 14 | + |
| 15 | +#include <array> |
| 16 | +#include <memory> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +using executorch::runtime::Error; |
| 20 | +using executorch::runtime::etensor::DeviceType; |
| 21 | +using executorch::runtime::etensor::ScalarType; |
| 22 | +using executorch::runtime::etensor::Tensor; |
| 23 | +using executorch::runtime::etensor::TensorImpl; |
| 24 | +using executorch::runtime::internal::copy_tensor_data; |
| 25 | + |
| 26 | +// Copying into a memory-planned tensor uses a host memcpy. A tensor that lives |
| 27 | +// on an accelerator cannot be filled that way, and doing it anyway crashed the |
| 28 | +// process with no message, so the runtime reports it instead. |
| 29 | +class CopyTensorDataDeviceTest : public ::testing::Test { |
| 30 | + protected: |
| 31 | + void SetUp() override { |
| 32 | + executorch::runtime::runtime_init(); |
| 33 | + } |
| 34 | + |
| 35 | + // Each tensor gets its own storage, so a host-to-host copy has a distinct |
| 36 | + // destination to land in and the data can be checked afterwards. Sharing one |
| 37 | + // buffer would make the copy a self-memcpy, which is undefined and which no |
| 38 | + // assertion could distinguish from the copy being skipped. |
| 39 | + Tensor make( |
| 40 | + DeviceType device, |
| 41 | + std::array<float, 4> values = {0.0f, 0.0f, 0.0f, 0.0f}) { |
| 42 | + storages_.push_back(std::make_unique<std::array<float, 4>>(values)); |
| 43 | + impls_.push_back(std::make_unique<TensorImpl>( |
| 44 | + ScalarType::Float, |
| 45 | + static_cast<ssize_t>(sizes_.size()), |
| 46 | + sizes_.data(), |
| 47 | + storages_.back()->data(), |
| 48 | + dim_order_.data(), |
| 49 | + strides_.data(), |
| 50 | + executorch::runtime::TensorShapeDynamism::STATIC, |
| 51 | + device)); |
| 52 | + return Tensor(impls_.back().get()); |
| 53 | + } |
| 54 | + |
| 55 | + std::array<executorch::aten::SizesType, 1> sizes_{4}; |
| 56 | + std::array<executorch::aten::DimOrderType, 1> dim_order_{0}; |
| 57 | + std::array<executorch::aten::StridesType, 1> strides_{1}; |
| 58 | + std::vector<std::unique_ptr<std::array<float, 4>>> storages_; |
| 59 | + std::vector<std::unique_ptr<TensorImpl>> impls_; |
| 60 | +}; |
| 61 | + |
| 62 | +TEST_F(CopyTensorDataDeviceTest, HostToHostIsCopied) { |
| 63 | + Tensor destination = make(DeviceType::CPU); |
| 64 | + Tensor source = make(DeviceType::CPU, {1.0f, 2.0f, 3.0f, 4.0f}); |
| 65 | + EXPECT_EQ(copy_tensor_data(destination, source), Error::Ok); |
| 66 | + // Assert the data actually moved. Checking only the returned Error would |
| 67 | + // still pass if the copy were removed entirely. |
| 68 | + const float* copied = destination.const_data_ptr<float>(); |
| 69 | + ASSERT_NE(copied, nullptr); |
| 70 | + EXPECT_EQ(copied[0], 1.0f); |
| 71 | + EXPECT_EQ(copied[1], 2.0f); |
| 72 | + EXPECT_EQ(copied[2], 3.0f); |
| 73 | + EXPECT_EQ(copied[3], 4.0f); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_F(CopyTensorDataDeviceTest, ADeviceDestinationIsRefused) { |
| 77 | + // This is the case that used to crash: a planned buffer on an accelerator, |
| 78 | + // filled by a host memcpy. |
| 79 | + Tensor destination = make(DeviceType::CUDA); |
| 80 | + Tensor source = make(DeviceType::CPU); |
| 81 | + EXPECT_EQ(copy_tensor_data(destination, source), Error::NotSupported); |
| 82 | +} |
| 83 | + |
| 84 | +TEST_F(CopyTensorDataDeviceTest, ADeviceSourceIsRefused) { |
| 85 | + Tensor destination = make(DeviceType::CPU); |
| 86 | + Tensor source = make(DeviceType::CUDA); |
| 87 | + EXPECT_EQ(copy_tensor_data(destination, source), Error::NotSupported); |
| 88 | +} |
0 commit comments