def trace_and_write_v2_saved_model(
    saved_model_dir: str,
    preprocessing_fn: Callable[
        [Mapping[str, common_types.TensorType]],
        Mapping[str, common_types.TensorType],
    ],
    input_signature: Mapping[str, tf.TypeSpec],
    base_temp_dir: Optional[str],
    baseline_analyzers_fingerprint: Mapping[
        str, graph_tools.AnalyzersFingerprint
    ],
    tensor_replacement_map: Optional[Dict[str, tf.Tensor]],
    output_keys_to_name_map: Optional[Dict[str, str]],
    save_options: Optional[tf.saved_model.SaveOptions],
):
  """Writes out a SavedModelV2 with preprocessing_fn traced using tf.function.

  The SavedModel written contains a method called `transform_fn` that
  represents the traced `preprocessing_fn`. Additionally, if this is the final
  SavedModel being written out, it will contain a method called `metadata_fn`
  that provides deferred schema annotations.

  Args:
    saved_model_dir: Path to write SavedModel to.
    preprocessing_fn: A user defined python function to be traced.
    input_signature: TypeSpecs describing the inputs to the `preprocessing_fn`.
    base_temp_dir: Base path to write temporary artifacts to.
    baseline_analyzers_fingerprint: A mapping from analyzer name to a set of
      paths that define its fingerprint.
    tensor_replacement_map: A map from placeholder tensor names to their
      evaluated replacement tensors.
    output_keys_to_name_map: A map from output dictionary keys to the names of
      the tensors that they represent.
    save_options: The options to use when saving the saved_model.

  Returns:
    A tuple containing a pair of `tf.ConcreteFunction`s:
      1. The traced preprocessing_fn.
      2. A metadata_fn that returns a dictionary containing the deferred
      annotations added to the graph when invoked with any valid input.

  Raises:
    RuntimeError: if analyzers in `preprocessing_fn` are encountered in a
    non-deterministic order.
  """
  concrete_transform_fn = _trace_and_write_transform_fn(
      saved_model_dir, preprocessing_fn, input_signature, base_temp_dir,
      tensor_replacement_map, output_keys_to_name_map, save_options)
  structured_inputs = tf2_utils.get_structured_inputs_from_func_graph(
      concrete_transform_fn.graph)
  _validate_analyzers_fingerprint(baseline_analyzers_fingerprint,
                                  concrete_transform_fn.graph,
                                  structured_inputs)

  # If the `TENSOR_REPLACEMENTS` graph collection is empty, all TFT analyzers
  # in the `preprocessing_fn` have already been evaluated.
  if not concrete_transform_fn.graph.get_collection(
      analyzer_nodes.TENSOR_REPLACEMENTS):
    metadata = _trace_and_get_metadata(concrete_transform_fn, structured_inputs,
                                       preprocessing_fn, base_temp_dir,
                                       tensor_replacement_map)
    metadata_io.write_metadata(metadata,
                               os.path.join(saved_model_dir, METADATA_DIR_NAME))
Writes out a SavedModelV2 with preprocessing_fn traced using tf.function.

  The SavedModel written contains a method called `transform_fn` that
  represents the traced `preprocessing_fn`. Additionally, if this is the final
  SavedModel being written out, it will contain a method called `metadata_fn`
  that provides deferred schema annotations.

  Args:
    saved_model_dir: Path to write SavedModel to.
    preprocessing_fn: A user defined python function to be traced.
    input_signature: TypeSpecs describing the inputs to the `preprocessing_fn`.
    base_temp_dir: Base path to write temporary artifacts to.
    baseline_analyzers_fingerprint: A mapping from analyzer name to a set of
      paths that define its fingerprint.
    tensor_replacement_map: A map from placeholder tensor names to their
      evaluated replacement tensors.
    output_keys_to_name_map: A map from output dictionary keys to the names of
      the tensors that they represent.
    save_options: The options to use when saving the saved_model.

  Returns:
    A tuple containing a pair of `tf.ConcreteFunction`s:
      1. The traced preprocessing_fn.
      2. A metadata_fn that returns a dictionary containing the deferred
      annotations added to the graph when invoked with any valid input.

  Raises:
    RuntimeError: if analyzers in `preprocessing_fn` are encountered in a
    non-deterministic order.
  
tensorflow_transform/impl_helper.py