code: def build_reshard_metadata(
      st_size: torch.Size,
      sharding_spec: shard_spec.ShardingSpec,
      world_size: int,
  ) -> Tuple[List[ShardMetadata], List[int]]:
      """
      Based the given sharding spec, we calculate the offset and local shard size.
      We then build a ShardMetadata on top of the calculation result.
  
      Args:
          st_size (torch.Size): The size of the sharded tensor.
          sharding_spec (:class:`torch.distributed._shard.sharding_spec.ShardingSpec`): The
              specification describing how the tensor is sharded.
          world_size (int): number of ranks.
  
      Returns:
          A Tuple of the followings:
              A List[`ShardMetadata`] which contains the metadata for the shard, including
                  offsets, lengths and device placement.
              A List[int] which contains the ranks in the order of placement.
      """
      shard_dim = int(sharding_spec.dim)  # type: ignore[attr-defined]
      shards_metadata = [None] * world_size
      ranks = []
      offsets = [0] * len(st_size)
      split_size = get_split_size(st_size[shard_dim], world_size)
      for idx, placement in enumerate(sharding_spec.placements):  # type: ignore[attr-defined]
          ranks.append(placement.rank())
          sharded_dim_size = get_chunked_dim_size(
              st_size[shard_dim], split_size, idx
          )
          local_tensor_size = list(st_size)
          local_tensor_size[shard_dim] = sharded_dim_size
          shards_metadata[placement.rank()] = ShardMetadata(  # type: ignore[call-overload]
              shard_offsets=copy.deepcopy(offsets),
              shard_sizes=local_tensor_size,
              placement=placement,
          )
          offsets[shard_dim] += sharded_dim_size
      return shards_metadata, ranks
  
file_path: pytorch/torch/distributed/_shard/sharded_tensor/reshard.py
imports: import copy
  from typing import List, Tuple
  import torch
  import torch.distributed
  from torch._C._distributed_c10d import ProcessGroup
  import torch.distributed._shard.sharding_spec
  from torch.distributed._shard.sharding_spec._internals import get_split_size, get_chunked_dim_size
  from torch.distributed.nn.functional import all_to_all, all_to_all_single
  from torch.distributed._shard.metadata import ShardMetadata
  from .shard import Shard
label_desc: Use image node_id 2 for calling a global function with example usage: build_reshard_metadata(st_size, sharding_spec, world_size) and returns: shards_metadata,  ranks