code: def _convert_cond_stage(
      d: tensor_dict_type, md: tensor_dict_type
  ) -> tensor_dict_type:
      def _get_dv(k: str) -> Tensor:
          v = d.get(k)
          if v is None:
              v = d[k.replace(".text_model", "")]
          return v
  
      if not d or not md:
          return {}
      d, md = map(shallow_copy_dict, [d, md])
      nd = {
          "condition_model.m.token_embedding.weight": _get_dv(
              "cond_stage_model.transformer.text_model.embeddings.token_embedding.weight"
          ),
          "condition_model.m.text_transformer.encoder.pos_encoding.pos_encoding": _get_dv(
              "cond_stage_model.transformer.text_model.embeddings.position_embedding.weight"
          )[
              None, ...
          ],
          "condition_model.m.text_transformer.encoder.head.norms.0.weight": _get_dv(
              "cond_stage_model.transformer.text_model.final_layer_norm.weight"
          ),
          "condition_model.m.text_transformer.encoder.head.norms.0.bias": _get_dv(
              "cond_stage_model.transformer.text_model.final_layer_norm.bias"
          ),
      }
      for key in [
          "condition_model.m.logit_scale",
          "condition_model.m.text_transformer.attention_mask",
          "condition_model.m.text_projection.weight",
          "condition_model.m.text_projection.bias",
      ]:
          nd[key] = md.pop(key)
      prefix = "cond_stage_model.transformer.text_model.encoder.layers"
      m_prefix = (
          "condition_model.m.text_transformer.encoder.mixing_blocks"
      )
      layer_indices = []
      for key in d:
          if not key.startswith(prefix):
              continue
          try:
              layer_indices.append(int(key.split(".")[5]))
          except:
              continue
      num_cond_layers = max(layer_indices) + 1
      for i in range(num_cond_layers):
          i_prefix = f"{prefix}.{i}"
          m_i_prefix = f"{m_prefix}.{i}"
          get_w = lambda n: _get_dv(
              f"{i_prefix}.self_attn.{n}_proj.weight"
          )
          get_b = lambda n: _get_dv(
              f"{i_prefix}.self_attn.{n}_proj.bias"
          )
          in_w = torch.cat(list(map(get_w, ["q", "k", "v"])))
          qkv_bias = torch.cat(list(map(get_b, ["q", "k", "v"])))
          m_i_token_mixing_prefix = f"{m_i_prefix}.token_mixing.net"
          nd[f"{m_i_token_mixing_prefix}.in_w"] = in_w
          nd[f"{m_i_token_mixing_prefix}.qkv_bias"] = qkv_bias
          out_w = _get_dv(f"{i_prefix}.self_attn.out_proj.weight")
          out_b = _get_dv(f"{i_prefix}.self_attn.out_proj.bias")
          nd[
              f"{m_i_token_mixing_prefix}.out_linear.linear.weight"
          ] = out_w
          nd[
              f"{m_i_token_mixing_prefix}.out_linear.linear.bias"
          ] = out_b
          get_fc_w = lambda n: _get_dv(f"{i_prefix}.mlp.{n}.weight")
          get_fc_b = lambda n: _get_dv(f"{i_prefix}.mlp.{n}.bias")
          m_i_channel_mixing_prefix = f"{m_i_prefix}.channel_mixing.net"
          nd[f"{m_i_channel_mixing_prefix}.0.linear.weight"] = get_fc_w(
              "fc1"
          )
          nd[f"{m_i_channel_mixing_prefix}.0.linear.bias"] = get_fc_b(
              "fc1"
          )
          nd[f"{m_i_channel_mixing_prefix}.3.linear.weight"] = get_fc_w(
              "fc2"
          )
          nd[f"{m_i_channel_mixing_prefix}.3.linear.bias"] = get_fc_b(
              "fc2"
          )
          for ln, m_ln in zip(
              ["layer_norm1", "layer_norm2"],
              ["token_norm", "channel_norm"],
          ):
              nd[f"{m_i_prefix}.{m_ln}.weight"] = _get_dv(
                  f"{i_prefix}.{ln}.weight"
              )
              nd[f"{m_i_prefix}.{m_ln}.bias"] = _get_dv(
                  f"{i_prefix}.{ln}.bias"
              )
      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 1 for calling a global function with example usage: _convert_cond_stage(d, md) and returns: nd, v, dict