onnxbase.py 12.0 KB
Newer Older
W
wjj19950828 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2022  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.

import os
W
WJJ1995 已提交
16 17
import sys
import importlib
W
wjj19950828 已提交
18 19 20 21
import numpy as np
import logging
import paddle
import onnx
W
WJJ1995 已提交
22 23 24
import shutil
from paddle.inference import create_predictor, PrecisionType
from paddle.inference import Config
W
wjj19950828 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
from onnx import helper
from onnx import TensorProto
from onnxruntime import InferenceSession

DTYPE_ONNX_STR_MAP = {
    'float32': TensorProto.FLOAT,
    'float64': TensorProto.DOUBLE,
    'int16': TensorProto.INT16,
    'int32': TensorProto.INT32,
    'int64': TensorProto.INT64,
    'bool': TensorProto.BOOL,
}


def compare(result, expect, delta=1e-10, rtol=1e-10):
    """
W
wjj19950828 已提交
41 42 43 44 45
    param meaning:
    result: onnx result
    expect: paddle result
    delta: absolute error
    rtol: relative error
W
wjj19950828 已提交
46 47 48 49 50 51
    """
    if type(result) == np.ndarray:
        if type(expect) == list:
            expect = expect[0]
        expect = np.array(expect)
        res = np.allclose(result, expect, atol=delta, rtol=rtol, equal_nan=True)
W
WJJ1995 已提交
52
        # print wrong results
W
wjj19950828 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        if res is False:
            if result.dtype == np.bool_:
                diff = abs(result.astype("int32") - expect.astype("int32"))
            else:
                diff = abs(result - expect)
            logging.error("Output has diff! max diff: {}".format(np.amax(diff)))
        if result.dtype != expect.dtype:
            logging.error(
                "Different output data types! res type is: {}, and expect type is: {}".
                format(result.dtype, expect.dtype))
        assert res
        assert result.shape == expect.shape, "result.shape: {} != expect.shape: {}".format(
            result.shape, expect.shape)
        assert result.dtype == expect.dtype, "result.dtype: {} != expect.dtype: {}".format(
            result.dtype, expect.dtype)
W
WJJ1995 已提交
68
    elif isinstance(result, (list, tuple)):
W
wjj19950828 已提交
69 70 71 72 73
        for i in range(len(result)):
            if isinstance(result[i], (np.generic, np.ndarray)):
                compare(result[i], expect[i], delta, rtol)
            else:
                compare(result[i].numpy(), expect[i], delta, rtol)
W
wjj19950828 已提交
74 75 76
    # deal with scalar tensor
    elif len(expect) == 1:
        compare(result, expect[0], delta, rtol)
W
WJJ1995 已提交
77 78
    else:
        raise Exception("Compare diff wrong!!!!!!")
W
wjj19950828 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101


def randtool(dtype, low, high, shape):
    """
    np random tools
    """
    if dtype == "int":
        return np.random.randint(low, high, shape)

    elif dtype == "float":
        return low + (high - low) * np.random.random(shape)

    elif dtype == "bool":
        return np.random.randint(low, high, shape).astype("bool")


class ONNXConverter(object):
    """
     onnx model transfer to paddle
    """

    def __init__(self,
                 file_name,
W
wjj19950828 已提交
102 103
                 min_opset_version,
                 max_opset_version,
W
wjj19950828 已提交
104 105 106 107 108 109
                 op_type=[],
                 inputs_name=[],
                 outputs_name=[],
                 inputs_shape=[],
                 delta=1e-5,
                 rtol=1e-5,
W
wjj19950828 已提交
110
                 attrs=[],
W
WJJ1995 已提交
111 112
                 enable_onnx_checker=True,
                 run_dynamic=False):
W
wjj19950828 已提交
113 114 115 116 117 118
        self.op_type = op_type
        assert isinstance(self.op_type,
                          str), "The dtype of op_type must be string!"
        self.seed = 33
        np.random.seed(self.seed)
        paddle.seed(self.seed)
W
wjj19950828 已提交
119
        self.places = ['cpu']
W
wjj19950828 已提交
120
        self.name = file_name
W
wjj19950828 已提交
121 122
        self.min_opset_version = min_opset_version
        self.max_opset_version = max_opset_version
W
wjj19950828 已提交
123 124 125 126 127 128 129 130 131 132 133
        self.pwd = os.getcwd()
        self.delta = delta
        self.rtol = rtol
        self.static = False
        self.kwargs_dict = {"input_data": ()}
        self.input_feed = {}
        self.inputs_dtype = []
        self.inputs_name = inputs_name
        self.outputs_name = outputs_name
        self.inputs_shape = inputs_shape
        self.attrs = attrs
W
wjj19950828 已提交
134
        self.enable_onnx_checker = enable_onnx_checker
W
WJJ1995 已提交
135
        self.run_dynamic = run_dynamic
W
wjj19950828 已提交
136 137 138 139 140 141 142 143 144 145

    def set_input_data(self, group_name, *args):
        """
        set input data
        """
        self.kwargs_dict[group_name] = args
        if isinstance(self.kwargs_dict[group_name][0], tuple):
            self.kwargs_dict[group_name] = self.kwargs_dict[group_name][0]

        i = 0
W
wjj19950828 已提交
146 147 148
        add_inputs_shape = False
        if len(self.inputs_shape) == 0:
            add_inputs_shape = True
W
wjj19950828 已提交
149 150 151 152 153
        for in_data in self.kwargs_dict[group_name]:
            if isinstance(in_data, list):
                for data in in_data:
                    self.inputs_dtype.append(str(data.dtype))
                    self.input_feed[self.inputs_name[i]] = data
W
wjj19950828 已提交
154 155
                    if add_inputs_shape:
                        self.inputs_shape.append(data.shape)
W
wjj19950828 已提交
156 157 158 159 160 161
                    i += 1
            else:
                if isinstance(in_data, tuple):
                    in_data = in_data[0]
                self.inputs_dtype.append(str(in_data.dtype))
                self.input_feed[self.inputs_name[i]] = in_data
W
wjj19950828 已提交
162 163
                if add_inputs_shape:
                    self.inputs_shape.append(in_data.shape)
W
wjj19950828 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                i += 1

    def _mkdir(self):
        """
        make dir to save all
        """
        save_path = os.path.join(self.pwd, self.name)
        if not os.path.exists(save_path):
            os.mkdir(save_path)

    def _onnx_to_paddle(self, ver):
        """
        convert onnx to paddle
        """
        from x2paddle.convert import onnx2paddle
        onnx_path = os.path.join(self.pwd, self.name,
                                 self.name + '_' + str(ver) + '.onnx')
        paddle_path = os.path.join(self.pwd, self.name,
                                   self.name + '_' + str(ver) + '_paddle')
W
wjj19950828 已提交
183 184 185 186
        onnx2paddle(
            onnx_path,
            paddle_path,
            convert_to_lite=False,
187 188
            enable_onnx_checker=self.enable_onnx_checker,
            disable_feedback=True)
W
wjj19950828 已提交
189 190 191 192 193

    def _mk_paddle_res(self, ver):
        """
        make paddle res
        """
W
WJJ1995 已提交
194 195
        # input data
        paddle_tensor_feed = list()
W
WJJ1995 已提交
196
        result = list()
W
wjj19950828 已提交
197
        for i in range(len(self.input_feed)):
W
WJJ1995 已提交
198 199 200
            paddle_tensor_feed.append(
                paddle.to_tensor(self.input_feed[self.inputs_name[i]]))

W
WJJ1995 已提交
201 202 203 204
        ## PaddleInference not support float64
        if "float64" in self.inputs_dtype:
            self.run_dynamic = True

W
WJJ1995 已提交
205 206 207
        if self.run_dynamic:
            paddle_path = os.path.join(self.pwd, self.name,
                                       self.name + '_' + str(ver) + '_paddle/')
W
WJJ1995 已提交
208 209 210 211 212 213 214 215 216
            restore = paddle.load(os.path.join(paddle_path, "model.pdparams"))
            sys.path.insert(0, paddle_path)
            import x2paddle_code
            # Solve the problem of function overloading caused by traversing the model
            importlib.reload(x2paddle_code)
            model = getattr(x2paddle_code, "ONNXModel")()
            model.set_dict(restore)
            model.eval()
            result = model(*paddle_tensor_feed)
W
WJJ1995 已提交
217
        else:
W
WJJ1995 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            paddle_model_path = os.path.join(
                self.pwd, self.name, self.name + '_' + str(ver) +
                '_paddle/inference_model/model.pdmodel')
            paddle_param_path = os.path.join(
                self.pwd, self.name, self.name + '_' + str(ver) +
                '_paddle/inference_model/model.pdiparams')
            config = Config()
            config.set_prog_file(paddle_model_path)
            if os.path.exists(paddle_param_path):
                config.set_params_file(paddle_param_path)
            # initial GPU memory(M), device ID
            config.enable_use_gpu(200, 0)
            # optimize graph and fuse op
            config.switch_ir_optim(False)
            config.enable_memory_optim()
            # disable feed, fetch OP, needed by zero_copy_run
            config.switch_use_feed_fetch_ops(False)
            config.disable_glog_info()
            pass_builder = config.pass_builder()
            predictor = create_predictor(config)
            input_names = predictor.get_input_names()
            output_names = predictor.get_output_names()
            for i in range(len(input_names)):
                input_tensor = predictor.get_input_handle(input_names[i])
                input_tensor.copy_from_cpu(self.input_feed[self.inputs_name[i]])
            predictor.run()
            for output_name in output_names:
                output_tensor = predictor.get_output_handle(output_name)
                result.append(output_tensor.copy_to_cpu())
        shutil.rmtree(
            os.path.join(self.pwd, self.name, self.name + '_' + str(ver) +
                         '_paddle/'))
W
wjj19950828 已提交
250 251
        # get paddle outputs
        if isinstance(result, (tuple, list)):
W
WJJ1995 已提交
252 253 254 255
            if isinstance(result[0], np.ndarray):
                result = tuple(out for out in result)
            else:
                result = tuple(out.numpy() for out in result)
W
wjj19950828 已提交
256
        else:
W
WJJ1995 已提交
257 258 259 260
            if isinstance(result, np.ndarray):
                result = (result, )
            else:
                result = (result.numpy(), )
W
wjj19950828 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        return result

    def _mk_onnx_res(self, ver):
        """
        make onnx res
        """
        sess = InferenceSession(
            os.path.join(self.pwd, self.name, self.name + '_' + str(ver) +
                         '.onnx'))
        ort_outs = sess.run(output_names=None, input_feed=self.input_feed)
        return ort_outs

    def set_onnx_inputs(self):
        graph_inputs = list()
        for i in range(len(self.inputs_name)):
            graph_inputs.append(
                helper.make_tensor_value_info(self.inputs_name[
                    i], DTYPE_ONNX_STR_MAP[self.inputs_dtype[i]],
                                              self.inputs_shape[i]))

        return graph_inputs

    def set_onnx_outputs(self):
        graph_outputs = list()
        for i in range(len(self.outputs_name)):
W
wjj19950828 已提交
286
            graph_outputs.append(onnx.ValueInfoProto(name=self.outputs_name[i]))
W
wjj19950828 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

        return graph_outputs

    def _mk_onnx_graph(self, ver):
        """
        make onnx graph
        """
        node = onnx.helper.make_node(
            self.op_type,
            inputs=self.inputs_name,
            outputs=self.outputs_name,
            **self.attrs, )
        graph_inputs = self.set_onnx_inputs()
        graph_outputs = self.set_onnx_outputs()
        graph = helper.make_graph(
            [node],
            self.name,
            graph_inputs,  # graph inputs
            graph_outputs,  # graph outputs
        )
        opset_imports = [helper.make_opsetid("", ver)]
        model = helper.make_model(
            graph, producer_name='onnx-example', opset_imports=opset_imports)
W
wjj19950828 已提交
310
        model = onnx.shape_inference.infer_shapes(model)
W
wjj19950828 已提交
311 312 313
        onnx.save(model,
                  os.path.join(self.pwd, self.name,
                               self.name + '_' + str(ver) + '.onnx'))
W
wjj19950828 已提交
314 315
        if self.enable_onnx_checker:
            onnx.checker.check_model(model)
W
wjj19950828 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329

    def run(self):
        """
        1. make onnx model
        2. convert onnx to paddle
        3. use onnx to make res
        4. compare diff
        """
        self._mkdir()
        for place in self.places:
            paddle.set_device(place)
            onnx_res = {}
            paddle_res = {}
            # export onnx models and make onnx res
W
wjj19950828 已提交
330
            for v in range(self.min_opset_version, self.max_opset_version + 1):
W
wjj19950828 已提交
331 332 333 334 335 336 337 338 339
                self._mk_onnx_graph(ver=v)
                self._onnx_to_paddle(ver=v)
                onnx_res[str(v)] = self._mk_onnx_res(ver=v)
                paddle_res[str(v)] = self._mk_paddle_res(ver=v)
                compare(
                    onnx_res[str(v)],
                    paddle_res[str(v)],
                    delta=self.delta,
                    rtol=self.rtol)