MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / run_infer

Function run_infer

light-eval/src/eval_math.py:128–172  ·  view source on GitHub ↗
(model, max_seq_len, data_path, infer_path, overwrite = False)

Source from the content-addressed store, hash-verified

126 return prompt
127
128def run_infer(model, max_seq_len, data_path, infer_path, overwrite = False):
129
130 infer_file = os.path.join(infer_path, f'math_infer.jsonl')
131 if not overwrite and os.path.exists(infer_file):
132 print(f"{infer_file} existed, skip!")
133 return
134
135 test_set = []
136 answer_set = []
137 few_shot_prompt = open("prompt/math_prompt.txt").read()
138 with open(os.path.join(data_path, "MATH_test.jsonl"), "r+", encoding="utf8") as f:
139 for idx, item in enumerate(jsonlines.Reader(f)):
140 full_prompt = resize_prompt(
141 model.tokenizer,
142 max_seq_len,
143 few_shot_prompt +
144 "\n\nProblem: " +
145 item["instruction"] +
146 "\nAnswer: Let's think step by step.\n"
147 )
148 test_set.append(full_prompt)
149 solution = item['output']
150 temp_ans = remove_boxed(math_util.last_boxed_only_string(solution))
151 answer_set.append(temp_ans)
152
153 batch_test_set = batch_data(test_set, batch_size=8)
154
155 res_completions = []
156 for batch_input in tqdm(batch_test_set, position=0, leave=True):
157
158 outputs = model.generate(prompts=batch_input, images=None, max_gen_len=512)
159
160 for output in outputs:
161 res_completions.append(output)
162
163 torch.distributed.barrier()
164 if torch.distributed.get_rank() == 0:
165
166 with jsonlines.open(infer_file, mode='w') as writer:
167 for (completion, prompt_answer) in zip(res_completions, answer_set):
168 record = {
169 'completion': completion,
170 'target_ans': prompt_answer
171 }
172 writer.write(record)
173
174def run_eval(infer_path):
175

Callers 1

mainFunction · 0.70

Calls 5

printFunction · 0.85
remove_boxedFunction · 0.85
resize_promptFunction · 0.70
batch_dataFunction · 0.70
generateMethod · 0.45

Tested by

no test coverage detected