Skip to content

Commit 0cddd2e

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 Differential Revision: D87122487
1 parent b4d72f1 commit 0cddd2e

File tree

7 files changed

+71
-13
lines changed

7 files changed

+71
-13
lines changed

examples/models/llama/main.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#include <gflags/gflags.h>
1111
#include <sstream>
1212
#include <vector>
13-
1413
#include <executorch/examples/models/llama/runner/runner.h>
1514

15+
#ifdef ET_EVENT_TRACER_ENABLED
16+
#include <executorch/devtools/etdump/etdump_flatcc.h>
17+
#endif
18+
1619
#if defined(ET_USE_THREADPOOL)
1720
#include <executorch/extension/threadpool/cpuinfo_utils.h>
1821
#include <executorch/extension/threadpool/threadpool.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,22 @@ 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(Error, "Failed to open etdump file at path: %s", FLAGS_etdump_path.c_str());
192+
} else {
193+
fwrite((uint8_t*)result.buf, 1, result.size, f);
194+
fclose(f);
195+
ET_LOG(Info, "ETDump file written to: %s", FLAGS_etdump_path.c_str());
196+
}
197+
free(result.buf);
198+
}
199+
}
200+
#endif
201+
160202
return 0;
161203
}

examples/models/llama/runner/runner.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,26 @@ 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
41+
) {
4042
if (data_path.has_value()) {
4143
std::vector<std::string> data_files;
4244
data_files.push_back(data_path.value());
4345
return create_llama_runner(
44-
model_path, tokenizer_path, std::move(data_files), temperature);
46+
model_path, tokenizer_path, std::move(data_files), temperature, std::move(event_tracer));
4547
}
4648
return create_llama_runner(
47-
model_path, tokenizer_path, std::vector<std::string>(), temperature);
49+
model_path, tokenizer_path, std::vector<std::string>(), temperature, std::move(event_tracer));
4850
}
4951

5052
std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
5153
const std::string& model_path,
5254
const std::string& tokenizer_path,
5355
std::vector<std::string> data_files,
54-
float temperature) {
56+
float temperature,
57+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer
58+
) {
5559
ET_LOG(
5660
Info,
5761
"Creating LLaMa runner: model_path=%s, tokenizer_path=%s",
@@ -70,7 +74,7 @@ std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
7074
return nullptr;
7175
}
7276
return llm::create_text_llm_runner(
73-
model_path, std::move(tokenizer), data_files);
77+
model_path, std::move(tokenizer), data_files, temperature, std::move(event_tracer));
7478
}
7579

7680
} // namespace example

examples/models/llama/runner/runner.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ 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
33+
);
3234

3335
std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
3436
const std::string& model_path,
3537
const std::string& tokenizer_path,
3638
std::vector<std::string> data_files = {},
37-
float temperature = -1.0f);
39+
float temperature = -1.0f,
40+
std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = nullptr
41+
);
3842

3943
std::unique_ptr<tokenizers::Tokenizer> load_llama_tokenizer(
4044
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ 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
205+
) {
204206
// Sanity check tokenizer
205207
if (!tokenizer || !tokenizer->is_loaded()) {
206208
ET_LOG(Error, "Tokenizer is null or not loaded");
@@ -211,9 +213,9 @@ std::unique_ptr<TextLLMRunner> create_text_llm_runner(
211213
std::unique_ptr<Module> module;
212214
if (data_files.size() > 0) {
213215
module = std::make_unique<Module>(
214-
model_path, data_files, Module::LoadMode::File);
216+
model_path, data_files, Module::LoadMode::File, std::move(event_tracer));
215217
} else {
216-
module = std::make_unique<Module>(model_path, Module::LoadMode::File);
218+
module = std::make_unique<Module>(model_path, Module::LoadMode::File, std::move(event_tracer));
217219
}
218220

219221
// Get metadata from Module

extension/llm/runner/llm_runner_helper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ 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
128+
);
127129

128130
/**
129131
* @brief Creates a MultimodalRunner instance with dependency injection

0 commit comments

Comments
 (0)