|
| 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/devtools/etdump/data_sink.h> |
| 10 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 11 | +#include <executorch/runtime/core/span.h> |
| 12 | +#include <executorch/runtime/platform/runtime.h> |
| 13 | +#include <executorch/test/utils/DeathTest.h> |
| 14 | +#include <gtest/gtest.h> |
| 15 | + |
| 16 | +using namespace ::testing; |
| 17 | +using executorch::aten::ScalarType; |
| 18 | +using executorch::aten::Tensor; |
| 19 | +using ::executorch::runtime::Span; |
| 20 | +using torch::executor::testing::TensorFactory; |
| 21 | + |
| 22 | +class DataSinkTest : public ::testing::Test { |
| 23 | + protected: |
| 24 | + void SetUp() override { |
| 25 | + torch::executor::runtime_init(); |
| 26 | + // Allocate a small buffer for testing |
| 27 | + buffer_size_ = 128; // Small size for testing |
| 28 | + buffer_ptr_ = malloc(buffer_size_); |
| 29 | + buffer_ = Span<uint8_t>(static_cast<uint8_t*>(buffer_ptr_), buffer_size_); |
| 30 | + data_sink_ = std::make_unique<executorch::etdump::DataSink>(buffer_); |
| 31 | + } |
| 32 | + |
| 33 | + void TearDown() override { |
| 34 | + free(buffer_ptr_); |
| 35 | + } |
| 36 | + |
| 37 | + size_t buffer_size_; |
| 38 | + void* buffer_ptr_; |
| 39 | + Span<uint8_t> buffer_; |
| 40 | + std::unique_ptr<executorch::etdump::DataSink> data_sink_; |
| 41 | +}; |
| 42 | + |
| 43 | +TEST_F(DataSinkTest, StorageSizeCheck) { |
| 44 | + EXPECT_EQ(data_sink_->get_storage_size(), buffer_size_); |
| 45 | +} |
| 46 | + |
| 47 | +TEST_F(DataSinkTest, WriteOneTensorAndCheckData) { |
| 48 | + TensorFactory<ScalarType::Float> tf; |
| 49 | + Tensor tensor = tf.make({1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 50 | + |
| 51 | + size_t offset = data_sink_->write_tensor(tensor); |
| 52 | + EXPECT_NE(offset, static_cast<size_t>(-1)); |
| 53 | + |
| 54 | + // Check that the data in the buffer matches the tensor data |
| 55 | + const float* buffer_data = |
| 56 | + reinterpret_cast<const float*>(buffer_.data() + offset); |
| 57 | + for (size_t i = 0; i < tensor.numel(); ++i) { |
| 58 | + EXPECT_EQ(buffer_data[i], tensor.const_data_ptr<float>()[i]); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +TEST_F(DataSinkTest, WriteMultiTensorsAndCheckData) { |
| 63 | + TensorFactory<ScalarType::Float> tf; |
| 64 | + std::vector<Tensor> tensors = { |
| 65 | + tf.make({1, 4}, {1.0, 2.0, 3.0, 4.0}), |
| 66 | + tf.make({1, 4}, {5.0, 6.0, 7.0, 8.0})}; |
| 67 | + size_t offset = 0; |
| 68 | + for (const auto& tensor : tensors) { |
| 69 | + offset = data_sink_->write_tensor(tensor); |
| 70 | + EXPECT_NE(offset, static_cast<size_t>(-1)); |
| 71 | + // Check that the data in the buffer matches the tensor data |
| 72 | + const float* buffer_data = |
| 73 | + reinterpret_cast<const float*>(buffer_.data() + offset); |
| 74 | + for (size_t i = 0; i < tensor.numel(); ++i) { |
| 75 | + EXPECT_EQ(buffer_data[i], tensor.const_data_ptr<float>()[i]); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(DataSinkTest, PointerAlignmentCheck) { |
| 81 | + TensorFactory<ScalarType::Float> tf; |
| 82 | + Tensor tensor = tf.make({1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 83 | + size_t offset = data_sink_->write_tensor(tensor); |
| 84 | + EXPECT_NE(offset, static_cast<size_t>(-1)); |
| 85 | + // Check that the offset pointer is 64-byte aligned |
| 86 | + const uint8_t* offset_ptr = buffer_.data() + offset; |
| 87 | + EXPECT_EQ(reinterpret_cast<uintptr_t>(offset_ptr) % 64, 0); |
| 88 | +} |
| 89 | + |
| 90 | +TEST_F(DataSinkTest, WriteUntilOverflow) { |
| 91 | + TensorFactory<ScalarType::Float> tf; |
| 92 | + Tensor tensor = tf.zeros({1, 8}); // Large tensor to fill the buffer |
| 93 | + |
| 94 | + // Write tensors until we run out of space |
| 95 | + for (size_t i = 0; i < 2; i++) { |
| 96 | + data_sink_->write_tensor(tensor); |
| 97 | + } |
| 98 | + |
| 99 | + // Attempting to write another tensor should raise an error |
| 100 | + ET_EXPECT_DEATH( |
| 101 | + data_sink_->write_tensor(tensor), |
| 102 | + "Ran out of space to store tensor data."); |
| 103 | +} |
0 commit comments