(examples, tokenizer, cutoff_len)
| 134 | return knapsacks |
| 135 | |
| 136 | def preprocess_packed_supervised_dataset(examples, tokenizer, cutoff_len): |
| 137 | valid_num = 0 |
| 138 | batch_input_ids, batch_labels = [], [] |
| 139 | lengths = [] |
| 140 | length2indexes = defaultdict(list) |
| 141 | for i in range(len(examples["input_ids"])): |
| 142 | input_ids, labels = examples["input_ids"][i], examples["labels"][i] |
| 143 | length = len(input_ids) |
| 144 | if length >= cutoff_len - 1: |
| 145 | continue |
| 146 | else: |
| 147 | lengths.append(length) |
| 148 | length2indexes[length].append(valid_num) |
| 149 | batch_input_ids.append(input_ids) |
| 150 | batch_labels.append(labels) |
| 151 | valid_num += 1 |
| 152 | model_inputs = defaultdict(list) |
| 153 | knapsacks = greedy_knapsack(lengths, cutoff_len - 1) |
| 154 | for knapsack in knapsacks: |
| 155 | packed_input_ids, packed_attention_masks, packed_labels = [], [], [] |
| 156 | for i, length in enumerate(knapsack): |
| 157 | index = length2indexes[length].pop() |
| 158 | packed_input_ids += batch_input_ids[index] |
| 159 | packed_labels += batch_labels[index] |
| 160 | packed_attention_masks += [1] * len(batch_input_ids[index]) |
| 161 | |
| 162 | if len(packed_input_ids) < cutoff_len: |
| 163 | pad_length = cutoff_len - len(packed_input_ids) |
| 164 | packed_input_ids += [tokenizer.pad_token_id] * pad_length |
| 165 | packed_labels += [IGNORE_INDEX] * pad_length |
| 166 | packed_attention_masks += [1] * pad_length # more efficient flash_attn |
| 167 | |
| 168 | if len(packed_input_ids) != cutoff_len: |
| 169 | raise ValueError("The length of packed example should be identical to the cutoff length.") |
| 170 | |
| 171 | model_inputs["input_ids"].append(packed_input_ids) |
| 172 | model_inputs["attention_mask"].append(packed_attention_masks) |
| 173 | model_inputs["position_ids"].append(list(range(len(packed_input_ids)))) |
| 174 | model_inputs["labels"].append(packed_labels) |
| 175 | return model_inputs |
| 176 | |
| 177 | def pad_sequence(examples, cutoff_len, tokenizer): |
| 178 | max_length = cutoff_len |
nothing calls this directly
no test coverage detected