code: import logging
  import os
  
  from transformers import glue_compute_metrics
  from transformers import glue_convert_examples_to_features as convert_examples_to_features
  from transformers import glue_output_modes
  from transformers import glue_processors
  
  from transformers.data.processors.glue import MnliMismatchedProcessor
  from transformers.data.processors.utils import InputFeatures
  from transformers.file_utils import is_tf_available
  if is_tf_available():
      import tensorflow as tf
  
  logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", level=logging.INFO
  )
  logger = logging.getLogger(__name__)
  
  from cartography.data_utils_glue import convert_string_to_unique_number
  from cartography.classification.mnli_utils import AdaptedMnliMismatchedProcessor, AdaptedMnliProcessor
  from cartography.classification.qnli_utils import AdaptedQnliProcessor
  from cartography.classification.snli_utils import SNLIProcessor
  from cartography.classification.winogrande_utils import WinograndeProcessor
  from cartography.classification.wnli_utils import AdaptedWnliProcessor
  from cartography.classification.rte_utils import AdaptedRteProcessor
  
  import pandas as pd
  
  
  glue_processors["snli"] = SNLIProcessor
  glue_processors["mnli"] = AdaptedMnliProcessor
  glue_processors["mnli-mm"] = AdaptedMnliMismatchedProcessor
  glue_processors["qnli"] = AdaptedQnliProcessor
  glue_processors["winogrande"] = WinograndeProcessor
  glue_processors["wnli"] = AdaptedWnliProcessor
  glue_processors["rte"] = AdaptedRteProcessor
  
  glue_output_modes["snli"] = "classification"
  glue_output_modes["winogrande"] = "classification"
  
  
  
  class AdaptedInputFeatures(InputFeatures):
      def __init__(self, input_ids, attention_mask=None, token_type_ids=None, label=None, example_id=None, lex = None,
                   const=None, subs=None, original_idx=None):
          self.input_ids = input_ids
          self.attention_mask = attention_mask
          self.token_type_ids = token_type_ids
          self.label = label
          self.example_id = example_id
          self.lex = lex
          self.const = const
          self.subs = subs
          self.original_idx = original_idx
  
  
  def get_instance_heuristics(task, data_split):
      mode = data_split
      ### UNCOMMENT FOR MNLI
      if 'dev' in data_split:
          mode = 'dev'
          if task.upper() == 'MNLI':
              mode = 'dev_matched'
  
      df = pd.read_csv("/home/jusun/adila001/{}/{}_heuristic.tsv".format(task.upper(), mode), delimiter="\t|\n")
      lexical = df["lexical"].tolist()
      if 'constituent' in set(df.columns):
          constituent = df["constituent"].tolist()
      else:
          constituent = [0 for i in range(df.shape[0])]
      subsequence = df["subsequence"].tolist()
      return lexical, constituent, subsequence
  
  def adapted_glue_convert_examples_to_features(
      examples,
      tokenizer,
      max_length=512,
      task=None,
      label_list=None,
      output_mode=None,
      pad_on_left=False,
      pad_token=0,
      pad_token_segment_id=0,
      mask_padding_with_zero=True,
      heuristics=True,
      data_split='train',
  ):
      """
      Adapted from `transformers`. New functionality: also return an integer ID for each example.
      Loads a data file into a list of ``InputFeatures``
  
      Args:
          examples: List of ``InputExamples`` or ``tf.data.Dataset`` containing the examples.
          tokenizer: Instance of a tokenizer that will tokenize the examples
          max_length: Maximum example length
          task: GLUE task
          label_list: List of labels. Can be obtained from the processor using the ``processor.get_labels()`` method
          output_mode: String indicating the output mode. Either ``regression`` or ``classification``
          pad_on_left: If set to ``True``, the examples will be padded on the left rather than on the right (default)
          pad_token: Padding token
          pad_token_segment_id: The segment ID for the padding token (It is usually 0, but can vary such as for XLNet where it is 4)
          mask_padding_with_zero: If set to ``True``, the attention mask will be filled by ``1`` for actual values
              and by ``0`` for padded values. If set to ``False``, inverts it (``1`` for padded values, ``0`` for
              actual values)
  
      Returns:
          If the ``examples`` input is a ``tf.data.Dataset``, will return a ``tf.data.Dataset``
          containing the task-specific features. If the input is a list of ``InputExamples``, will return
          a list of task-specific ``InputFeatures`` which can be fed to the model.
  
      """
      is_tf_dataset = False
      if is_tf_available() and isinstance(examples, tf.data.Dataset):
          is_tf_dataset = True
  
      if task is not None:
          processor = glue_processors[task]()
          if label_list is None:
              label_list = processor.get_labels()
              logger.info("Using label list %s for task %s" % (label_list, task))
          if output_mode is None:
              output_mode = glue_output_modes[task]
              logger.info("Using output mode %s for task %s" % (output_mode, task))
  
      label_map = {label: i for i, label in enumerate(label_list)}
  
      features = []
      lex = []
      const= []
      subs = []
      if heuristics==True:
          lex, const, subs = get_instance_heuristics(task, data_split)
  
      for (ex_index, example) in enumerate(examples):
          len_examples = 0
          if is_tf_dataset:
              example = processor.get_example_from_tensor_dict(example)
              example = processor.tfds_map(example)
              len_examples = tf.data.experimental.cardinality(examples)
          else:
              len_examples = len(examples)
          if ex_index % 10000 == 0:
              logger.info("Writing example %d/%d" % (ex_index, len_examples))
  
          inputs = tokenizer.encode_plus(example.text_a, example.text_b, add_special_tokens=True, max_length=max_length,)
          input_ids, token_type_ids = inputs["input_ids"], inputs["token_type_ids"]
  
          # The mask has 1 for real tokens and 0 for padding tokens. Only real
          # tokens are attended to.
          attention_mask = [1 if mask_padding_with_zero else 0] * len(input_ids)
  
          # Zero-pad up to the sequence length.
          padding_length = max_length - len(input_ids)
          if pad_on_left:
              input_ids = ([pad_token] * padding_length) + input_ids
              attention_mask = ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
              token_type_ids = ([pad_token_segment_id] * padding_length) + token_type_ids
          else:
              input_ids = input_ids + ([pad_token] * padding_length)
              attention_mask = attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)
              token_type_ids = token_type_ids + ([pad_token_segment_id] * padding_length)
  
          assert len(input_ids) == max_length, "Error with input length {} vs {}".format(len(input_ids), max_length)
          assert len(attention_mask) == max_length, "Error with input length {} vs {}".format(
              len(attention_mask), max_length
          )
          assert len(token_type_ids) == max_length, "Error with input length {} vs {}".format(
              len(token_type_ids), max_length
          )
  
          if output_mode == "classification":
              label = label_map[example.label]
          elif output_mode == "regression":
              label = float(example.label)
          else:
              raise KeyError(output_mode)
  
          example_int_id = convert_string_to_unique_number(example.guid)
          if ex_index < 5:
              logger.info("*** Example ***")
              logger.info(f"guid: {example_int_id}")
              logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
              logger.info("attention_mask: %s" % " ".join([str(x) for x in attention_mask]))
              logger.info("token_type_ids: %s" % " ".join([str(x) for x in token_type_ids]))
              logger.info("label: %s (id = %d)" % (example.label, label))
  
          features.append(
              AdaptedInputFeatures(input_ids=input_ids,
                                   attention_mask=attention_mask,
                                   token_type_ids=token_type_ids,
                                   label=label,
                                   example_id=example_int_id,
                                   lex=None if len(lex)==0 else lex[ex_index],
                                   const=None if len(const)==0 else const[ex_index],
                                   subs=None if len(const)==0else subs[ex_index],
                                   original_idx=ex_index))
      if is_tf_available() and is_tf_dataset:
  
          def gen():
              for ex in features:
                  yield (
                      {
                          "input_ids": ex.input_ids,
                          "attention_mask": ex.attention_mask,
                          "token_type_ids": ex.token_type_ids,
                      },
                      ex.label,
                  )
  
          return tf.data.Dataset.from_generator(
              gen,
              ({"input_ids": tf.int32, "attention_mask": tf.int32, "token_type_ids": tf.int32}, tf.int64),
              (
                  {
                      "input_ids": tf.TensorShape([None]),
                      "attention_mask": tf.TensorShape([None]),
                      "token_type_ids": tf.TensorShape([None]),
                  },
                  tf.TensorShape([]),
              ),
          )
      return features
  
  
  def adapted_glue_compute_metrics(task_name, preds, labels):
      "Adapted from `glue_compute_metrics` to also handle SNLI."
      try:
        return glue_compute_metrics(task_name, preds, labels)
      except KeyError:
        if task_name in ["snli", "winogrande", "toxic"]:
          # Since MNLI also uses accuracy.
          return glue_compute_metrics("mnli", preds, labels)
      raise KeyError(task_name)
  
  
apis: tensorflow.data.experimental.cardinality tensorflow.TensorShape