@@ -67,15 +67,13 @@ Here is a flow highlighting how to generate a `BundledProgram` given a PyTorch m
67
67
``` python
68
68
69
69
import torch
70
- from executorch import exir
70
+ from torch.export import export
71
+
71
72
from executorch.bundled_program.config import BundledConfig
72
73
from executorch.bundled_program.core import create_bundled_program
73
74
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
-
76
75
77
- from executorch.exir import ExecutorchBackendConfig
78
- from executorch.exir.passes import MemoryPlanningPass, ToOutVarPass
76
+ from executorch.exir import to_edge
79
77
80
78
81
79
# Step 1: ExecuTorch Program Export
@@ -118,13 +116,14 @@ capture_inputs = {
118
116
for m_name in method_names
119
117
}
120
118
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
128
127
129
128
# Step 2: Construct BundledConfig
130
129
@@ -291,12 +290,13 @@ Here's the example of the dtype of test input not meet model's requirement:
291
290
292
291
```python
293
292
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
+
297
295
from executorch.bundled_program.config import BundledConfig
298
296
from executorch.bundled_program.core import create_bundled_program
299
297
298
+ from executorch.exir import to_edge
299
+
300
300
301
301
class Module(torch.nn.Module):
302
302
def __init__(self):
@@ -318,16 +318,14 @@ method_names = ['forward']
318
318
inputs = torch.ones(2, 2, dtype=torch.float)
319
319
print(model(inputs))
320
320
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
331
329
332
330
333
331
# number of input sets to be verified
@@ -416,12 +414,13 @@ Another common error would be the method name in `BundledConfig` does not exist
416
414
417
415
``` python
418
416
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
+
422
419
from executorch.bundled_program.config import BundledConfig
423
420
from executorch.bundled_program.core import create_bundled_program
424
421
422
+ from executorch.exir import to_edge
423
+
425
424
426
425
427
426
class Module (torch .nn .Module ):
@@ -440,23 +439,18 @@ class Module(torch.nn.Module):
440
439
441
440
model = Module()
442
441
443
- # NOTE : wrong_forward is not an inference method in the above model.
444
- method_names = [' wrong_forward' ]
442
+ method_names = [' forward' ]
445
443
446
444
inputs = torch.ones(2 , 2 , dtype = torch.float)
447
- print (model(inputs))
448
445
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
+ }
459
451
452
+ # Emit the traced methods into ET Program.
453
+ program = to_edge(method_graphs).to_executorch().executorch_program
460
454
461
455
# Number of input sets to be verified
462
456
n_input = 10
@@ -476,7 +470,11 @@ expected_outpus = [
476
470
[[model(* x)] for x in inputs[0 ]]
477
471
]
478
472
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)
480
478
481
479
bundled_program = create_bundled_program(program, bundled_config)
482
480
@@ -518,6 +516,6 @@ File /executorch/bundled_program/core.py:147, in assert_valid_bundle(program, bu
518
516
150 but {str(method_name_of_bundled_config - method_name_of_program)} does not include."
519
517
152 # check if has been sorted in ascending alphabetical order of method name.
520
518
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.
522
520
```
523
521
:::
0 commit comments