Skip to content

Commit c56a54f

Browse files
committed
finetune lora example
1 parent 7fb6731 commit c56a54f

File tree

3 files changed

+78
-32
lines changed

3 files changed

+78
-32
lines changed

program-data-separation/cpp/lora_example/README.md

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ This directory contains the C++ code for the LoRA demo.
44

55
You'll learn how to:
66
1. Export LoRA PTE files that share a single foundation weight file.
7-
2. Load and run the LoRA PTE files, and notice that the runtime memory is not doubled as the foundation weights are shared.
7+
2. Load and run the LoRA PTE files, and notice that the runtime memory increases by the LoRA adapter size (small) and not the foundation weight size (large), because the foundation weights are shared.
88

99
Note:
1010
- Weight-sharing is supported with the XNNPACK backend.
11-
- Quantization (outside of embedding quantization) is not supported when weight-sharing.
11+
- Quantization (outside of embedding quantization) is currently not supported when weight-sharing.
1212
- There are many ways to fine-tune LoRA adapters. We will go through a few examples to create a demo.
1313

1414
## Size savings.
1515

16-
Size results will vary depending on the model and LoRA config. For this demo, we save ~5GB of disk space by storing weights in a separate, sharable file and ~5GB runtime memory by sharing weights at runtime through the XNNPACK weight cache. Detailed results are below.
1716
Size results will vary depending on the model and LoRA config. For this demo, we save ~5GB of disk space by storing weights in a separate, sharable file and ~5GB runtime memory by sharing weights at runtime through the XNNPACK weight cache. Detailed results are below.
1817

1918
### XNNPACK weight sharing.
@@ -24,16 +23,15 @@ The XNNPACK backend is a singleton. Weight sharing is implemented via the XNNPAC
2423
Download pre-trained dummy adapter to export and run along with a regular Llama-3-2-1B model.
2524

2625
## Fine-tune from scratch with Unsloth and Llama-3-2-1B.
27-
We can use [Unsloth](https://unsloth.ai/), a popular tool to finetune and train LLMs, to create our LoRA adapters. Unsloth provides a [colab notebook](https://docs.unsloth.ai/get-started/fine-tuning-llms-guide/datasets-guide#synthetic-dataset-notebook) that showcases how to generate data using the Meta Synthetic Data Kit.
28-
29-
The training notebook takes a few shortcuts to reduce the latency/compute. You can change these settings for better results.
30-
1. Play around with the chunk sizes and overlap to see what works best for your dataset.
31-
2. The notebook trains on the last three data files generated; increase this for better coverage of your dataset.
32-
3. At the training step, the notebook uses max_steps=60 to speed things up. Setting num_train_epochs=1 (or greater) for a full run and max_steps=None has better results.
26+
[Unsloth](https://unsloth.ai/) provides a [colab notebook](https://docs.unsloth.ai/get-started/fine-tuning-llms-guide/datasets-guide#synthetic-dataset-notebook) that showcases how to generate data using the Meta Synthetic Data Kit, and then fine-tune it to create a LoRA adapter.
3327

3428
For this demo, we trained on two datasets:
35-
1. executorch/docs/source: an adapter with domain knowledge of executorch. Using Meta Synthetic Data Kit, you can generate qa pairs based on the executorch documentation.
36-
2. Recent Nobel prize winners (2024-2025): an adapter with knowledge beyond the cutoff date of Llama-3-2-1B. This data was taken from [Wikipedia](https://en.wikipedia.org/wiki/List_of_Nobel_laureates).
29+
1. executorch/docs/source: an adapter with domain knowledge of executorch. This used Meta Synthetic Data Kit to generate qa pairs based on the documentation.
30+
2. Recent Nobel prize winners (2024-2025): an adapter with knowledge beyond the cutoff date of Llama-3-2-1B. This data was taken from [Wikipedia](https://en.wikipedia.org/wiki/List_of_Nobel_laureates), and formatted into the chat template for training.
31+
32+
The training notebook takes a few shortcuts to reduce the latency/compute. You can change these settings for better results.
33+
1. When generating data, play around with the chunk sizes and overlap to see what works best for your dataset.
34+
2. At the training step, the notebook uses max_steps=60 to speed things up. Setting num_train_epochs=1 (or greater) for a full run and max_steps=None has better results.
3735

3836
Unsloth will output the adapter artifacts to the specified directory (in the colab notebook, 'lora_model/'). You will see a few files like such:
3937
```
@@ -57,7 +55,7 @@ python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip
5755
```
5856
Or alternatively, [install conda on your machine](https://conda.io/projects/conda/en/latest/user-guide/install/index.html)
5957
```bash
60-
conda create -yn executorch-ptd python=3.10.0 && conda activate executorch-ptd
58+
conda create -yn executorch-lora python=3.10.0 && conda activate executorch-lora
6159
```
6260

6361
## Install executorch
@@ -70,14 +68,17 @@ pip install executorch==1.1.0.devYYYYMMDD --extra-index-url https://download.pyt
7068
Or [install from source](https://docs.pytorch.org/executorch/stable/using-executorch-building-from-source.html#install-executorch-pip-package-from-source).
7169

7270
```
73-
# Clone the ExecuTorch repo from GitHub.
74-
git clone https://github.com/pytorch/executorch.git && cd executorch
71+
# Move to the executorch subdirectory
72+
cd ~/executorch-examples/program-data-separation/cpp/executorch
73+
74+
# Update to recent main.
75+
git pull origin/main
7576
7677
# Install ExecuTorch pip package.
7778
./install_executorch.sh --editable
7879
```
7980

80-
NOTE: some features are not available in executorch==1.0.0, use main or a recent nightly.
81+
NOTE: use main or a recent nightly, as some features are not available in executorch==1.0.0.
8182

8283
## Download base model
8384
We're using https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct.
@@ -115,16 +116,16 @@ python -m executorch.extension.llm.export.export_llm \
115116
export.foundation_weights_file="foundation.ptd"
116117
```
117118

118-
Expect to see two files: '<model_name>.pte' and 'foundation.ptd'. Run the command again to generate more adapter PTE files. For example:
119+
Expect to see two files: '<model_name>.pte' and 'foundation.ptd'. Run the command again to generate more adapter PTE files. The generated `foundation.ptd` files should all be the same (we are using the same base model) and you only need to keep one of them.
119120

121+
Example files:
120122
```
121123
-rw-r--r-- 1 lfq users 45555712 Oct 17 18:05 et.pte
122124
-rw-r--r-- 1 lfq users 5994013600 Oct 17 18:05 foundation.ptd
123125
-rw-r--r-- 1 lfq users 45555712 Oct 17 18:00 nobel.pte
124126
```
125127

126-
The `foundation.ptd` file should be the same regardless of the adapter.
127-
Notice the adapter PTE files about the size of the adapter_model.safetensors file generated during training. The PTE contains the adapter weights (which are not shared) and the program.
128+
Notice the adapter PTE files are about the same size as the `adapter_model.safetensors` file generated during training. The PTE contains the adapter weights (which are not shared) and the program.
128129

129130
## Install runtime dependencies.
130131
The ExecuTorch repository is configured as a git submodule at `~/executorch-examples/program-data-separation/cpp/executorch`. To initialize it:
@@ -157,7 +158,7 @@ sh build_example.sh
157158
```bash
158159
cd ~/executorch-examples/program-data-separation/cpp/lora_example
159160

160-
DOWNLOADED_PATH=Llama-3.2-1B-Instruct
161+
DOWNLOADED_PATH=~/path/to/Llama-3.2-1B-Instruct/
161162
./build/bin/executorch_program_data_separation \
162163
--tokenizer_path="${DOWNLOADED_PATH}" \
163164
--model1="et.pte" \
@@ -166,19 +167,65 @@ DOWNLOADED_PATH=Llama-3.2-1B-Instruct
166167
--prompt="Who were the winners of the Nobel Prize in Physics in 2025?" \
167168
--apply_chat_template
168169
```
169-
Set `apply_chat_template` to true as this was trained as a chatbot.
170+
Passing in the `DOWNLOADED_PATH` as the tokenizer directory will invoke the HFTokenizer, and parse the additional tokenizers files: `tokenizer_config.json` and `special_tokens_map.json`. `special_tokens_map.json` tells us which bos/eos token to use, especially if there are multiple.
170171

171-
Sample output:
172+
`apply_chat_template` formats the prompt according to the LLAMA chat template, which is what the adapter was trained on.
172173

174+
Sample output:
175+
```
176+
I 00:00:00.538779 executorch:main.cpp:133] Generating with model et.pte..
177+
...
178+
I 00:00:06.999737 executorch:text_llm_runner.cpp:182] RSS after prompt prefill: 6941.296875 MiB (0 if unsupported)
179+
I don't have information on the winners of the Nobel Prize in Physics in 2025.<|eot_id|>
180+
...
181+
I 00:00:11.635379 executorch:main.cpp:141] Generating with model nobel.pte...
182+
...
183+
I 00:00:14.109447 executorch:text_llm_runner.cpp:182] RSS after prompt prefill: 8041.632812 MiB (0 if unsupported)
184+
John Clarke, Michel H. Devoret, John M. Martinis<|eot_id|>
185+
```
186+
We can see that the ExecuTorch-trained adapter model does not have knowledge of the recent Nobel Prize winners, as neither the base model or adapter was trained on it. Meanwhile, the Nobel-prize adapter model can answer well.
173187

174188

175189
Let's try with an executorch-specific prompt.
176-
```
177-
DOWNLOADED_PATH=Llama-3.2-1B-Instruct
190+
```bash
191+
cd ~/executorch-examples/program-data-separation/cpp/lora_example
192+
193+
DOWNLOADED_PATH=~/path/to/Llama-3.2-1B-Instruct/
178194
./build/bin/executorch_program_data_separation \
179195
--tokenizer_path="${DOWNLOADED_PATH}" \
180-
--model1="adapter_model1.pte" \
181-
--model2="adapter_model2.pte" \
196+
--model1="et.pte" \
197+
--model2="nobel.pte" \
182198
--weights="foundation.ptd" \
183-
--prompt="Help me get started with ExecuTorch"
199+
--prompt="Help me get started with ExecuTorch in 3 steps" \
200+
--apply_chat_template
201+
```
202+
203+
Sample output:
204+
```
205+
...
206+
I 00:00:00.554048 executorch:main.cpp:133] Generating with model et.pte...
207+
...
208+
Here are 3 steps to get started with ExecuTorch:
209+
210+
Step 1: Install ExecuTorch dependencies. This includes installing Python 3.8+ library, PyTorch library, and the ExecuTorch runtime.
211+
212+
Step 2: Set up a Python environment with pip and a virtual environment (e.g., conda) to isolate ExecuTorch dependencies.
213+
214+
Step 3: Clone the Execu
215+
I 00:00:27.243400 executorch:text_llm_runner.cpp:206] RSS after finishing text generation: 6940.410156 MiB (0 if unsupported)
216+
...
217+
I 00:00:27.243504 executorch:main.cpp:141] Generating with model nobel.pte...
218+
...
219+
Here are the 3 steps to get started with Excetorch:
220+
221+
**Step 1: Install Node.js and npm**
222+
223+
Excetorch is a JavaScript compiler, so you'll need Node.js and npm (the Node Package Manager) installed on your computer. You can download Node.js from the official website and npm from the npm website. Follow the installation instructions for your operating system.
224+
225+
**Step 2: Install Excetorch**
226+
227+
228+
I 00:00:50.189743 executorch:text_llm_runner.cpp:206] RSS after finishing text generation: 8039.152344 MiB (0 if unsupported)
184229
```
230+
231+
The ExecuTorch-trained adapter model has domain knowledge of ExecuTorch codebase, whereas the Nobel-prize trained adapter model does not.

program-data-separation/cpp/lora_example/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int main(int argc, char *argv[]) {
108108
llm::create_text_llm_runner(model1, std::move(tokenizer1),
109109
weights, temperature);
110110
std::unique_ptr<llm::TextLLMRunner> runner2 =
111-
llm::create_text_llm_runner(model1, std::move(tokenizer2),
111+
llm::create_text_llm_runner(model2, std::move(tokenizer2),
112112
weights, temperature);
113113

114114
llm::GenerationConfig config{
@@ -118,10 +118,11 @@ int main(int argc, char *argv[]) {
118118

119119
std::string formatted_prompt = std::string();
120120
if (FLAGS_apply_chat_template) {
121+
ET_LOG(Info, "Applying chat template...");
121122
// System Prompt.
122123
formatted_prompt += "<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n";
123-
formatted_prompt += "You are a helpful assistant.<|eot_id|>";
124-
// User prompt.
124+
formatted_prompt += "You are a helpful assistant.";
125+
formatted_prompt += "<|eot_id|>";
125126
formatted_prompt += "<|start_header_id|>user<|end_header_id|>\n";
126127
formatted_prompt += prompt;
127128
formatted_prompt += "<|eot_id|><|start_header_id|>assistant<|end_header_id|>";
@@ -144,6 +145,5 @@ int main(int argc, char *argv[]) {
144145
model2, error);
145146
return 1;
146147
}
147-
148148
return 0;
149149
}

program-data-separation/cpp/lora_example/quick_start.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip
99
```
1010
Or alternatively, [install conda on your machine](https://conda.io/projects/conda/en/latest/user-guide/install/index.html)
1111
```bash
12-
conda create -yn executorch-ptd python=3.10.0 && conda activate executorch-ptd
12+
conda create -yn executorch-lora python=3.10.0 && conda activate executorch-lora
1313
```
1414

1515
## Install executorch
@@ -18,7 +18,6 @@ Please install executorch. If you are using your own trained adapter (not the ex
1818
```
1919
pip install executorch==1.0.0
2020
```
21-
2221
You can also install from the nightly build.
2322
```
2423
pip install executorch==1.1.0.devYYYYMMDD --extra-index-url https://download.pytorch.org/whl/nightly/cpu

0 commit comments

Comments
 (0)