code: def copy_state_dict(
      loc: Dict[str, Union[torch.Tensor, ShardedTensor]],
      glob: Dict[str, torch.Tensor],
      exclude_predfix: Optional[str] = None,
  ) -> None:
      for name, tensor in loc.items():
          if exclude_predfix is not None and name.startswith(
              exclude_predfix
          ):
              continue
          else:
              assert name in glob, name
          global_tensor = glob[name]
          if isinstance(global_tensor, ShardedTensor):
              global_tensor = global_tensor.local_shards()[0].tensor
          if isinstance(tensor, ShardedTensor):
              for local_shard in tensor.local_shards():
                  assert global_tensor.ndim == local_shard.tensor.ndim
                  shard_meta = local_shard.metadata
                  t = global_tensor.detach()
                  if t.ndim == 1:
                      t = t[
                          shard_meta.shard_offsets[
                              0
                          ] : shard_meta.shard_offsets[0]
                          + local_shard.tensor.shape[0]
                      ]
                  elif t.ndim == 2:
                      t = t[
                          shard_meta.shard_offsets[
                              0
                          ] : shard_meta.shard_offsets[0]
                          + local_shard.tensor.shape[0],
                          shard_meta.shard_offsets[
                              1
                          ] : shard_meta.shard_offsets[1]
                          + local_shard.tensor.shape[1],
                      ]
                  else:
                      raise ValueError(
                          "Tensors with ndim > 2 are not supported"
                      )
                  local_shard.tensor.copy_(t)
          else:
              tensor.copy_(global_tensor)
  
imports: from enum import Enum
  from typing import Any, cast, Dict, List, Optional, Protocol, Tuple, Type, Union
  import torch
  import torch.distributed
  import torch.nn
  from fbgemm_gpu.split_embedding_configs import EmbOptimType
  from torch.distributed.optim import _apply_optimizer_in_backward
  from torchrec.distributed.embedding_types import EmbeddingTableConfig
  from torchrec.distributed.fbgemm_qcomm_codec import CommType, get_qcomm_codecs_registry, QCommsConfig
  from torchrec.distributed.model_parallel import DistributedModelParallel
  from torchrec.distributed.planner import EmbeddingShardingPlanner, ParameterConstraints, Topology
  from torchrec.distributed.test_utils.multi_process import MultiProcessContext
  from torchrec.distributed.test_utils.test_model import ModelInput, TestEBCSharder, TestEBSharder, TestETCSharder, TestETSharder, TestSparseNNBase
  from torchrec.distributed.types import EmbeddingModuleShardingPlan, ModuleSharder, ShardedTensor, ShardingEnv, ShardingPlan, ShardingType
  from torchrec.modules.embedding_configs import BaseEmbeddingConfig, EmbeddingBagConfig
  from torchrec.optim.keyed import CombinedOptimizer, KeyedOptimizerWrapper
  from torchrec.optim.optimizers import in_backward_optimizer_filter
label_desc: Use image node_id 3 for calling a global function with example usage: copy_state_dict(loc, glob, exclude_predfix) without return types