1*523fa7a6SAndroid Build Coastguard Worker# Developer Tools Examples 2*523fa7a6SAndroid Build Coastguard WorkerThis directory contains examples of BundledProgram and ETDump generation. 3*523fa7a6SAndroid Build Coastguard Worker 4*523fa7a6SAndroid Build Coastguard Worker## Directory structure 5*523fa7a6SAndroid Build Coastguard Worker```bash 6*523fa7a6SAndroid Build Coastguard Workerexamples/devtools 7*523fa7a6SAndroid Build Coastguard Worker├── scripts # Python scripts to illustrate export workflow of bundled program. 8*523fa7a6SAndroid Build Coastguard Worker├── executor_runner # Contains an example for both BundledProgram to verify ExecuTorch model, and generate ETDump for runtime results. 9*523fa7a6SAndroid Build Coastguard Worker├── CMakeLists.txt # Example CMakeLists.txt for building executor_runner with Developer Tools support. 10*523fa7a6SAndroid Build Coastguard Worker├── build_example_runner.sh # A convenient shell script to build the example_runner. 11*523fa7a6SAndroid Build Coastguard Worker├── test_example_runner.sh # A convenient shell script to run the example_runner. 12*523fa7a6SAndroid Build Coastguard Worker└── README.md # Current file 13*523fa7a6SAndroid Build Coastguard Worker``` 14*523fa7a6SAndroid Build Coastguard Worker 15*523fa7a6SAndroid Build Coastguard Worker## BundledProgram 16*523fa7a6SAndroid Build Coastguard Worker 17*523fa7a6SAndroid Build Coastguard WorkerWe will use an example model (in `torch.nn.Module`) and its representative inputs, both from [`models/`](../models) directory, to generate a [BundledProgram(`.bpte`)](../../docs/source/bundled-io.md) file using the [script](scripts/export_bundled_program.py). Then we will use [devtools/example_runner](example_runner/example_runner.cpp) to execute the `.bpte` model on the ExecuTorch runtime and verify the model on BundledProgram API. 18*523fa7a6SAndroid Build Coastguard Worker 19*523fa7a6SAndroid Build Coastguard Worker 20*523fa7a6SAndroid Build Coastguard Worker1. Sets up the basic development environment for ExecuTorch by [Setting up ExecuTorch from GitHub](https://pytorch.org/executorch/stable/getting-started-setup). 21*523fa7a6SAndroid Build Coastguard Worker 22*523fa7a6SAndroid Build Coastguard Worker2. Using the [script](scripts/export_bundled_program.py) to generate a BundledProgram binary file by retreiving a `torch.nn.Module` model and its representative inputs from the list of available models in the [`models/`](../models) dir. 23*523fa7a6SAndroid Build Coastguard Worker 24*523fa7a6SAndroid Build Coastguard Worker```bash 25*523fa7a6SAndroid Build Coastguard Workercd executorch # To the top level dir 26*523fa7a6SAndroid Build Coastguard Worker 27*523fa7a6SAndroid Build Coastguard Worker# To get a list of example models 28*523fa7a6SAndroid Build Coastguard Workerpython3 -m examples.devtools.scripts.export_bundled_program -h 29*523fa7a6SAndroid Build Coastguard Worker 30*523fa7a6SAndroid Build Coastguard Worker# To generate a specific `.bpte` model 31*523fa7a6SAndroid Build Coastguard Workerpython3 -m examples.devtools.scripts.export_bundled_program -m mv2 # for MobileNetv2 32*523fa7a6SAndroid Build Coastguard Worker 33*523fa7a6SAndroid Build Coastguard Worker# This should generate ./mv2_bundled.bpte file, if successful. 34*523fa7a6SAndroid Build Coastguard Worker``` 35*523fa7a6SAndroid Build Coastguard Worker 36*523fa7a6SAndroid Build Coastguard Worker3. Once we have the BundledProgram binary (`.bpte`) file, then let's run and verify it with ExecuTorch runtime and BundledProgram APIs using the [devtools/example_runner](example_runner/example_runner.cpp). 37*523fa7a6SAndroid Build Coastguard Worker 38*523fa7a6SAndroid Build Coastguard Worker```bash 39*523fa7a6SAndroid Build Coastguard Worker cd executorch 40*523fa7a6SAndroid Build Coastguard Worker ./examples/devtools/build_example_runner.sh 41*523fa7a6SAndroid Build Coastguard Worker ./cmake-out/examples/devtools/example_runner --bundled_program_path mv2_bundled.bpte --output_verification 42*523fa7a6SAndroid Build Coastguard Worker ``` 43*523fa7a6SAndroid Build Coastguard Worker 44*523fa7a6SAndroid Build Coastguard Worker 45*523fa7a6SAndroid Build Coastguard Worker## ETDump 46*523fa7a6SAndroid Build Coastguard Worker 47*523fa7a6SAndroid Build Coastguard Worker### Getting Started 48*523fa7a6SAndroid Build Coastguard Worker 49*523fa7a6SAndroid Build Coastguard WorkerAfter exporting a `BundledProgram`, runtime profiling and debug data can be collected in an ``ETDump``. An ``ETDump`` is a buffer containing data generated by hooks within the ExecuTorch runtime. 50*523fa7a6SAndroid Build Coastguard WorkerWe offer an example runner that accepts a `BundledProgram` (`.bpte`) and runs a single iteration over the first method defined. 51*523fa7a6SAndroid Build Coastguard Worker 52*523fa7a6SAndroid Build Coastguard WorkerRunning the program will generate an `ETDump` file (`.etdp`) at the location specified by `--etdump_path`. 53*523fa7a6SAndroid Build Coastguard Worker 54*523fa7a6SAndroid Build Coastguard Worker```bash 55*523fa7a6SAndroid Build Coastguard Worker ./cmake-out/examples/devtools/example_runner --bundled_program_path mv2_bundled.bpte --etdump_path mv2_etdump.etdp 56*523fa7a6SAndroid Build Coastguard Worker ``` 57*523fa7a6SAndroid Build Coastguard Worker 58*523fa7a6SAndroid Build Coastguard Worker### Parsing ETDump 59*523fa7a6SAndroid Build Coastguard Worker 60*523fa7a6SAndroid Build Coastguard WorkerOnce an `ETDump` has been generated, it can be viewed using the CLI inspector. This will print a tabular view of the data recorded in the ETDump. 61*523fa7a6SAndroid Build Coastguard Worker 62*523fa7a6SAndroid Build Coastguard Worker```bash 63*523fa7a6SAndroid Build Coastguard Worker python3 -m devtools.inspector.inspector_cli --etdump_path mv2_etdump.etdp 64*523fa7a6SAndroid Build Coastguard Worker ``` 65*523fa7a6SAndroid Build Coastguard Worker### ETDump C++ API 66*523fa7a6SAndroid Build Coastguard Worker 67*523fa7a6SAndroid Build Coastguard WorkerETDump profiling can also be used in a custom C++ program. `ETDumpGen` is an implementation of the abstract `EventTracer` class. Include the header file located at `devtools/etdump/etdump_flatcc.h`. To initialize the ETDump generator, construct it before loading the method from the program. 68*523fa7a6SAndroid Build Coastguard Worker 69*523fa7a6SAndroid Build Coastguard Worker```cpp 70*523fa7a6SAndroid Build Coastguard Worker executorch::etdump::ETDumpGen etdump_gen; 71*523fa7a6SAndroid Build Coastguard Worker Result<Method> method = 72*523fa7a6SAndroid Build Coastguard Worker program->load_method(method_name, &memory_manager, &etdump_gen); 73*523fa7a6SAndroid Build Coastguard Worker ``` 74*523fa7a6SAndroid Build Coastguard Worker 75*523fa7a6SAndroid Build Coastguard WorkerSince the `EventTracer` hooks are embedded within the runtime, profiling and debug data will be automatically recorded. 76*523fa7a6SAndroid Build Coastguard Worker 77*523fa7a6SAndroid Build Coastguard WorkerOnce execution has completed, finalize the ETDump buffer. This returns an `etdump_result`, a struct with the finalized buffer and its size. 78*523fa7a6SAndroid Build Coastguard Worker 79*523fa7a6SAndroid Build Coastguard Worker```cpp 80*523fa7a6SAndroid Build Coastguard Worker etdump_result result = etdump_gen.get_etdump_data(); 81*523fa7a6SAndroid Build Coastguard Worker if (result.buf != nullptr && result.size > 0) { 82*523fa7a6SAndroid Build Coastguard Worker FILE* f = fopen(FLAGS_etdump_path.c_str(), "w+"); 83*523fa7a6SAndroid Build Coastguard Worker fwrite((uint8_t*)result.buf, 1, result.size, f); 84*523fa7a6SAndroid Build Coastguard Worker fclose(f); 85*523fa7a6SAndroid Build Coastguard Worker free(result.buf); 86*523fa7a6SAndroid Build Coastguard Worker } 87*523fa7a6SAndroid Build Coastguard Worker ``` 88