lordChipotle commited on
Commit
c040fc4
·
verified ·
1 Parent(s): a36e863

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +147 -116
README.md CHANGED
@@ -1,118 +1,149 @@
1
- <model-card>
2
- <model-name>Llama‑3 8B GRPO‑GSM8K‑Merged</model-name>
3
- <overview>
4
- This model is a **Meta Llama‑3 8B‑Instruct** base checkpoint that was
5
- post‑trained with **Group‑wise Reinforcement Policy Optimization (GRPO)**
6
- on the **GSM8K grade‑school math dataset**. We merged the LoRA adapter
7
- into the full‑precision base weights, so no external adapter file is
8
- required at inference time.
9
- </overview>
10
-
11
- <model-details>
12
- <architecture>Meta Llama‑3 8B‑Instruct</architecture>
13
- <parameter-count>8.03 B (after LoRA merge; same as base)</parameter-count>
14
- <training-frameworks>Transformers 4.47, PEFT 0.10, TRL 0.8</training-frameworks>
15
- <license>LLAMA 3 Community License v1 + MIT (for GSM8K)</license>
16
- <language>English</language>
17
- </model-details>
18
-
19
- <dataset>
20
- <name>openai/gsm8k&nbsp;("main" split)</name>
21
- <size>7 473 train, 1 319 test</size>
22
- <data-processing>
23
- Each example was converted to a chat‑style prompt with a fixed system
24
- instruction that forces the model to wrap its chain‑of‑thought in
25
- <thinking>…</thinking> and the final numeric answer in
26
- <answer>…</answer>. The gold integer (extracted from the “####&nbsp;”
27
- line) was stored separately for reward computation.
28
- </data-processing>
29
- </dataset>
30
-
31
- <training-procedure>
32
- <algorithm>GRPO (Group‑wise Reinforcement Policy Optimization)</algorithm>
33
- <num-generations>2 completions per prompt</num-generations>
34
- <num-epochs>3</num-epochs>
35
- <learning-rate>2 × 10⁻⁴, cosine schedule, 500 warm‑up steps</learning-rate>
36
- <kl-beta>0.02 (KL penalty vs. frozen reference)</kl-beta>
37
- <batching>
38
- Per‑device batch = 8, grad‑accum = 4 → effective 32
39
- </batching>
40
- <hardware>
41
- Single NVIDIA A100 80 GB; flash‑attention disabled (SDPA used).
42
- </hardware>
43
- <reward-functions>
44
- (1) strict&nbsp;XML‑format (+1/‑1)
45
- (2) no‑tail‑after‑&lt;/answer&gt; (+0/‑0.5)
46
- (3) integer‑parsable (+1/‑1)
47
- (4) answer‑correctness (+1/0)
48
- summed to produce composite reward in [‑3.5, 4].
49
- </reward-functions>
50
- <stopping-criteria>
51
- 3 epochs or early stop if validation reward plateaued for 1 000 steps.
52
- </stopping-criteria>
53
- </training-procedure>
54
-
55
- <evaluation>
56
- <metric>Exact integer match on GSM8K test split</metric>
57
- <prompt-style>Greedy decoding, max_new_tokens = 128, temperature = 0</prompt-style>
58
- <accuracy>TODO %  <!-- replace with your final number after eval rerun --></accuracy>
59
- <note>Accuracy uses the integer after the “####” line as ground truth.</note>
60
- </evaluation>
61
-
62
- <intended-uses>
63
- <primary>
64
- • Tutoring or explanation of basic arithmetic word problems.
65
- • As a reward‑model or verifier for chain‑of‑thought tasks.
66
- </primary>
67
- <out-of-scope>
68
- • High‑stakes numeric reasoning (finance, medicine).
69
- • Non‑English math word problems (not in training set).
70
- • Any use that violates the LLAMA 3 or GSM8K licenses.
71
- </out-of-scope>
72
- </intended-uses>
73
-
74
- <limitations>
75
- • Still hallucinates reasoning steps; only the final integer is enforced.
76
- • Trained solely on short grade‑school problems (< 10 sentences).
77
- • No exposure to multi‑digit multiplication/division beyond GSM8K scope.
78
- • English‑only; performance on other languages unverified.
79
- </limitations>
80
-
81
- <ethical-considerations>
82
- • The chain‑of‑thought text may leak private reasoning if used on sensitive
83
- user queries; consider stripping <thinking> tags in production.
84
- • GSM8K is MIT‑licensed public data, but it contains synthetic examples that
85
- may reflect unintended cultural bias.
86
- </ethical-considerations>
87
-
88
- <how-to-use>
89
- <![CDATA[
90
- import torch
91
  from transformers import AutoModelForCausalLM, AutoTokenizer
92
 
93
- repo = "your-handle/llama3-gsm8k-grpo-merged"
94
- tok = AutoTokenizer.from_pretrained(repo)
95
- tok.pad_token = tok.eos_token # safety
96
- model = AutoModelForCausalLM.from_pretrained(repo, torch_dtype="auto").cuda()
97
-
98
- prompt = [
99
- {"role":"system", "content":"Respond in the required format."},
100
- {"role":"user", "content":"If a room has 5 chairs and each chair ..."}]
101
-
102
- text = tok.apply_chat_template(prompt, add_generation_prompt=True, tokenize=False)
103
- out = model.generate(**tok(text, return_tensors="pt").to(model.device),
104
- max_new_tokens=128, temperature=0)
105
- print(tok.decode(out[0], skip_special_tokens=True))
106
- ]]>
107
- </how-to-use>
108
-
109
- <citation>
110
- @misc{your2025llama3grpo,
111
- title = {Llama‑3 8B GRPO‑finetuned on GSM8K},
112
- author = {Your Name},
113
- year = {2025},
114
- howpublished = {HuggingFace repository},
115
- url = {https://huggingface.co/your-handle/llama3-gsm8k-grpo-merged}
116
- }
117
- </citation>
118
- </model-card>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Llama 3.1-8B Mathematical Reasoning (GRPO)
2
+
3
+ ## Model Description
4
+
5
+ This model is a fine-tuned version of [meta-llama/Meta-Llama-3.1-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) trained using **Group Relative Policy Optimization (GRPO)** to enable autonomous mathematical reasoning capabilities. Unlike traditional supervised fine-tuning approaches, this model learned to reason through reinforcement learning without requiring pre-labeled reasoning examples or teacher model distillation.
6
+
7
+ ## Key Features
8
+
9
+ - **Structured Reasoning**: Generates step-by-step mathematical reasoning enclosed in `<thinking>` tags
10
+ - **Formatted Answers**: Provides final numerical answers in `<answer>` tags
11
+ - **Autonomous Learning**: Trained using reward functions rather than human-annotated reasoning examples
12
+ - **Mathematical Focus**: Optimized for grade school math word problems requiring multi-step reasoning
13
+
14
+ ## Training Details
15
+
16
+ ### Training Method
17
+ - **Algorithm**: Group Relative Policy Optimization (GRPO)
18
+ - **Base Model**: meta-llama/Meta-Llama-3.1-8B-Instruct
19
+ - **Fine-tuning**: LoRA with NF4 quantization
20
+ - **Dataset**: GSM8K (Grade School Math 8K)
21
+
22
+ ### Training Configuration
23
+ - **LoRA rank**: 8
24
+ - **LoRA alpha**: 32
25
+ - **Learning rate**: 2e-4
26
+ - **Batch size**: 32 (effective)
27
+ - **Generations per prompt**: 2-4
28
+ - **Max completion length**: 128-256 tokens
29
+ - **Temperature**: 0.7
30
+
31
+ ### Reward Functions
32
+ The model was trained using multiple custom reward functions:
33
+ 1. **Format compliance**: Ensures proper `<thinking>` and `<answer>` tag structure
34
+ 2. **Answer correctness**: Validates numerical accuracy against ground truth
35
+ 3. **Integer validation**: Confirms answers are properly formatted integers
36
+ 4. **Output cleanliness**: Penalizes extraneous text after answer tags
37
+
38
+ ## Usage
39
+
40
+ ### Required Format
41
+ The model expects a specific system prompt to maintain the structured output format:
42
+
43
+ ```python
44
+ SYSTEM_PROMPT = """
45
+ Respond in the following format and make sure the entire response is wrapped in <reasoning> and <answer> tags with no other text outside of these tags:
46
+ <thinking>
47
+ your reasoning goes here and do not use newlines so that the entire reasoning becomes a single paragraph
48
+ </thinking>
49
+ <answer>
50
+ your answer goes here
51
+ </answer>
52
+ """
53
+ ```
54
+
55
+ ### Example Usage
56
+
57
+ ```python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  from transformers import AutoModelForCausalLM, AutoTokenizer
59
 
60
+ model = AutoModelForCausalLM.from_pretrained("your-username/llama3-grpo-math-reasoning")
61
+ tokenizer = AutoTokenizer.from_pretrained("your-username/llama3-grpo-math-reasoning")
62
+
63
+ messages = [
64
+ {"role": "system", "content": SYSTEM_PROMPT},
65
+ {"role": "user", "content": "Mark is looking to buy a total of 12 pieces of fruit at the store. He has already chosen 3 apples. He has also selected a bunch of bananas containing 4 bananas. How many oranges does he need to pick out to have 12 total pieces of fruit?"}
66
+ ]
67
+
68
+ input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
69
+ inputs = tokenizer(input_text, return_tensors="pt")
70
+
71
+ with torch.no_grad():
72
+ outputs = model.generate(
73
+ **inputs,
74
+ max_new_tokens=128,
75
+ temperature=0.7,
76
+ top_p=0.9,
77
+ do_sample=True
78
+ )
79
+
80
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
81
+ print(response)
82
+ ```
83
+
84
+ **Expected Output:**
85
+ ```
86
+ <thinking>
87
+ Mark wants 12 total pieces of fruit. He already has 3 apples and 4 bananas, which gives him 3 + 4 = 7 pieces of fruit. To reach 12 total pieces, he needs 12 - 7 = 5 more pieces of fruit, which would be oranges.
88
+ </thinking>
89
+ <answer>
90
+ 5
91
+ </answer>
92
+ ```
93
+
94
+ ## Performance
95
+
96
+ - **Dataset**: GSM8K test split (1,319 examples)
97
+ - **Evaluation Metric**: Exact match accuracy on final numerical answers
98
+ - **Performance**: [Insert actual accuracy from your evaluation]
99
+
100
+ ## Technical Details
101
+
102
+ ### Model Architecture
103
+ - **Base**: Llama 3.1-8B-Instruct
104
+ - **Parameters**: ~8B total, ~21M trainable (LoRA)
105
+ - **Quantization**: NF4 with double quantization
106
+ - **Precision**: bfloat16
107
+
108
+ ### GRPO Specifics
109
+ - **Group size**: 2-4 completions per prompt
110
+ - **KL coefficient (β)**: 0.02
111
+ - **Advantage normalization**: Group-based mean/std normalization
112
+ - **Policy updates**: Advantage-weighted importance sampling
113
+
114
+ ## Limitations
115
+
116
+ - **Domain**: Primarily trained on grade school mathematics; may not generalize to advanced mathematical concepts
117
+ - **Format dependency**: Requires specific system prompt for optimal performance
118
+ - **Language**: English only
119
+ - **Reasoning scope**: Optimized for multi-step arithmetic rather than complex mathematical proofs
120
+
121
+ ## Ethical Considerations
122
+
123
+ - **Educational use**: Designed to assist with mathematical learning, not replace mathematical education
124
+ - **Accuracy**: While trained for correctness, users should verify mathematical solutions for critical applications
125
+ - **Bias**: Inherits potential biases from the base Llama model and GSM8K dataset
126
+
127
+ ## Citation
128
+
129
+ If you use this model, please cite:
130
+
131
+ ```bibtex
132
+ @misc{llama3-grpo-math-reasoning,
133
+ title={Llama 3.1-8B Mathematical Reasoning via Group Relative Policy Optimization},
134
+ author={[Your Name]},
135
+ year={2025},
136
+ howpublished={\\url{https://huggingface.co/your-username/llama3-grpo-math-reasoning}}
137
+ }
138
+ ```
139
+
140
+ ## Acknowledgments
141
+
142
+ - Meta AI for the Llama 3.1 base model
143
+ - OpenAI for the GSM8K dataset
144
+ - Hugging Face TRL library for GRPO implementation
145
+ - The reinforcement learning and mathematical reasoning research communities
146
+
147
+ ## License
148
+
149
+ This model inherits the license from meta-llama/Meta-Llama-3.1-8B-Instruct. Please refer to the [original model's license](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) for usage terms.