diff --git a/python/tvm/relay/frontend/tflite.py b/python/tvm/relay/frontend/tflite.py index d489bd34f7ac..a2e090408e92 100644 --- a/python/tvm/relay/frontend/tflite.py +++ b/python/tvm/relay/frontend/tflite.py @@ -159,7 +159,12 @@ def convert_op_to_relay(self): op = self.subgraph.Operators(op_idx) op_code_str = self.get_op_code_str(op) output_tensors = self.get_output_tensors(op) + try: + from tflite.Operator import Operator + except ImportError: + raise ImportError("The tflite package must be installed") + assert isinstance(op, Operator) ret = self.convert_map[op_code_str](op) if len(output_tensors) == 1: @@ -288,12 +293,6 @@ def has_same_qnn_params(self, lhs_tensor, rhs_tensor): def is_quantized(self, op): """Check if an input tensor is quantized.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) first_tensor = input_tensors[0] return first_tensor.qnn_params is not None @@ -335,12 +334,10 @@ def convert_reshape(self, op): """Convert TFLite reshape""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.ReshapeOptions import ReshapeOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert input_tensors, "input tensors should not be empty" input_tensor = input_tensors[0] @@ -368,7 +365,6 @@ def _convert_resize(self, method, op): """Generic method to Convert TFLite RESIZE operators""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.ResizeBilinearOptions import ResizeBilinearOptions # ResizeNearestNeighborOptions was added in tflite v1.13 tflite_ver = 1120 @@ -378,7 +374,6 @@ def _convert_resize(self, method, op): except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -421,14 +416,12 @@ def convert_resize_nearest_neighbor(self, op): def convert_l2_normalization(self, op): """Convert TFLite L2_NORMALIZATION """ try: - from tflite.Operator import Operator from tflite.BuiltinOptions import BuiltinOptions from tflite.L2NormOptions import L2NormOptions from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -467,13 +460,11 @@ def convert_l2_normalization(self, op): def convert_lrn(self, op): """Convert TFLite LOCAL_RESPONSE_NORMALIZATION """ try: - from tflite.Operator import Operator from tflite.BuiltinOptions import BuiltinOptions from tflite.LocalResponseNormalizationOptions import LocalResponseNormalizationOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) if self.is_quantized(op): raise tvm.error.OpNotImplemented( 'TFlite quantized LRN operator is not supported yet.') @@ -503,12 +494,6 @@ def convert_lrn(self, op): def convert_logistic(self, op): """Convert TFLite LOGISTIC""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -529,12 +514,6 @@ def convert_logistic(self, op): def convert_softmax(self, op): """Convert TFLite softmax""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -564,12 +543,6 @@ def convert_softmax(self, op): def convert_tanh(self, op): """Convert TFLite TANH""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -581,12 +554,6 @@ def convert_tanh(self, op): def convert_relu(self, op): """Convert TFLite ReLU""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -598,12 +565,6 @@ def convert_relu(self, op): def convert_hard_swish(self, op): """Convert TFLite Hard swish""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) - input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -635,14 +596,12 @@ def _hard_swish(data): def convert_concatenation(self, op): """Convert TFLite concatenation""" try: - from tflite.Operator import Operator from tflite.ConcatenationOptions import ConcatenationOptions from tflite.BuiltinOptions import BuiltinOptions from tflite.ActivationFunctionType import ActivationFunctionType except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 1, "input tensors should greater than 1" in_exprs = [self.get_expr(input_tensor.tensor_idx) for input_tensor in input_tensors] @@ -683,12 +642,6 @@ def convert_concatenation(self, op): def _convert_unary_elemwise(self, relay_op, op): """Generic method to convert TFLite unary elemwise functions""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -784,12 +737,6 @@ def convert_neg(self, op): def convert_elu(self, op): """Convert TFLite ELU""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) - if self.is_quantized(op): raise tvm.error.OpNotImplemented( 'TFlite quantized ELU operator is not supported yet.') @@ -807,12 +754,6 @@ def convert_elu(self, op): def convert_square(self, op): """Convert TFLite SQUARE""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -834,7 +775,6 @@ def convert_square(self, op): def _convert_elemwise(self, relay_op, op): """Generic method to Convert TFLite elemwise""" try: - from tflite.Operator import Operator from tflite.AddOptions import AddOptions from tflite.SubOptions import SubOptions from tflite.MulOptions import MulOptions @@ -844,7 +784,6 @@ def _convert_elemwise(self, relay_op, op): except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1025,12 +964,6 @@ def convert_not_equal(self, op): def _convert_logical_binary(self, relay_op, op): """Generic method to convert logical binary ops""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1052,12 +985,6 @@ def convert_logical_or(self, op): def convert_zeros_like(self, op): """Convert TFLite ZEROS LIKE""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -1071,12 +998,10 @@ def _convert_reduce(self, relay_op, op): """Generic method to Convert TFLite MEAN operators""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.ReducerOptions import ReducerOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1135,7 +1060,6 @@ def _convert_reduce_any(self, op): def convert_fully_connected(self, op): """Convert TFLite fully connected""" try: - from tflite.Operator import Operator from tflite.FullyConnectedOptions import FullyConnectedOptions from tflite.BuiltinOptions import BuiltinOptions from tflite.TensorType import TensorType @@ -1143,7 +1067,6 @@ def convert_fully_connected(self, op): except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 2, "input tensors length should be >= 2" @@ -1238,12 +1161,10 @@ def convert_squeeze(self, op): """Convert TFLite squeeze""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.SqueezeOptions import SqueezeOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) output_tensors = self.get_output_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -1287,14 +1208,12 @@ def convert_conv(self, op, conv_type): from tflite.BuiltinOptions import BuiltinOptions from tflite.ActivationFunctionType import ActivationFunctionType from tflite.TensorType import TensorType - from tflite.Operator import Operator from tflite.Conv2DOptions import Conv2DOptions from tflite.DepthwiseConv2DOptions import DepthwiseConv2DOptions from tflite.Padding import Padding except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 2, "input tensors length should be >= 2" @@ -1455,12 +1374,10 @@ def convert_split(self, op): """split implementation.""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.SplitOptions import SplitOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be == 2" @@ -1490,12 +1407,6 @@ def convert_split(self, op): def convert_slice(self, op): """Convert TFLite SLICE""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be == 3" input_tensor = input_tensors[0] @@ -1519,12 +1430,6 @@ def convert_slice(self, op): def convert_transpose(self, op): """transpose implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" input_tensor = input_tensors[0] @@ -1545,13 +1450,11 @@ def convert_transpose(self, op): def convert_cast(self, op): """Convert TFLite CAST""" try: - from tflite.Operator import Operator from tflite.BuiltinOptions import BuiltinOptions from tflite.CastOptions import CastOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -1569,12 +1472,6 @@ def convert_cast(self, op): def convert_tile(self, op): """tile implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" input_tensor = input_tensors[0] @@ -1591,12 +1488,6 @@ def convert_tile(self, op): def convert_topk_v2(self, op): """ Convert TFLite TOPK_v2 """ - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" input_tensor = input_tensors[0] @@ -1612,13 +1503,11 @@ def convert_pool2d(self, op, pool_type): try: from tflite.BuiltinOptions import BuiltinOptions from tflite.ActivationFunctionType import ActivationFunctionType - from tflite.Operator import Operator from tflite.Pool2DOptions import Pool2DOptions from tflite.Padding import Padding except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -1689,12 +1578,6 @@ def convert_pool2d(self, op, pool_type): def convert_pad(self, op): """Convert TFLite PAD""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1740,7 +1623,6 @@ def convert_floor_mod(self, op): def convert_mirror_pad(self, op): """Convert TFLite MIRROR_PAD""" try: - from tflite.Operator import Operator from tflite.BuiltinOptions import BuiltinOptions from tflite.MirrorPadOptions import MirrorPadOptions except ImportError: @@ -1751,7 +1633,6 @@ def convert_mirror_pad(self, op): raise tvm.error.OpNotImplemented( 'TFlite quantized MIRROR_PAD operator is not supported yet.') - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -1779,12 +1660,10 @@ def convert_pack(self, op): """Convert TFLite pack""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.PackOptions import PackOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) >= 1, "input tensors should greater than 1" in_exprs = [self.get_expr(input_tensor.tensor_idx) for input_tensor in input_tensors] @@ -1806,12 +1685,10 @@ def convert_unpack(self, op): """Convert TFLite unpack""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.UnpackOptions import UnpackOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" input_tensor = input_tensors[0] @@ -1848,12 +1725,7 @@ def convert_unpack(self, op): def convert_batch_to_space_nd(self, op): """batch_to_space_nd implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be 3" @@ -1901,12 +1773,6 @@ def convert_batch_to_space_nd(self, op): def convert_space_to_batch_nd(self, op): """space_to_batch_nd implementation.""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be 3" @@ -1960,12 +1826,10 @@ def convert_depth_to_space(self, op): """Convert TFLite DEPTH_TO_SPACE""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.DepthToSpaceOptions import DepthToSpaceOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -1985,12 +1849,10 @@ def convert_space_to_depth(self, op): """Convert TFLite SPACE_TO_DEPTH""" try: from tflite.BuiltinOptions import BuiltinOptions - from tflite.Operator import Operator from tflite.SpaceToDepthOptions import SpaceToDepthOptions except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 1, "input tensors length should be 1" @@ -2008,12 +1870,6 @@ def convert_space_to_depth(self, op): def convert_prelu(self, op): """Convert TFLite PReLU""" - try: - from tflite.Operator import Operator - except ImportError: - raise ImportError("The tflite package must be installed") - - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 2, "input tensors length should be 2" @@ -2033,13 +1889,11 @@ def convert_transpose_conv(self, op): try: from tflite.BuiltinOptions import BuiltinOptions from tflite.TensorType import TensorType - from tflite.Operator import Operator from tflite.TransposeConvOptions import TransposeConvOptions from tflite.Padding import Padding except ImportError: raise ImportError("The tflite package must be installed") - assert isinstance(op, Operator) input_tensors = self.get_input_tensors(op) assert len(input_tensors) == 3, "input tensors length should be 3"