Skip to content

Commit 3db7900

Browse files
authored
Arm backend: Refactor setup.sh $PATH creation (pytorch#14083)
Break up the create_setup_path() function and move the path creation code specific for each module into their respective sub script to not clutter up setup.sh. Functions added in respective script are "setup_path_X" for each sub script. Signed-off-by: [email protected]
1 parent 589d3cd commit 3db7900

File tree

5 files changed

+72
-43
lines changed

5 files changed

+72
-43
lines changed

backends/arm/scripts/mlsdk_utils.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,29 @@ function setup_model_converter() {
124124
popd
125125
}
126126

127+
function setup_path_model_converter() {
128+
cd "${root_dir}"
129+
model_converter_bin_path="$(cd ${mlsdk_manifest_dir}/sw/model-converter/build && pwd)"
130+
append_env_in_setup_path PATH ${model_converter_bin_path}
131+
}
132+
133+
function setup_path_vgf_lib() {
134+
cd "${root_dir}"
135+
model_vgf_path="$(cd ${mlsdk_manifest_dir}/sw/vgf-lib/deploy && pwd)"
136+
append_env_in_setup_path PATH ${model_vgf_path}/bin
137+
append_env_in_setup_path LD_LIBRARY_PATH "${model_vgf_path}/lib"
138+
append_env_in_setup_path DYLD_LIBRARY_PATH "${model_vgf_path}/lib"
139+
}
140+
141+
function setup_path_emulation_layer() {
142+
cd "${root_dir}"
143+
model_emulation_layer_path="$(cd ${mlsdk_manifest_dir}/sw/emulation-layer/ && pwd)"
144+
prepend_env_in_setup_path LD_LIBRARY_PATH "${model_emulation_layer_path}/deploy/lib"
145+
prepend_env_in_setup_path DYLD_LIBRARY_PATH "${model_emulation_layer_path}/deploy/lib"
146+
prepend_env_in_setup_path VK_INSTANCE_LAYERS VK_LAYER_ML_Tensor_Emulation
147+
prepend_env_in_setup_path VK_INSTANCE_LAYERS VK_LAYER_ML_Graph_Emulation
148+
prepend_env_in_setup_path VK_ADD_LAYER_PATH "${model_emulation_layer_path}/deploy/share/vulkan/explicit_layer.d"
149+
}
150+
127151
#setup_model_converter() $1
128152
# `"$manifest_dir"'

backends/arm/scripts/toolchain_utils.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,8 @@ function setup_toolchain() {
8080
rm -rf "${toolchain_dir}"
8181
tar xf "${toolchain_dir}.tar.xz"
8282
}
83+
84+
function setup_path_toolchain() {
85+
toolchain_bin_path="$(cd ${toolchain_dir}/bin && pwd)"
86+
append_env_in_setup_path PATH ${toolchain_bin_path}
87+
}

backends/arm/scripts/utils.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
# Important to check for unset variables since this script is always sourced from setup.sh
8+
set -u
9+
10+
# Check if the script is being sourced
11+
(return 0 2>/dev/null)
12+
if [[ $? -ne 0 ]]; then
13+
echo "Error: This script must be sourced."
14+
exit 1
15+
fi
16+
717
function verify_md5() {
818
# Compare the md5 of a file with a provided expected value.
919

@@ -90,3 +100,19 @@ function check_os_support() {
90100
fi
91101
fi
92102
}
103+
104+
function prepend_env_in_setup_path() {
105+
echo "export $1=$2:\${$1-}" >> ${setup_path_script}.sh
106+
echo "set --path -pgx $1 $2" >> ${setup_path_script}.fish
107+
}
108+
109+
function append_env_in_setup_path() {
110+
echo "export $1=\${$1-}:$2" >> ${setup_path_script}.sh
111+
echo "set --path -agx $1 $2" >> ${setup_path_script}.fish
112+
}
113+
114+
function clear_setup_path() {
115+
# Clear setup_path_script
116+
echo "" > "${setup_path_script}.sh"
117+
echo "" > "${setup_path_script}.fish"
118+
}

backends/arm/scripts/vulkan_utils.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ function setup_vulkan_sdk() {
6464
exit 1
6565
fi
6666
}
67+
68+
function setup_path_vulkan() {
69+
cd "${root_dir}"
70+
vulkan_sdk_bin_path="$(cd ${vulkan_sdk_bin_dir} && pwd)"
71+
append_env_in_setup_path PATH ${vulkan_sdk_bin_path}
72+
}

examples/arm/setup.sh

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -183,62 +183,37 @@ function setup_ethos_u_tools() {
183183
CMAKE_POLICY_VERSION_MINIMUM=3.5 BUILD_PYBIND=1 pip install --no-dependencies -r $et_dir/backends/arm/requirements-arm-ethos-u.txt
184184
}
185185

186-
function prepend_env_in_setup_path() {
187-
echo "export $1=$2:\${$1-}" >> ${setup_path_script}.sh
188-
echo "set --path -pgx $1 $2" >> ${setup_path_script}.fish
189-
}
190-
191-
function append_env_in_setup_path() {
192-
echo "export $1=\${$1-}:$2" >> ${setup_path_script}.sh
193-
echo "set --path -agx $1 $2" >> ${setup_path_script}.fish
194-
}
195-
196186
function create_setup_path(){
197187
cd "${root_dir}"
198188

199-
# Clear setup_path_script
200-
echo "" > "${setup_path_script}.sh"
201-
echo "" > "${setup_path_script}.fish"
189+
clear_setup_path
202190

203191
if [[ "${enable_fvps}" -eq 1 ]]; then
204192
setup_path_fvp
205193
fi
206194

207195
if [[ "${enable_baremetal_toolchain}" -eq 1 ]]; then
208-
toolchain_bin_path="$(cd ${toolchain_dir}/bin && pwd)"
209-
append_env_in_setup_path PATH ${toolchain_bin_path}
196+
setup_path_toolchain
210197
fi
211198

212199
if [[ "${enable_vulkan_sdk}" -eq 1 ]]; then
213-
cd "${root_dir}"
214-
vulkan_sdk_bin_path="$(cd ${vulkan_sdk_bin_dir} && pwd)"
215-
append_env_in_setup_path PATH ${vulkan_sdk_bin_path}
200+
setup_path_vulkan
216201
fi
217202

218203
if [[ "${enable_model_converter}" -eq 1 ]]; then
219-
cd "${root_dir}"
220-
model_converter_bin_path="$(cd ${mlsdk_manifest_dir}/sw/model-converter/build && pwd)"
221-
append_env_in_setup_path PATH ${model_converter_bin_path}
204+
setup_path_model_converter
222205
fi
223206

224-
# Add Path for vgf-lib and emulation-layer
225207
if [[ "${enable_vgf_lib}" -eq 1 ]]; then
226-
cd "${root_dir}"
227-
model_vgf_path="$(cd ${mlsdk_manifest_dir}/sw/vgf-lib/deploy && pwd)"
228-
append_env_in_setup_path PATH ${model_vgf_path}/bin
229-
append_env_in_setup_path LD_LIBRARY_PATH "${model_vgf_path}/lib"
230-
append_env_in_setup_path DYLD_LIBRARY_PATH "${model_vgf_path}/lib"
208+
setup_path_vgf_lib
231209
fi
232210

233211
if [[ "${enable_emulation_layer}" -eq 1 ]]; then
234-
cd "${root_dir}"
235-
model_emulation_layer_path="$(cd ${mlsdk_manifest_dir}/sw/emulation-layer/ && pwd)"
236-
prepend_env_in_setup_path LD_LIBRARY_PATH "${model_emulation_layer_path}/deploy/lib"
237-
prepend_env_in_setup_path DYLD_LIBRARY_PATH "${model_emulation_layer_path}/deploy/lib"
238-
prepend_env_in_setup_path VK_INSTANCE_LAYERS VK_LAYER_ML_Tensor_Emulation
239-
prepend_env_in_setup_path VK_INSTANCE_LAYERS VK_LAYER_ML_Graph_Emulation
240-
prepend_env_in_setup_path VK_ADD_LAYER_PATH "${model_emulation_layer_path}/deploy/share/vulkan/explicit_layer.d"
212+
setup_path_emulation_layer
241213
fi
214+
215+
echo "[main] Update path by running 'source ${setup_path_script}.sh'"
216+
echo "[main] Or for fish shell use 'source ${setup_path_script}.fish'"
242217
}
243218

244219

@@ -305,13 +280,8 @@ if [[ $is_script_sourced -eq 0 ]]; then
305280
setup_model_converter ${root_dir} ${mlsdk_manifest_dir} ${enable_model_converter} ${enable_vgf_lib} ${enable_emulation_layer}
306281
fi
307282

308-
# Create new setup_path script
309-
if [[ "${enable_baremetal_toolchain}" -eq 1 || \
310-
"${enable_fvps}" -eq 1 || \
311-
"${enable_vulkan_sdk}" -eq 1 || \
312-
"${enable_model_converter}" -eq 1 ]]; then
313-
create_setup_path
314-
fi
283+
# Create the setup_path.sh used to create the PATH variable for shell
284+
create_setup_path
315285

316286
# Setup the tosa_reference_model and dependencies
317287
CMAKE_POLICY_VERSION_MINIMUM=3.5 BUILD_PYBIND=1 pip install --no-dependencies -r $et_dir/backends/arm/requirements-arm-tosa.txt
@@ -320,8 +290,6 @@ if [[ $is_script_sourced -eq 0 ]]; then
320290
setup_ethos_u_tools
321291
fi
322292

323-
echo "[main] Update path by running 'source ${setup_path_script}.sh'"
324-
hash fish 2>/dev/null && echo >&2 "[main] Or for fish shell use 'source ${setup_path_script}.fish'"
325293
echo "[main] success!"
326294
exit 0
327295
fi

0 commit comments

Comments
 (0)