Generate samples from the policy (and reference model, if doing DPO training) for the given batch of inputs.
(self, batch: Dict[str, torch.LongTensor])
| 180 | rank0_print(f'Loaded {len(self.eval_batches)} eval batches of size {config.eval_batch_size}') |
| 181 | |
| 182 | def get_batch_samples(self, batch: Dict[str, torch.LongTensor]) -> Tuple[str, str]: |
| 183 | """Generate samples from the policy (and reference model, if doing DPO training) for the given batch of inputs.""" |
| 184 | |
| 185 | # FSDP generation according to https://github.com/pytorch/pytorch/issues/100069 |
| 186 | ctx = lambda: (FSDP.summon_full_params(self.policy, writeback=False, recurse=False) if 'FSDP' in self.config.trainer else contextlib.nullcontext()) |
| 187 | with ctx(): |
| 188 | policy_output = self.policy.generate( |
| 189 | batch['prompt_input_ids'], attention_mask=batch['prompt_attention_mask'], max_length=self.config.max_length, do_sample=True, pad_token_id=self.tokenizer.pad_token_id) |
| 190 | |
| 191 | if self.config.loss.name in {'dpo', 'ipo'}: |
| 192 | ctx = lambda: (FSDP.summon_full_params(self.reference_model, writeback=False, recurse=False) if 'FSDP' in self.config.trainer else contextlib.nullcontext()) |
| 193 | with ctx(): |
| 194 | reference_output = self.reference_model.generate( |
| 195 | batch['prompt_input_ids'], attention_mask=batch['prompt_attention_mask'], max_length=self.config.max_length, do_sample=True, pad_token_id=self.tokenizer.pad_token_id) |
| 196 | |
| 197 | policy_output = pad_to_length(policy_output, self.config.max_length, self.tokenizer.pad_token_id) |
| 198 | policy_output = all_gather_if_needed(policy_output, self.rank, self.world_size) |
| 199 | policy_output_decoded = self.tokenizer.batch_decode(policy_output, skip_special_tokens=True) |
| 200 | |
| 201 | if self.config.loss.name in {'dpo', 'ipo'}: |
| 202 | reference_output = pad_to_length(reference_output, self.config.max_length, self.tokenizer.pad_token_id) |
| 203 | reference_output = all_gather_if_needed(reference_output, self.rank, self.world_size) |
| 204 | reference_output_decoded = self.tokenizer.batch_decode(reference_output, skip_special_tokens=True) |
| 205 | else: |
| 206 | reference_output_decoded = [] |
| 207 | |
| 208 | return policy_output_decoded, reference_output_decoded |
| 209 | |
| 210 | def concatenated_forward(self, model: nn.Module, batch: Dict[str, Union[List, torch.LongTensor]]) -> Tuple[torch.FloatTensor, torch.FloatTensor]: |
| 211 | """Run the given model on the given batch of inputs, concatenating the chosen and rejected inputs together. |
no test coverage detected