Skip to content

Commit 16a26fb

Browse files
committed
Cleanup doc wording and code snippets in a few locations
1 parent 4df0ade commit 16a26fb

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

docs/source/backends-xnnpack.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ quantizer.set_global(quantization_config)
8181
#### Quantizing a model with the XNNPACKQuantizer
8282
After configuring the quantizer, the model can be quantized by via the `prepare_pt2e` and `convert_pt2e` APIs.
8383
```python
84+
from torch.ao.quantization.quantize_pt2e import (
85+
prepare_pt2e,
86+
convert_pt2e,
87+
)
8488
from torch.export import export_for_training
8589

8690
exported_model = export_for_training(model_to_quantize, example_inputs).module()

docs/source/getting-started.md

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ This section is intended to describe the necessary steps to take PyTorch model a
55
- Run the model using the ExecuTorch runtime APIs on your development platform.
66
- Deploy the model to the target platform using the ExecuTorch runtime.
77

8+
## System Requirements
9+
The following are required to install the ExecuTorch host libraries, needed to export models and run from Python. Requirements for target end-user devices are backend dependent. See the appropriate backend documentation for more information.
10+
11+
- Python 3.10 - 3.12
12+
- g++ version 7 or higher, clang++ version 5 or higher, or another C++17-compatible toolchain.
13+
- Linux or MacOS operating system (Arm or x86).
14+
- Windows is supported via WSL.
15+
816
## Installation
9-
To use ExecuTorch, you will need to install both the Python package and the appropriate platform-specific runtime libraries.
17+
To use ExecuTorch, you will need to install both the Python package and the appropriate platform-specific runtime libraries. Pip is the recommended way to install the ExecuTorch python package.
1018

11-
Pip is the recommended way to install the ExecuTorch python package. This package includes the dependencies needed to export a PyTorch model, as well as Python runtime bindings for model testing and evaluation. It is common to install the package within a Python virtual environment, in order to meet the Python and dependency version requirements.
19+
This package includes the dependencies needed to export a PyTorch model, as well as Python runtime bindings for model testing and evaluation. Consider installing ExecuTorch wihin a virtual environment, such as one provided by [conda](https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html#creating-environments) or [venv](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#create-and-use-virtual-environments).
1220

1321
```
1422
pip install executorch
1523
```
1624

17-
To build the framework from source, see [Building From Source](using-executorch-building-from-source.md).
18-
19-
Backend delegates may require additional dependencies. See the appropriate backend documentation for more information.
25+
To build the framework from source, see [Building From Source](using-executorch-building-from-source.md). Backend delegates may require additional dependencies. See the appropriate backend documentation for more information.
2026

21-
#### System Requirements
22-
The following are required to install the ExecuTorch host libraries, needed to export models and run from Python. Requirements for target end-user devices are backend dependent. See the appropriate backend documentation for more information.
23-
24-
- Python 3.10 - 3.12
25-
- g++ version 7 or higher, clang++ version 5 or higher, or another C++17-compatible toolchain.
26-
- Linux or MacOS operating system (Arm or x86).
27-
- Windows is supported via WSL.
2827

2928
<hr/>
3029

@@ -44,15 +43,20 @@ ExecuTorch provides hardware acceleration for a wide variety of hardware. The mo
4443
For mobile use cases, consider using XNNPACK for Android and Core ML or XNNPACK for iOS as a first step. See [Hardware Backends](backends-overview.md) for more information.
4544

4645
### Exporting
47-
Exporting is done using Python APIs. ExecuTorch provides a high degree of customization during the export process, but the typical flow is as follows:
46+
Exporting is done using Python APIs. ExecuTorch provides a high degree of customization during the export process, but the typical flow is as follows. This example uses the MobileNet V2 image classification model implementation in torchvision, but the process supports any [export-compliant](https://pytorch.org/docs/stable/export.html) PyTorch model.
47+
4848
```python
49-
import executorch
49+
import torch
50+
import torchvision.models as models
51+
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights
52+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
53+
from executorch.exir import to_edge_transform_and_lower
5054

51-
model = MyModel() # The PyTorch model to export
52-
example_inputs = (torch.randn(1,3,64,64),) # A tuple of inputs
55+
model = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval()
56+
sample_inputs = (torch.randn(1, 3, 224, 224), )
5357

54-
et_program = executorch.exir.to_edge_transform_and_lower(
55-
torch.export.export(model, example_inputs),
58+
et_program = to_edge_transform_and_lower(
59+
torch.export.export(model, sample_inputs),
5660
partitioner=[XnnpackPartitioner()]
5761
).to_executorch()
5862

@@ -62,24 +66,27 @@ with open("model.pte", "wb") as f:
6266

6367
If the model requires varying input sizes, you will need to specify the varying dimensions and bounds as part of the `export` call. See [Model Export and Lowering](using-executorch-export.md) for more information.
6468

65-
The hardware backend to target is controlled by the partitioner parameter to to\_edge\_transform\_and\_lower. In this example, the XnnpackPartitioner is used to target mobile CPUs. See the delegate-specific documentation for a full description of the partitioner and available options.
69+
The hardware backend to target is controlled by the partitioner parameter to to\_edge\_transform\_and\_lower. In this example, the XnnpackPartitioner is used to target mobile CPUs. See the [backend-specific documentation](backends-overview.md) for information on how to use each backend.
6670

6771
Quantization can also be done at this stage to reduce model size and runtime. Quantization is backend-specific. See the documentation for the target backend for a full description of supported quantization schemes.
6872

6973
### Testing the Model
7074

7175
After successfully generating a .pte file, it is common to use the Python runtime APIs to validate the model on the development platform. This can be used to evaluate model accuracy before running on-device.
7276

73-
Inference can be run as follows:
77+
For the MobileNet V2 model from torchvision used in this example, image inputs are expected as a normalized, float32 tensor with a dimensions of (batch, channels, height, width). The output See [torchvision.models.mobilenet_v2](https://pytorch.org/vision/main/models/generated/torchvision.models.mobilenet_v2.html) for more information on the input and output tensor format for this model.
78+
7479
```python
80+
import torch
7581
from executorch.runtime import Runtime
82+
from typing import List
7683

7784
runtime = Runtime.get()
7885

79-
input_tensor = torch.randn(1,3,128,128)
80-
program = runtime.load_program("/path/to/mode.pte")
86+
input_tensor: torch.Tensor = torch.randn(1, 3, 224, 224)
87+
program = runtime.load_program("model.pte")
8188
method = program.load_method("forward")
82-
outputs = method.execute([input_tensor])
89+
outputs: List[torch.Tensor] = method.execute([input_tensor])
8390
```
8491

8592

@@ -101,13 +108,15 @@ To add the library to your app, download the AAR, and add it to the gradle build
101108

102109
```
103110
mkdir -p app/libs
104-
curl https://ossci-android.s3.amazonaws.com/executorch/release/executorch-241002/executorch.aar -o app/libs/executorch.aar
111+
curl https://ossci-android.s3.amazonaws.com/executorch/release/v0.5.0-rc3/executorch.aar -o app/libs/executorch.aar
105112
```
106113
And in gradle,
107114
```
108115
# app/build.gradle.kts
109116
dependencies {
110117
implementation(files("libs/executorch.aar"))
118+
implementation("com.facebook.soloader:soloader:0.10.5")
119+
implementation("com.facebook.fbjni:fbjni:0.5.1")
111120
}
112121
```
113122

docs/source/using-executorch-export.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Model(torch.nn.Module):
5858
torch.nn.ReLU(),
5959
torch.nn.Conv2d(8, 16, 3),
6060
torch.nn.ReLU(),
61-
torch.nn.AdaptiveAvgPool2d((1,1))
61+
torch.nn.AdaptiveAvgPool2d([1,1])
6262
)
6363
self.linear = torch.nn.Linear(16, 10)
6464

@@ -68,12 +68,14 @@ class Model(torch.nn.Module):
6868
y = self.linear(y)
6969
return y
7070

71-
model = Model()
71+
model = Model().eval()
7272
inputs = (torch.randn(1,1,16,16),)
7373
outputs = model(*inputs)
7474
print(f"Model output: {outputs}")
7575
```
7676

77+
Note that the model is set to evaluation mode using `.eval()`. Models should always be exported in evaluation mode unless performing on-device training. This mode configures certain operations with training-specific behavior, such as batch norm or dropout, to use the inference-mode configuration.
78+
7779
## Export and Lowering
7880

7981
To actually export and lower the model, call `export`, `to_edge_transform_and_lower`, and `to_executorch` in sequence. This yields an ExecuTorch program which can be serialized to a file. Putting it all together, lowering the example model above using the XNNPACK delegate for mobile CPU performance can be done as follows:

0 commit comments

Comments
 (0)