utils.py 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# -*- coding:UTF-8 -*-
# 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.

S
SunAhong1993 已提交
16
import paddle
W
wjj19950828 已提交
17
import x2paddle
W
wjj19950828 已提交
18
import hashlib
W
wjj19950828 已提交
19 20
import requests
import threading
W
wjj19950828 已提交
21
import uuid
W
wjj19950828 已提交
22
import json
W
wjj19950828 已提交
23 24

stats_api = "http://paddlepaddle.org.cn/paddlehub/stat"
S
SunAhong1993 已提交
25

26 27 28 29 30

def string(param):
    """ 生成字符串。
    """
    return "\'{}\'".format(param)
S
SunAhong1993 已提交
31 32 33 34 35 36 37 38 39 40 41 42


def check_version():
    version = paddle.__version__
    v0, v1, v2 = version.split('.')
    if not ((v0 == '0' and v1 == '0' and v2 == '0') or
            (int(v0) >= 2 and int(v1) >= 1)):
        return False
    else:
        return True


W
wjj19950828 已提交
43 44 45 46 47 48
def _md5(text: str):
    '''Calculate the md5 value of the input text.'''
    md5code = hashlib.md5(text.encode())
    return md5code.hexdigest()


W
wjj19950828 已提交
49 50 51 52 53 54
class ConverterCheck(threading.Thread):
    """
    Count the number of calls to model convertion
    """

    def __init__(self,
W
wjj19950828 已提交
55
                 task="ONNX",
W
wjj19950828 已提交
56
                 time_info=None,
W
wjj19950828 已提交
57 58 59 60 61
                 convert_state=None,
                 lite_state=None,
                 extra_info=None):
        threading.Thread.__init__(self)
        self._task = task
W
wjj19950828 已提交
62
        self._version = x2paddle.__version__
W
wjj19950828 已提交
63 64 65
        self._convert_state = convert_state
        self._lite_state = lite_state
        self._extra_info = extra_info
W
wjj19950828 已提交
66
        self._convert_id = _md5(str(uuid.uuid1())[-12:]) + "-" + str(time_info)
W
wjj19950828 已提交
67 68 69 70

    def run(self):
        params = {
            'task': self._task,
W
wjj19950828 已提交
71
            'x2paddle_version': self._version,
W
wjj19950828 已提交
72
            'paddle_version': paddle.__version__,
W
wjj19950828 已提交
73 74 75
            'from': 'x2paddle'
        }
        extra = {
W
wjj19950828 已提交
76
            'convert_state': self._convert_state,
W
wjj19950828 已提交
77
            'convert_id': self._convert_id,
W
wjj19950828 已提交
78 79
        }
        if self._lite_state is not None:
W
wjj19950828 已提交
80
            extra.update({'lite_state': self._lite_state})
W
wjj19950828 已提交
81
        if self._extra_info is not None:
W
wjj19950828 已提交
82
            extra.update(self._extra_info)
W
wjj19950828 已提交
83

W
wjj19950828 已提交
84
        params.update({"extra": json.dumps(extra)})
W
wjj19950828 已提交
85
        try:
W
wjj19950828 已提交
86
            requests.get(stats_api, params, timeout=2)
W
wjj19950828 已提交
87 88 89 90 91 92
        except Exception:
            pass

        return


S
SunAhong1993 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
class PaddleDtypes():
    def __init__(self, is_new_version=True):
        if is_new_version:
            self.t_float16 = paddle.float16
            self.t_float32 = paddle.float32
            self.t_float64 = paddle.float64
            self.t_uint8 = paddle.uint8
            self.t_int8 = paddle.int8
            self.t_int16 = paddle.int16
            self.t_int32 = paddle.int32
            self.t_int64 = paddle.int64
            self.t_bool = paddle.bool
        else:
106 107 108 109 110 111 112 113 114
            self.t_float16 = "paddle.fluid.core.VarDesc.VarType.FP16"
            self.t_float32 = "paddle.fluid.core.VarDesc.VarType.FP32"
            self.t_float64 = "paddle.fluid.core.VarDesc.VarType.FP64"
            self.t_uint8 = "paddle.fluid.core.VarDesc.VarType.UINT8"
            self.t_int8 = "paddle.fluid.core.VarDesc.VarType.INT8"
            self.t_int16 = "paddle.fluid.core.VarDesc.VarType.INT16"
            self.t_int32 = "paddle.fluid.core.VarDesc.VarType.INT32"
            self.t_int64 = "paddle.fluid.core.VarDesc.VarType.INT64"
            self.t_bool = "paddle.fluid.core.VarDesc.VarType.BOOL"
S
SunAhong1993 已提交
115 116 117 118


is_new_version = check_version()
paddle_dtypes = PaddleDtypes(is_new_version)