code: def _convert(
      d: tensor_dict_type, md: tensor_dict_type
  ) -> tensor_dict_type:
      key_mapping_inv.clear()
      d, md = map(shallow_copy_dict, [d, md])
      nd = {}
      d = {k: v.cpu() for k, v in d.items()}
      md = {k: v.cpu() for k, v in md.items()}
      if "cond_stage_model.channel_mapper.weight" in d:
          print(">  [info] injecting channel_mapper")
          assert (
              d["cond_stage_model.channel_mapper.weight"].shape
              == md["condition_model.channel_mapper.weight"].shape
          )
          k = "cond_stage_model.channel_mapper.weight"
          mk = "condition_model.channel_mapper.weight"
          nd[mk] = d[k]
          key_mapping_inv[mk] = k
      # condition
      cond_d = {}
      for k in list(d.keys()):
          if k.startswith("cond_stage_model"):
              cond_d[k] = d.pop(k)
      cond_md = {
          k: v for k, v in md.items() if k.startswith("condition_model")
      }
      for k in cond_md:
          md.pop(k)
      nd.update(_convert_cond_stage(cond_d, cond_md))
      # first stage
      fd = {
          k: v
          for k, v in d.items()
          if k.startswith("first_stage_model")
      }
      fmd = {k: v for k, v in md.items() if k.startswith("first_stage")}
      nd.update(_convert_first_stage(fd, fmd))
      # others
      od = {
          k: v
          for k, v in d.items()
          if not k.startswith("first_stage_model")
      }
      omd = {
          k: v for k, v in md.items() if not k.startswith("first_stage")
      }
      nd.update(_convert_others(od, omd))
      # ema
      ema_num_updates_k = "unet_ema.num_updates"
      if ema_num_updates_k in nd:
          nd.pop(ema_num_updates_k)
          key_mapping_inv.pop(ema_num_updates_k)
          normal_keys = [k for k in list(nd) if k.startswith("unet.")]
          ema_keys = [k for k in list(nd) if k.startswith("unet_ema")]
          for k, ema_k in zip(normal_keys, ema_keys):
              assert nd.pop(k).shape == nd[ema_k].shape
          for k, ema_k in zip(normal_keys, ema_keys):
              nd[k] = nd.pop(ema_k)
              key_mapping_inv[k] = key_mapping_inv.pop(ema_k)
      return nd
  
imports: import json
  import torch
  from torch import Tensor
  from typing import Dict
  from typing import List
  from typing import Union
  from typing import Optional
  from cftool.misc import shallow_copy_dict
  from cftool.array import tensor_dict_type
  from ..misc.toolkit import get_tensors
  from ..misc.toolkit import download_static
  from ..api.cv.diffusion import DiffusionAPI
label_desc: Use image node_id 4 for calling a global function with example usage: _convert(d, md) and returns: nd