(model, max_seq_len, tasks, data_path, infer_path, mode, overwrite = False)
| 135 | return prompt |
| 136 | |
| 137 | def run_infer(model, max_seq_len, tasks, data_path, infer_path, mode, overwrite = False): |
| 138 | |
| 139 | for task in tasks: |
| 140 | |
| 141 | task_infer_path = os.path.join(infer_path, f'{task}_infer.jsonl') |
| 142 | if not overwrite and os.path.exists(task_infer_path): |
| 143 | print(f"{task_infer_path} existed, skip!") |
| 144 | continue |
| 145 | |
| 146 | print(f'Testing {task} ...') |
| 147 | task_data = json.load(open(os.path.join(data_path, f'bbh/{task}.json'))) |
| 148 | with open(os.path.join(data_path, f'cot-prompts/{task}.txt'), 'r') as f: |
| 149 | task_prompt = f.readlines()[2:] |
| 150 | task_prompt = ''.join(task_prompt) |
| 151 | |
| 152 | test_set = [] |
| 153 | answer_set = [] |
| 154 | for item in task_data['examples']: |
| 155 | full_prompt = resize_prompt( |
| 156 | model.tokenizer, |
| 157 | max_seq_len, |
| 158 | task_prompt + |
| 159 | '\n\nQ: ' + |
| 160 | item['input'] + |
| 161 | "\nA: Let's think step by step." |
| 162 | ) |
| 163 | |
| 164 | test_set.append(full_prompt) |
| 165 | if mode == 'multiple_choice': |
| 166 | answer_set.append(item['target'][1]) |
| 167 | elif mode == 'free_form': |
| 168 | answer_set.append(item['target']) |
| 169 | |
| 170 | batch_prompt = batch_data(test_set, batch_size=8) |
| 171 | res_completions = [] |
| 172 | for batch_input in tqdm(batch_prompt): |
| 173 | |
| 174 | outputs = model.generate(prompts=batch_input, images=None, max_gen_len=1024) |
| 175 | |
| 176 | for output in outputs: |
| 177 | res_completions.append(output) |
| 178 | |
| 179 | torch.distributed.barrier() |
| 180 | if torch.distributed.get_rank() == 0: |
| 181 | with jsonlines.open(task_infer_path, mode='w') as writer: |
| 182 | for (prompt, completion, prompt_answer) in zip(task_data['examples'], res_completions, answer_set): |
| 183 | record = {'prompt': prompt, |
| 184 | 'completion': completion, |
| 185 | 'target_ans': prompt_answer |
| 186 | } |
| 187 | writer.write(record) |
| 188 | |
| 189 | def run_eval(tasks, infer_path, mode): |
| 190 |
no test coverage detected