Skip to content

Commit 14d847c

Browse files
navsudfacebook-github-bot
authored andcommitted
Add ETDump event tracer support to LLaMa runner
Summary: for op-level profiling of od-llms **Addresses reviewer feedback:** - Added `ET_EVENT_TRACER_ENABLED` ifdef guards around ETDump code to ensure normal builds compile without event tracer support - Replaced hardcoded `/data/local/tmp/etdump.bin` path with configurable `--etdump_path` flag (default: `"etdump.in"`) - ETDumpGen is created only when compiled with event tracer support enabled Reviewed By: larryliu0820, kimishpatel Differential Revision: D87122487
1 parent 268e885 commit 14d847c

File tree

7 files changed

+84
-13
lines changed

7 files changed

+84
-13
lines changed

examples/models/llama/main.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
* @lint-ignore-every CLANGTIDY facebook-hte-Deprecated
88
*/
99

10+
#include <executorch/examples/models/llama/runner/runner.h>
1011
#include <gflags/gflags.h>
1112
#include <sstream>
1213
#include <vector>
1314

14-
#include <executorch/examples/models/llama/runner/runner.h>
15+
#ifdef ET_EVENT_TRACER_ENABLED
16+
#include <executorch/devtools/etdump/etdump_flatcc.h>
17+
#endif
1518

1619
#if defined(ET_USE_THREADPOOL)
1720
#include <executorch/extension/threadpool/cpuinfo_utils.h>
@@ -64,6 +67,11 @@ DEFINE_int32(
6467

6568
DEFINE_bool(warmup, false, "Whether to run a warmup run.");
6669

70+
DEFINE_string(
71+
etdump_path,
72+
"etdump.in",
73+
"If an etdump path is provided, generate an ETDump file at the specified path for profiling purposes.");
74+
6775
// Helper function to parse comma-separated string lists
6876
std::vector<std::string> parseStringList(const std::string& input) {
6977
std::vector<std::string> result;
@@ -117,9 +125,26 @@ int32_t main(int32_t argc, char** argv) {
117125
->_unsafe_reset_threadpool(num_performant_cores);
118126
}
119127
#endif
128+
129+
#ifdef ET_EVENT_TRACER_ENABLED
130+
// Create ETDumpGen and get raw pointer reference for later access
131+
auto etdump_gen_ptr = std::make_unique<executorch::etdump::ETDumpGen>();
132+
executorch::etdump::ETDumpGen* etdump_gen = etdump_gen_ptr.get();
133+
#endif
134+
120135
// create llama runner
121136
std::unique_ptr<::executorch::extension::llm::TextLLMRunner> runner =
122-
example::create_llama_runner(model_path, tokenizer_path, data_paths);
137+
example::create_llama_runner(
138+
model_path,
139+
tokenizer_path,
140+
data_paths,
141+
temperature,
142+
#ifdef ET_EVENT_TRACER_ENABLED
143+
std::move(etdump_gen_ptr)
144+
#else
145+
nullptr
146+
#endif
147+
);
123148

124149
if (runner == nullptr) {
125150
ET_LOG(Error, "Failed to create llama runner");
@@ -157,5 +182,25 @@ int32_t main(int32_t argc, char** argv) {
157182
return 1;
158183
}
159184

185+
#ifdef ET_EVENT_TRACER_ENABLED
186+
if (etdump_gen != nullptr) {
187+
executorch::etdump::ETDumpResult result = etdump_gen->get_etdump_data();
188+
if (result.buf != nullptr && result.size > 0) {
189+
FILE* f = fopen(FLAGS_etdump_path.c_str(), "w+");
190+
if (f == nullptr) {
191+
ET_LOG(
192+
Error,
193+
"Failed to open etdump file at path: %s",
194+
FLAGS_etdump_path.c_str());
195+
} else {
196+
fwrite((uint8_t*)result.buf, 1, result.size, f);
197+
fclose(f);
198+
ET_LOG(Info, "ETDump file written to: %s", FLAGS_etdump_path.c_str());
199+
}
200+
free(result.buf);
201+
}
202+
}
203+
#endif
204+
160205
return 0;
161206
}

examples/models/llama/runner/runner.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,32 @@ std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
3636
const std::string& model_path,
3737
const std::string& tokenizer_path,
3838
std::optional<const std::string> data_path,
39-
float temperature) {
39+
float temperature,
40+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer) {
4041
if (data_path.has_value()) {
4142
std::vector<std::string> data_files;
4243
data_files.push_back(data_path.value());
4344
return create_llama_runner(
44-
model_path, tokenizer_path, std::move(data_files), temperature);
45+
model_path,
46+
tokenizer_path,
47+
std::move(data_files),
48+
temperature,
49+
std::move(event_tracer));
4550
}
4651
return create_llama_runner(
47-
model_path, tokenizer_path, std::vector<std::string>(), temperature);
52+
model_path,
53+
tokenizer_path,
54+
std::vector<std::string>(),
55+
temperature,
56+
std::move(event_tracer));
4857
}
4958

5059
std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
5160
const std::string& model_path,
5261
const std::string& tokenizer_path,
5362
std::vector<std::string> data_files,
54-
float temperature) {
63+
float temperature,
64+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer) {
5565
ET_LOG(
5666
Info,
5767
"Creating LLaMa runner: model_path=%s, tokenizer_path=%s",
@@ -70,7 +80,11 @@ std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
7080
return nullptr;
7181
}
7282
return llm::create_text_llm_runner(
73-
model_path, std::move(tokenizer), data_files);
83+
model_path,
84+
std::move(tokenizer),
85+
data_files,
86+
temperature,
87+
std::move(event_tracer));
7488
}
7589

7690
} // namespace example

examples/models/llama/runner/runner.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
2828
const std::string& model_path,
2929
const std::string& tokenizer_path,
3030
std::optional<const std::string> data_path,
31-
float temperature = -1.0f);
31+
float temperature = -1.0f,
32+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = nullptr);
3233

3334
std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
3435
const std::string& model_path,
3536
const std::string& tokenizer_path,
3637
std::vector<std::string> data_files = {},
37-
float temperature = -1.0f);
38+
float temperature = -1.0f,
39+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = nullptr);
3840

3941
std::unique_ptr<tokenizers::Tokenizer> load_llama_tokenizer(
4042
const std::string& tokenizer_path,

examples/models/llama/runner/targets.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def define_common_targets():
2828
exported_headers = [
2929
"runner.h",
3030
],
31+
deps = [
32+
"//executorch/devtools/etdump:etdump_flatcc",
33+
],
3134
preprocessor_flags = [
3235
"-DUSE_ATEN_LIB",
3336
] if aten else [],

examples/models/llama/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def define_common_targets():
1919
"//executorch/extension/evalue_util:print_evalue",
2020
"//executorch/extension/threadpool:threadpool",
2121
"//executorch/extension/threadpool:cpuinfo_utils",
22+
"//executorch/devtools/etdump:etdump_flatcc" + aten_suffix,
2223
],
2324
external_deps = [
2425
"gflags",

extension/llm/runner/llm_runner_helper.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ std::unique_ptr<TextLLMRunner> create_text_llm_runner(
200200
const std::string& model_path,
201201
std::unique_ptr<::tokenizers::Tokenizer> tokenizer,
202202
std::vector<std::string> data_files,
203-
float temperature) {
203+
float temperature,
204+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer) {
204205
// Sanity check tokenizer
205206
if (!tokenizer || !tokenizer->is_loaded()) {
206207
ET_LOG(Error, "Tokenizer is null or not loaded");
@@ -211,9 +212,13 @@ std::unique_ptr<TextLLMRunner> create_text_llm_runner(
211212
std::unique_ptr<Module> module;
212213
if (data_files.size() > 0) {
213214
module = std::make_unique<Module>(
214-
model_path, data_files, Module::LoadMode::File);
215+
model_path,
216+
data_files,
217+
Module::LoadMode::File,
218+
std::move(event_tracer));
215219
} else {
216-
module = std::make_unique<Module>(model_path, Module::LoadMode::File);
220+
module = std::make_unique<Module>(
221+
model_path, Module::LoadMode::File, std::move(event_tracer));
217222
}
218223

219224
// Get metadata from Module

extension/llm/runner/llm_runner_helper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ ET_EXPERIMENTAL std::unique_ptr<TextLLMRunner> create_text_llm_runner(
123123
const std::string& model_path,
124124
std::unique_ptr<::tokenizers::Tokenizer> tokenizer,
125125
std::vector<std::string> data_files = {},
126-
float temperature = -1.0f);
126+
float temperature = -1.0f,
127+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = nullptr);
127128

128129
/**
129130
* @brief Creates a MultimodalRunner instance with dependency injection

0 commit comments

Comments
 (0)