(self, instances: Sequence[Dict])
| 26 | predict_with_generate: bool |
| 27 | |
| 28 | def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]: |
| 29 | # Extract elements |
| 30 | sources = [f"{self.tokenizer.bos_token}{example['input']}" for example in instances] |
| 31 | targets = [f"{example['output']}{self.tokenizer.eos_token}" for example in instances] |
| 32 | # Tokenize |
| 33 | tokenized_sources_with_prompt = self.tokenizer( |
| 34 | sources, |
| 35 | max_length=self.source_max_len, |
| 36 | truncation=True, |
| 37 | add_special_tokens=False, |
| 38 | ) |
| 39 | tokenized_targets = self.tokenizer( |
| 40 | targets, |
| 41 | max_length=self.target_max_len, |
| 42 | truncation=True, |
| 43 | add_special_tokens=False, |
| 44 | ) |
| 45 | # Build the input and labels for causal LM |
| 46 | input_ids = [] |
| 47 | labels = [] |
| 48 | for tokenized_source, tokenized_target in zip( |
| 49 | tokenized_sources_with_prompt['input_ids'], |
| 50 | tokenized_targets['input_ids'] |
| 51 | ): |
| 52 | if not self.predict_with_generate: |
| 53 | input_ids.append(torch.tensor(tokenized_source + tokenized_target)) |
| 54 | if not self.train_on_source: |
| 55 | labels.append( |
| 56 | torch.tensor([IGNORE_INDEX for _ in range(len(tokenized_source))] + copy.deepcopy(tokenized_target)) |
| 57 | ) |
| 58 | else: |
| 59 | labels.append(torch.tensor(copy.deepcopy(tokenized_source + tokenized_target))) |
| 60 | else: |
| 61 | input_ids.append(torch.tensor(tokenized_source)) |
| 62 | # Apply padding |
| 63 | input_ids = pad_sequence(input_ids, batch_first=True, padding_value=self.tokenizer.pad_token_id) |
| 64 | labels = pad_sequence(labels, batch_first=True, padding_value=IGNORE_INDEX) if not self.predict_with_generate else None |
| 65 | data_dict = { |
| 66 | 'input_ids': input_ids, |
| 67 | 'attention_mask':input_ids.ne(self.tokenizer.pad_token_id), |
| 68 | } |
| 69 | if labels is not None: |
| 70 | data_dict['labels'] = labels |
| 71 | return data_dict |
| 72 | |
| 73 | |
| 74 | ALPACA_PROMPT_DICT = { |
nothing calls this directly
no outgoing calls
no test coverage detected