Skip to content

Commit b20af84

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
support etdump generation in executorch.runtime
Summary: make et.runtime support etdump generation. Differential Revision: D80373976
1 parent bc5186c commit b20af84

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

extension/pybindings/pybindings.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ struct PyProgram final {
14341434

14351435
std::unique_ptr<PyMethod> load_method(const std::string& method_name) {
14361436
Result<Method> res = state_->program_->load_method(
1437-
method_name.c_str(), memory_->mem_manager());
1437+
method_name.c_str(), memory_->mem_manager(), event_tracer_.get());
14381438
THROW_IF_ERROR(
14391439
res.error(),
14401440
"Failed to load method %s, error: 0x:%" PRIx32,
@@ -1459,6 +1459,39 @@ struct PyProgram final {
14591459
return std::make_unique<PyMethodMeta>(state_, std::move(res.get()));
14601460
}
14611461

1462+
bool has_etdump() {
1463+
return static_cast<bool>(event_tracer_);
1464+
}
1465+
1466+
void write_etdump_result_to_file(
1467+
const std::string& path,
1468+
const py::object& debug_buffer_path) {
1469+
if (!has_etdump()) {
1470+
throw std::runtime_error("No etdump found");
1471+
}
1472+
auto& etdump = *event_tracer_;
1473+
etdump_result result = etdump.get_etdump_data();
1474+
if (result.buf != nullptr && result.size > 0) {
1475+
write_data_to_file(path, result.buf, result.size);
1476+
free(result.buf);
1477+
if (debug_buffer_size_ > 0 &&
1478+
py::isinstance<py::str>(debug_buffer_path)) {
1479+
// Also write out the debug buffer to a separate file if requested.
1480+
std::string debug_buffer_path_str =
1481+
py::cast<std::string>(debug_buffer_path);
1482+
const auto debug_buffer = get_etdump_debug_buffer();
1483+
write_data_to_file(
1484+
debug_buffer_path_str, debug_buffer.data(), debug_buffer.size());
1485+
}
1486+
} else {
1487+
ET_LOG(
1488+
Info,
1489+
"No etdump data found, try rebuilding with "
1490+
"the CMake option EXECUTORCH_ENABLE_EVENT_TRACER or with "
1491+
"buck run --config executorch.event_tracer_enabled=true");
1492+
}
1493+
}
1494+
14621495
private:
14631496
std::shared_ptr<ProgramMemory> memory_;
14641497
std::shared_ptr<ProgramState> state_;
@@ -1685,6 +1718,13 @@ PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) {
16851718
"method_meta",
16861719
&PyProgram::method_meta,
16871720
py::arg("method_name"),
1721+
call_guard)
1722+
.def("has_etdump", &PyProgram::has_etdump, call_guard)
1723+
.def(
1724+
"write_etdump_result_to_file",
1725+
&PyProgram::write_etdump_result_to_file,
1726+
py::arg("path"),
1727+
py::arg("debug_buffer_path") = py::none(),
16881728
call_guard);
16891729
py::class_<PyMethod>(m, "ExecuTorchMethod")
16901730
.def("set_inputs", &PyMethod::set_inputs, py::arg("inputs"), call_guard)

runtime/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ def metadata(self, method_name: str) -> MethodMeta:
137137
"""
138138
return self._program.method_meta(method_name)
139139

140+
def write_etdump_result_to_file(self, etdump_path: str, debug_buffer_path: str) -> None:
141+
"""Writes the etdump and debug result to a file.
142+
143+
Args:
144+
etdump_path: The path to the etdump file.
145+
debug_buffer_path: The path to the debug buffer file.
146+
"""
147+
self._program.write_etdump_result_to_file(etdump_path, debug_buffer_path)
148+
140149

141150
class BackendRegistry:
142151
"""The registry of backends that are available to the runtime."""
@@ -201,6 +210,8 @@ def load_program(
201210
data: Union[bytes, bytearray, BinaryIO, Path, str],
202211
*,
203212
verification: Verification = Verification.InternalConsistency,
213+
enable_etdump: bool = False,
214+
debug_buffer_size: int = 0,
204215
) -> Program:
205216
"""Loads an ExecuTorch program from a PTE binary.
206217
@@ -214,8 +225,8 @@ def load_program(
214225
if isinstance(data, (Path, str)):
215226
p = self._legacy_module._load_program(
216227
str(data),
217-
enable_etdump=False,
218-
debug_buffer_size=0,
228+
enable_etdump=enable_etdump,
229+
debug_buffer_size=debug_buffer_size,
219230
program_verification=verification,
220231
)
221232
return Program(p, data=None)

0 commit comments

Comments
 (0)