Skip to content

Commit 139392c

Browse files
nil-is-allNikhil Viswanath Sivakumar
authored and
Nikhil Viswanath Sivakumar
committed
parent 5021c18
author Nikhil Viswanath Sivakumar <68182521+nil-is-all@users.noreply.github.com> 1741841357 -0500 committer Nikhil Viswanath Sivakumar <nikhilviswanath@meta.com> 1745433893 -0500 Migrate iOS Demo App parent 5021c18 author Nikhil Viswanath Sivakumar <68182521+nil-is-all@users.noreply.github.com> 1741841357 -0500 committer Nikhil Viswanath Sivakumar <nikhilviswanath@meta.com> 1745433648 -0500 Migrate iOS Demo App Migrate iOS app demo as README.md Reproduced https://github.com/pytorch/executorch/tree/main/examples/demo-apps/apple_ios/ExecuTorchDemo and added last line in docs to point to this GitHub repo Changed file path to executorch-examples/mv3/apple/MobileNetV3Demo/README.md Changed directory and made changes after local testing Final directory: executorch-examples/mv3/apple/ExecuTorchDemo Changes from [current demo](https://pytorch.org/executorch/main/demo-apps-ios.html#): 1) Check Swift Package Manager version under Prerequisites 2) Export Model - export.py for backends 3) Directory changes in Final Steps Files for iOS MV3 Demo App export script for MV3.pte and XNNPack, CoreML, MPS backends Update README.md Rename mv3/apple/ExecuTorchDemo/export.py to mv3/apple/ExecuTorchDemo/python/export.py Removed export.py code block from README.md Added code formatting for Clone the Demo App subsection added snapshot url Delete mv3/apple/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet directory atomically migrated apple/ExecuTorch to mv3/apple/ExecuTorchDemo Updated directory structure to have python in mv3 folder fixed typo in .gitignore Update project.pbxproj to pin to 0.6.0 (#11) MV2 cpp and python example (#13) Update README.md (#12) * Update README.md * Update README.md fix directory and resolve conflicts MV2 cpp and python example (#13) Update .gitignore (#17)
1 parent 5021c18 commit 139392c

27 files changed

+279
-0
lines changed

mv2/cpp/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,22 @@ This is a simple C++ demo application that uses the ExecuTorch library for Mobil
3636

3737
- CMake 3.18 or higher
3838
- C++17 compatible compiler
39+
<<<<<<< HEAD
3940
- ExecuTorch library (release/0.6)
4041

4142
## Notes
4243

4344
- Make sure you have the correct model file (`.pte`) compatible with ExecuTorch.
4445
- This demo currently initializes the input tensor with random data. In a real application, you would replace this with actual input data.
46+
=======
47+
- ExecutorTorch library (release/0.6)
48+
49+
## Notes
50+
51+
- Make sure you have the correct model file (`.pte`) compatible with ExecutorTorch.
52+
- This demo currently initializes the input tensor with random data. In a real application, you would replace this with actual input data.
53+
<<<<<<< HEAD
54+
>>>>>>> 00ac48b (MV2 cpp and python example (#13))
55+
=======
56+
>>>>>>> 54b0238 (MV2 cpp and python example (#13))
57+
>>>>>>> 7d5f612 (MV2 cpp and python example (#13))

mv3/apple/ExecuTorchDemo/ExecuTorchDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict/>
5+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"originHash" : "18bbbaa47c24d88e1d32540f35aedf6e1bd14398eb435a331199a9d3995e4e24",
3+
"pins" : [
4+
{
5+
"identity" : "executorch",
6+
"kind" : "remoteSourceControl",
7+
"location" : "https://github.com/pytorch/executorch",
8+
"state" : {
9+
"branch" : "swiftpm-0.6.0",
10+
"revision" : "65f4d1656eba10ece31e340b814731b7f98d91b4"
11+
}
12+
}
13+
],
14+
"version" : 3
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>BuildLocationStyle</key>
6+
<string>UseAppPreferences</string>
7+
<key>CustomBuildLocationType</key>
8+
<string>RelativeToDerivedData</string>
9+
<key>DerivedDataLocationStyle</key>
10+
<string>Default</string>
11+
<key>ShowSharedSchemesAutomaticallyEnabled</key>
12+
<true/>
13+
</dict>
14+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>App.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
<key>ImageClassification.xcscheme_^#shared#^_</key>
13+
<dict>
14+
<key>orderHint</key>
15+
<integer>2</integer>
16+
</dict>
17+
<key>MobileNetClassifier.xcscheme_^#shared#^_</key>
18+
<dict>
19+
<key>orderHint</key>
20+
<integer>3</integer>
21+
</dict>
22+
<key>MobileNetClassifierTest.xcscheme_^#shared#^_</key>
23+
<dict>
24+
<key>orderHint</key>
25+
<integer>1</integer>
26+
</dict>
27+
</dict>
28+
</dict>
29+
</plist>

mv3/apple/ExecuTorchDemo/README.md

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Building an ExecuTorch iOS Demo App
2+
3+
Welcome to the tutorial on setting up the ExecuTorch iOS Demo App!
4+
5+
This app uses the
6+
[MobileNet v3](https://pytorch.org/vision/main/models/mobilenetv3.html) model to
7+
process live camera images leveraging three different backends:
8+
[XNNPACK](https://github.com/google/XNNPACK),
9+
[Core ML](https://developer.apple.com/documentation/coreml) and
10+
[Metal Performance Shaders (MPS)](https://developer.apple.com/documentation/metalperformanceshaders)
11+
(Xcode 15+ and iOS 17+ only).
12+
13+
<p align="center">
14+
<img src="https://github.com/pytorch/executorch/blob/main/docs/source/_static/img/demo_ios_app.png" width="50%">
15+
</p>
16+
17+
## Prerequisites
18+
19+
Before we start, make sure you have the following tools installed:
20+
21+
### 1. Xcode 15+ and Command Line Tools
22+
23+
Install Xcode 15+ from the
24+
[Mac App Store](https://apps.apple.com/app/xcode/id497799835) and then install
25+
the Command Line Tools using the terminal:
26+
27+
```bash
28+
xcode-select --install
29+
```
30+
31+
### 2. Python 3.10+
32+
33+
Python 3.10 or above, along with `pip3`, should be pre-installed on MacOS 13.5+.
34+
If needed, [download Python](https://www.python.org/downloads/macos/) and
35+
install it. Verify the Python and pip versions using these commands:
36+
37+
```bash
38+
which python3 pip3
39+
python3 --version
40+
pip3 --version
41+
```
42+
43+
### 3. Check Swift Package Manager version
44+
The prebuilt ExecuTorch runtime, backend, and kernels are available as a Swift PM
45+
package. Ensure the latest SwiftPM version is linked to your Xcode installation.
46+
The steps to add the necessary package dependency are available in the
47+
[Using ExecuTorch for iOS](https://pytorch.org/executorch/main/using-executorch-ios.html#swift-package-manager) documentation.
48+
49+
### 4. Set Up ExecuTorch
50+
51+
Clone ExecuTorch and set up the environment as explained in the [Building from Source tutorial](https://pytorch.org/executorch/main/using-executorch-building-from-source.html):
52+
53+
```bash
54+
git clone -b viable/strict https://github.com/pytorch/executorch.git && cd executorch
55+
56+
python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip
57+
58+
./install_executorch.sh
59+
```
60+
61+
### 5. Backend Dependencies
62+
63+
Install additional dependencies for [Core ML](https://pytorch.org/executorch/main/backends-coreml.html) and [MPS](https://pytorch.org/executorch/main/backends-mps.html) backends:
64+
65+
```bash
66+
./backends/apple/coreml/scripts/install_requirements.sh
67+
68+
./backends/apple/mps/install_requirements.sh
69+
```
70+
71+
### 6. Clone the Demo App
72+
73+
```bash
74+
git clone --depth 1 https://github.com/pytorch-labs/executorch-examples.git
75+
```
76+
77+
## Models and Labels
78+
79+
Now, let's move on to exporting and bundling the MobileNet v3 model.
80+
81+
### 1. Export Model
82+
83+
Export the MobileNet v3 model using the command line and move the exported model to
84+
a specific location where the Demo App will pick them up:
85+
86+
```bash
87+
python3 -m examples.portable.scripts.export --model_name="mv3"
88+
89+
APP_PATH="executorch-examples/mv3/apple/ExecuTorchDemo/ExecuTorchDemo"
90+
mkdir -p "$APP_PATH/Resources/Models/MobileNet/"
91+
mv mv3.pte "$APP_PATH/Resources/Models/MobileNet/"
92+
```
93+
94+
Next, export the MobileNet v3 model with Core ML, MPS and XNNPACK backends by runnning this [`export.py`](https://github.com/pytorch-labs/executorch-examples/mv3/apple/ExecuTorchDemo/export.py) script.
95+
96+
```bash
97+
python3 export.py
98+
```
99+
100+
101+
### 2. Download Labels
102+
103+
Download the MobileNet model labels required for image classification:
104+
105+
```bash
106+
curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt \
107+
-o "$APP_PATH/Resources/Models/MobileNet/imagenet_classes.txt"
108+
```
109+
110+
## Final Steps
111+
112+
We're almost done! Now, we just need to open the project in Xcode, run the
113+
tests, and finally run the app.
114+
115+
### 1. Open Project in Xcode
116+
117+
Double-click on the project file under
118+
`executorch-examples/mv3/apple/ExecuTorchDemo/ExecuTorchDemo` to openit with Xcode, or run the command:
119+
120+
```bash
121+
open $APP_PATH.xcodeproj
122+
```
123+
124+
### 2. Run Tests
125+
126+
You can run tests on Simulaltor directly in Xcode with `Cmd + U` or use the
127+
command line:
128+
129+
```bash
130+
xcrun simctl create executorch "iPhone 15"
131+
xcodebuild clean test \
132+
-project executorch-examples/mv3/apple/ExecuTorchDemo/ExecuTorchDemo.xcodeproj \
133+
-scheme App \
134+
-destination name=executorch
135+
xcrun simctl delete executorch
136+
```
137+
138+
### 3. Run App
139+
140+
Finally, connect the device, set up Code Signing in Xcode, and then run the app
141+
using `Cmd + R`. Try installing a Release build for better performance.
142+
143+
Congratulations! You've successfully set up the ExecuTorch iOS Demo App. Now,
144+
you can explore and enjoy the power of ExecuTorch on your iOS device!
145+
146+
Learn more about integrating and running [ExecuTorch on Apple](https://pytorch.org/executorch/main/using-executorch-ios.html) platforms.
147+
148+
For specific examples, take a look at this [GitHub repository](https://github.com/pytorch-labs/executorch-examples/mv3/apple/ExecuTorchDemo/).

mv3/python/export.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
import torch
9+
import torchvision.models as models
10+
from executorch.backends.apple.coreml.partition import CoreMLPartitioner
11+
from executorch.backends.apple.mps.partition import MPSPartitioner
12+
from executorch.exir.backend.backend_details import CompileSpec
13+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
14+
from executorch.exir import to_edge_transform_and_lower
15+
import ssl
16+
import certifi
17+
18+
19+
def main() -> None:
20+
model = models.mobilenet_v3_small(weights="DEFAULT").eval()
21+
sample_inputs = (torch.randn(1, 3, 224, 224),)
22+
23+
et_program_coreml = to_edge_transform_and_lower(
24+
torch.export.export(model, sample_inputs),
25+
partitioner=[CoreMLPartitioner()],
26+
).to_executorch()
27+
28+
et_program_mps = to_edge_transform_and_lower(
29+
torch.export.export(model, sample_inputs),
30+
partitioner=[MPSPartitioner([CompileSpec("use_fp16", bytes([True]))])],
31+
).to_executorch()
32+
33+
et_program_xnnpack = to_edge_transform_and_lower(
34+
torch.export.export(model, sample_inputs),
35+
partitioner=[XnnpackPartitioner()],
36+
).to_executorch()
37+
38+
save_path = "executorch-examples/mv3/apple/ExecuTorchDemo/ExecuTorchDemo/Resources/Models/MobileNet/"
39+
with open(save_path+"mv3_coreml_all.pte", "wb") as file:
40+
et_program_coreml.write_to_file(file)
41+
with open(save_path+"mv3_mps_float16.pte", "wb") as file:
42+
et_program_mps.write_to_file(file)
43+
with open(save_path+"mv3_xnnpack_fp32.pte", "wb") as file:
44+
et_program_xnnpack.write_to_file(file)
45+
46+
47+
if __name__ == "__main__":
48+
main()

0 commit comments

Comments
 (0)