code: def collate_fn(cfg):
      def collate_fn_intra(batch):
          """
          Arg :
              batch () : 数据集
          Returna :
              x (dict) : key为词，value为长度
              y (List) : 关系对应值的集合
          """
          batch.sort(key=lambda data: data["seq_len"], reverse=True)
  
          max_len = 512
  
          def _padding(x, max_len):
              return x + [0] * (max_len - len(x))
  
          x, y = dict(), []
          word, word_len = [], []
          head_pos, tail_pos = [], []
          pcnn_mask = []
          for data in batch:
              word.append(_padding(data["token2idx"], max_len))
              word_len.append(data["seq_len"])
              y.append(int(data["rel2idx"]))
  
              if cfg.model_name != "lm":
                  head_pos.append(_padding(data["head_pos"], max_len))
                  tail_pos.append(_padding(data["tail_pos"], max_len))
                  if cfg.model_name == "cnn":
                      if cfg.use_pcnn:
                          pcnn_mask.append(
                              _padding(data["entities_pos"], max_len)
                          )
  
          x["word"] = torch.tensor(word)
          x["lens"] = torch.tensor(word_len)
          y = torch.tensor(y)
  
          if cfg.model_name != "lm":
              x["head_pos"] = torch.tensor(head_pos)
              x["tail_pos"] = torch.tensor(tail_pos)
              if cfg.model_name == "cnn" and cfg.use_pcnn:
                  x["pcnn_mask"] = torch.tensor(pcnn_mask)
              if cfg.model_name == "gcn":
                  B, L = len(batch), max_len
                  adj = torch.empty(B, L, L).random_(2)
                  x["adj"] = adj
          return x, y
  
      return collate_fn_intra
  
file_path: DeepKE/src/deepke/relation_extraction/standard/tools/dataset.py
imports: import torch
  from torch.utils.data import Dataset
  import os
  import sys
  from utils import load_pkl
label_desc: Use image node_id 1 for calling a global function with example usage: collate_fn(cfg) and returns: collate_fn_intra, x,  y, unknown