infrt_build.sh 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env bash

# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#=================================================
#                   Utils
#=================================================

21
set -e
22

23 24 25
# TARGET: CPU/GPU/TensorRt
TARGET=GPU

26 27 28 29 30 31 32
if [ -z ${BRANCH} ]; then
    BRANCH="develop"
fi

EXIT_CODE=0;
tmp_dir=`mktemp -d`

33 34 35 36 37
function update_pd_ops() {
   PADDLE_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}")/../../" && pwd )"
   # compile and install paddle
   rm -rf ${PADDLE_ROOT}/build && mkdir -p ${PADDLE_ROOT}/build
   cd ${PADDLE_ROOT}/build
38 39 40 41 42 43 44

   INFER_WITH_GPU=OFF
   if [ "${TARGET}" == "GPU" ] || [ "${TARGET}" == "gpu" ] || [ "${TARGET}" == "TensorRt" ] || [ "${TARGET}" == "tensorrt" ]; then
      INFER_WITH_GPU=ON
   fi

   cmake .. -DWITH_PYTHON=ON -DWITH_MKL=OFF -DWITH_GPU=$INFER_WITH_GPU -DPYTHON_EXECUTABLE=`which python3` -DWITH_XBYAK=OFF -DWITH_NCCL=OFF -DWITH_RCCL=OFF -DWITH_CRYPTO=OFF
45
   make -j24 paddle_python print_pten_kernels kernel_signature_generator
46
   cd ${PADDLE_ROOT}/build
47
   ./paddle/phi/tools/print_pten_kernels > ../tools/infrt/kernels.json
48
   ./paddle/fluid/pybind/kernel_signature_generator > ../tools/infrt/kernel_signature.json
49 50 51 52 53 54
   cd python/dist/
   python3 -m pip uninstall -y paddlepaddle
   python3 -m pip install  *whl
   # update pd_ops.td
   cd ${PADDLE_ROOT}/tools/infrt/
   python3 generate_pd_op_dialect_from_paddle_op_maker.py
55
   python3 generate_phi_kernel_dialect.py
H
huzhiqiang 已提交
56
   # generate test model
H
huzhiqiang 已提交
57
   cd ${PADDLE_ROOT}
58 59 60
   mkdir -p ${PADDLE_ROOT}/build/models
   python3 paddle/infrt/tests/models/abs_model.py ${PADDLE_ROOT}/build/paddle/infrt/tests/abs
   python3 paddle/infrt/tests/models/resnet50_model.py ${PADDLE_ROOT}/build/models/resnet50/model
61 62
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
function init() {
    RED='\033[0;31m'
    BLUE='\033[0;34m'
    BOLD='\033[1m'
    NONE='\033[0m'

    PADDLE_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}")/../../" && pwd )"
    export PADDLE_ROOT
    if [ -z "${SCRIPT_NAME}" ]; then
        SCRIPT_NAME=$0
    fi

    ENABLE_MAKE_CLEAN=${ENABLE_MAKE_CLEAN:-ON}

    # NOTE(chenweihang): For easy debugging, CI displays the C++ error stacktrace by default 
    export FLAGS_call_stack_level=2

    # set CI_SKIP_CPP_TEST if only *.py changed
    # In order to avoid using in some CI(such as daily performance), the current
    # branch must not be `${BRANCH}` which is usually develop.
    if [ ${CI_SKIP_CPP_TEST:-ON} == "OFF"  ];then
        echo "CI_SKIP_CPP_TEST=OFF"
    else
        if [ "$(git branch | grep "^\*" | awk '{print $2}')" != "${BRANCH}" ]; then
            git diff --name-only ${BRANCH} | grep -v "\.py$" || export CI_SKIP_CPP_TEST=ON
        fi
    fi
}

function infrt_gen_and_build() {
93
    parallel_number=24
94 95 96 97 98
    if [ "$1" != "" ]; then
      parallel_number=$1
    fi
    startTime_s=`date +%s`
    set +e
99

100
    mkdir -p ${PADDLE_ROOT}/build
101 102 103
    # step1. reinstall paddle and generate pd_ops.td
    update_pd_ops
    # step2. compile infrt
104 105
    cd ${PADDLE_ROOT}/build
    rm -f infrt_summary.txt
106 107 108 109 110 111 112

    INFER_WITH_GPU=OFF
    if [ "${TARGET}" == "GPU" ] || [ "${TARGET}" == "gpu" ] || [ "${TARGET}" == "TensorRt" ]; then
       INFER_WITH_GPU=ON
    fi

    cmake ..  -DWITH_MKL=OFF -DWITH_GPU=${INFER_WITH_GPU} -DWITH_TENSORRT=ON -DWITH_CRYPTO=OFF -DCMAKE_BUILD_TYPE=Release -DWITH_INFRT=ON -DINFRT_WITH_GPU=ON -DINFRT_WITH_TRT=ON -DWITH_PYTHON=OFF -DWITH_TESTING==${WITH_TESTING:-ON}; build_error=$?
113 114 115 116
    if [ "$build_error" != 0 ];then
        exit 7;
    fi

王明冬 已提交
117
    make -j ${parallel_number} infrt infrtopt infrtexec test_infrt_exec trt-exec phi-exec infrt_lib_dist paddle-mlir-convert;build_error=$?
118 119 120 121 122 123 124 125 126
    if [ "$build_error" != 0 ];then
        exit 7;
    fi
    endTime_s=`date +%s`
    [ -n "$startTime_firstBuild" ] && startTime_s=$startTime_firstBuild
    echo "Build Time: $[ $endTime_s - $startTime_s ]s"
    echo "ipipe_log_param_Infrt_Build_Time: $[ $endTime_s - $startTime_s ]s" >> ${PADDLE_ROOT}/build/infrt_summary.txt
}

127 128
function create_fake_models() {
    cd ${PADDLE_ROOT}/build
129
    cd python/dist/
130 131
    # create multi_fc model, this will generate "multi_fc_model"
    python3 -m pip uninstall -y paddlepaddle
132 133
    python3 -m pip install  *whl
    cd ${PADDLE_ROOT}/build
134
    python3 ${PADDLE_ROOT}/tools/infrt/fake_models/multi_fc.py
135
    python3 ${PADDLE_ROOT}/paddle/infrt/tests/models/linear.py
136 137
}

138
function test_infrt() {
139 140
    create_fake_models

141 142 143
    # install llvm-lit toolkit
    python3 -m pip install lit

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    mkdir -p ${PADDLE_ROOT}/build
    cd ${PADDLE_ROOT}/build
    if [ ${WITH_TESTING:-ON} == "ON" ] ; then
        cat <<EOF
        ========================================
            Running infrt unit tests ...
        ========================================
EOF
        tmpfile_rand=`date +%s%N`
        tmpfile=$tmp_dir/$tmpfile_rand
        ut_startTime_s=`date +%s`
        ctest --output-on-failure -R test_infrt* | tee $tmpfile
        ut_endTime_s=`date +%s`
        echo "infrt testCase Time: $[ $ut_endTime_s - $ut_startTime_s ]s"
        exit_code=0
        grep -q 'The following tests FAILED:' $tmpfile||exit_code=$?
        if [ $exit_code -eq 0 ]; then
            exit 8;
        fi
    fi
}

function main() {
167
    local CMD=$1
168
    local parallel_number=$2
169 170 171 172 173 174
    if [ -z "$1" ]; then
        echo "Usage:"
        echo "      (1)bash infrt_build.sh build_and_test"
        echo "      (2)bash infrt_build.sh build_only"
        echo "      (3)bash infrt_build.sh test_only"
        echo "      optional command: --update_pd_ops : pd_ops.td will be updated according to paddle's code."
175
        echo "                        --target= : GPU/gpu/CPU/cpu/TensorRt/tensorrt, default value is GPU."
176 177 178
        exit 0
    fi

179
    init
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    for i in "$@"; do
        case $i in
            --target=*)
              TARGET="${i#*=}"
              shift
              ;;
            build_and_test)
              infrt_gen_and_build ${parallel_number}
              test_infrt
              ;;
            build_only)
              infrt_gen_and_build ${parallel_number}
              ;;
            test_only)
              test_infrt
              ;;
            *)
              print_usage
              exit 1
              ;;
        esac
    done
203 204 205 206 207 208 209
    set +x
    if [[ -f ${PADDLE_ROOT}/build/infrt_summary.txt ]];then
      echo "=====================build summary======================"
      cat ${PADDLE_ROOT}/build/infrt_summary.txt
      echo "========================================================"
    fi
    echo "paddle_build script finished as expected!"
210 211 212 213
}

main $@

214
rm -rf $tmp_dir