xref: /aosp_15_r20/external/executorch/.ci/scripts/utils.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1#!/bin/bash
2# Copyright (c) Meta Platforms, Inc. and affiliates.
3# All rights reserved.
4# Copyright 2024 Arm Limited and/or its affiliates.
5#
6# This source code is licensed under the BSD-style license found in the
7# LICENSE file in the root directory of this source tree.
8
9reset_buck() {
10  # On MacOS, buck2 daemon can get into a weird non-responsive state
11  buck2 kill && buck2 clean
12  rm -rf ~/.buck/buckd
13}
14
15retry () {
16    "$@" || (sleep 30 && reset_buck && "$@") || (sleep 60 && reset_buck && "$@")
17}
18
19install_executorch() {
20  which pip
21  # Install executorch, this assumes that Executorch is checked out in the
22  # current directory.
23  if [[ "${1:-}" == "use-pt-pinned-commit" ]]; then
24    ./install_requirements.sh --pybind xnnpack --use-pt-pinned-commit
25  else
26    ./install_requirements.sh --pybind xnnpack
27  fi
28  # Just print out the list of packages for debugging
29  pip list
30}
31
32install_pip_dependencies() {
33  pushd .ci/docker || return
34  # Install all Python dependencies, including PyTorch
35  pip install --progress-bar off -r requirements-ci.txt
36  popd || return
37}
38
39install_flatc_from_source() {
40  # NB: This function could be used to install flatbuffer from source
41  pushd third-party/flatbuffers || return
42
43  cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
44  if [ "$(uname)" == "Darwin" ]; then
45    CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))
46  else
47    CMAKE_JOBS=$(( $(nproc) - 1 ))
48  fi
49  cmake --build . -j "${CMAKE_JOBS}"
50
51  # Copy the flatc binary to conda path
52  EXEC_PATH=$(dirname "$(which python)")
53  cp flatc "${EXEC_PATH}"
54
55  popd || return
56}
57
58install_arm() {
59  # NB: This function could be used to install Arm dependencies
60  # Setup arm example environment (including TOSA tools)
61  git config --global user.email "[email protected]"
62  git config --global user.name "Github Executorch"
63  bash examples/arm/setup.sh --i-agree-to-the-contained-eula
64
65  # Test tosa_reference flow
66  source examples/arm/ethos-u-scratch/setup_path.sh
67}
68
69build_executorch_runner_buck2() {
70  # Build executorch runtime with retry as this step is flaky on macos CI
71  retry buck2 build //examples/portable/executor_runner:executor_runner
72}
73
74build_executorch_runner_cmake() {
75  CMAKE_OUTPUT_DIR=cmake-out
76  # Build executorch runtime using cmake
77  rm -rf "${CMAKE_OUTPUT_DIR}" && mkdir "${CMAKE_OUTPUT_DIR}"
78
79  pushd "${CMAKE_OUTPUT_DIR}" || return
80  # This command uses buck2 to gather source files and buck2 could crash flakily
81  # on MacOS
82  retry cmake -DPYTHON_EXECUTABLE="${PYTHON_EXECUTABLE}" -DCMAKE_BUILD_TYPE=Release ..
83  popd || return
84
85  if [ "$(uname)" == "Darwin" ]; then
86    CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))
87  else
88    CMAKE_JOBS=$(( $(nproc) - 1 ))
89  fi
90  cmake --build "${CMAKE_OUTPUT_DIR}" -j "${CMAKE_JOBS}"
91}
92
93build_executorch_runner() {
94  if [[ $1 == "buck2" ]]; then
95    build_executorch_runner_buck2
96  elif [[ $1 == "cmake" ]]; then
97    build_executorch_runner_cmake
98  else
99    echo "Invalid build tool $1. Only buck2 and cmake are supported atm"
100    exit 1
101  fi
102}
103
104cmake_install_executorch_lib() {
105  echo "Installing libexecutorch.a and libportable_kernels.a"
106  rm -rf cmake-out
107  retry cmake -DBUCK2="$BUCK" \
108          -DCMAKE_INSTALL_PREFIX=cmake-out \
109          -DCMAKE_BUILD_TYPE=Release \
110          -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
111          -Bcmake-out .
112  cmake --build cmake-out -j9 --target install --config Release
113}
114
115download_stories_model_artifacts() {
116  # Download stories110M.pt and tokenizer from Github
117  curl -Ls "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories110M.pt" --output stories110M.pt
118  curl -Ls "https://raw.githubusercontent.com/karpathy/llama2.c/master/tokenizer.model" --output tokenizer.model
119  # Create params.json file
120  touch params.json
121  echo '{"dim": 768, "multiple_of": 32, "n_heads": 12, "n_layers": 12, "norm_eps": 1e-05, "vocab_size": 32000}' > params.json
122}
123
124do_not_use_nightly_on_ci() {
125  # An assert to make sure that we are not using PyTorch nightly on CI to prevent
126  # regression as documented in https://github.com/pytorch/executorch/pull/6564
127  TORCH_VERSION=$(pip list | grep -w 'torch ' | awk -F ' ' {'print $2'} | tr -d '\n')
128
129  # The version of PyTorch building from source looks like 2.6.0a0+gitc8a648d that
130  # includes the commit while nightly (2.6.0.dev20241019+cpu) or release (2.6.0)
131  # won't have that. Note that we couldn't check for the exact commit from the pin
132  # ci_commit_pins/pytorch.txt here because the value will be different when running
133  # this on PyTorch CI
134  if [[ "${TORCH_VERSION}" != *"+git"* ]]; then
135    echo "Unexpected torch version. Expected binary built from source, got ${TORCH_VERSION}"
136    exit 1
137  fi
138}
139