(self, tasks: List[Task], **kwargs)
| 134 | return score |
| 135 | |
| 136 | def process(self, tasks: List[Task], **kwargs): |
| 137 | reward_tasks = [] |
| 138 | for task in tasks: |
| 139 | if self.split_steps: |
| 140 | steps = task.output_str.split(self.step_token) |
| 141 | content = "".join( |
| 142 | (step + self.separate_token) for step in steps) |
| 143 | else: |
| 144 | content = self.separate_token + task.output_str + self.separate_token |
| 145 | # Combine messages using chat template |
| 146 | messages = [ |
| 147 | { |
| 148 | "role": |
| 149 | "system", |
| 150 | "content": |
| 151 | "Please reason step by step, and put your final answer within \\boxed{}." |
| 152 | }, |
| 153 | { |
| 154 | "role": "user", |
| 155 | "content": task.input_str |
| 156 | }, |
| 157 | { |
| 158 | "role": "assistant", |
| 159 | "content": content |
| 160 | }, |
| 161 | ] |
| 162 | processed_prompt = self.tokenizer.apply_chat_template( |
| 163 | messages, tokenize=False, add_generation_prompt=False) |
| 164 | |
| 165 | # TODO: support input_ids as model input, avoid doing it again in worker |
| 166 | reward_task = GenerationTask.create_from_prompt(processed_prompt) |
| 167 | reward_task.worker_tag = self.WorkerTag.REWARD |
| 168 | |
| 169 | # TODO: pack this logic |
| 170 | reward_task.max_tokens = 1 |
| 171 | reward_task.return_context_logits = True |
| 172 | reward_tasks.append(reward_task) |
| 173 | |
| 174 | yield reward_tasks |
| 175 | |
| 176 | scores = [] |
| 177 | for reward_task in reward_tasks: |
| 178 | assert reward_task.context_logits is not None |
| 179 | # TODO: consider running on cpu to not interrupt worker or move |
| 180 | # tokenizer to a worker |
| 181 | input_ids = self.tokenizer.encode( |
| 182 | reward_task.input_str, |
| 183 | return_tensors="pt", |
| 184 | ).to(reward_task.context_logits.device) |
| 185 | |
| 186 | if self.split_steps: |
| 187 | # TODO: align add_special_tokens with SamplingParams |
| 188 | token_mask = (input_ids == self.tokenizer.encode( |
| 189 | self.separate_token, add_special_tokens=True)[0]) |
| 190 | score = self._calc_steps_score(reward_task.context_logits, |
| 191 | token_mask) |
| 192 | else: |
| 193 | score = self._calc_last_token_score(reward_task.context_logits) |
nothing calls this directly
no test coverage detected