code: def copy_to_device(
      module: nn.Module,
      current_device: torch.device,
      to_device: torch.device,
  ) -> nn.Module:
      with sharded_model_copy(device=None):
          copy_module = copy.deepcopy(module)
  
      # Copy only weights with matching device.
      def _copy_if_device_match(tensor: torch.Tensor) -> torch.Tensor:
          if tensor.device == current_device:
              return tensor.to(to_device)
          return tensor
  
      # if this is a sharded module, customize the copy
      if isinstance(copy_module, CopyMixIn):
          return copy_module.copy(to_device)
      copied_param = {
          name: torch.nn.Parameter(
              _copy_if_device_match(param.data),
              requires_grad=param.requires_grad,
          )
          for name, param in copy_module.named_parameters(recurse=False)
      }
      copied_buffer = {
          name: _copy_if_device_match(buffer)
          for name, buffer in copy_module.named_buffers(recurse=False)
      }
      for name, param in copied_param.items():
          copy_module.register_parameter(name, param)
      for name, buffer in copied_buffer.items():
          copy_module.register_buffer(name, buffer)
      for child_name, child in copy_module.named_children():
          if not any(
              [
                  isinstance(submodule, CopyMixIn)
                  for submodule in child.modules()
              ]
          ):
              child_copy = child._apply(_copy_if_device_match)
          else:
              child_copy = copy_to_device(
                  child, current_device, to_device
              )
          copy_module.register_module(child_name, child_copy)
      return copy_module
  
imports: import copy
  import logging
  from collections import OrderedDict
  from itertools import accumulate
  from typing import Any, Dict, List, Optional, Set, Type, TypeVar, Union
  import torch
  from fbgemm_gpu.split_embedding_configs import EmbOptimType
  from torch import nn
  from torchrec import optim
  from torchrec.distributed.embedding_types import EmbeddingComputeKernel
  from torchrec.distributed.types import DataType, ParameterSharding, ShardedModule, ShardingType
  from torchrec.modules.embedding_configs import data_type_to_sparse_type
  from torchrec.types import CopyMixIn
label_desc: Use image node_id 7 for calling a global function with example usage: copy_to_device(module, current_device, to_device) and returns: copy_module, tensor, copy_module, tensor