Skip to content

Commit 8bed358

Browse files
authored
Fix Android ExecuTorchDemo docs (#5439) (#5456)
Summary: Pull Request resolved: #5439 Reviewed By: guangy10, dvorjackz Differential Revision: D62902373 Pulled By: kirklandsign fbshipit-source-id: 1ff5c3e4ce0dfb75d3094370326849a6569542f7 (cherry picked from commit 861a7bf)
1 parent 9003900 commit 8bed358

File tree

11 files changed

+142
-1
lines changed

11 files changed

+142
-1
lines changed

docs/source/_static/img/chat.png

133 KB
Loading
264 KB
Loading
1.72 MB
Loading
127 KB
Loading

docs/source/_static/img/logs.png

236 KB
Loading
371 KB
Loading
502 KB
Loading
194 KB
Loading
98.8 KB
Loading
Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,141 @@
1-
```{include} ../../../examples/demo-apps/android/LlamaDemo/README.md
1+
# ExecuTorch Llama Android Demo App
2+
3+
We’re excited to share that the newly revamped Android demo app is live and includes many new updates to provide a more intuitive and smoother user experience with a chat use case! The primary goal of this app is to showcase how easily ExecuTorch can be integrated into an Android demo app and how to exercise the many features ExecuTorch and Llama models have to offer.
4+
5+
This app serves as a valuable resource to inspire your creativity and provide foundational code that you can customize and adapt for your particular use case.
6+
7+
Please dive in and start exploring our demo app today! We look forward to any feedback and are excited to see your innovative ideas.
8+
9+
10+
## Key Concepts
11+
From this demo app, you will learn many key concepts such as:
12+
* How to prepare Llama models, build the ExecuTorch library, and model inferencing across delegates
13+
* Expose the ExecuTorch library via JNI layer
14+
* Familiarity with current ExecuTorch app-facing capabilities
15+
16+
The goal is for you to see the type of support ExecuTorch provides and feel comfortable with leveraging it for your use cases.
17+
18+
## Supporting Models
19+
As a whole, the models that this app supports are (varies by delegate):
20+
* Llama 3.1 8B
21+
* Llama 3 8B
22+
* Llama 2 7B
23+
* LLaVA-1.5 vision model (only XNNPACK)
24+
25+
26+
## Building the APK
27+
First it’s important to note that currently ExecuTorch provides support across 3 delegates. Once you identify the delegate of your choice, select the README link to get a complete end-to-end instructions for environment set-up to exporting the models to build ExecuTorch libraries and apps to run on device:
28+
29+
| Delegate | Resource |
30+
| ------------- | ------------- |
31+
| XNNPACK (CPU-based library) | [link](docs/delegates/xnnpack_README.md) |
32+
| QNN (Qualcomm AI Accelerators) | [link](docs/delegates/qualcomm_README.md) |
33+
| MediaTek (MediaTek AI Accelerators) | [link](docs/delegates/mediatek_README.md) |
34+
35+
## How to Use the App
36+
37+
This section will provide the main steps to use the app, along with a code snippet of the ExecuTorch API.
38+
39+
For loading the app, development, and running on device we recommend Android Studio:
40+
1. Open Android Studio and select "Open an existing Android Studio project" to open examples/demo-apps/android/LlamaDemo.
41+
2. Run the app (^R). This builds and launches the app on the phone.
42+
43+
### Opening the App
44+
45+
Below are the UI features for the app.
46+
47+
Select the settings widget to get started with picking a model, its parameters and any prompts.
48+
<p align="center">
49+
<img src="../_static/img/opening_the_app_details.png" width=800>
50+
</p>
51+
52+
53+
54+
### Select Models and Parameters
55+
56+
Once you've selected the model, tokenizer, and model type you are ready to click on "Load Model" to have the app load the model and go back to the main Chat activity.
57+
<p align="center">
58+
<img src="../_static/img/settings_menu.png" width=300>
59+
</p>
60+
61+
62+
63+
Optional Parameters:
64+
* Temperature: Defaulted to 0, you can adjust the temperature for the model as well. The model will reload upon any adjustments.
65+
* System Prompt: Without any formatting, you can enter in a system prompt. For example, "you are a travel assistant" or "give me a response in a few sentences".
66+
* User Prompt: More for the advanced user, if you would like to manually input a prompt then you can do so by modifying the `{{user prompt}}`. You can also modify the special tokens as well. Once changed then go back to the main Chat activity to send.
67+
68+
> [!TIP]
69+
> Helpful ExecuTorch API in app
70+
71+
```java
72+
// Upon returning to the Main Chat Activity
73+
mModule = new LlamaModule(
74+
ModelUtils.getModelCategory(mCurrentSettingsFields.getModelType()),
75+
modelPath,
76+
tokenizerPath,
77+
temperature);
78+
int loadResult = mModule.load();
279
```
80+
81+
* `modelCategory`: Indicate whether it’s a text-only or vision model
82+
* `modePath`: path to the .pte file
83+
* `tokenizerPath`: path to the tokenizer .bin file
84+
* `temperature`: model parameter to adjust the randomness of the model’s output
85+
86+
87+
### User Prompt
88+
Once model is successfully loaded then enter any prompt and click the send (i.e. generate) button to send it to the model.
89+
<p align="center">
90+
<img src="../_static/img/load_complete_and_start_prompt.png" width=300>
91+
</p>
92+
93+
You can provide it more follow-up questions as well.
94+
<p align="center">
95+
<img src="../_static/img/chat.png" width=300>
96+
</p>
97+
98+
> [!TIP]
99+
> Helpful ExecuTorch API in app
100+
```java
101+
mModule.generate(prompt,sequence_length, MainActivity.this);
102+
```
103+
* `prompt`: User formatted prompt
104+
* `sequence_length`: Number of tokens to generate in response to a prompt
105+
* `MainActivity.this`: Indicate that the callback functions (OnResult(), OnStats()) are present in this class.
106+
107+
[*LLaVA-1.5: Only for XNNPACK delegate*]
108+
109+
For LLaVA-1.5 implementation, select the exported LLaVA .pte and tokenizer file in the Settings menu and load the model. After this you can send an image from your gallery or take a live picture along with a text prompt to the model.
110+
111+
<p align="center">
112+
<img src="../_static/img/llava_example.png" width=300>
113+
</p>
114+
115+
116+
### Output Generated
117+
To show completion of the follow-up question, here is the complete detailed response from the model.
118+
<p align="center">
119+
<img src="../_static/img/chat_response.png" width=300>
120+
</p>
121+
122+
> [!TIP]
123+
> Helpful ExecuTorch API in app
124+
125+
Ensure you have the following functions in your callback class that you provided in the `mModule.generate()`. For this example, it is `MainActivity.this`.
126+
```java
127+
@Override
128+
public void onResult(String result) {
129+
//...result contains token from response
130+
//.. onResult will continue to be invoked until response is complete
131+
}
132+
133+
@Override
134+
public void onStats(float tps) {
135+
//...tps (tokens per second) stats is provided by framework
136+
}
137+
138+
```
139+
140+
## Reporting Issues
141+
If you encountered any bugs or issues following this tutorial please file a bug/issue here on [Github](https://github.com/pytorch/executorch/issues/new).

0 commit comments

Comments
 (0)