peiyi9979 commited on
Commit
58489d7
1 Parent(s): e396f74

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +41 -0
README.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Process reward models (mistral-7b) used in [Math-Shepherd](https://arxiv.org/pdf/2312.08935.pdf).
2
+
3
+ `Input`: question + step-by-step solutions with a special step tag `ки`, e.g.,
4
+ ```
5
+ Janet\u2019s ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes .... ? Step 1: Janet's ducks lay 16 eggs per day. ки\nStep 2: She eats three for breakfast every morning, so she has 16 - 3 = 13 eggs left. ки\nStep 3: She bakes muffins for her friends every day with four eggs, so she has 13 - 4 = 9 eggs left. ки\nStep 4: She sells the remainder at the farmers' market daily for $2 per fresh duck egg, so she makes 9 * $2 = $18 every day at the farmers' market. The answer is: 18 ки
6
+ ```
7
+ `Output`: the logits. You need to post-process it to achieve the score of each step.
8
+
9
+
10
+ ```python
11
+ from transformers import AutoTokenizer
12
+ from transformers import AutoModelForCausalLM
13
+ import torch
14
+
15
+ good_token = '+'
16
+ bad_token = '-'
17
+ step_tag = 'ки'
18
+
19
+ tokenizer = AutoTokenizer.from_pretrained('peiyi9979/math-shepherd-mistral-7b-prm')
20
+ candidate_tokens = tokenizer.encode(f"{good_token} {bad_token}")[1:] # [648, 387]
21
+ step_tag_id = tokenizer.encode(f"{step_tag}")[-1] # 12902
22
+ model = AutoModelForCausalLM.from_pretrained('peiyi9979/math-shepherd-mistral-7b-prm').eval()
23
+
24
+ question = """Janet\u2019s ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?"""
25
+ output1 = """Step 1: Janet's ducks lay 16 eggs per day. ки\nStep 2: She eats three for breakfast every morning, so she has 16 - 3 = 13 eggs left. ки\nStep 3: She bakes muffins for her friends every day with four eggs, so she has 13 - 4 = 9 eggs left. ки\nStep 4: She sells the remainder at the farmers' market daily for $2 per fresh duck egg, so she makes 9 * $2 = $18 every day at the farmers' market. The answer is: 18 ки""" # 18 is right
26
+ output2 = """Step 1: Janet's ducks lay 16 eggs per day. ки\nStep 2: She eats three for breakfast every morning, so she has 16 - 3 = 13 eggs left. ки\nStep 3: She bakes muffins for her friends every day with four eggs, so she has 13 - 4 = 9 eggs left. ки\nStep 4: She sells the remainder at the farmers' market daily for $2 per fresh duck egg, so she makes 9 * $2 = $17 every day at the farmers' market. The answer is: 17 ки""" # 17 is wrong
27
+
28
+ for output in [output1, output2]:
29
+ input_for_prm = f"{question} {output}"
30
+ input_id = torch.tensor([tokenizer.encode(input_for_prm)])
31
+
32
+ with torch.no_grad():
33
+ logits = model(input_id).logits[:,:,candidate_tokens]
34
+ scores = logits.softmax(dim=-1)[:,:,0]
35
+ step_scores = scores[input_id == step_tag_id]
36
+ print(step_scores)
37
+
38
+ # tensor([0.9955, 0.9958, 0.9983, 0.9957])
39
+ # tensor([0.9955, 0.9958, 0.9983, 0.0240])
40
+
41
+ ```