|
| 1 | +from collections import OrderedDict |
| 2 | +from functools import partial |
| 3 | + |
| 4 | +from transformers import AutoTokenizer |
| 5 | +from .sentence_keys_auto import get_sentence_keys |
| 6 | + |
| 7 | + |
| 8 | +def inserting_sepp(sent, start, end, this_tokenizer): |
| 9 | + return \ |
| 10 | + sent[:start].rstrip() + " " + this_tokenizer.sep_token + " " + sent[start:end] \ |
| 11 | + + " " + this_tokenizer.sep_token + " " + sent[end:].lstrip() |
| 12 | + |
| 13 | + |
| 14 | +def tokenize_superglue_copa(this_example, |
| 15 | + this_tokenizer, |
| 16 | + dataset_name, |
| 17 | + subdataset_name=None, |
| 18 | + **kwargs): |
| 19 | + return None |
| 20 | + |
| 21 | + |
| 22 | +def tokenize_superglue_wic_gpt2(this_example, |
| 23 | + this_tokenizer, |
| 24 | + dataset_name, |
| 25 | + subdataset_name=None, |
| 26 | + **kwargs): |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +def tokenize_superglue_wic(this_example, |
| 31 | + this_tokenizer, |
| 32 | + dataset_name, |
| 33 | + subdataset_name=None, |
| 34 | + **kwargs |
| 35 | + ): |
| 36 | + """ |
| 37 | + tokenize the data from the wic task (word-in-context dataset), |
| 38 | + e.g., sentence 1: "There's a lot of trash on the bed of the river" |
| 39 | + sentence 2: "I keep a glass of water next to my bed when I sleep", |
| 40 | + label = False (different word senses) |
| 41 | + In the superglue data, the position of the word in sentence 1 and 2 are provided |
| 42 | + What this function does is to update the span position after tokenization, based on each LM's own tokenizer, |
| 43 | + The key is to insert an [SEP] before and after the original sentence, then feed it into the LM's tokenizer. |
| 44 | + There are two challenges: |
| 45 | + (1) Each LM's tokenizations are different, e.g., in XLNet's tokenizer, the paddings are on the left' |
| 46 | + (2) Some LM's tokenization would add an underline symbol before the word, e.g., "There's a lot" |
| 47 | + -> [_There, _', _s, _a, _lot] |
| 48 | + When underline meets special char such as '"', "'", the tokenized sequence after adding [SEP] needs to be |
| 49 | + aligned with the sequence tokenized without [SEP]. We use a two pointer algorithm for the alignment |
| 50 | + """ |
| 51 | + sent1, sent2 = this_example["sentence1"], this_example["sentence2"] |
| 52 | + start1, end1 = this_example["start1"], this_example["end1"] |
| 53 | + start2, end2 = this_example["start2"], this_example["end2"] |
| 54 | + """ |
| 55 | + Add [SEP] to the sentence |
| 56 | + """ |
| 57 | + altered_sent1 = inserting_sepp(sent1, start1, end1, this_tokenizer) |
| 58 | + altered_sent2 = inserting_sepp(sent2, start2, end2, this_tokenizer) |
| 59 | + input_ids_sepp = this_tokenizer(*(altered_sent1, altered_sent2), |
| 60 | + padding="max_length", |
| 61 | + max_length=1024, |
| 62 | + truncation=True)["input_ids"] |
| 63 | + data_pair = (sent1, sent2) |
| 64 | + assert "max_seq_length" in kwargs, "max_seq_length must be provided for glue" |
| 65 | + this_data = this_tokenizer(*data_pair, padding="max_length", max_length=kwargs["max_seq_length"], truncation=True) |
| 66 | + input_ids = this_data["input_ids"] |
| 67 | + which_sepp = 0 |
| 68 | + |
| 69 | + """ |
| 70 | + span_start_end: a 2x2 array: |
| 71 | + * (span_start_end[0][0], span_start_end[0][1]) are the spans of the position of the word in the first sentence |
| 72 | + * (span_start_end[1][0], span_start_end[1][1]) are the spans of the position of the word in the second sentence |
| 73 | + """ |
| 74 | + span_start_end = [[-1, -1], [-1, -1]] |
| 75 | + |
| 76 | + ptr_sepp = ptr_nosepp = 0 |
| 77 | + try: |
| 78 | + padding_direction = this_tokenizer.padding_side |
| 79 | + if padding_direction == "left": |
| 80 | + padding_id = input_ids_sepp[0] |
| 81 | + while input_ids_sepp[ptr_sepp] == padding_id: |
| 82 | + ptr_sepp += 1 |
| 83 | + while input_ids[ptr_nosepp] == padding_id: |
| 84 | + ptr_nosepp += 1 |
| 85 | + except KeyError: |
| 86 | + pass |
| 87 | + sep_id = this_tokenizer.convert_tokens_to_ids([this_tokenizer.sep_token])[0] |
| 88 | + """ |
| 89 | + use two pointers to align the tokenized sequence before and after adding [SEP]; |
| 90 | + ptr_sepp: the pointer after adding; ptr_nosepp: the pointer without adding |
| 91 | + """ |
| 92 | + while ptr_sepp < len(input_ids_sepp) and ptr_nosepp < len(input_ids) and \ |
| 93 | + input_ids_sepp[ptr_sepp] != 0 and input_ids[ptr_nosepp] != 0: |
| 94 | + if input_ids_sepp[ptr_sepp] == input_ids[ptr_nosepp]: |
| 95 | + ptr_sepp += 1 |
| 96 | + ptr_nosepp += 1 |
| 97 | + else: |
| 98 | + if not (input_ids_sepp[ptr_sepp] == sep_id |
| 99 | + or this_tokenizer.convert_ids_to_tokens([input_ids_sepp[ptr_sepp]])[0] in ('▁', '_')): |
| 100 | + break |
| 101 | + if input_ids_sepp[ptr_sepp] == sep_id: |
| 102 | + span_start_end[int(which_sepp / 2)][which_sepp % 2] = ptr_nosepp |
| 103 | + which_sepp += 1 |
| 104 | + ptr_sepp += 1 |
| 105 | + else: |
| 106 | + ptr_sepp += 1 |
| 107 | + """ |
| 108 | + max_word_span is the maximum tokens of the word |
| 109 | + It is set to 16 following deberta: |
| 110 | + https://github.com/microsoft/DeBERTa/blob/master/DeBERTa/apps/tasks/superglue_tasks.py#L1054 |
| 111 | + """ |
| 112 | + max_word_span = 16 |
| 113 | + word_indices = [] |
| 114 | + for idx1 in range(2): |
| 115 | + if span_start_end[idx1][1] < kwargs["max_seq_length"]: |
| 116 | + first_span = [x for x in range(span_start_end[idx1][0], span_start_end[idx1][1]) |
| 117 | + if x < kwargs["max_seq_length"]] + [0] * (max_word_span - span_start_end[idx1][1] |
| 118 | + + span_start_end[idx1][0]) |
| 119 | + word_indices.append(first_span) |
| 120 | + this_data["word_spans"] = word_indices |
| 121 | + return this_data |
| 122 | + |
| 123 | + |
| 124 | +def tokenize_glue(this_example, |
| 125 | + this_tokenizer, |
| 126 | + dataset_name, |
| 127 | + subdataset_name=None, |
| 128 | + **kwargs): |
| 129 | + sentence_keys = get_sentence_keys(dataset_name, subdataset_name) |
| 130 | + |
| 131 | + if len(sentence_keys) > 1: |
| 132 | + sentence1_key, sentence2_key = sentence_keys[0], sentence_keys[1] |
| 133 | + else: |
| 134 | + sentence1_key = sentence_keys[0] |
| 135 | + sentence2_key = None |
| 136 | + |
| 137 | + data_pair = ( |
| 138 | + (this_example[sentence1_key],) if sentence2_key is None else ( |
| 139 | + this_example[sentence1_key], this_example[sentence2_key]) |
| 140 | + ) |
| 141 | + assert "max_seq_length" in kwargs, "max_seq_length must be provided for glue" |
| 142 | + return this_tokenizer(*data_pair, padding="max_length", max_length=kwargs["max_seq_length"], truncation=True) |
| 143 | + |
| 144 | + |
| 145 | +TOKENIZER_MAPPING = OrderedDict( |
| 146 | + [ |
| 147 | + (("glue", "rte"), tokenize_glue), |
| 148 | + (("glue", "mrpc"), tokenize_glue), |
| 149 | + (("glue", "cola"), tokenize_glue), |
| 150 | + (("glue", "wnli"), tokenize_glue), |
| 151 | + (("glue", "stsb"), tokenize_glue), |
| 152 | + (("glue", "sst2"), tokenize_glue), |
| 153 | + (("glue", "mnli"), tokenize_glue), |
| 154 | + (("glue", "qqp"), tokenize_glue), |
| 155 | + (("glue", "qnli"), tokenize_glue), |
| 156 | + (("super_glue", "wic"), tokenize_superglue_wic), |
| 157 | + ] |
| 158 | +) |
| 159 | + |
| 160 | + |
| 161 | +class AutoEncodeText: |
| 162 | + """ |
| 163 | + This is a generic input text tokenization class that will be instantiated as one of the |
| 164 | + tokenization classes of the library when created with the |
| 165 | + `~flaml.nlp.dataset.AutoEncodeText.from_model_and_dataset_name` class method. |
| 166 | +
|
| 167 | + This class cannot be instantiated directly using ``__init__()`` (throws an error). |
| 168 | + """ |
| 169 | + |
| 170 | + def __init__(self): |
| 171 | + raise EnvironmentError( |
| 172 | + "AutoEncodeText is designed to be instantiated " |
| 173 | + "using the `AutoEncodeText.from_model_and_dataset_name(cls," |
| 174 | + "data_raw,model_checkpoint_path,dataset_name,subdataset_name = None,**kwargs)` methods." |
| 175 | + ) |
| 176 | + |
| 177 | + @classmethod |
| 178 | + def from_model_and_dataset_name(cls, |
| 179 | + data_raw, |
| 180 | + model_checkpoint_path, |
| 181 | + dataset_name, |
| 182 | + subdataset_name=None, |
| 183 | + **kwargs): |
| 184 | + """ |
| 185 | + Instantiate one of the input text tokenization classes from the raw data, model checkpoint path, dataset name |
| 186 | + and sub dataset name. The raw data is used for creating a mapping function from the raw tokens to the |
| 187 | + tokenized token ids. |
| 188 | +
|
| 189 | + Args: |
| 190 | + data_raw: |
| 191 | + The raw data (a datasets.Dataset object) |
| 192 | +
|
| 193 | + model_checkpoint_path: |
| 194 | + A string variable which specifies the model path, e.g., "google/electra-base-discriminator" |
| 195 | +
|
| 196 | + dataset_name: |
| 197 | + A string variable which is the dataset name, e.g., "glue" |
| 198 | +
|
| 199 | + subdataset_name: |
| 200 | + A string variable which is the sub dataset name,e.g., "rte" |
| 201 | +
|
| 202 | + kwargs: |
| 203 | + The values in kwargs of any keys will be used for the mapping function |
| 204 | +
|
| 205 | + Examples: |
| 206 | + >>> from datasets import load_dataset |
| 207 | + >>> data_raw = load_dataset("glue", "rte") |
| 208 | + >>> AutoEncodeText.from_model_and_dataset_name(data_raw, "google/electra-base-discriminator", ["glue"], "rte") |
| 209 | +
|
| 210 | + """ |
| 211 | + if (dataset_name, subdataset_name) in TOKENIZER_MAPPING.keys(): |
| 212 | + this_tokenizer = AutoTokenizer.from_pretrained(model_checkpoint_path, use_fast=True) |
| 213 | + token_func = TOKENIZER_MAPPING[(dataset_name, subdataset_name)] |
| 214 | + return data_raw.map( |
| 215 | + partial(token_func, |
| 216 | + this_tokenizer=this_tokenizer, |
| 217 | + dataset_name=dataset_name, |
| 218 | + subdataset_name=subdataset_name, |
| 219 | + **kwargs), batched=False) |
| 220 | + raise ValueError( |
| 221 | + "Unrecognized method {},{} for this kind of AutoGridSearchSpace: {}.\n" |
| 222 | + "Method name should be one of {}.".format( |
| 223 | + dataset_name, subdataset_name, cls.__name__, ", ".join(c.__name__ for c in TOKENIZER_MAPPING.keys()) |
| 224 | + ) |
| 225 | + ) |
0 commit comments