diff --git a/qa/common/gen_common.py b/qa/common/gen_common.py new file mode 100644 index 0000000000..8bd97720b5 --- /dev/null +++ b/qa/common/gen_common.py @@ -0,0 +1,158 @@ +# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of NVIDIA CORPORATION nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# Common utilities for model generation scripts +import numpy as np + +np_dtype_string = np.dtype(object) + + +def np_to_onnx_dtype(np_dtype): + import onnx + + if np_dtype == bool: + return onnx.TensorProto.BOOL + elif np_dtype == np.int8: + return onnx.TensorProto.INT8 + elif np_dtype == np.int16: + return onnx.TensorProto.INT16 + elif np_dtype == np.int32: + return onnx.TensorProto.INT32 + elif np_dtype == np.int64: + return onnx.TensorProto.INT64 + elif np_dtype == np.uint8: + return onnx.TensorProto.UINT8 + elif np_dtype == np.uint16: + return onnx.TensorProto.UINT16 + elif np_dtype == np.float16: + return onnx.TensorProto.FLOAT16 + elif np_dtype == np.float32: + return onnx.TensorProto.FLOAT + elif np_dtype == np.float64: + return onnx.TensorProto.DOUBLE + elif np_dtype == np_dtype_string: + return onnx.TensorProto.STRING + return None + + +def np_to_model_dtype(np_dtype): + if np_dtype == bool: + return "TYPE_BOOL" + elif np_dtype == np.int8: + return "TYPE_INT8" + elif np_dtype == np.int16: + return "TYPE_INT16" + elif np_dtype == np.int32: + return "TYPE_INT32" + elif np_dtype == np.int64: + return "TYPE_INT64" + elif np_dtype == np.uint8: + return "TYPE_UINT8" + elif np_dtype == np.uint16: + return "TYPE_UINT16" + elif np_dtype == np.float16: + return "TYPE_FP16" + elif np_dtype == np.float32: + return "TYPE_FP32" + elif np_dtype == np.float64: + return "TYPE_FP64" + elif np_dtype == np_dtype_string: + return "TYPE_STRING" + return None + + +def np_to_trt_dtype(np_dtype): + import tensorrt as trt + + if np_dtype == bool: + return trt.bool + elif np_dtype == np.int8: + return trt.int8 + elif np_dtype == np.int32: + return trt.int32 + elif np_dtype == np.uint8: + return trt.uint8 + elif np_dtype == np.float16: + return trt.float16 + elif np_dtype == np.float32: + return trt.float32 + return None + + +def np_to_tf_dtype(np_dtype): + import tensorflow as tf + + if np_dtype == bool: + return tf.bool + elif np_dtype == np.int8: + return tf.int8 + elif np_dtype == np.int16: + return tf.int16 + elif np_dtype == np.int32: + return tf.int32 + elif np_dtype == np.int64: + return tf.int64 + elif np_dtype == np.uint8: + return tf.uint8 + elif np_dtype == np.uint16: + return tf.uint16 + elif np_dtype == np.float16: + return tf.float16 + elif np_dtype == np.float32: + return tf.float32 + elif np_dtype == np.float64: + return tf.float64 + elif np_dtype == np_dtype_string: + return tf.string + return None + + +def np_to_torch_dtype(np_dtype): + import torch + + if np_dtype == bool: + return torch.bool + elif np_dtype == np.int8: + return torch.int8 + elif np_dtype == np.int16: + return torch.int16 + elif np_dtype == np.int32: + return torch.int + elif np_dtype == np.int64: + return torch.long + elif np_dtype == np.uint8: + return torch.uint8 + elif np_dtype == np.uint16: + return None # Not supported in Torch + elif np_dtype == np.float16: + return None + elif np_dtype == np.float32: + return torch.float + elif np_dtype == np.float64: + return torch.double + elif np_dtype == np_dtype_string: + return None # Not supported in Torch + return None diff --git a/qa/common/gen_ensemble_model_utils.py b/qa/common/gen_ensemble_model_utils.py index ceac0340dc..dd4f6e326a 100755 --- a/qa/common/gen_ensemble_model_utils.py +++ b/qa/common/gen_ensemble_model_utils.py @@ -30,38 +30,13 @@ import numpy as np import test_util as tu +from gen_common import np_to_model_dtype BASIC_ENSEMBLE_TYPES = ["simple", "sequence", "fan"] np_dtype_string = np.dtype(object) -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - def fixed_to_variable_size(shape): return [-1] * len(shape) diff --git a/qa/common/gen_qa_dyna_sequence_implicit_models.py b/qa/common/gen_qa_dyna_sequence_implicit_models.py index 1c4815d5dd..ffa3f48ede 100755 --- a/qa/common/gen_qa_dyna_sequence_implicit_models.py +++ b/qa/common/gen_qa_dyna_sequence_implicit_models.py @@ -30,76 +30,12 @@ import os import numpy as np +from gen_common import np_to_model_dtype, np_to_onnx_dtype, np_to_trt_dtype FLAGS = None np_dtype_string = np.dtype(object) -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - - -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - def create_onnx_modelfile(models_dir, model_version, max_batch, dtype, shape): if not tu.validate_for_onnx_model(dtype, dtype, dtype, shape, shape, shape): return diff --git a/qa/common/gen_qa_dyna_sequence_models.py b/qa/common/gen_qa_dyna_sequence_models.py index 8a02497bfa..469d524ffb 100755 --- a/qa/common/gen_qa_dyna_sequence_models.py +++ b/qa/common/gen_qa_dyna_sequence_models.py @@ -30,128 +30,18 @@ import os import numpy as np +from gen_common import ( + np_to_model_dtype, + np_to_onnx_dtype, + np_to_tf_dtype, + np_to_torch_dtype, + np_to_trt_dtype, +) FLAGS = None np_dtype_string = np.dtype(object) -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_tf_dtype(np_dtype): - if np_dtype == bool: - return tf.bool - elif np_dtype == np.int8: - return tf.int8 - elif np_dtype == np.int16: - return tf.int16 - elif np_dtype == np.int32: - return tf.int32 - elif np_dtype == np.int64: - return tf.int64 - elif np_dtype == np.uint8: - return tf.uint8 - elif np_dtype == np.uint16: - return tf.uint16 - elif np_dtype == np.float16: - return tf.float16 - elif np_dtype == np.float32: - return tf.float32 - elif np_dtype == np.float64: - return tf.float64 - elif np_dtype == np_dtype_string: - return tf.string - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - - -def np_to_torch_dtype(np_dtype): - if np_dtype == bool: - return torch.bool - elif np_dtype == np.int8: - return torch.int8 - elif np_dtype == np.int16: - return torch.int16 - elif np_dtype == np.int32: - return torch.int - elif np_dtype == np.int64: - return torch.long - elif np_dtype == np.uint8: - return torch.uint8 - elif np_dtype == np.uint16: - return None # Not supported in Torch - elif np_dtype == np.float16: - return None - elif np_dtype == np.float32: - return torch.float - elif np_dtype == np.float64: - return torch.double - elif np_dtype == np_dtype_string: - return None # Not supported in Torch - return None - - def create_tf_modelfile( create_savedmodel, models_dir, model_version, max_batch, dtype, shape ): diff --git a/qa/common/gen_qa_identity_models.py b/qa/common/gen_qa_identity_models.py index 0aaac8d9e4..60b045a09c 100755 --- a/qa/common/gen_qa_identity_models.py +++ b/qa/common/gen_qa_identity_models.py @@ -32,129 +32,18 @@ import gen_ensemble_model_utils as emu import numpy as np +from gen_common import ( + np_to_model_dtype, + np_to_onnx_dtype, + np_to_tf_dtype, + np_to_trt_dtype, +) FLAGS = None np_dtype_string = np.dtype(object) from typing import List, Tuple -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_tf_dtype(np_dtype): - if np_dtype == bool: - return tf.bool - elif np_dtype == np.int8: - return tf.int8 - elif np_dtype == np.int16: - return tf.int16 - elif np_dtype == np.int32: - return tf.int32 - elif np_dtype == np.int64: - return tf.int64 - elif np_dtype == np.uint8: - return tf.uint8 - elif np_dtype == np.uint16: - return tf.uint16 - elif np_dtype == np.float16: - return tf.float16 - elif np_dtype == np.float32: - return tf.float32 - elif np_dtype == np.float64: - return tf.float64 - elif np_dtype == np_dtype_string: - return tf.string - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - return None - - -def np_to_torch_dtype(np_dtype): - if np_dtype == bool: - return torch.bool - elif np_dtype == np.int8: - return torch.int8 - elif np_dtype == np.int16: - return torch.int16 - elif np_dtype == np.int32: - return torch.int - elif np_dtype == np.int64: - return torch.long - elif np_dtype == np.uint8: - return torch.uint8 - elif np_dtype == np.uint16: - return None # Not supported in Torch - elif np_dtype == np.float16: - return None - elif np_dtype == np.float32: - return torch.float - elif np_dtype == np.float64: - return torch.double - elif np_dtype == np_dtype_string: - return List[str] - - def create_tf_modelfile( create_savedmodel, models_dir, model_version, io_cnt, max_batch, dtype, shape ): diff --git a/qa/common/gen_qa_implicit_models.py b/qa/common/gen_qa_implicit_models.py index 84ca98c47c..89872c3b92 100755 --- a/qa/common/gen_qa_implicit_models.py +++ b/qa/common/gen_qa_implicit_models.py @@ -32,102 +32,17 @@ import gen_ensemble_model_utils as emu import numpy as np +from gen_common import ( + np_to_model_dtype, + np_to_onnx_dtype, + np_to_torch_dtype, + np_to_trt_dtype, +) FLAGS = None np_dtype_string = np.dtype(object) -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - -def np_to_torch_dtype(np_dtype): - if np_dtype == bool: - return torch.bool - elif np_dtype == np.int8: - return torch.int8 - elif np_dtype == np.int16: - return torch.int16 - elif np_dtype == np.int32: - return torch.int - elif np_dtype == np.int64: - return torch.long - elif np_dtype == np.uint8: - return torch.uint8 - elif np_dtype == np.uint16: - return None # Not supported in Torch - elif np_dtype == np.float16: - return None - elif np_dtype == np.float32: - return torch.float - elif np_dtype == np.float64: - return torch.double - elif np_dtype == np_dtype_string: - return List[str] - return None - - def create_onnx_modelfile_wo_initial_state( models_dir, model_version, max_batch, dtype, shape ): diff --git a/qa/common/gen_qa_model_repository b/qa/common/gen_qa_model_repository index 85db0730b2..55bef11c46 100755 --- a/qa/common/gen_qa_model_repository +++ b/qa/common/gen_qa_model_repository @@ -154,6 +154,7 @@ cp ./gen_qa_ragged_models.py $HOST_SRCDIR/. cp ./test_util.py $HOST_SRCDIR/. cp ./gen_tag_sigdef.py $HOST_SRCDIR/. cp ./gen_qa_tf_parameters.py $HOST_SRCDIR/. +cp ./gen_common.py $HOST_SRCDIR/. ONNXSCRIPT=onnx_gen.cmds OPENVINOSCRIPT=openvino_gen.cmds diff --git a/qa/common/gen_qa_models.py b/qa/common/gen_qa_models.py index f050861a62..82d241f470 100755 --- a/qa/common/gen_qa_models.py +++ b/qa/common/gen_qa_models.py @@ -32,131 +32,19 @@ import gen_ensemble_model_utils as emu import numpy as np +from gen_common import ( + np_to_model_dtype, + np_to_onnx_dtype, + np_to_tf_dtype, + np_to_torch_dtype, + np_to_trt_dtype, +) FLAGS = None np_dtype_string = np.dtype(object) from typing import List, Tuple -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_tf_dtype(np_dtype): - if np_dtype == bool: - return tf.bool - elif np_dtype == np.int8: - return tf.int8 - elif np_dtype == np.int16: - return tf.int16 - elif np_dtype == np.int32: - return tf.int32 - elif np_dtype == np.int64: - return tf.int64 - elif np_dtype == np.uint8: - return tf.uint8 - elif np_dtype == np.uint16: - return tf.uint16 - elif np_dtype == np.float16: - return tf.float16 - elif np_dtype == np.float32: - return tf.float32 - elif np_dtype == np.float64: - return tf.float64 - elif np_dtype == np_dtype_string: - return tf.string - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.uint8: - return trt.uint8 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - return None - - -def np_to_torch_dtype(np_dtype): - if np_dtype == bool: - return torch.bool - elif np_dtype == np.int8: - return torch.int8 - elif np_dtype == np.int16: - return torch.int16 - elif np_dtype == np.int32: - return torch.int - elif np_dtype == np.int64: - return torch.long - elif np_dtype == np.uint8: - return torch.uint8 - elif np_dtype == np.uint16: - return None # Not supported in Torch - elif np_dtype == np.float16: - return None - elif np_dtype == np.float32: - return torch.float - elif np_dtype == np.float64: - return torch.double - elif np_dtype == np_dtype_string: - return List[str] - - def create_graphdef_modelfile( models_dir, max_batch, diff --git a/qa/common/gen_qa_noshape_models.py b/qa/common/gen_qa_noshape_models.py index b37f1c1bbb..af26017495 100755 --- a/qa/common/gen_qa_noshape_models.py +++ b/qa/common/gen_qa_noshape_models.py @@ -31,63 +31,12 @@ from builtins import range import numpy as np +from gen_common import np_to_model_dtype, np_to_tf_dtype FLAGS = None np_dtype_string = np.dtype(object) -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_tf_dtype(np_dtype): - if np_dtype == bool: - return tf.bool - elif np_dtype == np.int8: - return tf.int8 - elif np_dtype == np.int16: - return tf.int16 - elif np_dtype == np.int32: - return tf.int32 - elif np_dtype == np.int64: - return tf.int64 - elif np_dtype == np.uint8: - return tf.uint8 - elif np_dtype == np.uint16: - return tf.uint16 - elif np_dtype == np.float16: - return tf.float16 - elif np_dtype == np.float32: - return tf.float32 - elif np_dtype == np.float64: - return tf.float64 - elif np_dtype == np_dtype_string: - return tf.string - return None - - def create_savedmodel_modelfile( models_dir, max_batch, diff --git a/qa/common/gen_qa_ragged_models.py b/qa/common/gen_qa_ragged_models.py index 0ac70e80f6..18d465dc94 100755 --- a/qa/common/gen_qa_ragged_models.py +++ b/qa/common/gen_qa_ragged_models.py @@ -30,102 +30,16 @@ import os import numpy as np +from gen_common import ( + np_to_model_dtype, + np_to_onnx_dtype, + np_to_tf_dtype, + np_to_trt_dtype, +) np_dtype_string = np.dtype(object) -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - -def np_to_tf_dtype(np_dtype): - if np_dtype == bool: - return tf.bool - elif np_dtype == np.int8: - return tf.int8 - elif np_dtype == np.int16: - return tf.int16 - elif np_dtype == np.int32: - return tf.int32 - elif np_dtype == np.int64: - return tf.int64 - elif np_dtype == np.uint8: - return tf.uint8 - elif np_dtype == np.uint16: - return tf.uint16 - elif np_dtype == np.float16: - return tf.float16 - elif np_dtype == np.float32: - return tf.float32 - elif np_dtype == np.float64: - return tf.float64 - elif np_dtype == np_dtype_string: - return tf.string - return None - - -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - return None - - def create_savedmodel_modelfile(models_dir, model_version, dtype): # Create special identity model for batch input testing. # Because the ragged input and batch input are one dimensional vector diff --git a/qa/common/gen_qa_reshape_models.py b/qa/common/gen_qa_reshape_models.py index 983f06c1d0..4ac5347a79 100755 --- a/qa/common/gen_qa_reshape_models.py +++ b/qa/common/gen_qa_reshape_models.py @@ -32,130 +32,19 @@ import gen_ensemble_model_utils as emu import numpy as np +from gen_common import ( + np_to_model_dtype, + np_to_onnx_dtype, + np_to_tf_dtype, + np_to_torch_dtype, + np_to_trt_dtype, +) FLAGS = None np_dtype_string = np.dtype(object) from typing import List -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_tf_dtype(np_dtype): - if np_dtype == bool: - return tf.bool - elif np_dtype == np.int8: - return tf.int8 - elif np_dtype == np.int16: - return tf.int16 - elif np_dtype == np.int32: - return tf.int32 - elif np_dtype == np.int64: - return tf.int64 - elif np_dtype == np.uint8: - return tf.uint8 - elif np_dtype == np.uint16: - return tf.uint16 - elif np_dtype == np.float16: - return tf.float16 - elif np_dtype == np.float32: - return tf.float32 - elif np_dtype == np.float64: - return tf.float64 - elif np_dtype == np_dtype_string: - return tf.string - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - return None - - -def np_to_torch_dtype(np_dtype): - if np_dtype == bool: - return torch.bool - elif np_dtype == np.int8: - return torch.int8 - elif np_dtype == np.int16: - return torch.int16 - elif np_dtype == np.int32: - return torch.int - elif np_dtype == np.int64: - return torch.long - elif np_dtype == np.uint8: - return torch.uint8 - elif np_dtype == np.uint16: - return None # Not supported in Torch - elif np_dtype == np.float16: - return None - elif np_dtype == np.float32: - return torch.float - elif np_dtype == np.float64: - return torch.double - elif np_dtype == np_dtype_string: - return List[str] - return None - - def create_tf_modelfile( create_savedmodel, models_dir, diff --git a/qa/common/gen_qa_sequence_models.py b/qa/common/gen_qa_sequence_models.py index 01cd515c15..4c9ca9d8e5 100755 --- a/qa/common/gen_qa_sequence_models.py +++ b/qa/common/gen_qa_sequence_models.py @@ -31,128 +31,18 @@ import gen_ensemble_model_utils as emu import numpy as np +from gen_common import ( + np_to_model_dtype, + np_to_onnx_dtype, + np_to_tf_dtype, + np_to_torch_dtype, + np_to_trt_dtype, +) FLAGS = None np_dtype_string = np.dtype(object) -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_tf_dtype(np_dtype): - if np_dtype == bool: - return tf.bool - elif np_dtype == np.int8: - return tf.int8 - elif np_dtype == np.int16: - return tf.int16 - elif np_dtype == np.int32: - return tf.int32 - elif np_dtype == np.int64: - return tf.int64 - elif np_dtype == np.uint8: - return tf.uint8 - elif np_dtype == np.uint16: - return tf.uint16 - elif np_dtype == np.float16: - return tf.float16 - elif np_dtype == np.float32: - return tf.float32 - elif np_dtype == np.float64: - return tf.float64 - elif np_dtype == np_dtype_string: - return tf.string - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - -def np_to_onnx_dtype(np_dtype): - if np_dtype == bool: - return onnx.TensorProto.BOOL - elif np_dtype == np.int8: - return onnx.TensorProto.INT8 - elif np_dtype == np.int16: - return onnx.TensorProto.INT16 - elif np_dtype == np.int32: - return onnx.TensorProto.INT32 - elif np_dtype == np.int64: - return onnx.TensorProto.INT64 - elif np_dtype == np.uint8: - return onnx.TensorProto.UINT8 - elif np_dtype == np.uint16: - return onnx.TensorProto.UINT16 - elif np_dtype == np.float16: - return onnx.TensorProto.FLOAT16 - elif np_dtype == np.float32: - return onnx.TensorProto.FLOAT - elif np_dtype == np.float64: - return onnx.TensorProto.DOUBLE - elif np_dtype == np_dtype_string: - return onnx.TensorProto.STRING - - -def np_to_torch_dtype(np_dtype): - if np_dtype == bool: - return torch.bool - elif np_dtype == np.int8: - return torch.int8 - elif np_dtype == np.int16: - return torch.int16 - elif np_dtype == np.int32: - return torch.int - elif np_dtype == np.int64: - return torch.long - elif np_dtype == np.uint8: - return torch.uint8 - elif np_dtype == np.uint16: - return None # Not supported in Torch - elif np_dtype == np.float16: - return None - elif np_dtype == np.float32: - return torch.float - elif np_dtype == np.float64: - return torch.double - elif np_dtype == np_dtype_string: - return None # Not supported in Torch - return None - - def create_tf_modelfile( create_savedmodel, models_dir, model_version, max_batch, dtype, shape ): diff --git a/qa/common/gen_qa_trt_data_dependent_shape.py b/qa/common/gen_qa_trt_data_dependent_shape.py index 1b40455fd6..c6600ed919 100755 --- a/qa/common/gen_qa_trt_data_dependent_shape.py +++ b/qa/common/gen_qa_trt_data_dependent_shape.py @@ -32,46 +32,7 @@ import numpy as np import tensorrt as trt import test_util as tu - - -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None +from gen_common import np_to_model_dtype, np_to_trt_dtype # The 'nonzero' model that we use for data dependent shape is naturally diff --git a/qa/common/gen_qa_trt_format_models.py b/qa/common/gen_qa_trt_format_models.py index 9502cdb972..e077139aec 100755 --- a/qa/common/gen_qa_trt_format_models.py +++ b/qa/common/gen_qa_trt_format_models.py @@ -32,50 +32,11 @@ import numpy as np import tensorrt as trt import test_util as tu +from gen_common import np_to_model_dtype, np_to_trt_dtype np_dtype_string = np.dtype(object) -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - def trt_format_to_string(trt_format): # FIXME uncomment the following formats once TRT used is up-to-date # if trt_format == trt.TensorFormat.CDHW32: diff --git a/qa/common/gen_qa_trt_plugin_models.py b/qa/common/gen_qa_trt_plugin_models.py index ce6b309f3d..10c9d5284a 100755 --- a/qa/common/gen_qa_trt_plugin_models.py +++ b/qa/common/gen_qa_trt_plugin_models.py @@ -31,6 +31,7 @@ import numpy as np import tensorrt as trt +from gen_common import np_to_model_dtype, np_to_trt_dtype np_dtype_string = np.dtype(object) @@ -40,46 +41,6 @@ PLUGIN_CREATORS = trt.get_plugin_registry().plugin_creator_list -def np_to_model_dtype(np_dtype): - if np_dtype == bool: - return "TYPE_BOOL" - elif np_dtype == np.int8: - return "TYPE_INT8" - elif np_dtype == np.int16: - return "TYPE_INT16" - elif np_dtype == np.int32: - return "TYPE_INT32" - elif np_dtype == np.int64: - return "TYPE_INT64" - elif np_dtype == np.uint8: - return "TYPE_UINT8" - elif np_dtype == np.uint16: - return "TYPE_UINT16" - elif np_dtype == np.float16: - return "TYPE_FP16" - elif np_dtype == np.float32: - return "TYPE_FP32" - elif np_dtype == np.float64: - return "TYPE_FP64" - elif np_dtype == np_dtype_string: - return "TYPE_STRING" - return None - - -def np_to_trt_dtype(np_dtype): - if np_dtype == bool: - return trt.bool - elif np_dtype == np.int8: - return trt.int8 - elif np_dtype == np.int32: - return trt.int32 - elif np_dtype == np.float16: - return trt.float16 - elif np_dtype == np.float32: - return trt.float32 - return None - - def get_trt_plugin(plugin_name): plugin = None field_collection = None