code: def apply_rule(proto_name, ckpt_rule, tensor_names, state_dict):
      def check_rule(tensor_name, rule):
          if "Adam" in tensor_name or "adam" in tensor_name:
              return False
          assert isinstance(rule, str) and rule
          rule = rule.split("-")
          assert len(rule) < 3
          if len(rule) == 2:
              white, black = rule[0].split(" "), rule[1].split(" ")
          else:
              white, black = rule[0].split(" "), []
          for b in black:
              if b in tensor_name.split("."):
                  return False
          for w in white:
              if w not in tensor_name.split("."):
                  return False
          return True
  
      expression = [
          ele
          for ele in ckpt_rule.split("&&")
          if ele.startswith("expression_")
      ]
  
      ckpt_rule = [
          ele
          for ele in ckpt_rule.split("&&")
          if not ele.startswith("expression_")
      ]
  
      assert (len(ckpt_rule) > 0 and len(expression) < 2) or (
          len(ckpt_rule) == 0 and len(expression) > 0
      )
  
      if len(expression) < 2:
          expression = (
              "" if not expression else expression[0].split("_")[1]
          )
      else:
          expression = [exp.split("_")[1] for exp in expression]
  
      target_tn = []
      for cr in ckpt_rule:
          tmp = []
          for tn in tensor_names:
              if check_rule(tn, cr):
                  tmp.append(tn)
          assert len(tmp) == 1
          target_tn.extend(tmp)
      target_tensor = [state_dict[name] for name in target_tn]
      tt = {}
      if target_tensor:
          exec(
              "tt['save'] = [ele%s for ele in target_tensor]"
              % expression
          )
      else:
          if not isinstance(expression, list):
              expression = [expression]
          exec("tt['save'] = [%s]" % ",".join(expression))
  
      try:
          target_tensor = np.concatenate(tt["save"], axis=-1)
      except:
          target_tensor = tt["save"]
      print(
          "%s -> %s, convert finished!"
          % (target_tn if target_tn else "created", proto_name)
      )
      return target_tensor
  
file_path: lightseq/lightseq/training/ops/pytorch/export.py
imports: from collections import OrderedDict
  import numpy
  from lightseq.training.ops.pytorch.util import get_pos_embedding
  from lightseq.training import LSTransformerEncoderLayer, LSTransformerDecoderLayer
label_desc: Use image node_id 2 for calling a global function with example usage: apply_rule(proto_name, ckpt_rule, tensor_names, state_dict) and returns: target_tensor, True, False, False, False