code: def optimizer_state_to_device(state, device):
      r"""
      将一个 ``optimizer`` 的 ``state_dict`` 迁移到对应的设备。
  
      :param state: ``optimzier.state_dict()``。
      :param device: 要迁移到的目的设备。
      :return: 迁移后的新的 state_dict。
      """
      new_state = {}
      for name, param in state.items():
          if isinstance(param, dict):
              new_state[name] = optimizer_state_to_device(param, device)
          elif isinstance(param, torch.Tensor):
              new_state[name] = param.to(device).clone()
          else:
              new_state[name] = param
      return new_state
  
imports: import os
  from typing import Any, Dict, Optional, Union
  from enum import IntEnum
  import contextlib
  import random
  import numpy
  import inspect
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP.envs.utils import get_global_seed
  from fastNLP.envs import get_global_rank, FASTNLP_BACKEND_LAUNCH, FASTNLP_GLOBAL_SEED
  from fastNLP.core.samplers import re_instantiate_sampler, ReproducibleBatchSampler, ReproducibleSampler
  from fastNLP.core.utils import auto_param_call, apply_to_collection
  from fastNLP.core.log import logger
label_desc: Use image node_id 5 for calling a global function with example usage: optimizer_state_to_device(state, device) and returns: new_state