xref: /aosp_15_r20/external/executorch/examples/xnnpack/quantization/test_quantize.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1#!/bin/bash
2# Copyright (c) Meta Platforms, Inc. and affiliates.
3# 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
8# Test the end-to-end quantization flow.
9
10set -e
11
12# shellcheck source=/dev/null
13source "$(dirname "${BASH_SOURCE[0]}")/../../../.ci/scripts/utils.sh"
14
15get_shared_lib_ext() {
16  UNAME=$(uname)
17  if [[ $UNAME == "Darwin" ]];
18  then
19    EXT=".dylib"
20  elif [[ $UNAME == "Linux" ]];
21  then
22    EXT=".so"
23  else
24    echo "Unsupported platform $UNAME"
25    exit 1
26  fi
27  echo $EXT
28}
29
30test_buck2_quantization() {
31  echo "Building quantized ops shared library"
32  SO_LIB=$($BUCK build //kernels/quantized:aot_lib --show-output | grep "buck-out" | cut -d" " -f2)
33
34  echo "Run example.py"
35  ${PYTHON_EXECUTABLE} -m "examples.xnnpack.quantization.example" --so_library="$SO_LIB" --model_name="$1"
36
37  echo 'Running executor_runner'
38  $BUCK run //examples/portable/executor_runner:executor_runner -- --model_path="./${1}_quantized.pte"
39  # should give correct result
40
41  echo "Removing ${1}_quantized.pte"
42  rm "./${1}_quantized.pte"
43}
44
45test_cmake_quantization() {
46  echo "Building quantized ops shared library"
47  SITE_PACKAGES="$(${PYTHON_EXECUTABLE} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')"
48  CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch"
49
50  (rm -rf cmake-out \
51    && mkdir cmake-out \
52    && cd cmake-out \
53    && retry cmake \
54      -DCMAKE_BUILD_TYPE=Release \
55      -DEXECUTORCH_BUILD_XNNPACK="$EXECUTORCH_BUILD_XNNPACK" \
56      -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
57      -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \
58      -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
59      -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
60
61  cmake --build cmake-out -j4
62
63  EXT=$(get_shared_lib_ext)
64  SO_LIB="cmake-out/kernels/quantized/libquantized_ops_aot_lib$EXT"
65
66  echo "Run example.py, shared library $SO_LIB"
67  ${PYTHON_EXECUTABLE} -m "examples.xnnpack.quantization.example" --so_library="$SO_LIB" --model_name="$1"
68
69  echo 'Running executor_runner'
70  cmake-out/executor_runner --model_path="./${1}_quantized.pte"
71  # should give correct result
72
73  echo "Removing ${1}_quantized.pte"
74  rm "./${1}_quantized.pte"
75}
76
77if [[ -z $PYTHON_EXECUTABLE ]];
78then
79  PYTHON_EXECUTABLE=python3
80fi
81if [[ -z $BUCK ]];
82then
83  BUCK=buck2
84fi
85if [[ "${XNNPACK_DELEGATION}" == true ]];
86then
87  EXECUTORCH_BUILD_XNNPACK=ON
88else
89  EXECUTORCH_BUILD_XNNPACK=OFF
90fi
91if [[ "$1" == "cmake" ]];
92then
93  test_cmake_quantization "$2"
94elif [[ "$1" == "buck2" ]];
95then
96  test_buck2_quantization "$2"
97else
98  test_cmake_quantization "$2"
99  test_buck2_quantization "$2"
100fi
101