class_docstr: Performs TFLite conversion.
class_docstr_tok: ["Performs","TFLite","conversion","."]
docstr: Rewrites the provided model.
  
  Args:
    original_model: A `ModelDescription` specifying the original model to be
      rewritten.
    rewritten_model: A `ModelDescription` specifying the format and location
      of the rewritten model.
  
  Raises:
    ValueError: If the model could not be sucessfully rewritten.
docstr_tok: ["Rewrites","the","provided","model",".","Args",":","original_model",":","A","`","ModelDescription","`","specifying","the","original","model","to","be","rewritten",".","rewritten_model",":","A","`","ModelDescription","`","specifying","the","format","and","location","of","the","rewritten","model",".","Raises",":","ValueError",":","If","the","model","could","not","be","sucessfully","rewritten","."]
code: def _rewrite(
      self,
      original_model: rewriter.ModelDescription,
      rewritten_model: rewriter.ModelDescription,
  ):
      """Rewrites the provided model.
  
      Args:
        original_model: A `ModelDescription` specifying the original model to be
          rewritten.
        rewritten_model: A `ModelDescription` specifying the format and location
          of the rewritten model.
  
      Raises:
        ValueError: If the model could not be sucessfully rewritten.
      """
      if rewritten_model.model_type not in [
          rewriter.ModelType.TFLITE_MODEL,
          rewriter.ModelType.ANY_MODEL,
      ]:
          raise ValueError(
              "TFLiteConverter can only convert to the TFLite format."
          )
  
      # TODO(dzats): We create a temporary directory with a SavedModel that does
      # not contain an assets or assets.extra directory. Remove this when the
      # TFLite converter can convert models having these directories.
      tmp_model_dir = os.path.join(
          _ensure_str(rewritten_model.path),
          "tmp-rewrite-" + str(int(time.time())),
      )
      if fileio.exists(tmp_model_dir):
          raise ValueError(
              "TFLiteConverter is unable to create a unique path "
              "for the temp rewriting directory."
          )
  
      fileio.makedirs(tmp_model_dir)
      _create_tflite_compatible_saved_model(
          _ensure_str(original_model.path), tmp_model_dir
      )
  
      converter = self._create_tflite_converter(
          saved_model_path=tmp_model_dir,
          quantization_optimizations=self._quantization_optimizations,
          quantization_supported_types=self._quantization_supported_types,
          representative_dataset=self._representative_dataset,
          signature_key=self._signature_key,
          **self._kwargs,
      )
      tflite_model = converter.convert()
  
      output_path = os.path.join(
          _ensure_str(rewritten_model.path), self._filename
      )
      with fileio.open(_ensure_str(output_path), "wb") as f:
          f.write(_ensure_bytes(tflite_model))
      fileio.rmtree(tmp_model_dir)
  
      copy_pairs = []
      if self._copy_assets:
          src = os.path.join(
              _ensure_str(original_model.path),
              tf.saved_model.ASSETS_DIRECTORY,
          )
          dst = os.path.join(
              _ensure_str(rewritten_model.path),
              tf.saved_model.ASSETS_DIRECTORY,
          )
          if fileio.isdir(src):
              fileio.mkdir(dst)
              copy_pairs.append((src, dst))
      if self._copy_assets_extra:
          src = os.path.join(
              _ensure_str(original_model.path), EXTRA_ASSETS_DIRECTORY
          )
          dst = os.path.join(
              _ensure_str(rewritten_model.path), EXTRA_ASSETS_DIRECTORY
          )
          if fileio.isdir(src):
              fileio.mkdir(dst)
              copy_pairs.append((src, dst))
      for src, dst in copy_pairs:
          io_utils.copy_dir(src, dst)
  
file_path: tfx/tfx/components/trainer/rewriting/tflite_rewriter.py
imports: import os
  import time
  from typing import Iterable, Optional, Sequence
  import numpy
  import tensorflow
  from tfx.components.trainer.rewriting import rewriter
  from tfx.dsl.io import fileio
  from tfx.utils import io_utils
label_desc: Use image node_id 4 for calling the TFLiteRewriter obj's underlying member method code with example usage: obj._rewrite(original_model, rewritten_model) without return types