code: def get_strategy(
      strategy: str,
      cluster_resolver: Optional[
          tf.distribute.cluster_resolver.ClusterResolver
      ] = None,
      variable_partitioner: Optional[
          tf.distribute.experimental.partitioners.Partitioner
      ] = _USE_DEFAULT_VARIABLE_PARTITIONER,
      tpu: Optional[str] = "",
  ) -> Union[
      None,
      tf.distribute.MirroredStrategy,
      tf.distribute.MultiWorkerMirroredStrategy,
      tf.distribute.experimental.ParameterServerStrategy,
      tf.distribute.experimental.TPUStrategy,
  ]:
      """Creates and initializes the requested tf.distribute strategy.
  
      Example usage:
  
      ```python
      strategy = get_strategy("MirroredStrategy")
      ```
  
      Args:
        strategy: Key for a `tf.distribute` strategy to be used to train the model.
          Choose from ["MirroredStrategy", "MultiWorkerMirroredStrategy",
          "ParameterServerStrategy", "TPUStrategy"]. If None, no distributed
          strategy will be used.
        cluster_resolver: A cluster_resolver to build strategy.
        variable_partitioner: Variable partitioner to be used in
          ParameterServerStrategy. If the argument is not specified, a recommended
          `tf.distribute.experimental.partitioners.MinSizePartitioner` is used. If
          the argument is explicitly specified as `None`, no partitioner is used and
          that variables are not partitioned. This arg is used only when the
          strategy is `tf.distribute.experimental.ParameterServerStrategy`.
          See `tf.distribute.experimental.ParameterServerStrategy` class doc for
          more information.
        tpu: TPU address for TPUStrategy. Not used for other strategy.
  
      Returns:
        A strategy will be used for distributed training.
  
      Raises:
        ValueError if `strategy` is not supported.
      """
      if strategy is None:
          return None
      elif strategy == MIRRORED_STRATEGY:
          return tf.distribute.MirroredStrategy()
      elif strategy == MWMS_STRATEGY:
          return tf.distribute.MultiWorkerMirroredStrategy(
              cluster_resolver=cluster_resolver
          )
      elif strategy == TPU_STRATEGY:
          resolver = tf.distribute.cluster_resolver.TPUClusterResolver(
              tpu=tpu
          )
          tf.config.experimental_connect_to_cluster(resolver)
          tf.tpu.experimental.initialize_tpu_system(resolver)
          strategy = tf.distribute.experimental.TPUStrategy(resolver)
          return strategy
      elif strategy == PS_STRATEGY:
          if cluster_resolver is None:
              cluster_resolver = (
                  tf.distribute.cluster_resolver.TFConfigClusterResolver()
              )
          if variable_partitioner == _USE_DEFAULT_VARIABLE_PARTITIONER:
              cluster_spec = cluster_resolver.cluster_spec()
              # The following assumes parameter servers are named "ps".
              variable_partitioner = tf.distribute.experimental.partitioners.MinSizePartitioner(
                  max_shards=cluster_spec.num_tasks("ps")
              )
          return tf.distribute.experimental.ParameterServerStrategy(
              cluster_resolver, variable_partitioner
          )
      else:
          # TODO: integrate PSStrategy to pipeline.
          raise ValueError("Unsupported strategy {}".format(strategy))
  
file_path: ranking/tensorflow_ranking/python/keras/strategy_utils.py
imports: import os
  from typing import Any, Optional, Union
  import tensorflow
label_desc: Use image node_id 1 for calling a global function with example usage: get_strategy(strategy, cluster_resolver, variable_partitioner, tpu) and returns: None, tf, tf, strategy, tf