count_all_enforce.sh 3.5 KB
Newer Older
1
#!/bin/bash
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2020 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.

17 18 19 20
# This script is used to count all PADDLE checks in the paddle/fluid directory,
#   including the total PADDLE check number, the valid check number and the
#   invalid check number under paddle/fluid and its main sub-directories.

21
# Usage: bash count_all_enforce.sh (run in tools directory)
22 23 24

# Result Example:

25 26 27 28 29 30 31 32 33 34
#     paddle/fluid/framework - total: 1065, valid: 267, invalid: 798
#     paddle/fluid/imperative - total: 135, valid: 118, invalid: 17
#     paddle/fluid/inference - total: 449, valid: 158, invalid: 291
#     paddle/fluid/memory - total: 60, valid: 10, invalid: 50
#     paddle/fluid/operators - total: 4225, valid: 1061, invalid: 3164
#     paddle/fluid/platform - total: 240, valid: 39, invalid: 201
#     paddle/fluid/pybind - total: 98, valid: 53, invalid: 45
#     paddle/fluid/string - total: 0, valid: 0, invalid: 0
#     paddle/fluid/testdata - total: 0, valid: 0, invalid: 0
#     paddle/fluid/train - total: 6, valid: 0, invalid: 6
35 36 37 38 39 40 41 42 43 44 45
#     ----------------------------
#     PADDLE ENFORCE & THROW COUNT
#     ----------------------------
#     All PADDLE_ENFORCE{_**} & PADDLE_THROW Count: 6278
#     Valid PADDLE_ENFORCE{_**} & PADDLE_THROW Count: 1706
#     Invalid PADDLE_ENFORCE{_**} & PADDLE_THROW Count: 4572

ROOT_DIR=../paddle/fluid
ALL_PADDLE_CHECK_CNT=0
VALID_PADDLE_CHECK_CNT=0

46
function enforce_count(){
47
    paddle_check=`grep -r -zoE "(PADDLE_ENFORCE[A-Z_]{0,9}|PADDLE_THROW)\(.[^,\);]*.[^;]*\);\s" $1 || true`
48
    total_check_cnt=`echo "$paddle_check" | grep -cE "(PADDLE_ENFORCE|PADDLE_THROW)" || true`
49
    valid_check_cnt=`echo "$paddle_check" | grep -zoE '(PADDLE_ENFORCE[A-Z_]{0,9}|PADDLE_THROW)\((.[^,;]+,)*.[^";]*(errors::).[^"]*".[^";]{20,}.[^;]*\);\s' | grep -cE "(PADDLE_ENFORCE|PADDLE_THROW)" || true`
50 51 52 53
    eval $2=$total_check_cnt
    eval $3=$valid_check_cnt
}

54
function count_dir_recursively(){
55 56 57 58 59
    for file in `ls $1`
    do
        if [ -d $1"/"$file ];then
            level=$(($2+1))
            if [ $level -le 1 ]; then
60
                enforce_count $1"/"$file total_check_cnt valid_check_cnt
61
                dir_name=$1
62
                echo "${dir_name#../}/"$file" | ${total_check_cnt} | ${valid_check_cnt} | $(($total_check_cnt-$valid_check_cnt))"
63 64
                ALL_PADDLE_CHECK_CNT=$(($ALL_PADDLE_CHECK_CNT+$total_check_cnt))
                VALID_PADDLE_CHECK_CNT=$(($VALID_PADDLE_CHECK_CNT+$valid_check_cnt))
65
                count_dir_recursively $1"/"$file $level
66 67 68 69 70
            fi
        fi
    done
}

71 72 73 74 75 76 77 78 79 80
main() {
    count_dir_recursively $ROOT_DIR 0

    echo "----------------------------"
    echo "PADDLE ENFORCE & THROW COUNT"
    echo "----------------------------"
    echo "All PADDLE_ENFORCE{_**} & PADDLE_THROW Count: ${ALL_PADDLE_CHECK_CNT}"
    echo "Valid PADDLE_ENFORCE{_**} & PADDLE_THROW Count: ${VALID_PADDLE_CHECK_CNT}"
    echo "Invalid PADDLE_ENFORCE{_**} & PADDLE_THROW Count: $(($ALL_PADDLE_CHECK_CNT-$VALID_PADDLE_CHECK_CNT))"
}
81

82 83 84
if [ "${1}" != "--source-only" ]; then
    main "${@}"
fi