Skip to content

Commit 63d269a

Browse files
Songhao Jiafacebook-github-bot
authored andcommitted
Update export api to long term version
Summary: Update the export apis to long term version apis in bundled program code base and its documentations. Differential Revision: D50256862
1 parent 52c8752 commit 63d269a

File tree

2 files changed

+47
-48
lines changed

2 files changed

+47
-48
lines changed

bundled_program/tests/common.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import string
1010
from typing import List, Tuple, Union
1111

12-
import executorch.exir as exir
1312
import torch
13+
1414
from executorch.bundled_program.config import BundledConfig
15+
from executorch.exir import to_edge
1516
from executorch.exir.schema import Program
17+
from torch.export import export
1618

1719
# @manual=fbsource//third-party/pypi/typing-extensions:typing-extensions
1820
from typing_extensions import TypeAlias
@@ -210,12 +212,14 @@ def get_common_program() -> Tuple[Program, BundledConfig]:
210212
for m_name in eager_model.method_names
211213
}
212214

213-
program = (
214-
exir.capture_multiple(eager_model, capture_inputs)
215-
.to_edge()
216-
.to_executorch()
217-
.program
218-
)
215+
# Trace to FX Graph and emit the program
216+
method_graphs = {
217+
m_name: export(getattr(eager_model, m_name), capture_inputs[m_name])
218+
for m_name in eager_model.method_names
219+
}
220+
221+
program = to_edge(method_graphs).to_executorch().executorch_program
222+
219223
_, bundled_config = get_random_config_with_eager_model(
220224
eager_model=eager_model,
221225
method_names=eager_model.method_names,

docs/source/sdk-bundled-io.md

Lines changed: 36 additions & 41 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
7575

76-
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
@@ -119,12 +117,12 @@ capture_inputs = {
119117
}
120118

121119
# 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-
)
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+
program = to_edge(method_graphs).to_executorch().executorch_program
128126

129127
# Step 2: Construct BundledConfig
130128

@@ -291,12 +289,13 @@ Here's the example of the dtype of test input not meet model's requirement:
291289
292290
```python
293291
import torch
294-
from executorch import exir
295-
from executorch.exir import ExecutorchBackendConfig
296-
from executorch.exir.passes import MemoryPlanningPass, ToOutVarPass
292+
from torch.export import export
293+
297294
from executorch.bundled_program.config import BundledConfig
298295
from executorch.bundled_program.core import create_bundled_program
299296
297+
from executorch.exir import to_edge
298+
300299
301300
class Module(torch.nn.Module):
302301
def __init__(self):
@@ -318,16 +317,13 @@ method_names = ['forward']
318317
inputs = torch.ones(2, 2, dtype=torch.float)
319318
print(model(inputs))
320319
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-
)
320+
# Trace to FX Graph and emit the program
321+
method_graphs = {
322+
m_name: export(getattr(model, m_name), (inputs, ))
323+
for m_name in method_names
324+
}
325+
326+
program = to_edge(method_graphs).to_executorch().executorch_program
331327
332328
333329
# number of input sets to be verified
@@ -416,12 +412,13 @@ Another common error would be the method name in `BundledConfig` does not exist
416412

417413
```python
418414
import torch
419-
from executorch import exir
420-
from executorch.exir import ExecutorchBackendConfig
421-
from executorch.exir.passes import MemoryPlanningPass, ToOutVarPass
415+
from torch.export import export
416+
422417
from executorch.bundled_program.config import BundledConfig
423418
from executorch.bundled_program.core import create_bundled_program
424419

420+
from executorch.exir import to_edge
421+
425422

426423

427424
class Module(torch.nn.Module):
@@ -440,23 +437,17 @@ class Module(torch.nn.Module):
440437

441438
model = Module()
442439

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

446442
inputs = torch.ones(2, 2, dtype=torch.float)
447-
print(model(inputs))
448443

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-
)
444+
# Trace to FX Graph and emit the program
445+
method_graphs = {
446+
m_name: export(getattr(model, m_name), (inputs, ))
447+
for m_name in method_names
448+
}
459449

450+
program = to_edge(method_graphs).to_executorch().executorch_program
460451

461452
# Number of input sets to be verified
462453
n_input = 10
@@ -476,7 +467,11 @@ expected_outpus = [
476467
[[model(*x)] for x in inputs[0]]
477468
]
478469

479-
bundled_config = BundledConfig(method_names, inputs, expected_outpus)
470+
471+
# NOTE: WRONG_FORWARD is not an inference method in the above model.
472+
wrong_method_names = ['WRONG_FORWARD']
473+
474+
bundled_config = BundledConfig(wrong_method_names, inputs, expected_outpus)
480475

481476
bundled_program = create_bundled_program(program, bundled_config)
482477

0 commit comments

Comments
 (0)