Skip to content

Commit b5b4131

Browse files
committed
newLPlanv1
1 parent 88e8020 commit b5b4131

18 files changed

Lines changed: 452 additions & 982 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Customer Support Chatbot with Llama and ExecuTorch on Arm-Based Mobile Devices (with Agentic AI Capabilities)
3+
minutes_to_complete: 60
4+
5+
who_is_this_for: This learning plan is designed for developers with basic knowledge of Python, Mobile development, and machine learning concepts.It guides you through creating an on-device customer support chatbot using Meta's Llama models deployed via PyTorch's ExecuTorch runtime.The focus is on Arm-based Android devices.The chatbot will handle common customer queries (e.g., product info, troubleshooting) with low latency, privacy (no cloud dependency), and optimized performance.Incorporates agentic AI capabilities, transforming the chatbot from reactive (simple Q&A) to proactive and autonomous. Agentic AI enables the bot to plan multi-step actions, use external tools,reason over user intent, and adapt responses dynamically. This is achieved by extending the core LLM with tool-calling mechanisms and multi-agent orchestration.
6+
7+
learning_objectives:
8+
- Explain the architecture and capabilities of Llama models (e.g., Llama 3.2 1B/3B) for mobile use.
9+
- Master the process of quantizing LLMs (e.g., 4-bit PTQ) to reduce model size and enable efficient inference on resource-constrained mobile devices.
10+
- Gain proficiency in using ExecuTorch to export PyTorch models to .pte format for on-device deployment.
11+
- Learn to leverage Arm-specific optimizations (e.g., XNNPACK, KleidiAI) to achieve 2-3x faster inference on Arm-based Android devices.
12+
- Implement real-time inference with Llama models, enabling seamless customer support interactions (e.g., handling FAQs, troubleshooting).
13+
14+
prerequisites:
15+
- Basic Understanding of Machine Learning & Deep Learning (Familiarity with concepts like supervised learning, neural networks, transfer learning and Understanding of model training, validation, & overfitting concepts).
16+
- Familiarity with Deep Learning Frameworks (Experience with PyTorch for building, training neural networks and Knowledge of Hugging Face Transformers for working with pre-trained LLMs.
17+
- An Arm-powered smartphone with the i8mm feature running Android, with 16GB of RAM.
18+
- A USB cable to connect your smartphone to your development machine.
19+
- An AWS Graviton4 r8g.16xlarge instance to test Arm performance optimizations, or any [Arm based instance](/learning-paths/servers-and-cloud-computing/csp/) from a cloud service provider or an on-premise Arm server or Arm based laptop.
20+
- Android Debug Bridge (adb) installed on your device. Follow the steps in [adb](https://developer.android.com/tools/adb) to install Android SDK Platform Tools. The adb tool is included in this package.
21+
- Java 17 JDK. Follow the steps in [Java 17 JDK](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html) to download and install JDK for host.
22+
- Android Studio. Follow the steps in [Android Studio](https://developer.android.com/studio) to download and install Android Studio for host.
23+
- Python 3.10.
24+
25+
author: Parichay Das
26+
27+
### Tags
28+
skilllevels: Introductory
29+
subjects: ML
30+
armips:
31+
- Neoverse
32+
33+
tools_software_languages:
34+
- LLM
35+
- GenAI
36+
- Python
37+
- PyTorch
38+
- ExecuTorch
39+
operatingsystems:
40+
- Linux
41+
- Windows
42+
- Android
43+
44+
45+
further_reading:
46+
- resource:
47+
title: Hugging Face Documentation
48+
link: https://huggingface.co/docs
49+
type: documentation
50+
- resource:
51+
title: PyTorch Documentation
52+
link: https://pytorch.org/docs/stable/index.html
53+
type: documentation
54+
- resource:
55+
title: Android
56+
link: https://www.android.com/
57+
type: website
58+
59+
60+
### FIXED, DO NOT MODIFY
61+
# ================================================================================
62+
weight: 1 # _index.md always has weight of 1 to order correctly
63+
layout: "learningpathall" # All files under learning paths have this same wrapper
64+
learning_path_main_page: "yes" # This should be surfaced when looking for related content. Only set for _index.md of learning path content.
65+
---

content/learning-paths/embedded-and-microcontrollers/finetuning-llms-on-arm-powered-mobile-devices-with-executorch/_next-steps.md renamed to content/learning-paths/embedded-and-microcontrollers/customer-support-chatbot-with-llama-and-executorch-on-arm-based-mobile-devices/_next-steps.md

File renamed without changes.

content/learning-paths/embedded-and-microcontrollers/finetuning-llms-on-arm-powered-mobile-devices-with-executorch/example-picture.png renamed to content/learning-paths/embedded-and-microcontrollers/customer-support-chatbot-with-llama-and-executorch-on-arm-based-mobile-devices/example-picture.png

File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Overview
3+
weight: 2
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Understanding Llama: Meta’s Large Language Model
10+
Llama is a family of large language models trained using publicly available datasets. These models demonstrate strong performance across a range of natural language processing (NLP) tasks, including language translation, question answering, and text summarization.
11+
12+
In addition to their analytical capabilities, Llama models can generate human-like, coherent, and contextually relevant text, making them highly effective for applications that rely on natural language generation. Consequently, they serve as powerful tools in areas such as chatbots, virtual assistants, and language translation, as well as in creative and content-driven domains where producing natural and engaging text is essential.
13+
14+
Please note that the models are subject to the [acceptable use policy](https://github.com/meta-llama/llama/blob/main/USE_POLICY.md) and this [responsible use guide](https://github.com/meta-llama/llama/blob/main/RESPONSIBLE_USE_GUIDE.md) .
15+
16+
17+
18+
## Quantization
19+
A practical approach to make models fit within smartphone memory constraints is through 4-bit groupwise per-token dynamic quantization of all linear layers. In this technique, dynamic quantization is applied to activations—meaning the quantization parameters are computed at runtime based on the observed minimum and maximum activation values. Meanwhile, the model weights are statically quantized, where each channel is quantized in groups using 4-bit signed integers. This method significantly reduces memory usage while maintaining model performance for on-device inference.
20+
21+
This method ensures efficient memory usage while maintaining model performance on resource-constrained devices.
22+
23+
For further information, refer to [torchao: PyTorch Architecture Optimization](https://github.com/pytorch-labs/ao/).
24+
25+
The table below evaluates WikiText perplexity using [LM Eval](https://github.com/EleutherAI/lm-evaluation-harness).
26+
27+
The results are for two different groupsizes, with max_seq_len 2048, and 1000 samples:
28+
29+
|Model | Baseline (FP32) | Groupwise 4-bit (128) | Groupwise 4-bit (256)
30+
|--------|-----------------| ---------------------- | ---------------
31+
|Llama 2 7B | 9.2 | 10.2 | 10.7
32+
|Llama 3 8B | 7.9 | 9.4 | 9.7
33+
34+
Note that groupsize less than 128 was not enabled in this example, since the model was still too large. This is because current efforts have focused on enabling FP32, and support for FP16 is under way.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Environment Setup
3+
weight: 3
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Android NDK and Android Studio - Environment Setup
10+
11+
#### Plartform Required
12+
- An AWS Graviton4 r8g.16xlarge instance to test Arm performance optimizations, or any [Arm based instance](/learning-paths/servers-and-cloud-computing/csp/) from a cloud service provider or an on-premise Arm server or Arm based laptop.
13+
- An Arm-powered smartphone with the i8mm feature running Android, with 16GB of RAM.
14+
- A USB cable to connect your smartphone to your development machine.
15+
16+
The installation and configuration of Android Studio can be accomplished through the following steps:
17+
1. Download and install the latest version of [Android Studio](https://developer.android.com/studio).
18+
2. Launch Android Studio and access the Settings dialog.
19+
3. Navigate to Languages & Frameworks → Android SDK.
20+
4. Under the SDK Platforms tab, ensure that Android 14.0 (“UpsideDownCake”) is selected.
21+
22+
Next, proceed to install the required version of the Android NDK by first setting up the Android Command Line Tools.
23+
Linux:
24+
```bash
25+
curl https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -o commandlinetools.zip
26+
unzip commandlinetools.zip
27+
./commandlinetools/bin/sdkmanager --install "ndk;26.1.10909697"
28+
```
29+
Install the NDK in the same directory where Android Studio has installed the SDK, which is typically located at ~/Library/Android/sdk by default. Then, configure the necessary environment variables as follows:
30+
```bash
31+
export ANDROID_HOME="$(realpath ~/Library/Android/sdk)"
32+
export PATH=$ANDROID_HOME/cmdline-tools/bin/:$PATH
33+
sdkmanager --sdk_root="${ANDROID_HOME}" --install "ndk;28.0.12433566"
34+
export ANDROID_NDK=$ANDROID_HOME/ndk/28.0.12433566/
35+
```
36+
37+
#### Install Java 17 JDK
38+
1. Open the Java SE 17 Archive [Downloads](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html) page in your browser.
39+
2. Select an appropriate download for your development machine operating system.
40+
41+
#### Install Git and cmake
42+
```bash
43+
sudo apt-get install git cmake
44+
```
45+
46+
#### Install Python 3.10
47+
```bash
48+
sudo apt-get install python3.10
49+
```
50+
51+
#### Set up ExecuTorch
52+
ExecuTorch is an end-to-end framework designed to facilitate on-device inference across a wide range of mobile and edge platforms, including wearables, embedded systems, and microcontrollers. As a component of the PyTorch Edge ecosystem, it streamlines the efficient deployment of PyTorch models on edge devices. For further details, refer to the [ExecuTorch Overview](https://pytorch.org/executorch/stable/overview/).
53+
54+
It is recommended to create an isolated Python environment to install the ExecuTorch dependencies. Instructions are available for setting up either a Python virtual environment or a Conda virtual environment—you only need to choose one of these options.
55+
56+
##### Install Required Tools ( Python environment setup)
57+
```python
58+
python3 -m venv exec_env
59+
source exec_env/bin/activate
60+
pip install torch torchvision torchaudio
61+
pip install executorch
62+
```
63+
##### Clone Required Repositories
64+
```bash
65+
git clone https://github.com/pytorch/executorch.git
66+
git clone https://github.com/pytorch/text.git
67+
```
68+
##### Download Pretrained Model (Llama 3.1 Instruct)
69+
Download the quantized model weights optimized for mobile deployment from either the Meta AI Hub or Hugging Face.
70+
```
71+
git lfs install
72+
git clone https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct
73+
```
74+
75+
##### Verify Arm SDK Path
76+
```
77+
ANDROID_SDK_ROOT=/Users/<you>/Library/Android/sdk
78+
ANDROID_NDK_HOME=$ANDROID_SDK_ROOT/ndk/26.1.10909125
79+
```

content/learning-paths/embedded-and-microcontrollers/finetuning-llms-on-arm-powered-mobile-devices-with-executorch/how-to-3.md renamed to content/learning-paths/embedded-and-microcontrollers/customer-support-chatbot-with-llama-and-executorch-on-arm-based-mobile-devices/how-to-3.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,64 @@
11
---
2-
title: Model Loading & Optimization for ARM
2+
title: Model Preparation and Conversion
33
weight: 4
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

9-
## Model Loading & Optimization for ARM
9+
To begin working with Llama 3, the pre-trained model parameters can be accessed through Meta’s Llama Downloads page. Users are required to request access by submitting their details and reviewing and accepting the Responsible Use Guide. Upon approval, a license and a download link—valid for 24 hours—are provided. For this exercise, the Llama 3.2 1B Instruct model is utilized; however, the same procedures can be applied to other available variants with only minor modifications.
1010

11-
###### Hugging Face Authentication
12-
```bash
13-
Login to Hugging Face (needed for Llama models)
14-
pip install huggingface_hub
15-
huggingface-cli login
11+
Convert the model into an ExecuTorch-compatible format optimized for Arm devices
12+
## Script the Model
1613

17-
Enter your token when prompted
18-
Get token from: https://huggingface.co/settings/tokens
14+
```python
15+
import torch
16+
from transformers import AutoModelForCausalLM
17+
18+
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B-Instruct", torch_dtype=torch.float16)
19+
scripted_model = torch.jit.script(model)
20+
scripted_model.save("llama_exec.pt")
21+
22+
```
23+
24+
Install the llama-stack package from pip.
25+
```python
26+
pip install llama-stack
27+
```
28+
29+
Run the command to download, and paste the download link from the email when prompted.
30+
```python
31+
llama model download --source meta --model-id Llama3.2-1B-Instruct
32+
```
33+
34+
When the download is finished, the installation path is printed as output.
35+
```python
36+
Successfully downloaded model to /<path-to-home>/.llama/checkpoints/Llama3.2-1B-Instruct
37+
```
38+
Verify by viewing the downloaded files under this path:
39+
```
40+
ls $HOME/.llama/checkpoints/Llama3.2-1B-Instruct
41+
checklist.chk consolidated.00.pth params.json tokenizer.model
1942
2043
```
2144

45+
Export the model and generate a .pte file by running the appropriate Python command. This command will export the model and save the resulting file in your current working directory.
46+
```python
47+
python3 -m examples.models.llama.export_llama \
48+
--checkpoint $HOME/.llama/checkpoints/Llama3.2-1B-Instruct/consolidated.00.pth \
49+
--params $HOME/.llama/checkpoints/Llama3.2-1B-Instruct/params.json \
50+
-kv --use_sdpa_with_kv_cache -X --xnnpack-extended-ops -qmode 8da4w \
51+
--group_size 64 -d fp32 \
52+
--metadata '{"get_bos_id":128000, "get_eos_ids":[128009, 128001, 128006, 128007]}' \
53+
--embedding-quantize 4,32 \
54+
--output_name="llama3_1B_kv_sdpa_xnn_qe_4_64_1024_embedding_4bit.pte" \
55+
--max_seq_length 1024 \
56+
--max_context_length 1024
57+
```
58+
59+
Because Llama 3 has a larger vocabulary size, it is recommended to quantize the embeddings using the parameter --embedding-quantize 4,32. This helps to further optimize memory usage and reduce the overall model size.
60+
61+
2262
###### Load a pre-fine-tuned model (from Hugging Face)
2363
- Example: meta-llama/Llama-3-8B-Instruct or a customer-support fine-tuned variant
2464

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: Building the Chatbot Logic
3+
4+
### FIXED, DO NOT MODIFY
5+
layout: learningpathall
6+
---
7+
8+
## Conversation Framework (Python prototype)
9+
```python
10+
from transformers import AutoTokenizer
11+
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
12+
13+
def generate_response(model, query, context):
14+
prompt = f"### Context:\n{context}\n### User Query:\n{query}\n### Assistant Response:"
15+
inputs = tokenizer(prompt, return_tensors="pt")
16+
outputs = model.generate(**inputs, max_new_tokens=200)
17+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
18+
```
19+
20+
###### Context Memory (Simple JSON Store)
21+
22+
```python
23+
import json
24+
25+
def update_memory(user_id, query, response):
26+
memory = json.load(open("chat_memory.json", "r"))
27+
memory[user_id].append({"query": query, "response": response})
28+
json.dump(memory, open("chat_memory.json", "w"))
29+
30+
```
31+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: Adding Agentic AI Capabilities
3+
weight: 6
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
Enable the chatbot to perform reasoning, make decisions, and execute actions autonomously
9+
10+
## Define Agentic Loop
11+
```python
12+
class AgenticChatbot:
13+
def __init__(self, model):
14+
self.model = model
15+
16+
def observe(self, input):
17+
return f"User said: {input}"
18+
19+
def think(self, observation):
20+
return f"Decide best next step based on intent."
21+
22+
def act(self, decision):
23+
if "refund" in decision:
24+
return "Processing refund..."
25+
elif "troubleshoot" in decision:
26+
return "Let's check your device settings."
27+
else:
28+
return "Connecting you with an agent."
29+
30+
def respond(self, query):
31+
obs = self.observe(query)
32+
thought = self.think(obs)
33+
action = self.act(thought)
34+
return f"Reasoning: {thought}\nAction: {action}"
35+
```
36+
## Integrate Llama with Reasoning Loop
37+
```python
38+
def generate_agentic_response(query, context):
39+
reasoning = agent.respond(query)
40+
model_response = generate_response(model, query, context)
41+
return reasoning + "\n\n" + model_response
42+
```

0 commit comments

Comments
 (0)