class_docstr: Base class for distributed computation models and defines interface methods.
  This class is public and should be used for other custom derived distributed models.
class_docstr_tok: ["Base","class","for","distributed","computation","models","and","defines","interface","methods",".","This","class","is","public","and","should","be","used","for","other","custom","derived","distributed","models","."]
code: def broadcast(
      self,
      tensor: Union[torch.Tensor, float, str, None],
      src: int = 0,
      safe_mode: bool = False,
  ) -> Union[torch.Tensor, float, str]:
      if not (
          isinstance(tensor, (torch.Tensor, Number, str))
          or tensor is None
      ):
          raise TypeError(f"Unhandled input type {type(tensor)}")
  
      rank = self.get_rank()
      if tensor is None:
          if rank == src:
              raise ValueError("Source data can not be None")
          elif not safe_mode:
              raise ValueError(
                  "Argument safe_mode should be True if None is passed for non src rank"
              )
  
      device = self.device()
      tensor_to_number = tensor_to_str = False
  
      if safe_mode:
          tensor = self._setup_placeholder(tensor, device, rank == src)
  
      if tensor is None:
          raise RuntimeError(
              "Internal error, tensor is None. Please, file an issue if you encounter this error."
          )
  
      if isinstance(
          tensor, (Number, float)
      ):  # have to use Number and float to satisfy mypy
          tensor_to_number = True
          if rank != src:
              tensor = torch.empty(1, device=device, dtype=torch.float)
          else:
              tensor = torch.tensor(
                  [tensor], device=device, dtype=torch.float
              )
      elif isinstance(tensor, str):
          tensor_to_str = True
          max_length = self._get_max_length(tensor, device)
          if rank != src:
              tensor = torch.empty(
                  1, max_length + 1, device=device, dtype=torch.long
              )
          else:
              tensor = self._encode_str(tensor, device, size=max_length)
  
      tensor = self._apply_op(tensor, device, self._do_broadcast, src)
  
      if tensor_to_number:
          return tensor.item()
      if tensor_to_str:
          list_str = self._decode_str(tensor)
          return list_str[0]
      return tensor
  
imports: from abc import ABCMeta, abstractmethod
  from numbers import Number
  from typing import Any, Callable, cast, List, Optional, Union
  import torch
  from packaging.version import Version
label_desc: Use image node_id 27 for calling the ComputationModel obj's underlying member method code with example usage: obj.broadcast(tensor, src, safe_mode) and returns: tensor, tensor, list_str