code: def read_in_q_k_v(state_dict):
      prefix = ""
  
      # first: transformer encoder
      for i in range(6):
          # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias)
          in_proj_weight = state_dict.pop(
              f"{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight"
          )
          in_proj_bias = state_dict.pop(
              f"{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias"
          )
          # next, add query, keys and values (in that order) to the state dict
          state_dict[
              f"encoder.layers.{i}.self_attn.q_proj.weight"
          ] = in_proj_weight[:256, :]
          state_dict[
              f"encoder.layers.{i}.self_attn.q_proj.bias"
          ] = in_proj_bias[:256]
          state_dict[
              f"encoder.layers.{i}.self_attn.k_proj.weight"
          ] = in_proj_weight[256:512, :]
          state_dict[
              f"encoder.layers.{i}.self_attn.k_proj.bias"
          ] = in_proj_bias[256:512]
          state_dict[
              f"encoder.layers.{i}.self_attn.v_proj.weight"
          ] = in_proj_weight[-256:, :]
          state_dict[
              f"encoder.layers.{i}.self_attn.v_proj.bias"
          ] = in_proj_bias[-256:]
      # next: transformer decoder (which is a bit more complex because it also includes cross-attention)
      for i in range(6):
          # read in weights + bias of input projection layer of self-attention
          in_proj_weight = state_dict.pop(
              f"{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_weight"
          )
          in_proj_bias = state_dict.pop(
              f"{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_bias"
          )
          # next, add query, keys and values (in that order) to the state dict
          state_dict[
              f"decoder.layers.{i}.self_attn.q_proj.weight"
          ] = in_proj_weight[:256, :]
          state_dict[
              f"decoder.layers.{i}.self_attn.q_proj.bias"
          ] = in_proj_bias[:256]
          state_dict[
              f"decoder.layers.{i}.self_attn.k_proj.weight"
          ] = in_proj_weight[256:512, :]
          state_dict[
              f"decoder.layers.{i}.self_attn.k_proj.bias"
          ] = in_proj_bias[256:512]
          state_dict[
              f"decoder.layers.{i}.self_attn.v_proj.weight"
          ] = in_proj_weight[-256:, :]
          state_dict[
              f"decoder.layers.{i}.self_attn.v_proj.bias"
          ] = in_proj_bias[-256:]
          # read in weights + bias of input projection layer of cross-attention
          in_proj_weight_cross_attn = state_dict.pop(
              f"{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_weight"
          )
          in_proj_bias_cross_attn = state_dict.pop(
              f"{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_bias"
          )
          # next, add query, keys and values (in that order) of cross-attention to the state dict
          state_dict[
              f"decoder.layers.{i}.encoder_attn.q_proj.weight"
          ] = in_proj_weight_cross_attn[:256, :]
          state_dict[
              f"decoder.layers.{i}.encoder_attn.q_proj.bias"
          ] = in_proj_bias_cross_attn[:256]
          state_dict[
              f"decoder.layers.{i}.encoder_attn.k_proj.weight"
          ] = in_proj_weight_cross_attn[256:512, :]
          state_dict[
              f"decoder.layers.{i}.encoder_attn.k_proj.bias"
          ] = in_proj_bias_cross_attn[256:512]
          state_dict[
              f"decoder.layers.{i}.encoder_attn.v_proj.weight"
          ] = in_proj_weight_cross_attn[-256:, :]
          state_dict[
              f"decoder.layers.{i}.encoder_attn.v_proj.bias"
          ] = in_proj_bias_cross_attn[-256:]
  
file_path: H2O/h2o_flexgen/benchmark/third_party/transformers/src/transformers/models/table_transformer/convert_table_transformer_original_pytorch_checkpoint_to_pytorch.py
imports: import argparse
  from collections import OrderedDict
  from pathlib import Path
  import torch
  from PIL import Image
  from torchvision.transforms import functional
  from huggingface_hub import hf_hub_download
  from transformers import DetrFeatureExtractor, TableTransformerConfig, TableTransformerForObjectDetection
  from transformers.utils import logging
label_desc: Use image node_id 3 for calling a global function with example usage: read_in_q_k_v(state_dict) without return types