code: def dump_optimizer_state(opt: torch.optim.Optimizer):
      """Convert optimizer state into a format of DecentralizedAverager's get_current_state/load_state_from_peers"""
      with torch.no_grad():
          flat_metadata, flat_tensors = [], []
          for elem in nested_flatten(opt.state_dict()):
              if isinstance(elem, torch.Tensor):
                  flat_metadata.append(
                      dict(type="tensor", index=len(flat_tensors))
                  )
                  flat_tensors.append(elem.cpu())
              else:
                  flat_metadata.append(dict(type="value", value=elem))
          return flat_metadata, flat_tensors
  
imports: import logging
  import threading
  import time
  from concurrent.futures import ThreadPoolExecutor
  from contextlib import nullcontext
  from itertools import chain
  from typing import Any, Callable, Dict, Iterable, Iterator, Optional, Sequence, Tuple, Union
  import torch
  from packaging.version import Version
  import hivemind
  from hivemind.averaging import DecentralizedAverager
  from hivemind.averaging.control import StepControl
  from hivemind.compression import CompressionInfo, TensorRole
  from hivemind.optim.grad_scaler import GradScaler
  from hivemind.utils import DHTExpiration, PerformanceEMA, get_logger, nested_flatten, nested_pack
label_desc: Use image node_id 2 for calling a global function with example usage: dump_optimizer_state(opt) and returns: flat_metadata,  flat_tensors