code: def hf_state_dict(model):
      """
      Args:
          model: huggingface model replaced with lightseq layer
      Returns:
          Dict: The huggingface state dict
      """
  
      def unwrap_model(model):
          # since there could be multiple levels of wrapping, unwrap recursively
          if hasattr(model, "module"):
              return unwrap_model(model.module)
          else:
              return model
  
      def inject_hf_layer(config, hf_layer, ls_layer, is_decoder=False):
          if not is_decoder:
              for layer_id in range(config.encoder_layers):
                  weight, bias = ls_layer[layer_id].params_dict()
                  layer = hf_layer[layer_id]
                  layer.self_attn.q_proj.weight.data.copy_(
                      weight["self_attn.q_proj"]
                  )
                  layer.self_attn.q_proj.bias.data.copy_(
                      bias["self_attn.q_proj"]
                  )
                  layer.self_attn.k_proj.weight.data.copy_(
                      weight["self_attn.k_proj"]
                  )
                  layer.self_attn.k_proj.bias.data.copy_(
                      bias["self_attn.k_proj"]
                  )
                  layer.self_attn.v_proj.weight.data.copy_(
                      weight["self_attn.v_proj"]
                  )
                  layer.self_attn.v_proj.bias.data.copy_(
                      bias["self_attn.v_proj"]
                  )
                  layer.self_attn.out_proj.weight.data.copy_(
                      weight["self_attn.out_proj"]
                  )
                  layer.self_attn.out_proj.bias.data.copy_(
                      bias["self_attn.out_proj"]
                  )
                  layer.self_attn_layer_norm.weight.data.copy_(
                      weight["self_attn_layer_norm"]
                  )
                  layer.self_attn_layer_norm.bias.data.copy_(
                      bias["self_attn_layer_norm"]
                  )
                  layer.fc1.weight.data.copy_(weight["fc1"])
                  layer.fc1.bias.data.copy_(bias["fc1"])
                  layer.fc2.weight.data.copy_(weight["fc2"])
                  layer.fc2.bias.data.copy_(bias["fc2"])
                  layer.final_layer_norm.weight.data.copy_(
                      weight["final_layer_norm"]
                  )
                  layer.final_layer_norm.bias.data.copy_(
                      bias["final_layer_norm"]
                  )
          else:
              encoder_attn_k_proj_w = None
              encoder_attn_k_proj_b = None
              encoder_attn_v_proj_w = None
              encoder_attn_v_proj_b = None
              for layer_id in range(config.decoder_layers):
                  weight, bias = ls_layer[layer_id].params_dict()
                  layer = hf_layer[layer_id]
                  layer.self_attn.q_proj.weight.data.copy_(
                      weight["self_attn.q_proj"]
                  )
                  layer.self_attn.q_proj.bias.data.copy_(
                      bias["self_attn.q_proj"]
                  )
                  layer.self_attn.k_proj.weight.data.copy_(
                      weight["self_attn.k_proj"]
                  )
                  layer.self_attn.k_proj.bias.data.copy_(
                      bias["self_attn.k_proj"]
                  )
                  layer.self_attn.v_proj.weight.data.copy_(
                      weight["self_attn.v_proj"]
                  )
                  layer.self_attn.v_proj.bias.data.copy_(
                      bias["self_attn.v_proj"]
                  )
                  layer.self_attn.out_proj.weight.data.copy_(
                      weight["self_attn.out_proj"]
                  )
                  layer.self_attn.out_proj.bias.data.copy_(
                      bias["self_attn.out_proj"]
                  )
                  layer.self_attn_layer_norm.weight.data.copy_(
                      weight["self_attn_layer_norm"]
                  )
                  layer.self_attn_layer_norm.bias.data.copy_(
                      bias["self_attn_layer_norm"]
                  )
                  layer.fc1.weight.data.copy_(weight["fc1"])
                  layer.fc1.bias.data.copy_(bias["fc1"])
                  layer.fc2.weight.data.copy_(weight["fc2"])
                  layer.fc2.bias.data.copy_(bias["fc2"])
                  layer.final_layer_norm.weight.data.copy_(
                      weight["final_layer_norm"]
                  )
                  layer.final_layer_norm.bias.data.copy_(
                      bias["final_layer_norm"]
                  )
  
                  layer.encoder_attn.q_proj.weight.data.copy_(
                      weight["encoder_attn.q_proj"]
                  )
                  layer.encoder_attn.q_proj.bias.data.copy_(
                      bias["encoder_attn.q_proj"]
                  )
                  layer.encoder_attn.out_proj.weight.data.copy_(
                      weight["encoder_attn.out_proj"]
                  )
                  layer.encoder_attn.out_proj.bias.data.copy_(
                      bias["encoder_attn.out_proj"]
                  )
                  layer.encoder_attn_layer_norm.weight.data.copy_(
                      weight["encoder_attn_layer_norm"]
                  )
                  layer.encoder_attn_layer_norm.bias.data.copy_(
                      bias["encoder_attn_layer_norm"]
                  )
                  if layer_id == 0:
                      encoder_attn_k_proj_w = weight[
                          "encoder_attn.k_proj"
                      ]
                      encoder_attn_k_proj_b = bias[
                          "encoder_attn.k_proj"
                      ]
                      encoder_attn_v_proj_w = weight[
                          "encoder_attn.v_proj"
                      ]
                      encoder_attn_v_proj_b = bias[
                          "encoder_attn.v_proj"
                      ]
                  layer.encoder_attn.k_proj.weight.data.copy_(
                      encoder_attn_k_proj_w[layer_id]
                  )
                  layer.encoder_attn.k_proj.bias.data.copy_(
                      encoder_attn_k_proj_b[layer_id]
                  )
                  layer.encoder_attn.v_proj.weight.data.copy_(
                      encoder_attn_v_proj_w[layer_id]
                  )
                  layer.encoder_attn.v_proj.bias.data.copy_(
                      encoder_attn_v_proj_b[layer_id]
                  )
  
      model_to_save = unwrap_model(model)
      if not isinstance(model_to_save, LSBartPretrainedModel):
          raise ValueError("Must be ligtseq replaced model")
      # reload original modules
      ls_encoder_layer = model_to_save.model.encoder.layers
      ls_decoder_layer = model_to_save.model.decoder.layers
      model_to_save.model.encoder.layers = nn.ModuleList(
          [
              BartEncoderLayer(model.config)
              for _ in range(model.config.encoder_layers)
          ]
      )
      model_to_save.model.decoder.layers = nn.ModuleList(
          [
              BartDecoderLayer(model.config)
              for _ in range(model.config.decoder_layers)
          ]
      )
  
      inject_hf_layer(
          model.config,
          model_to_save.model.encoder.layers,
          ls_encoder_layer,
      )
      inject_hf_layer(
          model.config,
          model_to_save.model.decoder.layers,
          ls_decoder_layer,
          is_decoder=True,
      )
      state_dict = model_to_save.state_dict()
      # replace with lightseq modules
      model_to_save.model.encoder.layers = ls_encoder_layer
      model_to_save.model.decoder.layers = ls_decoder_layer
      return state_dict
  
imports: import torch
  from torch import nn
  from dataclasses import dataclass
  from lightseq.training.ops.pytorch.transformer_encoder_layer import LSTransformerEncoderLayer
  from lightseq.training.ops.pytorch.transformer_decoder_layer import LSTransformerDecoderLayer
  from lightseq.training.ops.pytorch.quantization import weight_quant_config, act_quant_config, relu_quant_config
  from transformers import BartPretrainedModel, BartForConditionalGeneration, BartForSequenceClassification, BartForQuestionAnswering
  from transformers.models.bart.modeling_bart import BartEncoderLayer, BartDecoderLayer
label_desc: Use image node_id 6 for calling a global function with example usage: hf_state_dict(model) and returns: state_dict, unwrap_model, model