Skip to content

Commit 264a91b

Browse files
Arm Backend: Improve VGF runtime and update MLSDK setup (#13155)
* Adds portability extensions to instance_info * Explicitly sets poolSizes in descriptor pool * Update Arm MLSDK manifest url to default to newly publish repo during setup.sh * Add new exports to setup_path.sh for mlsdk components Signed-off-by: Ryan O'Shea <[email protected]> Co-authored-by: Ryan O'Shea <[email protected]>
1 parent bbd23a6 commit 264a91b

File tree

4 files changed

+101
-36
lines changed

4 files changed

+101
-36
lines changed

backends/arm/runtime/VGFBackend.cpp

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,60 @@ VkResult vkml_allocate_basics(
264264
.engineVersion = 0,
265265
.apiVersion = VK_API_VERSION_1_3,
266266
};
267+
268+
std::vector<const char*> requested_extensions;
269+
VkInstanceCreateFlags instance_flags = 0;
270+
271+
#ifdef __APPLE__
272+
instance_flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
273+
274+
uint32_t extension_count = 0;
275+
result = vkEnumerateInstanceExtensionProperties(
276+
nullptr, &extension_count, nullptr);
277+
278+
if (result != VK_SUCCESS) {
279+
ET_LOG(Error, "Failed to enumerate instance extensions");
280+
return result;
281+
}
282+
283+
std::vector<VkExtensionProperties> extension_properties(extension_count);
284+
result = vkEnumerateInstanceExtensionProperties(
285+
nullptr, &extension_count, extension_properties.data());
286+
287+
if (result != VK_SUCCESS) {
288+
ET_LOG(Error, "Failed to enumerate instance extensions");
289+
return result;
290+
}
291+
292+
if (std::any_of(
293+
extension_properties.begin(),
294+
extension_properties.end(),
295+
[](const auto& extension) {
296+
return strcmp(
297+
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME,
298+
extension.extensionName) == 0;
299+
})) {
300+
requested_extensions.push_back(
301+
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
302+
}
303+
304+
if (requested_extensions.empty()) {
305+
ET_LOG(Error, "VK_KHR_portability_enumeration not found");
306+
}
307+
308+
#endif
309+
267310
VkInstanceCreateInfo instance_info{
268311
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
269312
.pNext = nullptr,
270-
.flags = 0,
313+
.flags = instance_flags,
271314
.pApplicationInfo = &app_info,
272-
0,
273-
nullptr,
274-
0,
275-
nullptr};
315+
.enabledLayerCount = 0,
316+
.ppEnabledLayerNames = nullptr,
317+
.enabledExtensionCount =
318+
static_cast<uint32_t>(requested_extensions.size()),
319+
.ppEnabledExtensionNames = requested_extensions.data(),
320+
};
276321
result = vkCreateInstance(&instance_info, nullptr, instance);
277322
if (result != VK_SUCCESS) {
278323
ET_LOG(Error, "Failed to create VkInstance");

backends/arm/runtime/VGFSetup.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,30 @@ bool VgfRepr::process_vgf(const char* vgf_data, ArrayRef<CompileSpec> specs) {
517517
return false;
518518
}
519519

520+
std::vector<VkDescriptorPoolSize> poolSizes;
521+
poolSizes.reserve(layout_bindings.size());
522+
for (const auto& b : layout_bindings) {
523+
bool found = false;
524+
for (size_t idx = 0; idx < poolSizes.size(); ++idx) {
525+
if (poolSizes[idx].type == b.descriptorType) {
526+
poolSizes[idx].descriptorCount += b.descriptorCount;
527+
found = true;
528+
break;
529+
}
530+
}
531+
if (!found) {
532+
poolSizes.push_back({b.descriptorType, b.descriptorCount});
533+
}
534+
}
535+
520536
// Create descriptor pool and descriptors for pipeline
521537
const VkDescriptorPoolCreateInfo descriptor_pool_info = {
522538
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
523539
.pNext = nullptr,
524540
.flags = 0,
525541
.maxSets = static_cast<uint32_t>(set_count),
526-
.poolSizeCount = 0,
527-
.pPoolSizes = nullptr,
542+
.poolSizeCount = static_cast<uint32_t>(poolSizes.size()),
543+
.pPoolSizes = poolSizes.data(),
528544
};
529545
result = vkCreateDescriptorPool(
530546
vk_device, &descriptor_pool_info, nullptr, &vk_descriptor_pool);

backends/arm/scripts/mlsdk_utils.sh

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
set -euo pipefail
88

9-
# TODO
10-
mlsdk_manifest_url=""
9+
mlsdk_manifest_url="https://github.com/arm/ai-ml-sdk-manifest.git"
1110

1211
script_dir=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
1312

@@ -55,8 +54,9 @@ function download_ai_mlsdk_manifest() {
5554
function setup_model_converter() {
5655
local work_dir="$1"
5756
local manifest_dir="$2"
58-
local enable_vgf_lib="$3"
59-
local enable_emulation_layer="$4"
57+
local enable_model_converter="$3"
58+
local enable_vgf_lib="$4"
59+
local enable_emulation_layer="$5"
6060

6161
if [[ -z "$work_dir" ]]; then
6262
echo "Error: work_dir parameter is required."
@@ -76,29 +76,34 @@ function setup_model_converter() {
7676
pushd "$manifest_dir"
7777

7878
# model-converter
79-
# TODO: Remove macOS patch after mlsdk fully supports macOS
80-
if [[ "$(uname)" == "Darwin" ]]; then
79+
if [[ "${enable_model_converter}" -eq 1 ]]; then
80+
# TODO: Remove this workaround once MLSDK has full Darwin support
81+
# Do not indent sed command, the whitespace is significant for the patch to work.
82+
if [[ "$(uname)" == "Darwin" ]]; then
8183
sed -i '' '/^ *print(f"Unsupported host platform/ i\
8284
if system == "Darwin":\
83-
# Use default Apple toolchain (Clang) on macOS\
8485
return True\
8586
\
8687
' sw/model-converter/scripts/build.py
88+
fi
89+
python sw/model-converter/scripts/build.py -j$(nproc)
8790
fi
88-
python sw/model-converter/scripts/build.py -j$(nproc)
8991

9092
# libvgf
9193
if [[ "${enable_vgf_lib}" -eq 1 ]]; then
92-
# TODO: Remove macOS patch after mlsdk fully supports macOS
94+
# TODO: Remove this workaround once MLSDK has full Darwin support
95+
# Do not indent sed command, the whitespace is significant for the patch to work.
9396
if [[ "$(uname)" == "Darwin" ]]; then
9497
sed -i '' '/^ *print(f"ERROR: Unsupported host platform/ i\
9598
if system == "Darwin":\
96-
# Use default Apple toolchain (Clang) on macOS\
9799
return True\
98100
\
99101
' sw/vgf-lib/scripts/build.py
100102
fi
101-
python sw/vgf-lib/scripts/build.py -j$(nproc)
103+
pushd sw/vgf-lib
104+
python scripts/build.py -j$(nproc)
105+
cmake --install build --prefix deploy
106+
popd
102107
fi
103108

104109
# emu layer
@@ -110,7 +115,9 @@ function setup_model_converter() {
110115
-DSPIRV_HEADERS_PATH=../../dependencies/SPIRV-Headers \
111116
-DSPIRV_TOOLS_PATH=../../dependencies/SPIRV-Tools \
112117
-DVULKAN_HEADERS_PATH=../../dependencies/Vulkan-Headers
118+
113119
cmake --build build
120+
cmake --install build --prefix deploy
114121
popd
115122
fi
116123

examples/arm/setup.sh

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ target_toolchain=""
2323
enable_fvps=1
2424
enable_vela=1
2525
enable_model_converter=0 # model-converter tool for VGF output
26-
enable_vgf_lib=0 # vgf reader - runtime backend dependency
26+
enable_vgf_lib=0 # vgf reader - runtime backend dependency
2727
enable_emulation_layer=0 # Vulkan layer driver - emulates Vulkan ML extensions
28-
mlsdk_manifest_url=""
28+
mlsdk_manifest_url="https://github.com/arm/ai-ml-sdk-manifest.git"
2929

3030

3131
# Figure out if setup.sh was called or sourced and save it into "is_script_sourced"
@@ -370,14 +370,19 @@ function create_setup_path(){
370370
# Add Path for vgf-lib and emulation-layer
371371
if [[ "${enable_vgf_lib}" -eq 1 ]]; then
372372
cd "${root_dir}"
373-
model_vgf_lib_bin_path="$(cd ${mlsdk_manifest_dir}/sw/vgf-lib/build && pwd)"
374-
echo "export PATH=\${PATH}:${model_vgf_lib_bin_path}" >> ${setup_path_script}
373+
model_vgf_path="$(cd ${mlsdk_manifest_dir}/sw/vgf-lib/deploy && pwd)"
374+
echo "export PATH=\${PATH}:${model_vgf_path}/bin" >> ${setup_path_script}
375+
echo "export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:${model_vgf_path}/lib" >> ${setup_path_script}
376+
echo "export DYLD_LIBRARY_PATH=\${DYLD_LIBRARY_PATH}:${model_vgf_path}/lib" >> ${setup_path_script}
375377
fi
376378

377379
if [[ "${enable_emulation_layer}" -eq 1 ]]; then
378380
cd "${root_dir}"
379-
model_emulation_layer_bin_path="$(cd ${mlsdk_manifest_dir}/sw/vgf-lib/build && pwd)"
380-
echo "export PATH=\${PATH}:${model_emulation_layer_bin_path}" >> ${setup_path_script}
381+
model_emulation_layer_path="$(cd ${mlsdk_manifest_dir}/sw/emulation-layer/ && pwd)"
382+
echo "export LD_LIBRARY_PATH=${model_emulation_layer_path}/deploy/lib:\${LD_LIBRARY_PATH}" >> ${setup_path_script}
383+
echo "export DYLD_LIBRARY_PATH=${model_emulation_layer_path}/deploy/lib:\${DYLD_LIBRARY_PATH}" >> ${setup_path_script}
384+
echo "export VK_INSTANCE_LAYERS=VK_LAYER_ML_Graph_Emulation:VK_LAYER_ML_Tensor_Emulation:\${VK_INSTANCE_LAYERS}" >> ${setup_path_script}
385+
echo "export VK_ADD_LAYER_PATH=${model_emulation_layer_path}/deploy/share/vulkan/explicit_layer.d:\${VK_ADD_LAYER_PATH}" >> ${setup_path_script}
381386
fi
382387
}
383388

@@ -434,19 +439,11 @@ if [[ $is_script_sourced -eq 0 ]]; then
434439
setup_fvp
435440
fi
436441

437-
438-
if [[ -z "$mlsdk_manifest_url" && "${enable_model_converter}" -eq 1 ]]; then
439-
echo "Warning: mlsdk-manifest-url is not set, but model converter setup is not skipped."
440-
echo " Please set the --mlsdk-manifest-url option to the correct URL."
441-
echo " Skipping MLSDK model converter setup."
442-
enable_model_converter=0 # Q: Can we assume if we enable mlsdk, we will always enable model converter
443-
enable_vgf_lib=0
444-
enable_emulation_layer=0
445-
fi
446-
447-
if [[ "${enable_model_converter}" -eq 1 ]]; then
442+
if [[ "${enable_model_converter}" -eq 1 || \
443+
"${enable_vgf_lib}" -eq 1 || \
444+
"${enable_emulation_layer}" -eq 1 ]]; then
448445
source $et_dir/backends/arm/scripts/mlsdk_utils.sh -u "${mlsdk_manifest_url}"
449-
setup_model_converter ${root_dir} ${mlsdk_manifest_dir} ${enable_vgf_lib} ${enable_emulation_layer}
446+
setup_model_converter ${root_dir} ${mlsdk_manifest_dir} ${enable_model_converter} ${enable_vgf_lib} ${enable_emulation_layer}
450447
fi
451448

452449
# Create new setup_path script

0 commit comments

Comments
 (0)