1#!/usr/bin/env bash 2# Copyright (c) Meta Platforms, Inc. and affiliates. 3# All rights reserved. 4# 5# Copyright 2023-2024 Arm Limited and/or its affiliates. 6# 7# This source code is licensed under the BSD-style license found in the 8# LICENSE file in the root directory of this source tree. 9 10set -eu 11 12if [[ "${1:-'.'}" == "-h" || "${#}" -gt 2 ]]; then 13 echo "Usage: $(basename $0) <--i-agree-to-the-contained-eula> [path-to-a-scratch-dir]" 14 echo "Supplied args: $*" 15 exit 1 16fi 17 18 19######## 20### Helper functions 21######## 22ARCH="$(uname -m)" 23OS="$(uname -s)" 24 25function verify_md5() { 26 [[ $# -ne 2 ]] \ 27 && { echo "[${FUNCNAME[0]}] Invalid number of args, expecting 2, but got $#"; exit 1; } 28 local ref_checksum="${1}" 29 local file="${2}" 30 31 if [[ "${OS}" == "Darwin" ]]; then 32 local file_checksum="$(md5 -q $file)" 33 else 34 local file_checksum="$(md5sum $file | awk '{print $1}')" 35 fi 36 if [[ ${ref_checksum} != ${file_checksum} ]]; then 37 echo "Mismatched MD5 checksum for file: ${file}. Expecting ${ref_checksum} but got ${file_checksum}. Exiting." 38 exit 1 39 fi 40} 41 42######## 43### Hardcoded constants 44######## 45script_dir=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd) 46 47if [[ "${ARCH}" == "x86_64" ]]; then 48 # FVPs 49 corstone300_url="https://developer.arm.com/-/media/Arm%20Developer%20Community/Downloads/OSS/FVP/Corstone-300/FVP_Corstone_SSE-300_11.22_20_Linux64.tgz?rev=018659bd574f4e7b95fa647e7836ccf4&hash=22A79103C6FA5FFA7AFF3BE0447F3FF9" 50 corstone300_model_dir="Linux64_GCC-9.3" 51 corstone300_md5_checksum="98e93b949d0fbac977292d8668d34523" 52 53 corstone320_url="https://developer.arm.com/-/media/Arm%20Developer%20Community/Downloads/OSS/FVP/Corstone-320/FVP_Corstone_SSE-320_11.27_25_Linux64.tgz?rev=a507bffc219a4d5792f1192ab7002d89&hash=D9A824AA8227D2E679C9B9787FF4E8B6FBE3D7C6" 54 corstone320_model_dir="Linux64_GCC-9.3" 55 corstone320_md5_checksum="3deb3c68f9b2d145833f15374203514d" 56 57 # toochain 58 toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/12.3.rel1/binrel/arm-gnu-toolchain-12.3.rel1-x86_64-arm-none-eabi.tar.xz" 59 toolchain_dir="arm-gnu-toolchain-12.3.rel1-x86_64-arm-none-eabi" 60 toolchain_md5_checksum="00ebb1b70b1f88906c61206457eacb61" 61elif [[ "${ARCH}" == "aarch64" ]] || [[ "${ARCH}" == "arm64" ]]; then 62 # FVPs 63 corstone300_url="https://developer.arm.com/-/media/Arm%20Developer%20Community/Downloads/OSS/FVP/Corstone-300/FVP_Corstone_SSE-300_11.22_20_Linux64_armv8l.tgz?rev=9cc6e9a32bb947ca9b21fa162144cb01&hash=7657A4CF27D42E892E3F08D452AAB073" 64 corstone300_model_dir="Linux64_armv8l_GCC-9.3" 65 corstone300_md5_checksum="cbbabbe39b07939cff7a3738e1492ef1" 66 67 corstone320_url="https://developer.arm.com/-/media/Arm%20Developer%20Community/Downloads/OSS/FVP/Corstone-320/FVP_Corstone_SSE-320_11.27_25_Linux64_armv8l.tgz?rev=b6ebe0923cb84f739e017385fd3c333c&hash=8965C4B98E2FF7F792A099B08831FE3CB6120493" 68 corstone320_model_dir="Linux64_armv8l_GCC-9.3" 69 corstone320_md5_checksum="3889f1d80a6d9861ea4aa6f1c88dd0ae" 70 71 # toochain 72 if [[ "${OS}" == "Darwin" ]]; then 73 toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/12.3.rel1/binrel/arm-gnu-toolchain-12.3.rel1-darwin-arm64-arm-none-eabi.tar.xz" 74 toolchain_dir="arm-gnu-toolchain-12.3.rel1-darwin-arm64-arm-none-eabi" 75 toolchain_md5_checksum="53d034e9423e7f470acc5ed2a066758e" 76 elif [[ "${OS}" == "Linux" ]]; then 77 toolchain_url="https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu/12.3.rel1/binrel/arm-gnu-toolchain-12.3.rel1-aarch64-arm-none-eabi.tar.xz" 78 toolchain_dir="arm-gnu-toolchain-12.3.rel1-aarch64-arm-none-eabi" 79 toolchain_md5_checksum="02c9b0d3bb1110575877d8eee1f223f2" 80 fi 81else 82 echo "[main] Error: only x86-64 & aarch64/arm64 architecture is supported for now!"; exit 1; 83fi 84 85# ethos-u 86ethos_u_repo_url="https://review.mlplatform.org/ml/ethos-u/ethos-u" 87ethos_u_base_rev="24.08" 88 89# tosa reference model 90tosa_reference_model_url="https://review.mlplatform.org/tosa/reference_model" 91tosa_reference_model_rev="f9ea4ab7da19318fe36b1c34d68a3e40fd6e56c5" 92 93######## 94### Mandatory user args 95######## 96eula_acceptance="${1:-'.'}" 97if [[ "${eula_acceptance}" != "--i-agree-to-the-contained-eula" ]]; then 98 if [[ ${ARM_FVP_INSTALL_I_AGREE_TO_THE_CONTAINED_EULA} != "True" ]]; then 99 echo "Must pass first positional argument '--i-agree-to-the-contained-eula' to agree to EULA associated with downloading the FVP. Exiting!" 100 exit 1 101 else 102 echo "Arm EULA for FVP agreed to with ARM_FVP_INSTALL_I_AGREE_TO_THE_CONTAINED_EULA=True environment variable" 103 fi 104else 105 shift; # drop this arg 106fi 107 108######## 109### Optional user args 110######## 111root_dir=${1:-"${script_dir}/ethos-u-scratch"} 112mkdir -p ${root_dir} 113root_dir=$(realpath ${root_dir}) 114 115######## 116### Functions 117######## 118 119function setup_fvp() { 120 if [[ "${OS}" != "Linux" ]]; then 121 echo "[${FUNCNAME[0]}] Warning: FVP only supported with Linux OS, skipping FVP setup..." 122 echo "[${FUNCNAME[0]}] Warning: For MacOS, using https://github.com/Arm-Examples/FVPs-on-Mac is recommended." 123 echo "[${FUNCNAME[0]}] Warning: Follow the instructions and make sure the path is set correctly." 124 return 1 125 fi 126 127 # Download and install the Corstone 300 FVP simulator platform 128 fvps=("corstone300" "corstone320") 129 130 for fvp in "${fvps[@]}"; do 131 cd "${root_dir}" 132 if [[ ! -e "FVP_${fvp}.tgz" ]]; then 133 echo "[${FUNCNAME[0]}] Downloading FVP ${fvp}..." 134 url_variable=${fvp}_url 135 fvp_url=${!url_variable} 136 curl --output "FVP_${fvp}.tgz" "${fvp_url}" 137 md5_variable=${fvp}_md5_checksum 138 fvp_md5_checksum=${!md5_variable} 139 verify_md5 ${fvp_md5_checksum} FVP_${fvp}.tgz 140 fi 141 142 echo "[${FUNCNAME[0]}] Installing FVP ${fvp}..." 143 rm -rf FVP-${fvp} 144 mkdir -p FVP-${fvp} 145 cd FVP-${fvp} 146 tar xf ../FVP_${fvp}.tgz 147 148 # Install the FVP 149 case ${fvp} in 150 corstone300) 151 ./FVP_Corstone_SSE-300.sh --i-agree-to-the-contained-eula --force --destination ./ --quiet --no-interactive 152 ;; 153 corstone320) 154 ./FVP_Corstone_SSE-320.sh --i-agree-to-the-contained-eula --force --destination ./ --quiet --no-interactive 155 ;; 156 *) 157 echo "[${FUNCNAME[0]}] Error: Unknown FVP model ${fvp}. Exiting." 158 exit 1 159 ;; 160 esac 161 162 model_dir_variable=${fvp}_model_dir 163 fvp_model_dir=${!model_dir_variable} 164 fvp_bin_path="$(cd models/${fvp_model_dir} && pwd)" 165 export PATH=${PATH}:${fvp_bin_path} 166 167 echo "export PATH=\${PATH}:${fvp_bin_path}" >> ${setup_path_script} 168 done 169 170 # Fixup for Corstone-320 python dependency 171 echo "export LD_LIBRARY_PATH=${root_dir}/FVP-corstone320/python/lib/" >> ${setup_path_script} 172} 173 174function setup_toolchain() { 175 # Download and install the arm-none-eabi toolchain 176 cd "${root_dir}" 177 if [[ ! -e gcc.tar.xz ]]; then 178 echo "[${FUNCNAME[0]}] Downloading toolchain ..." 179 curl --output gcc.tar.xz "${toolchain_url}" 180 verify_md5 ${toolchain_md5_checksum} gcc.tar.xz 181 fi 182 183 echo "[${FUNCNAME[0]}] Installing toolchain ..." 184 rm -rf "${toolchain_dir}" 185 tar xf gcc.tar.xz 186 toolchain_bin_path="$(cd ${toolchain_dir}/bin && pwd)" 187 export PATH=${PATH}:${toolchain_bin_path} 188 hash arm-none-eabi-gcc 189 echo "export PATH=\${PATH}:${toolchain_bin_path}" >> ${setup_path_script} 190} 191 192function setup_ethos_u() { 193 # This is the main dir which will pull more repos to do baremetal software dev for cs300 194 echo "[${FUNCNAME[0]}] Setting up the repo" 195 cd "${root_dir}" 196 [[ ! -d ethos-u ]] && \ 197 git clone ${ethos_u_repo_url} 198 cd ethos-u 199 git reset --hard ${ethos_u_base_rev} 200 python3 ./fetch_externals.py -c ${ethos_u_base_rev}.json fetch 201 pip install pyelftools 202 echo "[${FUNCNAME[0]}] Done @ $(git describe --all --long 3> /dev/null) in ${root_dir}/ethos-u dir." 203} 204 205function patch_repo() { 206 # This is a temporary hack until it finds a better home in one for the ARM Ml repos 207 name="$(basename $repo_dir)" 208 echo -e "[${FUNCNAME[0]}] Preparing ${name}..." 209 cd $repo_dir 210 git fetch 211 git reset --hard ${base_rev} 212 213 patch_dir=${script_dir}/ethos-u-setup/${name}/patches/ 214 [[ -e ${patch_dir} && $(ls -A ${patch_dir}) ]] && \ 215 git am -3 ${patch_dir}/*.patch 216 217 echo -e "[${FUNCNAME[0]}] Patched ${name} @ $(git describe --all --long 2> /dev/null) in ${repo_dir} dir.\n" 218} 219 220function setup_tosa_reference_model() { 221 # The debug flow on the host includes running on a reference implementation of TOSA 222 # This is useful primarily for debug of quantization accuracy, but also for internal 223 # errors for the early codebase 224 cd "${root_dir}" 225 if [[ ! -e reference_model ]]; then 226 git clone ${tosa_reference_model_url} 227 cd reference_model 228 git checkout ${tosa_reference_model_rev} 229 git submodule update --init --recursive 230 cd .. 231 fi 232 cd reference_model 233 mkdir -p build 234 cd build 235 cmake .. 236 237 # make use of half the cores for building 238 if [[ "${OS}" == "Linux" ]]; then 239 n=$(( $(nproc) / 2 )) 240 elif [[ "${OS}" == "Darwin" ]]; then 241 n=$(( $(sysctl -n hw.logicalcpu) / 2 )) 242 else 243 n=1 244 fi 245 246 if [[ "$n" -lt 1 ]]; then 247 n=1 248 fi 249 250 make -j"${n}" 251 cd reference_model 252 tosa_bin_path=`pwd` 253 echo "export PATH=\${PATH}:${tosa_bin_path}" >> "${setup_path_script}" 254} 255 256function setup_vela() { 257 # 258 # Prepare the Vela compiler for AoT to Ethos-U compilation 259 # 260 cd "${root_dir}" 261 if [[ ! -e ethos-u-vela ]]; then 262 git clone https://review.mlplatform.org/ml/ethos-u/ethos-u-vela 263 repo_dir="${root_dir}/ethos-u-vela" 264 base_rev=57ce18c89ccc6f6309333dccb24ed30dc68b571f 265 patch_repo 266 fi 267 cd "${root_dir}/ethos-u-vela" 268 269 # different command for conda vs venv 270 VNV=$(python3 -c "import sys; print('venv') if (sys.prefix != sys.base_prefix) else print('not_venv')") 271 if [ ${VNV} == "venv" ]; then 272 pip install . 273 else 274 # if not venv, we need the site-path where the vela 275 vela_path=$(python -c "import site; print(site.USER_BASE+'/bin')") 276 echo "export PATH=\${PATH}:${vela_path}" >> ${setup_path_script} 277 pip install . --user 278 fi 279} 280 281######## 282### main 283######## 284# do basic checks 285# Make sure we are on a supported platform 286if [[ "${ARCH}" != "x86_64" ]] && [[ "${ARCH}" != "aarch64" ]] \ 287 && [[ "${ARCH}" != "arm64" ]]; then 288 echo "[main] Error: only x86-64 & aarch64 architecture is supported for now!" 289 exit 1 290fi 291 292cd "${script_dir}" 293 294# Setup the root dir 295cd "${root_dir}" 296echo "[main] Using root dir ${root_dir}" 297 298setup_path_script="${root_dir}/setup_path.sh" 299echo "" > "${setup_path_script}" 300 301# Setup toolchain 302setup_toolchain 303 304# Setup the ethos-u dev environment 305setup_ethos_u 306 307# Patch the ethos-u dev environment to include executorch application 308repo_dir="${root_dir}/ethos-u/core_platform" 309base_rev=b728c774158248ba2cad8e78a515809e1eb9b77f 310patch_repo 311 312# Setup the tosa_reference_model 313setup_tosa_reference_model 314 315# Setup vela and patch in codegen fixes 316setup_vela 317 318# Setup FVP 319setup_fvp 320 321echo "[main] update path by doing 'source ${setup_path_script}'" 322 323echo "[main] success!" 324exit 0 325