xref: /aosp_15_r20/external/executorch/backends/apple/coreml/scripts/install_requirements.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1#!/usr/bin/env bash
2#
3# Copyright © 2023 Apple Inc. 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
8SCRIPT_DIR_PATH="$(
9    cd -- "$(dirname "$0")" >/dev/null 2>&1
10    pwd -P
11)"
12
13red=`tput setaf 1`
14green=`tput setaf 2`
15
16EXECUTORCH_ROOT_PATH=$(realpath "$SCRIPT_DIR_PATH/../../../../")
17COREML_DIR_PATH="$EXECUTORCH_ROOT_PATH/backends/apple/coreml"
18COREMLTOOLS_DIR_PATH="$COREML_DIR_PATH/third-party/coremltools"
19PROTOBUF_FILES_DIR_PATH="$COREMLTOOLS_DIR_PATH/build/mlmodel/format/"
20
21cd "$EXECUTORCH_ROOT_PATH"
22
23rm -rf "$COREML_DIR_PATH/third-party"
24mkdir "$COREML_DIR_PATH/third-party"
25
26echo "${green}ExecuTorch: Cloning coremltools."
27git clone --depth 1 --branch 8.0 "https://github.com/apple/coremltools.git" $COREMLTOOLS_DIR_PATH
28cd $COREMLTOOLS_DIR_PATH
29
30STATUS=$?
31if [ $STATUS -ne 0 ]; then
32    echo "${red}ExecuTorch: Failed to clone coremltools."
33    exit 1
34fi
35
36echo "${green}ExecuTorch: Installing coremltools dependencies."
37pip install -r "$COREMLTOOLS_DIR_PATH/reqs/build.pip"
38STATUS=$?
39if [ $STATUS -ne 0 ]; then
40    echo "${red}ExecuTorch: Failed to install coremltools dependencies."
41    exit 1
42fi
43
44mkdir "$COREMLTOOLS_DIR_PATH/build"
45cmake -S "$COREMLTOOLS_DIR_PATH" -B "$COREMLTOOLS_DIR_PATH/build"
46cmake --build "$COREMLTOOLS_DIR_PATH/build" --parallel
47
48echo "${green}ExecuTorch: Installing coremltools."
49pip install "$COREMLTOOLS_DIR_PATH"
50# CoreMLTools have started supporting numpy 2.0,
51# but ExecuTorch example model test env is still using older transformers,
52# so for now we will need to downgrade numpy to 1.x
53# TODO: Remove this numpy downgrade once later transformers starts to be used
54pip install numpy==1.26.4
55STATUS=$?
56if [ $STATUS -ne 0 ]; then
57    echo "${red}ExecuTorch: Failed to install coremltools."
58    exit 1
59fi
60
61echo "${green}ExecuTorch: Cloning nlohmann."
62git clone https://github.com/nlohmann/json.git "$COREML_DIR_PATH/third-party/nlohmann_json"
63STATUS=$?
64if [ $STATUS -ne 0 ]; then
65    echo "${red}ExecuTorch: Failed to clone nlohmann."
66    exit 1
67fi
68
69sh "$COREML_DIR_PATH/scripts/install_inmemoryfs.sh"
70
71echo "${green}ExecuTorch: Copying protobuf files."
72mkdir -p "$COREML_DIR_PATH/runtime/sdk/format/"
73cp -rf "$PROTOBUF_FILES_DIR_PATH" "$COREML_DIR_PATH/runtime/sdk/format/"
74