code: def _optimize_post(model, lightweight_bmm=False):
      from packaging import version
      from bigdl.llm.transformers.models.llama import (
          llama_attention_forward_4_31,
      )
      from bigdl.llm.transformers.models.llama import (
          llama_attention_selective_batching_forward_4_31,
      )
      from bigdl.llm.transformers.models.llama import (
          llama_model_selective_batching_forward_4_31,
      )
      from bigdl.llm.transformers.models.llama import (
          llama_rms_norm_forward,
      )
      from bigdl.llm.transformers.models.llama import llama_mlp_forward
      from transformers.modeling_utils import PreTrainedModel
  
      # All huggingface format models are inherited from `PreTrainedModel`
      if not isinstance(model, PreTrainedModel):
          logger.info(
              "Only HuggingFace Transformers models are currently "
              "supported for further optimizations"
          )
          return model
  
      vllm_selective_batching = os.getenv(
          "VLLM_ENABLE_SELECTIVE_BATCHING"
      )
      enable_vllm_se_batching = vllm_selective_batching is not None
      enable_vllm_se_batching = (
          enable_vllm_se_batching
          and vllm_selective_batching.lower() == "true"
      )
  
      trans_version = transformers.__version__
      if version.parse(trans_version) >= version.parse("4.31.0"):
          convert_forward(
              model,
              transformers.models.llama.modeling_llama.LlamaRMSNorm,
              llama_rms_norm_forward,
          )
          convert_forward(
              model,
              transformers.models.llama.modeling_llama.LlamaMLP,
              llama_mlp_forward,
          )
          if version.parse(trans_version) >= version.parse("4.36.0"):
              # transformers version >= 4.36.0
              from bigdl.llm.transformers.models.llama import (
                  llama_attention_forward_4_36,
              )
  
              convert_forward(
                  model,
                  transformers.models.llama.modeling_llama.LlamaAttention,
                  llama_attention_forward_4_36,
              )
          else:
              # transformers version between 4.31.0 - 4.35.2
              convert_forward(
                  model,
                  transformers.models.llama.modeling_llama.LlamaAttention,
                  llama_attention_forward_4_31,
              )
              if enable_vllm_se_batching:
                  convert_forward(
                      model,
                      transformers.models.llama.modeling_llama.LlamaModel,
                      llama_model_selective_batching_forward_4_31,
                  )
                  convert_forward(
                      model,
                      transformers.models.llama.modeling_llama.LlamaAttention,
                      llama_attention_selective_batching_forward_4_31,
                  )
      else:
          # todo implement 4.28.0 ~ 4.30.2
          pass
  
      # convert all nn.LayerNorm
      from bigdl.llm.transformers.models.bloom import (
          bloom_layer_norm_forward,
      )
  
      convert_forward(model, nn.LayerNorm, bloom_layer_norm_forward)
  
      if (
          model.config.architectures is not None
          and model.config.architectures[0]
          in ["ChatGLMModel", "ChatGLMForConditionalGeneration"]
      ):
          if model.config.num_layers == 28 and hasattr(
              model.config, "rope_ratio"
          ):
              # chatglm2-6b-32k
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.chatglm2_32k import (
                  chatglm2_32k_attention_forward,
              )
  
              convert_forward(
                  model,
                  module.SelfAttention,
                  chatglm2_32k_attention_forward,
              )
          elif (
              hasattr(model.config, "padded_vocab_size")
              and model.config.padded_vocab_size == 65024
          ):
              # chatglm2-6b
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.chatglm2 import (
                  chatglm2_attention_forward_8eb45c,
              )
              from bigdl.llm.transformers.models.chatglm2 import (
                  core_attn_forward_8eb45c,
              )
              from bigdl.llm.transformers.models.chatglm2 import (
                  chatglm_rms_norm_forward,
              )
              from bigdl.llm.transformers.models.chatglm2 import (
                  chatglm2_model_forward,
              )
  
              convert_forward(
                  model,
                  module.SelfAttention,
                  chatglm2_attention_forward_8eb45c,
              )
              convert_forward(
                  model, module.CoreAttention, core_attn_forward_8eb45c
              )
              convert_forward(
                  model, module.ChatGLMModel, chatglm2_model_forward
              )
              convert_forward(
                  model, module.RMSNorm, chatglm_rms_norm_forward
              )
          elif (
              hasattr(model.config, "vocab_size")
              and model.config.vocab_size == 130528
          ):
              # chatglm-6b
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.chatglm import (
                  chatglm_attention_forward,
              )
  
              convert_forward(
                  model, module.SelfAttention, chatglm_attention_forward
              )
      elif "mpt" in model.config.model_type:
          if model.config.architectures is not None:
              modeling_module_name = model.__class__.__module__
              attention_module_name = (
                  ".".join(modeling_module_name.split(".")[:-1])
                  + ".attention"
              )
              module = importlib.import_module(attention_module_name)
              from bigdl.llm.transformers.models.mpt import (
                  mpt_multihead_attention_forward,
              )
  
              convert_forward(
                  model,
                  module.MultiheadAttention,
                  mpt_multihead_attention_forward,
              )
      elif "gptj" in model.config.model_type:
          # dolly-v1-6b
          modeling_module_name = model.__class__.__module__
          module = importlib.import_module(modeling_module_name)
          from bigdl.llm.transformers.models.gptj import (
              gptj_attention_forward,
          )
  
          convert_forward(
              model, module.GPTJAttention, gptj_attention_forward
          )
      elif "bloom" in model.config.model_type:
          modeling_module_name = model.__class__.__module__
          module = importlib.import_module(modeling_module_name)
          from bigdl.llm.transformers.models.bloom import (
              bloom_attention_forward,
          )
  
          convert_forward(
              model, module.BloomAttention, bloom_attention_forward
          )
      elif (
          "falcon" in model.config.model_type
          or "RefinedWeb" in model.config.model_type
      ):
          if model.config.architectures is not None:
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              if "RWForCausalLM" in model.config.architectures:
                  if model.config.hidden_size == 4544:
                      # falcon-7b need to check performance drop after kv cache support.
                      # from bigdl.llm.transformers.models.falcon import rw_attention_forward_7b
                      # convert_forward(model,
                      #                 module.Attention,
                      #                 rw_attention_forward_7b
                      #                 )
                      pass
                  else:
                      # falcon-40b
                      from bigdl.llm.transformers.models.falcon import (
                          rw_attention_forward_40b,
                      )
  
                      convert_forward(
                          model,
                          module.Attention,
                          rw_attention_forward_40b,
                      )
              elif "FalconForCausalLM" in model.config.architectures:
                  if model.config.hidden_size != 4544:
                      # falcon-180b and new falcon-40b
                      from bigdl.llm.transformers.models.falcon import (
                          falcon_attention_forward,
                      )
  
                      convert_forward(
                          model,
                          module.FalconAttention,
                          falcon_attention_forward,
                      )
      elif (
          model.config.model_type == "baichuan"
          and model.config.vocab_size == 125696
      ):
          # baichuan2
          if model.config.hidden_size == 4096:
              # baichuan2-7B
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.baichuan2 import (
                  baichuan_attention_forward_7b,
              )
              from bigdl.llm.transformers.models.baichuan2 import (
                  baichuan_mlp_forward,
              )
  
              convert_forward(
                  model, module.Attention, baichuan_attention_forward_7b
              )
              convert_forward(
                  model, module.RMSNorm, llama_rms_norm_forward
              )
              convert_forward(model, module.MLP, baichuan_mlp_forward)
          elif model.config.hidden_size == 5120:
              # baichuan2-13B
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.baichuan2 import (
                  baichuan_attention_forward_13b,
              )
              from bigdl.llm.transformers.models.baichuan2 import (
                  baichuan_13b_rms_norm_forward,
              )
              from bigdl.llm.transformers.models.baichuan2 import (
                  baichuan_mlp_forward,
              )
              from bigdl.llm.transformers.models.baichuan2 import (
                  baichuan_13b_get_alibi_mask,
              )
  
              convert_forward(
                  model,
                  module.BaichuanAttention,
                  baichuan_attention_forward_13b,
              )
              # baichuan2-13B's RMSNorm is a little different
              convert_forward(
                  model, module.RMSNorm, baichuan_13b_rms_norm_forward
              )
              convert_forward(model, module.MLP, baichuan_mlp_forward)
              replace_func(
                  model,
                  module.BaichuanModel,
                  "get_alibi_mask",
                  baichuan_13b_get_alibi_mask,
              )
      elif model.config.model_type == "baichuan":
          # baichuan1
          if model.config.hidden_size == 4096:
              # baichuan-7B
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.baichuan import (
                  baichuan_attention_forward_7b,
              )
  
              convert_forward(
                  model, module.Attention, baichuan_attention_forward_7b
              )
              convert_forward(
                  model, module.RMSNorm, llama_rms_norm_forward
              )
          elif model.config.hidden_size == 5120:
              # baichuan-13B
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.baichuan import (
                  baichuan_attention_forward_13b,
              )
              from bigdl.llm.transformers.models.baichuan2 import (
                  baichuan_13b_rms_norm_forward,
              )
  
              convert_forward(
                  model,
                  module.BaichuanAttention,
                  baichuan_attention_forward_13b,
              )
              # baichuan-13B's RMSNorm is a little different
              convert_forward(
                  model, module.RMSNorm, baichuan_13b_rms_norm_forward
              )
      elif model.config.model_type == "gpt_neox":
          from bigdl.llm.transformers.models.gptneox import (
              gptneox_attention_forward,
          )
  
          convert_forward(
              model,
              transformers.models.gpt_neox.modeling_gpt_neox.GPTNeoXAttention,
              gptneox_attention_forward,
          )
      elif model.config.model_type == "internlm":
          modeling_module_name = model.__class__.__module__
          module = importlib.import_module(modeling_module_name)
          from bigdl.llm.transformers.models.internlm import (
              internlm_attention_forward,
          )
  
          convert_forward(
              model,
              module.InternLMAttention,
              internlm_attention_forward,
          )
          convert_forward(
              model, module.InternLMRMSNorm, llama_rms_norm_forward
          )
      elif model.config.model_type == "qwen":
          if hasattr(model.config, "visual"):
              # for Qwen-VL-Chat
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.qwen_vl import (
                  qwen_attention_forward_vl,
              )
  
              convert_forward(
                  model, module.QWenAttention, qwen_attention_forward_vl
              )
          else:
              # for Qwen-7B and Qwen-14B
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              from bigdl.llm.transformers.models.qwen import (
                  qwen_attention_forward,
              )
              from bigdl.llm.transformers.models.qwen import (
                  qwen_mlp_forward,
              )
              from bigdl.llm.transformers.models.chatglm2 import (
                  chatglm_rms_norm_forward,
              )
  
              convert_forward(
                  model, module.QWenAttention, qwen_attention_forward
              )
              convert_forward(
                  model, module.RMSNorm, chatglm_rms_norm_forward
              )
              convert_forward(model, module.QWenMLP, qwen_mlp_forward)
      elif model.config.model_type == "aquila":
          modeling_module_name = model.__class__.__module__
          module = importlib.import_module(modeling_module_name)
          from bigdl.llm.transformers.models.aquila import (
              aquila_attention_forward,
          )
  
          convert_forward(
              model, module.AquilaAttention, aquila_attention_forward
          )
          convert_forward(
              model, module.AquilaRMSNorm, llama_rms_norm_forward
          )
      elif model.config.model_type == "mixtral":
          # For mistralai/Mixtral-8x7B-v0.1
          invalidInputError(
              version.parse(trans_version) >= version.parse("4.36.0"),
              "Please upgrade transformers to 4.36.0 or higher version "
              "to run Mixtral models.",
          )
          modeling_module_name = model.__class__.__module__
          module = importlib.import_module(modeling_module_name)
          from bigdl.llm.transformers.models.mixtral import (
              mixtral_moeblock_forward,
              mixtral_attention_forward,
              mixtral_mlp_forward,
          )
  
          convert_forward(
              model, module.MixtralAttention, mixtral_attention_forward
          )
          convert_forward(
              model, module.MixtralRMSNorm, llama_rms_norm_forward
          )
          convert_forward(
              model,
              module.MixtralSparseMoeBlock,
              mixtral_moeblock_forward,
          )
          convert_forward(
              model,
              module.MixtralBLockSparseTop2MLP,
              mixtral_mlp_forward,
          )
      elif model.config.model_type == "mistral":
          if (
              model.config.architectures is not None
              and model.config.architectures[0] == "MixtralForCausalLM"
          ):
              # For DiscoResearch/mixtral-7b-8expert
              invalidInputError(
                  version.parse(trans_version)
                  >= version.parse("4.36.0"),
                  "Please upgrade transformers to 4.36.0 or higher version "
                  "to run Mixtral models.",
              )
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              convert_forward(
                  model, module.MistralRMSNorm, llama_rms_norm_forward
              )
          else:
              if version.parse(trans_version) >= version.parse(
                  "4.36.0"
              ):
                  modeling_module_name = model.__class__.__module__
                  module = importlib.import_module(modeling_module_name)
                  from bigdl.llm.transformers.models.mistral import (
                      mistral_attention_forward_4_36,
                  )
  
                  convert_forward(
                      model,
                      module.MistralAttention,
                      mistral_attention_forward_4_36,
                  )
                  convert_forward(
                      model,
                      module.MistralRMSNorm,
                      llama_rms_norm_forward,
                  )
                  convert_forward(
                      model, module.MistralMLP, llama_mlp_forward
                  )
              else:
                  modeling_module_name = model.__class__.__module__
                  module = importlib.import_module(modeling_module_name)
                  from bigdl.llm.transformers.models.mistral import (
                      mistral_attention_forward,
                  )
  
                  convert_forward(
                      model,
                      module.MistralAttention,
                      mistral_attention_forward,
                  )
                  convert_forward(
                      model,
                      module.MistralRMSNorm,
                      llama_rms_norm_forward,
                  )
                  convert_forward(
                      model, module.MistralMLP, llama_mlp_forward
                  )
      elif model.config.model_type == "Yi":
          modeling_module_name = model.__class__.__module__
          module = importlib.import_module(modeling_module_name)
          convert_forward(
              model, module.YiRMSNorm, llama_rms_norm_forward
          )
      elif model.config.model_type == "whisper" and lightweight_bmm:
          if platform.system().lower() == "windows":
              from bigdl.llm.transformers.bmm import SafeBMM
  
              modeling_module_name = model.__class__.__module__
              module = importlib.import_module(modeling_module_name)
              old_fwd = module.WhisperAttention.forward
  
              def safe_bmm_fwd(*args, **kwargs):
                  with SafeBMM():
                      return old_fwd(*args, **kwargs)
  
              convert_forward(
                  model, module.WhisperAttention, safe_bmm_fwd
              )
      elif model.config.model_type == "rwkv":
          # rwkv v4
          modeling_module_name = model.__class__.__module__
          module = importlib.import_module(modeling_module_name)
          from bigdl.llm.transformers.models.rwkv4 import (
              rwkv_attention_forward,
          )
  
          convert_forward(
              model, module.RwkvSelfAttention, rwkv_attention_forward
          )
      return model
  
imports: import platform
  import torch
  import torch.nn
  from accelerate import init_empty_weights
  import warnings
  import transformers
  import importlib.util
  from bigdl.llm.ggml.quantize import ggml_tensor_qtype
  from .utils import logger
  from typing import Union
  import numpy
  import os
  from bigdl.llm.utils.common import invalidInputError
label_desc: Use image node_id 13 for calling a global function with example usage: _optimize_post(model, lightweight_bmm) and returns: model, model, old_fwd