Skip to content

Commit 0c78c98

Browse files
jackzhxngfacebook-github-bot
authored andcommitted
Export mini phi3 LoRA model (#4062)
Summary: LoRA export example for exporting mini phi3 with lora from TorchTune to executorch. Reviewed By: JacobSzwejbka Differential Revision: D58897306
1 parent f538eae commit 0c78c98

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Summary
2+
In this exmaple, we export a model ([phi-3-mini](https://github.com/pytorch/executorch/tree/main/examples/models/phi-3-mini)) appended with additional LoRA layers to ExecuTorch.
3+
4+
## Instructions
5+
### Step 1: [Optional] Install ExecuTorch dependencies
6+
`./install_requirements.sh` in ExecuTorch root directory.
7+
8+
### Step 2: Install TorchTune nightly
9+
The LoRA model used is recent and is not yet officially released on `TorchTune`. To be able to run this example, you will need to run the following to install TorchTune nighly:
10+
- `./examples/models/llava_encoder/install_requirements.sh`'
11+
12+
### Step 3: Export and run the model
13+
1. Export the model to ExecuTorch.
14+
```
15+
python export_model.py
16+
```
17+
18+
2. Run the model using an example runtime. For more more detailed steps on this, check out [Build & Run](https://pytorch.org/executorch/stable/getting-started-setup.html#build-run).
19+
```
20+
# Clean and configure the CMake build system. Compiled programs will appear in the executorch/cmake-out directory we create here.
21+
(rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake ..)
22+
23+
# Build the executor_runner target
24+
cmake --build cmake-out --target executor_runner -j9
25+
26+
./cmake-out/executor_runner --model_path mini_phi3_lora.pte
27+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
from executorch.exir import to_edge
8+
from torch import int64, long, no_grad, randint, Tensor, zeros
9+
from torch.export import export, ExportedProgram
10+
from torch.nn.attention import sdpa_kernel, SDPBackend
11+
from torchtune.models.phi3._model_builders import lora_phi3_mini
12+
13+
14+
@no_grad()
15+
def export_mini_phi3_lora(model) -> None:
16+
"""
17+
Export the example mini-phi3 with LoRA model to executorch.
18+
19+
Note: need to use the SDPBackend's custom kernel for sdpa (scalable
20+
dot product attention) because the default sdpa kernel used in the
21+
model results in a internally mutating graph.
22+
"""
23+
model.eval()
24+
# 1. torch.export: Defines the program with the ATen operator set.
25+
print("Exporting to aten dialect")
26+
example_args = (randint(0, 100, (1, 100), dtype=long),)
27+
with sdpa_kernel([SDPBackend.MATH]):
28+
aten_dialect: ExportedProgram = export(model, example_args)
29+
30+
# 2. to_edge: Make optimizations for Edge devices.
31+
print("Lowering to edge dialect")
32+
edge_program = to_edge(aten_dialect)
33+
34+
# 3. to_executorch: Convert the graph to an ExecuTorch program.
35+
print("Exporting to executorch")
36+
executorch_program = edge_program.to_executorch()
37+
38+
# 4. Save the compiled .pte program.
39+
print("Saving to mini_phi3_lora.pte")
40+
with open("mini_phi3_lora.pte", "wb") as file:
41+
file.write(executorch_program.buffer)
42+
43+
print("Done.")
44+
45+
46+
def run_mini_phi3_lora(model) -> Tensor:
47+
"""Run the model and return the result."""
48+
args = zeros([3072, 1], dtype=int64)
49+
model.eval()
50+
res = model(args)
51+
return res
52+
53+
54+
def main() -> None:
55+
mini_lora_model = lora_phi3_mini(
56+
lora_attn_modules=[
57+
"q_proj",
58+
]
59+
)
60+
export_mini_phi3_lora(mini_lora_model)
61+
62+
63+
if __name__ == "__main__":
64+
main()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
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+
# Install nightly build of TorchTune.
9+
pip install --pre torchtune --extra-index-url https://download.pytorch.org/whl/nightly/cpu --no-cache-dir
10+
pip install tiktoken

0 commit comments

Comments
 (0)