code: def _AddCrossSliceMetrics(  # pylint: disable=invalid-name
      sliced_combiner_outputs: beam.pvalue.PCollection[
          Tuple[slicer.SliceKeyType, metric_types.MetricsDict]
      ],
      cross_slice_specs: Optional[
          Iterable[config_pb2.CrossSlicingSpec]
      ],
      cross_slice_computations: List[
          metric_types.CrossSliceMetricComputation
      ],
  ) -> beam.pvalue.PCollection[
      Tuple[
          slicer.SliceKeyOrCrossSliceKeyType, metric_types.MetricsDict
      ]
  ]:
      """Generates CrossSlice metrics from SingleSlices."""
  
      def is_slice_applicable(
          sliced_combiner_output: Tuple[
              slicer.SliceKeyType, metric_types.MetricsDict
          ],
          slicing_specs: Union[
              config_pb2.SlicingSpec, Iterable[config_pb2.SlicingSpec]
          ],
      ) -> bool:
          slice_key, _ = sliced_combiner_output
          for slicing_spec in slicing_specs:
              if slicer.SingleSliceSpec(
                  spec=slicing_spec
              ).is_slice_applicable(slice_key):
                  return True
          return False
  
      def is_not_slice_applicable(
          sliced_combiner_output: Tuple[
              slicer.SliceKeyType, metric_types.MetricsDict
          ],
          slicing_specs: Union[
              config_pb2.SlicingSpec, Iterable[config_pb2.SlicingSpec]
          ],
      ) -> bool:
          return not is_slice_applicable(
              sliced_combiner_output, slicing_specs
          )
  
      def compute_cross_slices(
          baseline_slice: Tuple[
              slicer.SliceKeyType, metric_types.MetricsDict
          ],
          comparison_slices: Iterable[
              Tuple[
                  slicer.SliceKeyType, Dict[metric_types.MetricKey, Any]
              ]
          ],
      ) -> Iterator[
          Tuple[
              slicer.CrossSliceKeyType,
              Dict[metric_types.MetricKey, Any],
          ]
      ]:
          baseline_slice_key, baseline_metrics = baseline_slice
          for (
              comparison_slice_key,
              comparison_metrics,
          ) in comparison_slices:
              result = {}
              for (
                  comparison_metric_key,
                  comparison_metric_value,
              ) in comparison_metrics.items():
                  if (
                      comparison_metric_key not in baseline_metrics
                      or _is_private_metrics(comparison_metric_key)
                      or not isinstance(
                          comparison_metric_key, metric_types.MetricKey
                      )
                      or isinstance(
                          comparison_metric_key, metric_types.PlotKey
                      )
                      or isinstance(
                          comparison_metric_key,
                          metric_types.AttributionsKey,
                      )
                  ):
                      continue
                  result[comparison_metric_key] = (
                      baseline_metrics[comparison_metric_key]
                      - comparison_metric_value
                  )
  
              # Compute cross slice comparison for CrossSliceDerivedComputations
              for c in cross_slice_computations:
                  result.update(
                      c.cross_slice_comparison(
                          baseline_metrics, comparison_metrics
                      )
                  )
  
              yield ((baseline_slice_key, comparison_slice_key), result)
  
      cross_slice_outputs = []
      for cross_slice_ind, cross_slice_spec in enumerate(
          cross_slice_specs
      ):
          baseline_slices = (
              sliced_combiner_outputs
              | "FilterBaselineSlices(%d)" % cross_slice_ind
              >> beam.Filter(
                  is_slice_applicable, [cross_slice_spec.baseline_spec]
              )
          )
  
          if cross_slice_spec.slicing_specs:
              slicing_specs = list(cross_slice_spec.slicing_specs)
              comparison_slices = (
                  sliced_combiner_outputs
                  | "FilterToComparisonSlices(%d)" % cross_slice_ind
                  >> beam.Filter(is_slice_applicable, slicing_specs)
              )
          else:
              # When slicing_specs is not set, consider all available slices except the
              # baseline as candidates.
              comparison_slices = (
                  sliced_combiner_outputs
                  | "FilterOutBaselineSlices(%d)" % cross_slice_ind
                  >> beam.Filter(
                      is_not_slice_applicable,
                      [cross_slice_spec.baseline_spec],
                  )
              )
  
          cross_slice_outputs.append(
              baseline_slices
              | "GenerateCrossSlices(%d)" % cross_slice_ind
              >> beam.FlatMap(
                  compute_cross_slices,
                  comparison_slices=beam.pvalue.AsIter(
                      comparison_slices
                  ),
              )
          )
  
      if cross_slice_outputs:
          cross_slice_outputs = (
              cross_slice_outputs
              | "FlattenCrossSliceResults" >> beam.Flatten()
          )
          return [
              sliced_combiner_outputs,
              cross_slice_outputs,
          ] | "CombineSingleSlicesWithCrossSlice" >> beam.Flatten()
      else:
          return sliced_combiner_outputs
  
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 6 for calling a global function with example usage: _AddCrossSliceMetrics(sliced_combiner_outputs, cross_slice_specs, cross_slice_computations) and returns: False, unknown, unknown, sliced_combiner_outputs, True