code: def convert_keras_model(
      model: tf.keras.Model,
      *,  # Require remaining arguments to be keyword-only.
      inference_input_type: tf.DType = tf.float32,
      inference_output_type: tf.DType = tf.float32,
      target: str = "arm",
      experimental_default_int8_range: Optional[
          tuple[float, float]
      ] = None,
  ) -> bytes:
      """Converts a Keras model to TFLite flatbuffer.
  
      !!! example
          ```python
          tflite_model = convert_keras_model(model)
          with open("/tmp/my_model.tflite", "wb") as f:
              f.write(tflite_model)
          ```
  
      # Arguments
          model: The model to convert.
          inference_input_type: Data type of the input layer. Defaults to `tf.float32`,
              must be either `tf.float32` or `tf.int8`.
          inference_output_type: Data type of the output layer. Defaults to `tf.float32`,
              must be either `tf.float32` or `tf.int8`.
          target: Target hardware platform. Defaults to "arm", must be either "arm"
              or "xcore".
          experimental_default_int8_range: Tuple of integers representing `(min, max)`
              range values for all arrays without a specified range. Intended for
              experimenting with quantization via "dummy quantization". (default None)
  
      # Returns
          The converted data in serialized format.
      """
      if not isinstance(model, tf.keras.Model):
          raise ValueError(
              f"Expected `model` argument to be a `tf.keras.Model` instance, got `{model}`."
          )
      if (
          hasattr(model, "dtype_policy")
          and model.dtype_policy.name != "float32"
      ):
          raise ValueError(
              "Mixed precision float16 models are not supported by the TFLite converter, "
              "please convert them to float32 first. See also: "
              "https://github.com/tensorflow/tensorflow/issues/46380"
          )
      _validate_options(
          inference_input_type=inference_input_type,
          inference_output_type=inference_output_type,
          target=target,
          experimental_default_int8_range=experimental_default_int8_range,
      )
  
      # First attempt conversion as saved model
      try:
          with tempfile.TemporaryDirectory() as saved_model_dir:
              model.save(saved_model_dir, save_format="tf")
  
              return convert_saved_model(
                  saved_model_dir,
                  inference_input_type=inference_input_type,
                  inference_output_type=inference_output_type,
                  experimental_default_int8_range=experimental_default_int8_range,
                  target=target,
              )
      except Exception:
          warnings.warn(
              "Saved-model conversion failed, falling back to graphdef-based conversion."
          )
  
      func = concrete_function_from_keras_model(model)
      frozen_func = convert_variables_to_constants_v2(
          func, lower_control_flow=False
      )
      input_tensors = [
          tensor
          for tensor in frozen_func.inputs
          if tensor.dtype != tf.dtypes.resource
      ]
      output_tensors = frozen_func.outputs
  
      graph_def = frozen_func.graph.as_graph_def()
      should_quantize = (
          _contains_training_quant_op(graph_def)
          or experimental_default_int8_range is not None
      )
  
      # Checks dimensions in input tensor.
      for tensor in input_tensors:
          # Note that shape_list might be empty for scalar shapes.
          shape_list = tensor.shape.as_list()
          if None in shape_list[1:]:
              raise ValueError(
                  "None is only supported in the 1st dimension. Tensor '{0}' has "
                  "invalid shape '{1}'.".format(
                      get_tensor_name(tensor), shape_list
                  )
              )
          elif shape_list and shape_list[0] is None:
              # Set the batch size to 1 if undefined.
              shape = tensor.shape.as_list()
              shape[0] = 1
              tensor.set_shape(shape)
  
      tflite_buffer = convert_graphdef_to_tflite_flatbuffer(
          graph_def.SerializeToString(),
          [get_tensor_name(tensor) for tensor in input_tensors],
          [
              DataType.Name(tensor.dtype.as_datatype_enum)
              for tensor in input_tensors
          ],
          [tensor.shape.as_list() for tensor in input_tensors],
          [get_tensor_name(tensor) for tensor in output_tensors],
          should_quantize,
          target,
          experimental_default_int8_range,
      )
  
      if should_quantize and (
          inference_input_type != tf.float32
          or inference_output_type != tf.float32
      ):
          tflite_buffer = modify_integer_quantized_model_io_type(
              tflite_buffer,
              inference_input_type=inference_input_type,
              inference_output_type=inference_output_type,
          )
  
      return tflite_buffer
  
file_path: compute-engine/larq_compute_engine/mlir/python/converter.py
imports: import os
  import warnings
  from typing import Optional, Union
  import tensorflow
  import tempfile
  from larq_compute_engine.mlir._tf_tfl_flatbuffer import convert_graphdef_to_tflite_flatbuffer, convert_saved_model_to_tflite_flatbuffer
  from larq_compute_engine.mlir.python.util import modify_integer_quantized_model_io_type
  from tensorflow.core.framework.types_pb2 import DataType
  from tensorflow.lite.python.util import get_tensor_name
  from tensorflow.python.eager import def_function
  from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
  from tensorflow.python.keras.saving import saving_utils
label_desc: Use image node_id 5 for calling a global function with example usage: convert_keras_model(model) and returns: tflite_buffer, convert_saved_model