-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathetdump_flatcc.h
213 lines (179 loc) · 6.22 KB
/
etdump_flatcc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* 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.
*/
#pragma once
#include <cstdint>
#include <executorch/devtools/etdump/data_sinks/buffer_data_sink.h>
#include <executorch/devtools/etdump/data_sinks/data_sink_base.h>
#include <executorch/runtime/core/event_tracer.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/core/span.h>
#include <executorch/runtime/platform/platform.h>
#define ETDUMP_VERSION 0
struct flatcc_builder;
namespace executorch {
namespace etdump {
using ::executorch::runtime::DelegateDebugIntId;
using ::executorch::runtime::EventTracerFilterBase;
using ::executorch::runtime::Result;
namespace internal {
struct ETDumpStaticAllocator {
ETDumpStaticAllocator() = default;
void
set_buffer(uint8_t* buffer, size_t total_buf_size, size_t alloc_buf_size) {
data = buffer;
data_size = alloc_buf_size;
allocated = 0;
out_size = total_buf_size - alloc_buf_size;
// The front of the buffer is the end of the allocation buffer.
// We start writing from the end of the allocation buffer, and
// move backwards.
front_cursor = &buffer[alloc_buf_size + out_size];
front_left = out_size;
}
// Pointer to backing buffer to allocate from.
uint8_t* data{nullptr};
// Size of backing buffer.
size_t data_size{0};
// Current allocation offset.
size_t allocated{0};
// Size of build buffer.
size_t out_size{0};
// Pointer to front of build buffer.
uint8_t* front_cursor{nullptr};
// Bytes left in front of front_cursor.
size_t front_left{0};
};
} // namespace internal
struct ETDumpResult {
void* buf;
size_t size;
};
class ETDumpGen : public ::executorch::runtime::EventTracer {
public:
ETDumpGen(::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0});
~ETDumpGen() override;
void clear_builder();
void create_event_block(const char* name) override;
virtual ::executorch::runtime::EventTracerEntry start_profiling(
const char* name,
::executorch::runtime::ChainID chain_id = -1,
::executorch::runtime::DebugHandle debug_handle = 0) override;
virtual void end_profiling(
::executorch::runtime::EventTracerEntry prof_entry) override;
virtual ::executorch::runtime::EventTracerEntry start_profiling_delegate(
const char* name,
DelegateDebugIntId delegate_debug_index) override;
virtual void end_profiling_delegate(
::executorch::runtime::EventTracerEntry prof_entry,
const void* metadata,
size_t metadata_len) override;
virtual void log_profiling_delegate(
const char* name,
DelegateDebugIntId delegate_debug_index,
et_timestamp_t start_time,
et_timestamp_t end_time,
const void* metadata,
size_t metadata_len) override;
virtual void track_allocation(
::executorch::runtime::AllocatorID id,
size_t size) override;
virtual ::executorch::runtime::AllocatorID track_allocator(
const char* name) override;
virtual Result<bool> log_evalue(
const ::executorch::runtime::EValue& evalue,
::executorch::runtime::LoggedEValueType evalue_type =
::executorch::runtime::LoggedEValueType::kIntermediateOutput)
override;
/**
* Log an intermediate tensor output from a delegate.
*/
virtual Result<bool> log_intermediate_output_delegate(
const char* name,
DelegateDebugIntId delegate_debug_index,
const executorch::aten::Tensor& output) override;
/**
* Log an intermediate tensor array output from a delegate.
*/
virtual Result<bool> log_intermediate_output_delegate(
const char* name,
DelegateDebugIntId delegate_debug_index,
const ::executorch::runtime::ArrayRef<executorch::aten::Tensor> output)
override;
/**
* Log an intermediate int output from a delegate.
*/
virtual Result<bool> log_intermediate_output_delegate(
const char* name,
DelegateDebugIntId delegate_debug_index,
const int& output) override;
/**
* Log an intermediate bool output from a delegate.
*/
virtual Result<bool> log_intermediate_output_delegate(
const char* name,
DelegateDebugIntId delegate_debug_index,
const bool& output) override;
/**
* Log an intermediate double output from a delegate.
*/
virtual Result<bool> log_intermediate_output_delegate(
const char* name,
DelegateDebugIntId delegate_debug_index,
const double& output) override;
/**
* Set the filter of event tracer for delegation intermediate outputs.
*/
virtual void set_delegation_intermediate_output_filter(
EventTracerFilterBase* event_tracer_filter) override;
Result<bool> set_debug_buffer(::executorch::runtime::Span<uint8_t> buffer);
void set_data_sink(DataSinkBase* data_sink);
ETDumpResult get_etdump_data();
size_t get_num_blocks();
DataSinkBase* get_data_sink();
bool is_static_etdump();
void reset();
private:
enum class State {
Init,
BlockCreated,
AddingAllocators,
AddingEvents,
Done,
};
void check_ready_to_add_events();
int64_t create_string_entry(const char* name);
/**
* Templated helper function used to log various types of intermediate output.
* Supported types include tensor, tensor array, int, bool and double.
*/
template <typename T>
Result<bool> log_intermediate_output_delegate_helper(
const char* name,
DelegateDebugIntId delegate_debug_index,
const T& output);
long write_tensor_or_raise_error(executorch::aten::Tensor tensor);
struct flatcc_builder* builder_;
size_t num_blocks_ = 0;
DataSinkBase* data_sink_;
// It is only for set_debug_buffer function.
BufferDataSink buffer_data_sink_;
int bundled_input_index_ = -1;
State state_ = State::Init;
struct internal::ETDumpStaticAllocator alloc_;
EventTracerFilterBase* filter_ = nullptr;
};
} // namespace etdump
} // namespace executorch
namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using etdump_result = ::executorch::etdump::ETDumpResult;
using ::executorch::etdump::ETDumpGen;
} // namespace executor
} // namespace torch