Skip to content

Commit 3bb1941

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
Update export api in documentation to long term version (#888)
Summary: Pull Request resolved: #888 Update the export apis to long term version apis in bp documentations. bundled program code base will be updated in the next diff. Reviewed By: guangy10 Differential Revision: D50256862 fbshipit-source-id: fb68fc9815d10f2da65a7f2259846da29ab4f46d
1 parent 751e1cc commit 3bb1941

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

docs/source/sdk-bundled-io.md

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,13 @@ Here is a flow highlighting how to generate a `BundledProgram` given a PyTorch m
6767
```python
6868

6969
import torch
70-
from executorch import exir
70+
from torch.export import export
71+
7172
from executorch.bundled_program.config import BundledConfig
7273
from executorch.bundled_program.core import create_bundled_program
7374
from executorch.bundled_program.serialize import serialize_from_bundled_program_to_flatbuffer
74-
from executorch.bundled_program.serialize import deserialize_from_flatbuffer_to_bundled_program
75-
7675

77-
from executorch.exir import ExecutorchBackendConfig
78-
from executorch.exir.passes import MemoryPlanningPass, ToOutVarPass
76+
from executorch.exir import to_edge
7977

8078

8179
# Step 1: ExecuTorch Program Export
@@ -118,13 +116,14 @@ capture_inputs = {
118116
for m_name in method_names
119117
}
120118

121-
# Trace to FX Graph and emit the program
122-
program = (
123-
exir.capture_multiple(model, capture_inputs)
124-
.to_edge()
125-
.to_executorch()
126-
.program
127-
)
119+
# Find each method of model needs to be traced my its name, export its FX Graph.
120+
method_graphs = {
121+
m_name: export(getattr(model, m_name), capture_inputs[m_name])
122+
for m_name in method_names
123+
}
124+
125+
# Emit the traced methods into ET Program.
126+
program = to_edge(method_graphs).to_executorch().executorch_program
128127

129128
# Step 2: Construct BundledConfig
130129

@@ -291,12 +290,13 @@ Here's the example of the dtype of test input not meet model's requirement:
291290
292291
```python
293292
import torch
294-
from executorch import exir
295-
from executorch.exir import ExecutorchBackendConfig
296-
from executorch.exir.passes import MemoryPlanningPass, ToOutVarPass
293+
from torch.export import export
294+
297295
from executorch.bundled_program.config import BundledConfig
298296
from executorch.bundled_program.core import create_bundled_program
299297
298+
from executorch.exir import to_edge
299+
300300
301301
class Module(torch.nn.Module):
302302
def __init__(self):
@@ -318,16 +318,14 @@ method_names = ['forward']
318318
inputs = torch.ones(2, 2, dtype=torch.float)
319319
print(model(inputs))
320320
321-
# Trace to FX Graph.
322-
program = (
323-
exir.capture(model, (inputs,))
324-
.to_edge()
325-
.to_executorch(
326-
config=ExecutorchBackendConfig(
327-
memory_planning_pass=MemoryPlanningPass(), to_out_var_pass=ToOutVarPass()
328-
)
329-
).program
330-
)
321+
# Find each method of model needs to be traced my its name, export its FX Graph.
322+
method_graphs = {
323+
m_name: export(getattr(model, m_name), (inputs, ))
324+
for m_name in method_names
325+
}
326+
327+
# Emit the traced methods into ET Program.
328+
program = to_edge(method_graphs).to_executorch().executorch_program
331329
332330
333331
# number of input sets to be verified
@@ -416,12 +414,13 @@ Another common error would be the method name in `BundledConfig` does not exist
416414

417415
```python
418416
import torch
419-
from executorch import exir
420-
from executorch.exir import ExecutorchBackendConfig
421-
from executorch.exir.passes import MemoryPlanningPass, ToOutVarPass
417+
from torch.export import export
418+
422419
from executorch.bundled_program.config import BundledConfig
423420
from executorch.bundled_program.core import create_bundled_program
424421

422+
from executorch.exir import to_edge
423+
425424

426425

427426
class Module(torch.nn.Module):
@@ -440,23 +439,18 @@ class Module(torch.nn.Module):
440439

441440
model = Module()
442441

443-
# NOTE: wrong_forward is not an inference method in the above model.
444-
method_names = ['wrong_forward']
442+
method_names = ['forward']
445443

446444
inputs = torch.ones(2, 2, dtype=torch.float)
447-
print(model(inputs))
448445

449-
# Trace to FX Graph.
450-
program = (
451-
exir.capture(model, (inputs,))
452-
.to_edge()
453-
.to_executorch(
454-
config=ExecutorchBackendConfig(
455-
memory_planning_pass=MemoryPlanningPass(), to_out_var_pass=ToOutVarPass()
456-
)
457-
).program
458-
)
446+
# Find each method of model needs to be traced my its name, export its FX Graph.
447+
method_graphs = {
448+
m_name: export(getattr(model, m_name), (inputs, ))
449+
for m_name in method_names
450+
}
459451

452+
# Emit the traced methods into ET Program.
453+
program = to_edge(method_graphs).to_executorch().executorch_program
460454

461455
# Number of input sets to be verified
462456
n_input = 10
@@ -476,7 +470,11 @@ expected_outpus = [
476470
[[model(*x)] for x in inputs[0]]
477471
]
478472

479-
bundled_config = BundledConfig(method_names, inputs, expected_outpus)
473+
474+
# NOTE: MISSING_METHOD_NAME is not an inference method in the above model.
475+
wrong_method_names = ['MISSING_METHOD_NAME']
476+
477+
bundled_config = BundledConfig(wrong_method_names, inputs, expected_outpus)
480478

481479
bundled_program = create_bundled_program(program, bundled_config)
482480

@@ -518,6 +516,6 @@ File /executorch/bundled_program/core.py:147, in assert_valid_bundle(program, bu
518516
150 but {str(method_name_of_bundled_config - method_name_of_program)} does not include."
519517
152 # check if has been sorted in ascending alphabetical order of method name.
520518
153 for bp_plan_id in range(1, len(bundled_config.execution_plan_tests)):
521-
AssertionError: All method names in bundled config should be found in program.execution_plan, but {'wrong_forward'} does not include.
519+
AssertionError: All method names in bundled config should be found in program.execution_plan, but {'MISSING_METHOD_NAME'} does not include.
522520
```
523521
:::

0 commit comments

Comments
 (0)