Skip to content

Commit bb60c25

Browse files
authored
[0.6 documentation] Fix Page Developer Tools: Bundled Program (#10222)
1 parent ae2d822 commit bb60c25

File tree

1 file changed

+18
-93
lines changed

1 file changed

+18
-93
lines changed

docs/source/bundled-io.md

+18-93
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This stage mainly focuses on the creation of a `BundledProgram` and dumping it o
1717

1818
### Step 1: Create a Model and Emit its ExecuTorch Program.
1919

20-
ExecuTorch Program can be emitted from user's model by using ExecuTorch APIs. Follow the [Generate Sample ExecuTorch program](./getting-started-setup.md) or [Exporting to ExecuTorch tutorial](./tutorials/export-to-executorch-tutorial).
20+
ExecuTorch Program can be emitted from user's model by using ExecuTorch APIs. Follow the [Generate and emit sample ExecuTorch program](./getting-started.md#exporting) or [Exporting to ExecuTorch tutorial](./tutorials/export-to-executorch-tutorial).
2121

2222
### Step 2: Construct `List[MethodTestSuite]` to hold test info
2323

@@ -89,7 +89,7 @@ Here is a flow highlighting how to generate a `BundledProgram` given a PyTorch m
8989
```python
9090
import torch
9191

92-
from executorch.exir import to_edge
92+
from executorch.exir import to_edge_transform_and_lower
9393
from executorch.devtools import BundledProgram
9494

9595
from executorch.devtools.bundled_program.config import MethodTestCase, MethodTestSuite
@@ -105,8 +105,8 @@ class SampleModel(torch.nn.Module):
105105

106106
def __init__(self) -> None:
107107
super().__init__()
108-
self.a: torch.Tensor = 3 * torch.ones(2, 2, dtype=torch.int32)
109-
self.b: torch.Tensor = 2 * torch.ones(2, 2, dtype=torch.int32)
108+
self.register_buffer('a', 3 * torch.ones(2, 2, dtype=torch.int32))
109+
self.register_buffer('b', 2 * torch.ones(2, 2, dtype=torch.int32))
110110

111111
def forward(self, x: torch.Tensor, q: torch.Tensor) -> torch.Tensor:
112112
z = x.clone()
@@ -136,7 +136,7 @@ method_graph = export(
136136

137137

138138
# Emit the traced method into ET Program.
139-
et_program = to_edge(method_graph).to_executorch()
139+
et_program = to_edge_transform_and_lower(method_graph).to_executorch()
140140

141141
# Step 2: Construct MethodTestSuite for Each Method
142142

@@ -180,7 +180,6 @@ serialized_bundled_program = serialize_from_bundled_program_to_flatbuffer(
180180
save_path = "bundled_program.bpte"
181181
with open(save_path, "wb") as f:
182182
f.write(serialized_bundled_program)
183-
184183
```
185184

186185
We can also regenerate `BundledProgram` from flatbuffer file if needed:
@@ -199,103 +198,29 @@ This stage mainly focuses on executing the model with the bundled inputs and and
199198

200199

201200
### Get ExecuTorch Program Pointer from `BundledProgram` Buffer
202-
We need the pointer to ExecuTorch program to do the execution. To unify the process of loading and executing `BundledProgram` and Program flatbuffer, we create an API:
203-
204-
:::{dropdown} `get_program_data`
205-
206-
```{eval-rst}
207-
.. doxygenfunction:: ::executorch::bundled_program::get_program_data
208-
```
209-
:::
210-
211-
Here's an example of how to use the `get_program_data` API:
212-
```c++
213-
// Assume that the user has read the contents of the file into file_data using
214-
// whatever method works best for their application. The file could contain
215-
// either BundledProgram data or Program data.
216-
void* file_data = ...;
217-
size_t file_data_len = ...;
218-
219-
// If file_data contains a BundledProgram, get_program_data() will return a
220-
// pointer to the Program data embedded inside it. Otherwise it will return
221-
// file_data, which already pointed to Program data.
222-
const void* program_ptr;
223-
size_t program_len;
224-
status = executorch::bundled_program::get_program_data(
225-
file_data, file_data_len, &program_ptr, &program_len);
226-
ET_CHECK_MSG(
227-
status == Error::Ok,
228-
"get_program_data() failed with status 0x%" PRIx32,
229-
status);
230-
```
201+
We need the pointer to ExecuTorch program to do the execution. To unify the process of loading and executing `BundledProgram` and Program flatbuffer, we create an API for this
202+
`executorch::bundled_program::get_program_data`. Check out an [example usage](https://github.com/pytorch/executorch/blob/release/0.6/examples/devtools/example_runner/example_runner.cpp#L128-L137) of this API.
231203

232204
### Load Bundled Input to Method
233-
To execute the program on the bundled input, we need to load the bundled input into the method. Here we provided an API called `executorch::bundled_program::load_bundled_input`:
234-
235-
:::{dropdown} `load_bundled_input`
236-
237-
```{eval-rst}
238-
.. doxygenfunction:: ::executorch::bundled_program::load_bundled_input
239-
```
240-
:::
205+
To execute the program on the bundled input, we need to load the bundled input into the method. Here we provided an API called `executorch::bundled_program::load_bundled_input`. Check out an [example usage](https://github.com/pytorch/executorch/blob/release/0.6/examples/devtools/example_runner/example_runner.cpp#L253-L259) of this API.
241206

242207
### Verify the Method's Output.
243-
We call `executorch::bundled_program::verify_method_outputs` to verify the method's output with bundled expected outputs. Here's the details of this API:
244-
245-
:::{dropdown} `verify_method_outputs`
246-
247-
```{eval-rst}
248-
.. doxygenfunction:: ::executorch::bundled_program::verify_method_outputs
249-
```
250-
:::
251-
208+
We call `executorch::bundled_program::verify_method_outputs` to verify the method's output with bundled expected outputs. Check out an [example usage](https://github.com/pytorch/executorch/blob/release/0.6/examples/devtools/example_runner/example_runner.cpp#L300-L311) of this API.
252209

253210
### Runtime Example
254211

255-
Here we provide an example about how to run the bundled program step by step. Most of the code is borrowed from [executor_runner](https://github.com/pytorch/executorch/blob/main/examples/devtools/example_runner/example_runner.cpp), and please review that file if you need more info and context:
256-
257-
```c++
258-
// method_name is the name for the method we want to test
259-
// memory_manager is the executor::MemoryManager variable for executor memory allocation.
260-
// program is the ExecuTorch program.
261-
Result<Method> method = program->load_method(method_name, &memory_manager);
262-
263-
ET_CHECK_MSG(
264-
method.ok(),
265-
"load_method() failed with status 0x%" PRIx32,
266-
method.error());
267-
268-
// Load testset_idx-th input in the buffer to plan
269-
status = executorch::bundled_program::load_bundled_input(
270-
*method,
271-
program_data.bundled_program_data(),
272-
FLAGS_testset_idx);
273-
ET_CHECK_MSG(
274-
status == Error::Ok,
275-
"load_bundled_input failed with status 0x%" PRIx32,
276-
status);
277-
278-
// Execute the plan
279-
status = method->execute();
280-
ET_CHECK_MSG(
281-
status == Error::Ok,
282-
"method->execute() failed with status 0x%" PRIx32,
283-
status);
284-
285-
// Verify the result.
286-
status = executorch::bundled_program::verify_method_outputs(
287-
*method,
288-
program_data.bundled_program_data(),
289-
FLAGS_testset_idx,
290-
FLAGS_rtol,
291-
FLAGS_atol);
292-
ET_CHECK_MSG(
293-
status == Error::Ok,
294-
"Bundle verification failed with status 0x%" PRIx32,
295-
status);
212+
Please checkout our [example runner](https://github.com/pytorch/executorch/blob/release/0.6/examples/devtools/README.md#bundledprogram) for a bundled program. You could run these commands to test with the BundledProgram binary (`.bpte`) file you generated in the previous step:
296213

214+
```bash
215+
cd executorch
216+
./examples/devtools/build_example_runner.sh
217+
./cmake-out/examples/devtools/example_runner --bundled_program_path {your-bpte-file} --output_verification
297218
```
298219

220+
It is expected to see no output from running the above mentioned snippet.
221+
222+
For a detailed example of how the runner should be like, please refer to our [example runner](https://github.com/pytorch/executorch/blob/release/0.6/examples/devtools/example_runner/example_runner.cpp).
223+
299224
## Common Errors
300225

301226
Errors will be raised if `List[MethodTestSuites]` doesn't match the `Program`. Here're two common situations:

0 commit comments

Comments
 (0)