code: def _AddDerivedCrossSliceAndDiffMetrics(  # pylint: disable=invalid-name
      sliced_base_metrics: beam.PCollection[
          Tuple[slicer.SliceKeyType, metric_types.MetricsDict]
      ],
      derived_computations: List[metric_types.DerivedMetricComputation],
      cross_slice_computations: List[
          metric_types.CrossSliceMetricComputation
      ],
      cross_slice_specs: Optional[
          Iterable[config_pb2.CrossSlicingSpec]
      ] = None,
      baseline_model_name: Optional[str] = None,
  ) -> beam.PCollection[
      Tuple[
          slicer.SliceKeyOrCrossSliceKeyType, metric_types.MetricsDict
      ]
  ]:
      """A PTransform for adding cross slice and derived metrics.
  
      This PTransform uses the input PCollection of sliced metrics to compute
      derived metrics, cross-slice diff metrics, and cross-model diff metrics, in
      that order. This means that cross-slice metrics are computed for base and
      derived metrics, and that cross-model diffs are computed for base and derived
      metrics corresponding to both single slices and cross-slice pairs.
  
      Args:
        sliced_base_metrics: A PCollection of per-slice MetricsDicts containing the
          metrics to be used as inputs for derived, cross-slice, and diff metrics.
        derived_computations: List of DerivedMetricComputations.
        cross_slice_computations: List of CrossSliceMetricComputation.
        cross_slice_specs: List of CrossSlicingSpec.
        baseline_model_name: Name for baseline model.
  
      Returns:
        PCollection of sliced dict of metrics, containing all base metrics (that are
        non-private), derived metrics, cross-slice metrics, and diff metrics.
      """
  
      def add_derived_metrics(
          sliced_metrics: Tuple[
              slicer.SliceKeyType, metric_types.MetricsDict
          ],
          derived_computations: List[
              metric_types.DerivedMetricComputation
          ],
      ) -> Tuple[slicer.SliceKeyType, metric_types.MetricsDict]:
          """Merges per-metric dicts into single dict and adds derived metrics."""
          slice_key, metrics = sliced_metrics
          result = copy.copy(metrics)
          for c in derived_computations:
              result.update(c.result(result))
          return slice_key, result
  
      def add_diff_metrics(
          sliced_metrics: Tuple[
              Union[slicer.SliceKeyType, slicer.CrossSliceKeyType],
              Dict[metric_types.MetricKey, Any],
          ],
          baseline_model_name: Optional[str],
      ) -> Tuple[
          slicer.SliceKeyType, Dict[metric_types.MetricKey, Any]
      ]:
          """Add diff metrics if there is a baseline model."""
  
          slice_key, metrics = sliced_metrics
          result = copy.copy(metrics)
  
          if baseline_model_name:
              diff_result = {}
              for k, v in result.items():
                  if _is_private_metrics(k):
                      continue
                  if k.is_diff:
                      # For metrics which directly produce diff metrics, we skip this step
                      continue
                  if (
                      k.model_name != baseline_model_name
                      and k.make_baseline_key(baseline_model_name)
                      in result
                  ):
                      # Check if metric is diffable, skip plots and non-numerical values.
                      if _is_metric_diffable(v):
                          diff_result[k.make_diff_key()] = (
                              v
                              - result[
                                  k.make_baseline_key(
                                      baseline_model_name
                                  )
                              ]
                          )
              result.update(diff_result)
          return slice_key, result
  
      return (
          sliced_base_metrics
          | "AddDerivedMetrics"
          >> beam.Map(add_derived_metrics, derived_computations)
          | "AddCrossSliceMetrics"
          >> _AddCrossSliceMetrics(  # pylint: disable=no-value-for-parameter
              cross_slice_specs, cross_slice_computations
          )
          | "AddDiffMetrics"
          >> beam.Map(add_diff_metrics, baseline_model_name)
      )
  
imports: import copy
  import datetime
  import itertools
  import numbers
  from typing import Any, Dict, Iterable, Iterator, List, NamedTuple, Optional, Tuple, Type, TypeVar, Union
  import apache_beam
  import numpy
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.api import types
  from tensorflow_model_analysis.evaluators import counter_util
  from tensorflow_model_analysis.evaluators import eval_saved_model_util
  from tensorflow_model_analysis.evaluators import evaluator
  from tensorflow_model_analysis.evaluators import jackknife
  from tensorflow_model_analysis.evaluators import keras_util
  from tensorflow_model_analysis.evaluators import metrics_validator
  from tensorflow_model_analysis.evaluators import poisson_bootstrap
  from tensorflow_model_analysis.extractors import slice_key_extractor
  from tensorflow_model_analysis.metrics import metric_specs
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.slicer import slicer_lib
  from tensorflow_model_analysis.utils import model_util
  from tensorflow_model_analysis.utils import util
  from tensorflow_metadata.proto.v0 import schema_pb2
label_desc: Use image node_id 8 for calling a global function with example usage: _AddDerivedCrossSliceAndDiffMetrics(sliced_base_metrics, derived_computations, cross_slice_computations, cross_slice_specs, baseline_model_name) and returns: unknown, slice_key,  result, slice_key,  result