Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit dc832fb

Browse files
Update et pin (#1166)
* add new torchao experimental kernels to torchchat * add doc * update torchao library name * typo * add multithreading to ET runner * update lib names * update lib name * update torchao ExecuTorch op lib to static * remove old comment * add dylib for poor perf repro testing * change /Users/scroy to /Users/scroy * remove single-threaded reference in docs * add github workflow for testing * fix torchchat-root in install script * bug fixes * ci fixes * fix * ci fixes * fix ci * ci fixes * ci update * ci fixes * ci fixes * ci fixes * ci fixes * ci update * update et pin * update python version * update python version * init * typo * update pytorch pin * tensor_ptr arg order change * fixes * update * Add import for quantized decomposed ops Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: * remove whole archive from custom_op * add imported lib * add imported lib --------- Co-authored-by: Mengwei Liu <[email protected]>
1 parent b037b71 commit dc832fb

File tree

11 files changed

+58
-84
lines changed

11 files changed

+58
-84
lines changed

.github/workflows/pull.yml

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ jobs:
434434
- name: Setup Python
435435
uses: actions/setup-python@v2
436436
with:
437-
python-version: 3.10.11
437+
python-version: '3.10.11'
438438
- name: Setup Xcode
439439
if: runner.os == 'macOS'
440440
uses: maxim-lobanov/setup-xcode@v1
@@ -577,7 +577,7 @@ jobs:
577577
- name: Setup Python
578578
uses: actions/setup-python@v2
579579
with:
580-
python-version: 3.10.11
580+
python-version: '3.10.11'
581581
- name: Print machine info
582582
run: |
583583
uname -a
@@ -625,6 +625,7 @@ jobs:
625625
with:
626626
runner: macos-m1-stable # neeps MPS, was macos-m1-stable
627627
script: |
628+
export PYTHON_VERSION="3.10"
628629
set -x
629630
# NS/MC: Remove previous installation of torch and torchao first
630631
# as this script does not install anything into conda env but rather as system dep
@@ -737,6 +738,7 @@ jobs:
737738
with:
738739
runner: macos-m1-stable # needs MPS, was macos-m1-stable
739740
script: |
741+
export PYTHON_VERSION="3.10"
740742
set -x
741743
# NS/MC: Remove previous installation of torch and torchao first
742744
# as this script does not install anything into conda env but rather as system dep
@@ -914,31 +916,19 @@ jobs:
914916
continue-on-error: true
915917
run: |
916918
echo "Installing ExecuTorch"
917-
bash torchchat/utils/scripts/build_native.sh et
918-
- name: Install ET pip
919+
bash torchchat/utils/scripts/install_et.sh
920+
- name: Install ExecuTorch python
919921
run: |
920-
echo "ET build directory"
921-
ls et-build | cat
922-
922+
echo "Install ExecuTorch python"
923923
pushd et-build/src/executorch
924-
if [ $(git rev-parse HEAD) != ${{env.et-git-hash}} ]; then
925-
echo "Mismatched hash. Make sure branch install_et.sh matches branch from Github cache."
926-
echo "On commit $(git rev-parse HEAD)"
927-
echo "Expected commit ${{env.et-git-hash}}"
928-
exit 1
929-
fi
930-
pip install .
924+
chmod +x ./install_requirements.sh
925+
chmod +x ./install_requirements.py
926+
./install_requirements.sh
931927
popd
932928
- name: Install runner
933929
run: |
934-
# Pull submodules (re2, abseil) for Tiktoken
935-
git submodule sync
936-
git submodule update --init
937-
938-
export TORCHCHAT_ROOT=${PWD}
939-
cmake -S . -B ./cmake-out -G Ninja
940-
cmake --build ./cmake-out --target et_run
941-
930+
echo "Installing runner"
931+
bash torchchat/utils/scripts/build_native.sh et
942932
- name: Run inference
943933
run: |
944934
python torchchat.py download stories15M

install/.pins/et-pin.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
91298923a0076c1b41059efb6dad2876426e4b03
1+
af098c31b6f8d5f38e40a5cf35784b0969d97df8

install/install_requirements.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ fi
4747
# NOTE: If a newly-fetched version of the executorch repo changes the value of
4848
# PYTORCH_NIGHTLY_VERSION, you should re-run this script to install the necessary
4949
# package versions.
50-
PYTORCH_NIGHTLY_VERSION=dev20240814
50+
PYTORCH_NIGHTLY_VERSION=dev20240901
5151

5252
# Nightly version for torchvision
53-
VISION_NIGHTLY_VERSION=dev20240814
53+
VISION_NIGHTLY_VERSION=dev20240901
5454

5555
# Nightly version for torchtune
5656
TUNE_NIGHTLY_VERSION=dev20240916

runner/build_android.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export CMAKE_OUT_DIR="cmake-out-android"
2424
export EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT="OFF"
2525
export EXECUTORCH_BUILD_KERNELS_CUSTOM="ON"
2626
export CMAKE_OUT_DIR="cmake-out-android"
27-
# export DCMAKE_INSTALL_PREFIX=cmake-out-android
28-
#
2927

3028
build_runner_et() {
3129
rm -rf cmake-out-android
@@ -43,5 +41,5 @@ install_executorch_python_libs $ENABLE_ET_PYBIND
4341
export CMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake
4442
export ANDROID_ABI=arm64-v8a
4543
export ANDROID_PLATFORM=android-23
46-
install_executorch
44+
install_executorch_cpp_libs
4745
build_runner_et

runner/et.cmake

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ if(executorch_FOUND)
6262

6363
set(EXECUTORCH_SRC_ROOT ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src/executorch)
6464
set(XNNPACK_ROOT ${EXECUTORCH_SRC_ROOT}/backends/xnnpack)
65-
list(APPEND _srcs ${XNNPACK_ROOT}/threadpool/cpuinfo_utils.cpp)
65+
list(APPEND _srcs ${EXECUTORCH_SRC_ROOT}/extension/threadpool/cpuinfo_utils.cpp)
6666
list(APPEND _common_include_directories
6767
${XNNPACK_ROOT}/third-party/cpuinfo/include)
6868

6969
list(APPEND _common_include_directories
7070
${XNNPACK_ROOT}/third-party/pthreadpool/include)
7171
endif()
72+
add_library(custom_ops STATIC IMPORTED)
73+
set_property(TARGET custom_ops PROPERTY IMPORTED_LOCATION ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install/lib/libcustom_ops.a)
7274

7375
target_include_directories(executorch INTERFACE ${_common_include_directories}) # Ideally ExecuTorch installation process would do this
7476
add_executable(et_run ${_srcs})
@@ -80,7 +82,9 @@ if(executorch_FOUND)
8082
et_run PRIVATE
8183
executorch
8284
extension_module
85+
extension_tensor
8386
extension_data_loader
87+
extension_threadpool
8488
optimized_kernels
8589
quantized_kernels
8690
portable_kernels
@@ -93,39 +97,25 @@ if(executorch_FOUND)
9397
XNNPACK
9498
pthreadpool
9599
cpuinfo
100+
custom_ops
96101
)
97102
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
98103
target_link_options_shared_lib(quantized_ops_lib)
99104
target_link_options_shared_lib(xnnpack_backend)
105+
target_link_options_shared_lib(custom_ops)
106+
100107
# Not clear why linking executorch as whole-archive outside android/apple is leading
101108
# to double registration. Most likely because of linkage issues.
102109
# Will figure this out later. Until then use this.
103110
if(ANDROID OR APPLE)
104111
target_link_options_shared_lib(executorch)
105112
endif()
106113

107-
target_link_libraries(et_run PRIVATE
108-
"$<LINK_LIBRARY:WHOLE_ARCHIVE,${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install/lib/libcustom_ops.a>")
109114
# This one is needed for cpuinfo where it uses android specific log lib
110115
if(ANDROID)
111116
target_link_libraries(et_run PRIVATE log)
112117
endif()
113118

114-
# Adding target_link_options_shared_lib as commented out below leads to this:
115-
#
116-
# CMake Error at Utils.cmake:22 (target_link_options):
117-
# Cannot specify link options for target
118-
# "/Users/scroy/etorch/torchchat/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops_lib.a"
119-
# which is not built by this project.
120-
# Call Stack (most recent call first):
121-
# Utils.cmake:30 (macos_kernel_link_options)
122-
# CMakeLists.txt:41 (target_link_options_shared_lib)
123-
#
124-
#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
125-
126-
# This works on mac, but appears to run into issues on linux
127-
# It is needed to solve:
128-
# E 00:00:00.055965 executorch:method.cpp:536] Missing operator: [8] llama::sdpa_with_kv_cache.out
129119
else()
130120
MESSAGE(WARNING "ExecuTorch package not found")
131121
endif()

runner/run.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ torch::Device aoti_device(torch::kCPU);
3939

4040
#else // __ET_MODEL__
4141
#include <executorch/extension/module/module.h>
42-
#include <executorch/extension/runner_util/managed_tensor.h>
42+
#include <executorch/extension/tensor/tensor_ptr.h>
4343
#include <executorch/runtime/core/evalue.h>
4444
#include <executorch/runtime/core/exec_aten/exec_aten.h>
4545
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
4646

4747
#if defined(ET_USE_ADAPTIVE_THREADS)
48-
#include <executorch/backends/xnnpack/threadpool/cpuinfo_utils.h>
49-
#include <executorch/backends/xnnpack/threadpool/threadpool.h>
48+
#include <executorch/extension/threadpool/cpuinfo_utils.h>
49+
#include <executorch/extension/threadpool/threadpool.h>
5050
#endif
5151

5252
using exec_aten::ScalarType;
5353
using torch::executor::EValue;
54-
using torch::executor::ManagedTensor;
54+
using executorch::extension::TensorPtr;
55+
using executorch::extension::make_tensor_ptr;
5556
using torch::executor::Module;
5657
using torch::executor::Result;
5758
#endif
@@ -212,11 +213,11 @@ float* forward(Transformer* transformer, int token, int pos) {
212213
.to(torch::kCPU);
213214
auto logits = result[0].data_ptr();
214215
#else // __ET_MODEL__
215-
ManagedTensor pos_managed(pos_buffer, {1}, ScalarType::Long);
216-
ManagedTensor tokens_managed(token_buffer, {1, 1}, ScalarType::Long);
216+
TensorPtr pos_managed = make_tensor_ptr({1}, pos_buffer, ScalarType::Long);
217+
TensorPtr tokens_managed = make_tensor_ptr({1, 1}, token_buffer, ScalarType::Long);
217218
std::vector<EValue> inputs;
218-
auto tmp1 = EValue(tokens_managed.get_aliasing_tensor());
219-
auto tmp2 = EValue(pos_managed.get_aliasing_tensor());
219+
auto tmp1 = EValue(tokens_managed);
220+
auto tmp2 = EValue(pos_managed);
220221

221222
inputs.push_back(tmp1);
222223
inputs.push_back(tmp2);

torchchat/export.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def forward(self, x, freqs_cis, mask, input_pos=None):
194194
return self.wo(output)
195195

196196
def replace_attention_with_custom_sdpa_attention(module: nn.Module):
197-
from executorch.examples.models.llama2.custom_ops import ( # noqa
197+
from executorch.extension.llm.custom_ops import ( # noqa
198198
sdpa_with_kv_cache,
199199
)
200200

@@ -304,7 +304,6 @@ def export_for_et(model, device, output_path) -> str:
304304
edge_manager = edge_manager.to_backend(XnnpackDynamicallyQuantizedPartitioner())
305305
export_program = edge_manager.to_executorch(
306306
ExecutorchBackendConfig(
307-
extract_constant_segment=True,
308307
extract_delegate_segments=True,
309308
passes=[
310309
QuantFusionPass(),

torchchat/model.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,10 @@ def apply_rotary_emb(x: Tensor, freqs_cis: Tensor) -> Tensor:
932932
from executorch.extension.pybindings import portable_lib as exec_lib
933933

934934
# ET changed the way it's loading the custom ops so it's not included in portable_lib but has to be loaded separately.
935-
from executorch.examples.models.llama2.custom_ops import sdpa_with_kv_cache # no-qa
935+
# For quantized_decomposed ops
936+
from executorch.kernels import quantized # no-qa
937+
# For llama::sdpa_with_kv_cache.out, preprocess ops
938+
from executorch.extension.llm.custom_ops import sdpa_with_kv_cache # no-qa
936939

937940
class PTEModel(nn.Module):
938941
def __init__(self, config, path) -> None:

torchchat/utils/scripts/build_native.sh

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if [ $# -eq 0 ]; then
2525
show_help
2626
exit 1
2727
fi
28+
2829
while (( "$#" )); do
2930
case "$1" in
3031
-h|--help)
@@ -49,15 +50,7 @@ while (( "$#" )); do
4950
esac
5051
done
5152

52-
if [ -z "${TORCHCHAT_ROOT}" ]; then
53-
# Get the absolute path of the current script
54-
SCRIPT_PATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
55-
# Get the absolute path of the parent directory
56-
TORCHCHAT_ROOT="$(dirname "$SCRIPT_PATH")"
57-
source "$TORCHCHAT_ROOT/scripts/install_utils.sh"
58-
else
59-
source "$TORCHCHAT_ROOT/torchchat/utils/scripts/install_utils.sh"
60-
fi
53+
source "$(dirname "${BASH_SOURCE[0]}")/install_utils.sh"
6154

6255
if [ -z "${ET_BUILD_DIR}" ]; then
6356
ET_BUILD_DIR="et-build"
@@ -68,16 +61,17 @@ pushd ${TORCHCHAT_ROOT}
6861
git submodule update --init
6962
git submodule sync
7063
if [[ "$TARGET" == "et" ]]; then
71-
find_cmake_prefix_path
72-
install_pip_dependencies
73-
clone_executorch
74-
install_executorch_libs false
64+
if [ ! -d "${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install" ]; then
65+
echo "Directory ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install does not exist."
66+
echo "Make sure you run install_executorch_libs"
67+
exit 1
68+
fi
7569
fi
7670
popd
7771

7872
# CMake commands
7973
if [[ "$TARGET" == "et" ]]; then
80-
cmake -S . -B ./cmake-out -DCMAKE_PREFIX_PATH=`python3 -c 'import torch;print(torch.utils.cmake_prefix_path)'` -DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=1" -G Ninja
74+
cmake -S . -B ./cmake-out -DCMAKE_PREFIX_PATH=`python3 -c 'import torch;print(torch.utils.cmake_prefix_path)'` -DET_USE_ADAPTIVE_THREADS=ON -DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=1" -G Ninja
8175
else
8276
cmake -S . -B ./cmake-out -DCMAKE_PREFIX_PATH=`python3 -c 'import torch;print(torch.utils.cmake_prefix_path)'` -DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=0" -G Ninja
8377
fi

torchchat/utils/scripts/install_et.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ENABLE_ET_PYBIND="${1:-true}"
1717

1818
pushd ${TORCHCHAT_ROOT}
1919
find_cmake_prefix_path
20-
install_pip_dependencies
2120
clone_executorch
2221
install_executorch_libs $ENABLE_ET_PYBIND
22+
install_executorch_python_libs $ENABLE_ET_PYBIND
2323
popd

0 commit comments

Comments
 (0)