|
| 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 <gtest/gtest.h> |
| 10 | +#include <cstdint> |
| 11 | +#include <cstdlib> |
| 12 | +#include <cstring> |
| 13 | + |
| 14 | +#include <executorch/devtools/etdump/data_sinks/buffer_data_sink.h> |
| 15 | +#include <executorch/devtools/etdump/etdump_flatcc.h> |
| 16 | +#include <executorch/devtools/etdump/etdump_schema_flatcc_builder.h> |
| 17 | +#include <executorch/devtools/etdump/etdump_schema_flatcc_reader.h> |
| 18 | +#include <executorch/runtime/core/device_allocator.h> |
| 19 | +#include <executorch/runtime/core/portable_type/tensor_impl.h> |
| 20 | +#include <executorch/runtime/core/test/mock_cuda_allocator.h> |
| 21 | +#include <executorch/runtime/platform/runtime.h> |
| 22 | + |
| 23 | +using namespace ::executorch::etdump; |
| 24 | +using namespace ::executorch::runtime; |
| 25 | +using namespace ::executorch::runtime::etensor; |
| 26 | +using namespace ::executorch::runtime::testing; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +// Backs its device memory with host memory, so the copy path runs without a |
| 31 | +// GPU. |
| 32 | +MockCudaAllocator g_mock_cuda; |
| 33 | + |
| 34 | +class ETDumpDeviceTest : public ::testing::Test { |
| 35 | + protected: |
| 36 | + // The registry only accepts allocators with static lifetime and aborts on a |
| 37 | + // second registration for the same device type, so register once per binary. |
| 38 | + static void SetUpTestSuite() { |
| 39 | + register_device_allocator(&g_mock_cuda); |
| 40 | + } |
| 41 | + |
| 42 | + void SetUp() override { |
| 43 | + runtime_init(); |
| 44 | + g_mock_cuda.reset(); |
| 45 | + etdump_gen_ = new ETDumpGen(); |
| 46 | + debug_buf_ = malloc(kDebugBufSize); |
| 47 | + } |
| 48 | + |
| 49 | + void TearDown() override { |
| 50 | + delete etdump_gen_; |
| 51 | + free(debug_buf_); |
| 52 | + } |
| 53 | + |
| 54 | + static constexpr size_t kDebugBufSize = 2048; |
| 55 | + |
| 56 | + ETDumpGen* etdump_gen_ = nullptr; |
| 57 | + void* debug_buf_ = nullptr; |
| 58 | + int32_t sizes_[1] = {4}; |
| 59 | + uint8_t dim_order_[1] = {0}; |
| 60 | + int32_t strides_[1] = {1}; |
| 61 | +}; |
| 62 | + |
| 63 | +TEST_F(ETDumpDeviceTest, LogTensorOnDeviceCopiesItBackToHost) { |
| 64 | + ASSERT_EQ(get_device_allocator(DeviceType::CUDA), &g_mock_cuda); |
| 65 | + |
| 66 | + float device_data[] = {1.5f, 2.5f, 3.5f, 4.5f}; |
| 67 | + TensorImpl impl( |
| 68 | + ScalarType::Float, |
| 69 | + 1, |
| 70 | + sizes_, |
| 71 | + device_data, |
| 72 | + dim_order_, |
| 73 | + strides_, |
| 74 | + TensorShapeDynamism::STATIC, |
| 75 | + DeviceType::CUDA, |
| 76 | + 0); |
| 77 | + Tensor tensor(&impl); |
| 78 | + |
| 79 | + auto buffer_data_sink = BufferDataSink::create(debug_buf_, kDebugBufSize); |
| 80 | + ASSERT_TRUE(buffer_data_sink.ok()); |
| 81 | + |
| 82 | + etdump_gen_->create_event_block("test_block"); |
| 83 | + etdump_gen_->set_data_sink(&buffer_data_sink.get()); |
| 84 | + etdump_gen_->log_evalue(EValue(tensor)); |
| 85 | + |
| 86 | + EXPECT_EQ(g_mock_cuda.d2h_count_, 1); |
| 87 | + EXPECT_EQ(g_mock_cuda.last_d2h_size_, sizeof(device_data)); |
| 88 | + EXPECT_EQ(g_mock_cuda.last_d2h_src_, device_data); |
| 89 | + EXPECT_EQ(g_mock_cuda.last_d2h_index_, 0); |
| 90 | + |
| 91 | + ETDumpResult result = etdump_gen_->get_etdump_data(); |
| 92 | + ASSERT_TRUE(result.buf != nullptr); |
| 93 | + ASSERT_TRUE(result.size != 0); |
| 94 | + |
| 95 | + size_t size = 0; |
| 96 | + void* buf = flatbuffers_read_size_prefix(result.buf, &size); |
| 97 | + etdump_ETDump_table_t etdump = |
| 98 | + etdump_ETDump_as_root_with_identifier(buf, etdump_ETDump_file_identifier); |
| 99 | + etdump_RunData_vec_t run_data_vec = etdump_ETDump_run_data(etdump); |
| 100 | + ASSERT_EQ(etdump_RunData_vec_len(run_data_vec), 1); |
| 101 | + etdump_Event_vec_t events = |
| 102 | + etdump_RunData_events(etdump_RunData_vec_at(run_data_vec, 0)); |
| 103 | + ASSERT_EQ(etdump_Event_vec_len(events), 1); |
| 104 | + etdump_Tensor_table_t logged = |
| 105 | + etdump_Value_tensor(etdump_DebugEvent_debug_entry( |
| 106 | + etdump_Event_debug_event(etdump_Event_vec_at(events, 0)))); |
| 107 | + |
| 108 | + // The sink aligns each blob, so the bytes start at the recorded offset rather |
| 109 | + // than at the front of the buffer. |
| 110 | + const long offset = etdump_Tensor_offset(logged); |
| 111 | + ASSERT_GE(offset, 0); |
| 112 | + EXPECT_EQ( |
| 113 | + memcmp((uint8_t*)debug_buf_ + offset, device_data, sizeof(device_data)), |
| 114 | + 0); |
| 115 | + |
| 116 | + free(result.buf); |
| 117 | +} |
| 118 | + |
| 119 | +TEST_F(ETDumpDeviceTest, LogTensorOnCpuDoesNotStageThroughTheAllocator) { |
| 120 | + float host_data[] = {1.5f, 2.5f, 3.5f, 4.5f}; |
| 121 | + TensorImpl impl( |
| 122 | + ScalarType::Float, |
| 123 | + 1, |
| 124 | + sizes_, |
| 125 | + host_data, |
| 126 | + dim_order_, |
| 127 | + strides_, |
| 128 | + TensorShapeDynamism::STATIC, |
| 129 | + DeviceType::CPU, |
| 130 | + 0); |
| 131 | + Tensor tensor(&impl); |
| 132 | + |
| 133 | + auto buffer_data_sink = BufferDataSink::create(debug_buf_, kDebugBufSize); |
| 134 | + ASSERT_TRUE(buffer_data_sink.ok()); |
| 135 | + |
| 136 | + etdump_gen_->create_event_block("test_block"); |
| 137 | + etdump_gen_->set_data_sink(&buffer_data_sink.get()); |
| 138 | + etdump_gen_->log_evalue(EValue(tensor)); |
| 139 | + |
| 140 | + EXPECT_EQ(g_mock_cuda.d2h_count_, 0); |
| 141 | + |
| 142 | + ETDumpResult result = etdump_gen_->get_etdump_data(); |
| 143 | + ASSERT_TRUE(result.buf != nullptr); |
| 144 | + ASSERT_TRUE(result.size != 0); |
| 145 | + free(result.buf); |
| 146 | +} |
| 147 | + |
| 148 | +} // namespace |
0 commit comments