message: Remove redundant quantize and dequantize ops pairs
diff: @@ -568,6 +568,16 @@ def get_quantize_opcode_idx(model):
  return quant_opcode_idxs
  +def get_dequantize_opcode_idx(model):
  + """Returns the quantize op idx."""
  + quant_opcode_idxs = []
  + for idx, opcode in enumerate(model.operatorCodes):
  + builtin_code = schema_util.get_builtin_code_from_operator_code(opcode)
  + if builtin_code == schema_fb.BuiltinOperator.DEQUANTIZE:
  + quant_opcode_idxs.append(idx)
  + return quant_opcode_idxs
  +
  +
  def _remove_tensors_from_model(model, remove_tensors_idxs):
  """Remove tensors from model."""
  if not remove_tensors_idxs:
  @@ -711,11 +721,7 @@ def _modify_model_output_type(model, inference_output_type=dtypes.float32):
  operators = subgraph.operators
  # Find all dequantize operators
  - dequant_opcode_idxs = []
  - for idx, opcode in enumerate(model.operatorCodes):
  - builtin_code = schema_util.get_builtin_code_from_operator_code(opcode)
  - if builtin_code == schema_fb.BuiltinOperator.DEQUANTIZE:
  - dequant_opcode_idxs.append(idx)
  + dequant_opcode_idxs = get_dequantize_opcode_idx(model)
  if operators and not dequant_opcode_idxs:
  for output in subgraph.outputs:
  output_type = _convert_tflite_enum_type_to_tf_type(tensors[output].type)
  @@ -817,10 +823,12 @@ def _remove_redundant_quantize_ops(model):
  # Find all quantize operators.
  quant_opcode_idxs = get_quantize_opcode_idx(model)
  + dequant_opcode_idxs = get_dequantize_opcode_idx(model)
  # Find all redundant quant tensors.
  - redundant_quant_tensors = {}
  all_quant_ops = []
  + redundant_quant_tensors = {}
  + output_dequant_tensors = {}
  for op in operators:
  if op.opcodeIndex in quant_opcode_idxs:
  all_quant_ops.append(op)
  @@ -831,10 +839,12 @@ def _remove_redundant_quantize_ops(model):
  # This is a requantize op, so write down its input tensor index.
  if input_type != dtypes.float32 and output_type != dtypes.float32:
  redundant_quant_tensors[op.inputs[0]] = op
  + if op.opcodeIndex in dequant_opcode_idxs and \
  + op.outputs[0] in subgraph.outputs:
  + output_dequant_tensors[op.inputs[0]] = op
  # Remove all the quant ops which produce the redundant quant tensors.
  for op in all_quant_ops:
  - if op.opcodeIndex in quant_opcode_idxs:
  output_tensor_idx = op.outputs[0]
  if output_tensor_idx in redundant_quant_tensors:
  requantize_op = redundant_quant_tensors[output_tensor_idx]
  @@ -842,6 +852,15 @@ def _remove_redundant_quantize_ops(model):
  requantize_op.inputs[0] = op.inputs[0]
  operators.remove(op)
  + # Remove all the quant ops which connect to the output dequant op.
  + for op in all_quant_ops:
  + output_tensor_idx = op.outputs[0]
  + if output_tensor_idx in output_dequant_tensors:
  + dequant_op = output_dequant_tensors[output_tensor_idx]
  + subgraph.outputs[subgraph.outputs == dequant_op.outputs[0]] = op.inputs[0]
  + operators.remove(op)
  + operators.remove(dequant_op)
  +
  def modify_model_io_type(
  model, inference_input_type=dtypes.float32,
  