code: def bwc_tensor_model_parallel_rank(mpu=None):
      """Backwards-compatible way of querying the tensor model parallel rank from
      an ``mpu`` object.
  
      *Tensor* model parallelism means that tensors are physically split across
      processes. This contrasts with *pipeline* model parallelism, in which the
      layers are partitioned but tensors left intact.
  
      The API for tensor model parallelism has changed across versions and this
      helper provides a best-effort implementation across versions of ``mpu``
      objects.  The preferred mechanism is
      ``mpu.get_tensor_model_parallel_rank()``.
  
      This should "just work" with both Megatron-LM and DeepSpeed's pipeline
      parallelism.
  
      Args:
          mpu (model parallel unit, optional): The tensor model parallel rank.
              If ``mpu=None``, returns 0. Defaults to ``None``.
  
      Returns:
          int: the rank
      """
      if mpu is None:
          # No model parallelism in easy :)
          return 0
  
      if hasattr(mpu, "get_tensor_model_parallel_rank"):
          # New Megatron and DeepSpeed convention (post pipeline-parallelism release)
          return mpu.get_tensor_model_parallel_rank()
      elif hasattr(mpu, "get_slice_parallel_rank"):
          # Some DeepSpeed + pipeline parallelism versions
          return mpu.get_slice_parallel_rank()
      else:
          # Deprecated Megatron and DeepSpeed convention
          return mpu.get_model_parallel_rank()
  
imports: from collections.abc import Iterable
  from deepspeed.moe.utils import is_moe_param
  import os
  import psutil
  import gc
  from math import sqrt
  from math import floor
  from bisect import bisect_left
  import torch
  from torch import inf
  from deepspeed import comm
  from deepspeed.utils import groups, logger
  from deepspeed.runtime.constants import PIPE_REPLICATED
  from numpy import prod
label_desc: Use image node_id 5 for calling a global function with example usage: bwc_tensor_model_parallel_rank(mpu) and returns: int, mpu, mpu, mpu