Skip to content

Commit 41319f9

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
create MetaETDumpGen class to abstract copy_tensor_to_debug_buffer function (#8157)
Summary: This diff creates a new class called `MetaETDumpGen` to abstract the `copy_tensor_to_debug_buffer` function from the `ETDumpGen` class, while keep the `ETDumpGen` class as it is, which makes user able to implement their own way dumping data into buffer, while make the API back-compatible. Differential Revision: D69079952
1 parent 6b58e2e commit 41319f9

File tree

2 files changed

+77
-55
lines changed

2 files changed

+77
-55
lines changed

devtools/etdump/etdump_flatcc.cpp

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static uint8_t* alignPointer(void* ptr, size_t alignment) {
107107
} // namespace
108108

109109
// Constructor implementation
110-
ETDumpGen::ETDumpGen(Span<uint8_t> buffer) {
110+
ETDumpGenBase::ETDumpGenBase(Span<uint8_t> buffer) {
111111
constexpr size_t max_alloc_buf_size = 128 * 1024;
112112

113113
// Initialize the flatcc builder_ using the buffer and buffer size.
@@ -140,14 +140,14 @@ ETDumpGen::ETDumpGen(Span<uint8_t> buffer) {
140140
reset();
141141
}
142142

143-
ETDumpGen::~ETDumpGen() {
143+
ETDumpGenBase::~ETDumpGenBase() {
144144
flatcc_builder_clear(builder_);
145145
if (!is_static_etdump()) {
146146
free(builder_);
147147
}
148148
}
149149

150-
void ETDumpGen::reset() {
150+
void ETDumpGenBase::reset() {
151151
state_ = State::Init;
152152
num_blocks_ = 0;
153153
flatcc_builder_reset(builder_);
@@ -158,7 +158,7 @@ void ETDumpGen::reset() {
158158
etdump_ETDump_run_data_push_start(builder_);
159159
}
160160

161-
void ETDumpGen::create_event_block(const char* name) {
161+
void ETDumpGenBase::create_event_block(const char* name) {
162162
if (state_ == State::AddingEvents) {
163163
etdump_RunData_events_end(builder_);
164164
} else if (state_ == State::Done) {
@@ -176,24 +176,24 @@ void ETDumpGen::create_event_block(const char* name) {
176176
state_ = State::BlockCreated;
177177
}
178178

179-
int64_t ETDumpGen::create_string_entry(const char* name) {
179+
int64_t ETDumpGenBase::create_string_entry(const char* name) {
180180
return flatbuffers_string_create_str(builder_, name);
181181
}
182182

183-
// ETDumpGen has the following possible states, ETDumpGen_Init,
184-
// ETDumpGen_Block_Created, ETDumpGen_Adding_Allocators,
183+
// ETDumpGenBase family classes has the following possible states,
184+
// ETDumpGen_Init, ETDumpGen_Block_Created, ETDumpGen_Adding_Allocators,
185185
// ETDumpGen_Adding_Events. Right after boot-up the state of ETDump will be
186186
// ETDumpGen_Init. At this point we have an option of adding allocators that
187187
// we want to track. Once we've completed adding the allocators we want to track
188-
// we will close the allocators table and move ETDumpGen to the
189-
// ETDumpGen_Adding_Events state. After this point we can start adding events to
190-
// ETDump as we wish.
191-
// The reason we need to maintain this state machine inside of ETDumpGen is
192-
// because, once a table of one type has been closed and another table of a
193-
// different type is opened after it we cannot open another table of the first
194-
// type again. In this case once we close the allocators table and start pushing
195-
// to the events table we cannot push to the allocators table again.
196-
void ETDumpGen::check_ready_to_add_events() {
188+
// we will close the allocators table and move ETDumpGenBase family classes to
189+
// the ETDumpGen_Adding_Events state. After this point we can start adding
190+
// events to ETDump as we wish. The reason we need to maintain this state
191+
// machine inside of ETDumpGenBase family classes is because, once a table of
192+
// one type has been closed and another table of a different type is opened
193+
// after it we cannot open another table of the first type again. In this case
194+
// once we close the allocators table and start pushing to the events table we
195+
// cannot push to the allocators table again.
196+
void ETDumpGenBase::check_ready_to_add_events() {
197197
if (state_ != State::AddingEvents) {
198198
ET_CHECK_MSG(
199199
(state_ == State::AddingAllocators || state_ == State::BlockCreated),
@@ -206,7 +206,7 @@ void ETDumpGen::check_ready_to_add_events() {
206206
}
207207
}
208208

209-
EventTracerEntry ETDumpGen::start_profiling(
209+
EventTracerEntry ETDumpGenBase::start_profiling(
210210
const char* name,
211211
ChainID chain_id,
212212
DebugHandle debug_handle) {
@@ -227,7 +227,7 @@ EventTracerEntry ETDumpGen::start_profiling(
227227

228228
// TODO: Update all occurrences of the ProfileEvent calls once the
229229
// EventTracerEntry struct is updated.
230-
EventTracerEntry ETDumpGen::start_profiling_delegate(
230+
EventTracerEntry ETDumpGenBase::start_profiling_delegate(
231231
const char* name,
232232
DebugHandle delegate_debug_index) {
233233
ET_CHECK_MSG(
@@ -247,7 +247,7 @@ EventTracerEntry ETDumpGen::start_profiling_delegate(
247247
return prof_entry;
248248
}
249249

250-
void ETDumpGen::end_profiling_delegate(
250+
void ETDumpGenBase::end_profiling_delegate(
251251
EventTracerEntry event_tracer_entry,
252252
const void* metadata,
253253
size_t metadata_len) {
@@ -280,7 +280,7 @@ void ETDumpGen::end_profiling_delegate(
280280
etdump_RunData_events_push_end(builder_);
281281
}
282282

283-
void ETDumpGen::log_profiling_delegate(
283+
void ETDumpGenBase::log_profiling_delegate(
284284
const char* name,
285285
DebugHandle delegate_debug_index,
286286
et_timestamp_t start_time,
@@ -312,43 +312,43 @@ void ETDumpGen::log_profiling_delegate(
312312
etdump_RunData_events_push_end(builder_);
313313
}
314314

315-
void ETDumpGen::log_intermediate_output_delegate(
315+
void ETDumpGenBase::log_intermediate_output_delegate(
316316
const char* name,
317317
DebugHandle delegate_debug_index,
318318
const Tensor& output) {
319319
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
320320
}
321321

322-
void ETDumpGen::log_intermediate_output_delegate(
322+
void ETDumpGenBase::log_intermediate_output_delegate(
323323
const char* name,
324324
DebugHandle delegate_debug_index,
325325
const ArrayRef<Tensor> output) {
326326
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
327327
}
328328

329-
void ETDumpGen::log_intermediate_output_delegate(
329+
void ETDumpGenBase::log_intermediate_output_delegate(
330330
const char* name,
331331
DebugHandle delegate_debug_index,
332332
const int& output) {
333333
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
334334
}
335335

336-
void ETDumpGen::log_intermediate_output_delegate(
336+
void ETDumpGenBase::log_intermediate_output_delegate(
337337
const char* name,
338338
DebugHandle delegate_debug_index,
339339
const bool& output) {
340340
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
341341
}
342342

343-
void ETDumpGen::log_intermediate_output_delegate(
343+
void ETDumpGenBase::log_intermediate_output_delegate(
344344
const char* name,
345345
DebugHandle delegate_debug_index,
346346
const double& output) {
347347
log_intermediate_output_delegate_helper(name, delegate_debug_index, output);
348348
}
349349

350350
template <typename T>
351-
void ETDumpGen::log_intermediate_output_delegate_helper(
351+
void ETDumpGenBase::log_intermediate_output_delegate_helper(
352352
const char* name,
353353
DebugHandle delegate_debug_index,
354354
const T& output) {
@@ -430,7 +430,7 @@ void ETDumpGen::log_intermediate_output_delegate_helper(
430430
etdump_RunData_events_push_end(builder_);
431431
}
432432

433-
void ETDumpGen::end_profiling(EventTracerEntry prof_entry) {
433+
void ETDumpGenBase::end_profiling(EventTracerEntry prof_entry) {
434434
et_timestamp_t end_time = et_pal_current_ticks();
435435
ET_CHECK_MSG(
436436
prof_entry.delegate_event_id_type == DelegateDebugIdType::kNone,
@@ -451,7 +451,7 @@ void ETDumpGen::end_profiling(EventTracerEntry prof_entry) {
451451
etdump_RunData_events_push_end(builder_);
452452
}
453453

454-
AllocatorID ETDumpGen::track_allocator(const char* name) {
454+
AllocatorID ETDumpGenBase::track_allocator(const char* name) {
455455
ET_CHECK_MSG(
456456
(state_ == State::BlockCreated || state_ == State::AddingAllocators),
457457
"Allocators can only be added immediately after a new block is created and before any events are added.");
@@ -464,7 +464,7 @@ AllocatorID ETDumpGen::track_allocator(const char* name) {
464464
return etdump_RunData_allocators_reserved_len(builder_);
465465
}
466466

467-
void ETDumpGen::track_allocation(
467+
void ETDumpGenBase::track_allocation(
468468
AllocatorID allocator_id,
469469
size_t allocation_size) {
470470
check_ready_to_add_events();
@@ -474,7 +474,7 @@ void ETDumpGen::track_allocation(
474474
etdump_RunData_events_push_end(builder_);
475475
}
476476

477-
ETDumpResult ETDumpGen::get_etdump_data() {
477+
ETDumpResult ETDumpGenBase::get_etdump_data() {
478478
ETDumpResult result;
479479
if (state_ == State::AddingEvents) {
480480
etdump_RunData_events_end(builder_);
@@ -504,25 +504,13 @@ ETDumpResult ETDumpGen::get_etdump_data() {
504504
return result;
505505
}
506506

507-
void ETDumpGen::set_debug_buffer(Span<uint8_t> buffer) {
507+
void ETDumpGenBase::set_debug_buffer(Span<uint8_t> buffer) {
508508
debug_buffer_ = buffer;
509509
}
510510

511-
size_t ETDumpGen::copy_tensor_to_debug_buffer(executorch::aten::Tensor tensor) {
512-
if (tensor.nbytes() == 0) {
513-
return static_cast<size_t>(-1);
514-
}
515-
uint8_t* offset_ptr =
516-
alignPointer(debug_buffer_.data() + debug_buffer_offset_, 64);
517-
debug_buffer_offset_ = (offset_ptr - debug_buffer_.data()) + tensor.nbytes();
518-
ET_CHECK_MSG(
519-
debug_buffer_offset_ <= debug_buffer_.size(),
520-
"Ran out of space to store intermediate outputs.");
521-
memcpy(offset_ptr, tensor.const_data_ptr(), tensor.nbytes());
522-
return (size_t)(offset_ptr - debug_buffer_.data());
523-
}
524-
525-
void ETDumpGen::log_evalue(const EValue& evalue, LoggedEValueType evalue_type) {
511+
void ETDumpGenBase::log_evalue(
512+
const EValue& evalue,
513+
LoggedEValueType evalue_type) {
526514
if (debug_buffer_.empty()) {
527515
return;
528516
}
@@ -635,17 +623,32 @@ void ETDumpGen::log_evalue(const EValue& evalue, LoggedEValueType evalue_type) {
635623
etdump_RunData_events_push_end(builder_);
636624
}
637625

638-
size_t ETDumpGen::get_num_blocks() {
626+
size_t ETDumpGenBase::get_num_blocks() {
639627
return num_blocks_;
640628
}
641629

642-
bool ETDumpGen::is_static_etdump() {
630+
bool ETDumpGenBase::is_static_etdump() {
643631
return alloc_.data != nullptr;
644632
}
645633

646-
size_t ETDumpGen::get_debug_buffer_size() const {
634+
size_t ETDumpGenBase::get_debug_buffer_size() const {
647635
return debug_buffer_.size();
648636
}
649637

638+
size_t ETDumpGen::copy_tensor_to_debug_buffer(
639+
const executorch::aten::Tensor& tensor) {
640+
if (tensor.nbytes() == 0) {
641+
return static_cast<size_t>(-1);
642+
}
643+
uint8_t* offset_ptr =
644+
alignPointer(debug_buffer_.data() + debug_buffer_offset_, 64);
645+
debug_buffer_offset_ = (offset_ptr - debug_buffer_.data()) + tensor.nbytes();
646+
ET_CHECK_MSG(
647+
debug_buffer_offset_ <= debug_buffer_.size(),
648+
"Ran out of space to store intermediate outputs.");
649+
memcpy(offset_ptr, tensor.const_data_ptr(), tensor.nbytes());
650+
return (size_t)(offset_ptr - debug_buffer_.data());
651+
}
652+
650653
} // namespace etdump
651654
} // namespace executorch

devtools/etdump/etdump_flatcc.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ struct ETDumpResult {
6363
size_t size;
6464
};
6565

66-
class ETDumpGen : public ::executorch::runtime::EventTracer {
66+
class ETDumpGenBase : public ::executorch::runtime::EventTracer {
6767
public:
68-
ETDumpGen(::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0});
69-
~ETDumpGen() override;
68+
ETDumpGenBase(
69+
::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0});
70+
71+
virtual ~ETDumpGenBase() override;
7072
void clear_builder();
7173

7274
void create_event_block(const char* name) override;
@@ -147,6 +149,15 @@ class ETDumpGen : public ::executorch::runtime::EventTracer {
147149
bool is_static_etdump();
148150
void reset();
149151

152+
protected:
153+
// Declare the function as pure virtual
154+
virtual size_t copy_tensor_to_debug_buffer(
155+
const executorch::aten::Tensor& tensor) = 0;
156+
157+
// Make them as protected to be accessible by derived classes
158+
::executorch::runtime::Span<uint8_t> debug_buffer_;
159+
size_t debug_buffer_offset_ = 0;
160+
150161
private:
151162
enum class State {
152163
Init,
@@ -158,7 +169,6 @@ class ETDumpGen : public ::executorch::runtime::EventTracer {
158169

159170
void check_ready_to_add_events();
160171
int64_t create_string_entry(const char* name);
161-
size_t copy_tensor_to_debug_buffer(executorch::aten::Tensor tensor);
162172

163173
/**
164174
* Templated helper function used to log various types of intermediate output.
@@ -172,13 +182,22 @@ class ETDumpGen : public ::executorch::runtime::EventTracer {
172182

173183
struct flatcc_builder* builder_;
174184
size_t num_blocks_ = 0;
175-
::executorch::runtime::Span<uint8_t> debug_buffer_;
176-
size_t debug_buffer_offset_ = 0;
185+
177186
int bundled_input_index_ = -1;
178187
State state_ = State::Init;
179188
struct internal::ETDumpStaticAllocator alloc_;
180189
};
181190

191+
class ETDumpGen : public ETDumpGenBase {
192+
public:
193+
ETDumpGen(::executorch::runtime::Span<uint8_t> buffer = {nullptr, (size_t)0})
194+
: ETDumpGenBase(buffer) {}
195+
196+
protected:
197+
size_t copy_tensor_to_debug_buffer(
198+
const executorch::aten::Tensor& tensor) override;
199+
};
200+
182201
} // namespace etdump
183202
} // namespace executorch
184203

0 commit comments

Comments
 (0)