Skip to content

Commit 9023780

Browse files
authored
Arm backend: workaround pip emulation layer env exports on macOS (pytorch#17815)
When the emulation_layer CLI prints no export lines (e.g. macOS unsupported platform message), derive the deploy path from the installed pip package and populate the Vulkan layer/lib env vars. Also parse DYLD_LIBRARY_PATH when provided by the CLI output. Signed-off-by: Usamah Zaheer <usamah.zaheer@arm.com>
1 parent 76ebaa0 commit 9023780

1 file changed

Lines changed: 56 additions & 3 deletions

File tree

‎backends/arm/scripts/mlsdk_utils.sh‎

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# Copyright 2025 Arm Limited and/or its affiliates.
2+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
33
#
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.
@@ -221,13 +221,56 @@ function setup_path_emulation_layer_from_pip() {
221221

222222
local output
223223
if ! output=$(emulation_layer 2>/dev/null); then
224-
echo "[mlsdk_utils] Failed to query emulation_layer environment; skipping"
225-
return
224+
output=""
226225
fi
227226

228227
local exports
229228
exports=$(echo "$output" | grep '^export ' || true)
230229

230+
if [[ -z "${exports}" ]] || echo "$output" | grep -q "Unsupported platform"; then
231+
local py="python3"
232+
if ! command -v "${py}" >/dev/null 2>&1; then
233+
py="python"
234+
fi
235+
236+
local pkg_dir=""
237+
if command -v "${py}" >/dev/null 2>&1; then
238+
pkg_dir=$("${py}" - <<'PY'
239+
import importlib.util
240+
import os
241+
import sys
242+
243+
spec = importlib.util.find_spec("emulation_layer")
244+
if spec is None:
245+
print("")
246+
sys.exit(0)
247+
if spec.submodule_search_locations:
248+
print(list(spec.submodule_search_locations)[0])
249+
elif spec.origin:
250+
print(os.path.dirname(spec.origin))
251+
else:
252+
print("")
253+
PY
254+
)
255+
fi
256+
257+
if [[ -n "${pkg_dir}" && -d "${pkg_dir}/deploy" ]]; then
258+
local deploy_dir="${pkg_dir}/deploy"
259+
prepend_env_in_setup_path LD_LIBRARY_PATH "${deploy_dir}/lib"
260+
prepend_env_in_setup_path DYLD_LIBRARY_PATH "${deploy_dir}/lib"
261+
prepend_env_in_setup_path VK_LAYER_PATH "${deploy_dir}/share/vulkan/explicit_layer.d"
262+
prepend_env_in_setup_path VK_ADD_LAYER_PATH "${deploy_dir}/share/vulkan/explicit_layer.d"
263+
prepend_env_in_setup_path VK_INSTANCE_LAYERS VK_LAYER_ML_Tensor_Emulation
264+
prepend_env_in_setup_path VK_INSTANCE_LAYERS VK_LAYER_ML_Graph_Emulation
265+
return
266+
fi
267+
fi
268+
269+
if [[ -z "${exports}" ]]; then
270+
echo "[mlsdk_utils] Failed to query emulation_layer environment; skipping"
271+
return
272+
fi
273+
231274
local ld_line
232275
ld_line=$(echo "$exports" | grep 'LD_LIBRARY_PATH=' || true)
233276
if [[ -n "${ld_line}" ]]; then
@@ -238,6 +281,16 @@ function setup_path_emulation_layer_from_pip() {
238281
fi
239282
fi
240283

284+
local dyld_line
285+
dyld_line=$(echo "$exports" | grep 'DYLD_LIBRARY_PATH=' || true)
286+
if [[ -n "${dyld_line}" ]]; then
287+
local dyld_value=${dyld_line#export DYLD_LIBRARY_PATH=}
288+
dyld_value=${dyld_value%%:\$DYLD_LIBRARY_PATH*}
289+
if [[ -n "${dyld_value}" ]]; then
290+
prepend_env_in_setup_path DYLD_LIBRARY_PATH "${dyld_value}"
291+
fi
292+
fi
293+
241294
local vk_add_line
242295
vk_add_line=$(echo "$exports" | grep 'VK_ADD_LAYER_PATH=' || true)
243296
if [[ -n "${vk_add_line}" ]]; then

0 commit comments

Comments
 (0)