class_docstr: Class to choose devices for Ops in a replicated training setup.
  
  This class is not to be used directly by users.  See instead
  `replica_device_setter()` below.
docstr: Choose a device for `op`.
  
  Args:
    op: an `Operation`.
  
  Returns:
    The device to use for the `Operation`.
code: def device_function(self, op):
      """Choose a device for `op`.
  
      Args:
        op: an `Operation`.
  
      Returns:
        The device to use for the `Operation`.
      """
      # If we don't return early here, either merge_devices is True, or op.device
      # is empty (in which case merging is a no-op). So we can always merge below.
      if not self._merge_devices and op.device:
          return op.device
  
      current_device = pydev.DeviceSpec.from_string(op.device or "")
  
      # The ps_device will be used for specified ops (ps_ops) whenever it is
      # present and ps_tasks is non-zero. However, its task number will only be
      # set (using ps_strategy) if there is a job field in ps_device that won't be
      # changed by the job field (if present) in current_device.
      node_def = (
          op if isinstance(op, node_def_pb2.NodeDef) else op.node_def
      )
      if (
          self._ps_tasks
          and self._ps_device
          and node_def.op in self._ps_ops
      ):
          ps_device = pydev.DeviceSpec.from_string(self._ps_device)
  
          current_job, ps_job = current_device.job, ps_device.job
          if ps_job and (not current_job or current_job == ps_job):
              ps_device = ps_device.replace(task=self._ps_strategy(op))
  
          ps_device = ps_device.make_merged_spec(current_device)
          return ps_device.to_string()
  
      worker_device = pydev.DeviceSpec.from_string(
          self._worker_device or ""
      )
      worker_device = worker_device.make_merged_spec(current_device)
      return worker_device.to_string()
  
file_path: tensorflow/tensorflow/python/training/device_setter.py
imports: from tensorflow.core.framework import node_def_pb2
  from tensorflow.python.framework import device
  from tensorflow.python.platform import tf_logging
  from tensorflow.python.training import server_lib
  from tensorflow.python.util.tf_export import tf_export
label_desc: Use image node_id 2 for calling the _ReplicaDeviceChooser obj's underlying member method code with example usage: obj.device_function(op) and returns: worker_device, op, ps_device