code: def collate_fn(batch, word2id, label2id):
      batch.sort(key=lambda x: len(x.text_a.split(" ")), reverse=True)
      max_len = len(batch[0].text_a.split(" "))
      inputs = []
      targets = []
      masks = []
  
      UNK = word2id.get("<unk>")
      PAD = word2id.get("<pad>")
      for item in batch:
          input = item.text_a.split(" ")
          target = item.label.copy()
  
          input = [word2id.get(w, UNK) for w in input]
          target = [label2id.get(l) for l in target]
          pad_len = max_len - len(input)
          assert len(input) == len(target)
          inputs.append(input + [PAD] * pad_len)
          targets.append(target + [0] * pad_len)
          masks.append([1] * len(input) + [0] * pad_len)
  
      return (
          torch.tensor(inputs),
          torch.tensor(targets),
          torch.tensor(masks).bool(),
      )
  
file_path: DeepKE/src/deepke/name_entity_re/standard/tools/dataset.py
imports: import torch
label_desc: Use image node_id 2 for calling a global function with example usage: collate_fn(batch, word2id, label2id) and returns: torch,  torch,  torch