Skip to content

Commit 8276297

Browse files
metascroymalfet
authored andcommitted
Fix cmakelist lists for runner-et to reflect upstream changes in ET (#378)
* Fix cmakelist lists for runner-et to reflect upstream changes in ET * update docs for runners + et install * temp fix to broken ET export. Add example to doc
1 parent f4c2f1f commit 8276297

File tree

6 files changed

+92
-20
lines changed

6 files changed

+92
-20
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ __pycache__/
88

99
.model-artifacts/
1010
.venv
11+
12+
# Build directories
13+
et-build/*
14+
runner-et/cmake-out/*
15+
runner-aoti/cmake-out/*
16+
17+
# pte files
18+
*.pte

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ NOTE: The exported model will be large. We suggest you quantize the model, expla
185185

186186
### ExecuTorch
187187
ExecuTorch enables you to optimize your model for execution on a mobile or embedded device, but can also be used on desktop for testing.
188+
Before running ExecuTorch commands, you must first set-up ExecuTorch in torchchat, see [Set-up Executorch](docs/executorch_setup.md).
188189

189190
**Examples**
190191
The following example uses the Stories15M model.

docs/executorch_setup.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Set-up executorch
2+
3+
Before running any commands in torchchat that require ExecuTorch, you must first install ExecuTorch.
4+
5+
To install ExecuTorch, run the following commands *from the torchchat root directory*.
6+
7+
```
8+
export TORCHCHAT_ROOT=${PWD}
9+
export ENABLE_ET_PYBIND=true
10+
./scripts/install_et.sh $ENABLE_ET_PYBIND
11+
```
12+
13+
This will download the ExecuTorch repo to ./et-build/src and install various ExecuTorch libraries to ./et-build/install.

docs/runner_build.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Building runner-aoti and runner-et
2+
Building the runners is straightforward and is covered in the next sections.
3+
4+
## Building and running runner-aoti
5+
To build runner-aoti, run the following commands *from the torchchat root directory*
6+
7+
```
8+
cmake -S ./runner-aoti -B ./runner-aoti/cmake-out -G Ninja -DCMAKE_PREFIX_PATH=`python -c 'import torch;print(torch.utils.cmake_prefix_path)'`
9+
cmake --build ./runner-aoti/cmake-out
10+
```
11+
12+
After running these, the runner-aoti binary is located at ./runner-aoti/cmake-out/run.
13+
14+
Let us try using it with an example.
15+
We first download stories15M and export it to AOTI.
16+
17+
```
18+
python torchchat.py download stories15M
19+
python torchchat.py export --output-dso-path ./model.dso
20+
```
21+
22+
We also need a tokenizer.bin file for the stories15M model:
23+
24+
```
25+
wget ./tokenizer.bin https://github.com/karpathy/llama2.c/raw/master/tokenizer.bin
26+
```
27+
28+
We can now execute the runner with:
29+
30+
```
31+
./runner-aoti/cmake-out/run ./model.dso -z ./tokenizer.bin -i "Once upon a time"
32+
```
33+
34+
## Building and running runner-et
35+
Before building runner-et, you must first set-up ExecuTorch by following [Set-up Executorch](executorch_setup.md).
36+
37+
38+
To build runner-et, run the following commands *from the torchchat root directory*
39+
40+
```
41+
export TORCHCHAT_ROOT=${PWD}
42+
cmake -S ./runner-et -B ./runner-et/cmake-out -G Ninja
43+
cmake --build ./runner-et/cmake-out
44+
```
45+
46+
After running these, the runner-et binary is located at ./runner-et/cmake-out/runner-et.
47+
48+
Let us try using it with an example.
49+
We first download stories15M and export it to ExecuTorch.
50+
51+
```
52+
python torchchat.py download stories15M
53+
python torchchat.py export stories15M --output-pte-path ./model.pte
54+
```
55+
56+
We also need a tokenizer.bin file for the stories15M model:
57+
58+
```
59+
wget ./tokenizer.bin https://github.com/karpathy/llama2.c/raw/master/tokenizer.bin
60+
```
61+
62+
We can now execute the runner with:
63+
64+
```
65+
./runner-et/cmake-out/runner_et ./model.pte -z ./tokenizer.bin -i "Once upon a time"
66+
```

export_et.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def export_model(model, device, output_path, args=None) -> str: # noqa: C901
8383
else:
8484
raise ValueError(f"Unsupported dtype for ET export: {target_precision}")
8585

86-
replace_attention_with_custom_sdpa_attention(model)
86+
# TODO: we can bring with pack when dynamo error P1220158146 is resolved
87+
# replace_attention_with_custom_sdpa_attention(model)
8788
with torch.nn.attention.sdpa_kernel(
8889
[torch.nn.attention.SDPBackend.MATH]
8990
), torch.no_grad():

runner-et/CMakeLists.txt

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ target_link_libraries(
4141
runner_et PRIVATE
4242
executorch
4343
extension_module
44-
${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/extension/data_loader/libextension_data_loader.a # This one does not get installed by ExecuTorch
45-
${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops.a # This one does not get installed by ExecuTorch
44+
${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/lib/libextension_data_loader.a # This one gets installed in build directory by ExecuTorch
4645
optimized_kernels
4746
portable_kernels
4847
cpublas
@@ -60,20 +59,4 @@ target_link_options_shared_lib(XNNPACK)
6059
target_link_options_shared_lib(pthreadpool)
6160
target_link_options_shared_lib(cpuinfo)
6261
target_link_options_shared_lib(executorch)
63-
target_link_libraries(runner_et PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops_lib.a>")
64-
65-
# Adding target_link_options_shared_lib as commented out below leads to this:
66-
#
67-
# CMake Error at Utils.cmake:22 (target_link_options):
68-
# Cannot specify link options for target
69-
# "/Users/scroy/etorch/torchchat/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops_lib.a"
70-
# which is not built by this project.
71-
# Call Stack (most recent call first):
72-
# Utils.cmake:30 (macos_kernel_link_options)
73-
# CMakeLists.txt:41 (target_link_options_shared_lib)
74-
#
75-
#target_link_options_shared_lib("${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops_lib.a") # This one does not get installed by ExecuTorch
76-
77-
# This works on mac, but appears to run into issues on linux
78-
# It is needed to solve:
79-
# E 00:00:00.055965 executorch:method.cpp:536] Missing operator: [8] llama::sdpa_with_kv_cache.out
62+
target_link_libraries(runner_et PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops.a>")

0 commit comments

Comments
 (0)