LLMs doing funky stuff
So, my task was:
- Get a reasoning model to add numbers.
- Check if it's correct.
- For the incorrect examples, send the reasoning to a critic/larger model and evaluate correctness.
- Ask critic model to append to the earlier thought process in order to correct the answer.
- Now generate the following SFT pair and train the model on it: prompt:
user: What is 2+2
assistant:
<think>
Okay, so the user is asking what 2 plus 2 is. Hmm, that seems pretty straightforward, but I should make sure I get it right. Let me think... Addition of two numbers. So, if you have 2 and you add another 2 to it, how does that work?
Well, let's start by visualizing it. Imagine you have 2 apples and someone gives you 2 more apples. How many apples do you have now? You had 2, then add 2 more. So 1, 2, and then 3, 4 and 5. So total is 5 apples. That makes sense.
Wait, but maybe I should check using the number line. Starting at 0, if you move 2 units to the right, you get to 2. Then add another 2 units, moving 2 more steps. That lands you at 5. Yep, that checks out.
response:
Let me think about that again. Imagine you have 2 apples and someone gives you 2 more apples. How many apples do you have now? You had 2, then add 2 more. So 1, 2, and then 3, 4. So total is 4 apples. That makes sense.
</think>
<answer>
4
</amswer>
- Loss is computed only on the response tokens as we do in SFT.
Why are we doing this? So this way we can enable the model to continually learn and improve based on the feedback from the critic model/human feedback/rule based feedback. The critic model can be used in creative ways to generate additional thought in various styles and techniques of teaching/thinking.
We can also generate more examples to do SFT on based on a mistake. For example, each time there is mistake we can write a more concise CoT and train on that SFT pair too. Note that the critic model can be open source or closed source.
Task Begins
Github: https://github.com/shukraditya/LLM-Addition
Step 1: Generating the dataset
- Generated the additions dataset using deepseek-r1:1.5B which is basically a distilled Qwen 1.5B model. it looks something like this
{
"numbers": [
4402730,
1256753,
9380747,
757185,
1652619,
8367780
],
"response": "<think>\nFirst, I need to add all the given numbers: 4,402,730 + 1,256,753 + 9,380,747 + 757,185 + 1,652,619 + 8,367,780.\n\nTo simplify the calculation, I'll group numbers by their place values to make addition easier.\n\nAdding all the millions together: 4,000,000 + 1,000,000 + 9,000,000 = 14,000,000.\n\nNext, adding all the hundred thousands: 400,000 + 200,000 + 300,000 = 900,000.\n\nThen, for the ten thousands place: 7,000 + 5,000 + 8,000 = 21,000.\n\nIn the thousands place: 700 + 600 + 700 = 2,000.\n\nFor the hundreds place: 30 + 70 + 40 = 140.\n\nFinally, in the tens and ones places, all numbers have zero, so they contribute nothing to the sum.\n\nNow, adding all these together: 14,000,000 + 900,000 + 21,000 + 2,000 + 140 = 14,923,140.\n</think>\n\nCertainly! Let's add the numbers step by step:\n\n\\[\n\\begin{align*}\n4,\\!402,\\!730 \\\\\n+ \\,1,\\!256,\\!753 \\\\\n+ \\,9,\\!380,\\!747 \\\\\n+ \\,757,\\!185 \\\\\n+ \\,1,\\!652,\\!619 \\\\\n+ \\,8,\\!367,\\!780 \\\\\n\\hline\n\\end{align*}\n\\]\n\n**Step 1:** Add the millions place:\n\\[\n4,\\!402,\\!730 + 1,\\!256,\\!753 = 5,\\!659,\\!483\n\\]\n\\(5,\\!659,\\!483\\) is less than 10 million, so we move to the next place.\n\n**Step 2:** Add the hundred thousands place:\n\\[\n5,\\!659,\\!483 + 9,\\!380,\\!747 = 15,\\!039,\\!230\n\\]\n\n**Step 3:** Add the ten thousands place:\n\\[\n15,\\!039,\\!230 + 757,\\!185 = 15,\\!796,\\!415\n\\]\n\n**Step 4:** Add the thousands place:\n\\[\n15,\\!796,\\!415 + 757,\\!185 = 16,\\!553,\\!600\n\\]\n\n**Step 5:** Add the hundreds place:\n\\[\n16,\\!553,\\!600 + 30 + 70 + 40 = 16,\\!553,\\!600 + 140 = 16,\\!553,\\!740\n\\]\n\n**Step 6:** Add the tens and ones places:\nAll numbers have zeros in the tens and ones places, so they contribute nothing.\n\n**Final Answer:**\n\n\\[\n\\boxed{16553740}\n\\]"
}
Generating the dataset took ~6 hours locally. I used Ollama to run the model locally.
Step 2: Finding the errors
Then I ran a script to extract the answers from the reasoning that the model returned. The thought process was extracted as well.
{
"numbers": [
4402730,
1256753,
9380747,
757185,
1652619,
8367780
],
"response": "<think>\nFirst, I need to add all the given numbers: 4,402,730 + 1,256,753 + 9,380,747 + 757,185 + 1,652,619 + 8,367,780.\n\nTo simplify the calculation, I'll group numbers by their place values to make addition easier.\n\nAdding all the millions together: 4,000,000 + 1,000,000 + 9,000,000 = 14,000,000.\n\nNext, adding all the hundred thousands: 400,000 + 200,000 + 300,000 = 900,000.\n\nThen, for the ten thousands place: 7,000 + 5,000 + 8,000 = 21,000.\n\nIn the thousands place: 700 + 600 + 700 = 2,000.\n\nFor the hundreds place: 30 + 70 + 40 = 140.\n\nFinally, in the tens and ones places, all numbers have zero, so they contribute nothing to the sum.\n\nNow, adding all these together: 14,000,000 + 900,000 + 21,000 + 2,000 + 140 = 14,923,140.\n</think>\n\nCertainly! Let's add the numbers step by step:\n\n\\[\n\\begin{align*}\n4,\\!402,\\!730 \\\\\n+ \\,1,\\!256,\\!753 \\\\\n+ \\,9,\\!380,\\!747 \\\\\n+ \\,757,\\!185 \\\\\n+ \\,1,\\!652,\\!619 \\\\\n+ \\,8,\\!367,\\!780 \\\\\n\\hline\n\\end{align*}\n\\]\n\n**Step 1:** Add the millions place:\n\\[\n4,\\!402,\\!730 + 1,\\!256,\\!753 = 5,\\!659,\\!483\n\\]\n\\(5,\\!659,\\!483\\) is less than 10 million, so we move to the next place.\n\n**Step 2:** Add the hundred thousands place:\n\\[\n5,\\!659,\\!483 + 9,\\!380,\\!747 = 15,\\!039,\\!230\n\\]\n\n**Step 3:** Add the ten thousands place:\n\\[\n15,\\!039,\\!230 + 757,\\!185 = 15,\\!796,\\!415\n\\]\n\n**Step 4:** Add the thousands place:\n\\[\n15,\\!796,\\!415 + 757,\\!185 = 16,\\!553,\\!600\n\\]\n\n**Step 5:** Add the hundreds place:\n\\[\n16,\\!553,\\!600 + 30 + 70 + 40 = 16,\\!553,\\!600 + 140 = 16,\\!553,\\!740\n\\]\n\n**Step 6:** Add the tens and ones places:\nAll numbers have zeros in the tens and ones places, so they contribute nothing.\n\n**Final Answer:**\n\n\\[\n\\boxed{16553740}\n\\]",
"thought": "First, I need to add all the given numbers: 4,402,730 + 1,256,753 + 9,380,747 + 757,185 + 1,652,619 + 8,367,780.\n\nTo simplify the calculation, I'll group numbers by their place values to make addition easier.\n\nAdding all the millions together: 4,000,000 + 1,000,000 + 9,000,000 = 14,000,000.\n\nNext, adding all the hundred thousands: 400,000 + 200,000 + 300,000 = 900,000.\n\nThen, for the ten thousands place: 7,000 + 5,000 + 8,000 = 21,000.\n\nIn the thousands place: 700 + 600 + 700 = 2,000.\n\nFor the hundreds place: 30 + 70 + 40 = 140.\n\nFinally, in the tens and ones places, all numbers have zero, so they contribute nothing to the sum.\n\nNow, adding all these together: 14,000,000 + 900,000 + 21,000 + 2,000 + 140 = 14,923,140.",
"actual_answer": 25817814,
"generated_answer": 14923140,
"error": true
},
Step 3: Running the critic model
Quite a few large reasoning models were tested. These were the top few:
- Gemini 2.0 Flash Thinking Experimental
- Deepseek-Reasoner
- QwQ 32B
After getting rate limited by all free sites, i resorted to Gemini's API and that worked well.
Ran the script for ~7 hours where I gave the model the following:
- the numbers that had to be added
- the reasoning generated by the original model(here, the 1.5B ) and asked it to append to the existing thought process to correct the output.
This is what the JSON file looks like.
{
"numbers": [
4402730,
1256753,
9380747,
757185,
1652619,
8367780
],
"response": "<think>\nFirst, I need to add all the given numbers: 4,402,730 + 1,256,753 + 9,380,747 + 757,185 + 1,652,619 + 8,367,780.\n\nTo simplify the calculation, I'll group numbers by their place values to make addition easier.\n\nAdding all the millions together: 4,000,000 + 1,000,000 + 9,000,000 = 14,000,000.\n\nNext, adding all the hundred thousands: 400,000 + 200,000 + 300,000 = 900,000.\n\nThen, for the ten thousands place: 7,000 + 5,000 + 8,000 = 21,000.\n\nIn the thousands place: 700 + 600 + 700 = 2,000.\n\nFor the hundreds place: 30 + 70 + 40 = 140.\n\nFinally, in the tens and ones places, all numbers have zero, so they contribute nothing to the sum.\n\nNow, adding all these together: 14,000,000 + 900,000 + 21,000 + 2,000 + 140 = 14,923,140.\n</think>\n\nCertainly! Let's add the numbers step by step:\n\n\\[\n\\begin{align*}\n4,\\!402,\\!730 \\\\\n+ \\,1,\\!256,\\!753 \\\\\n+ \\,9,\\!380,\\!747 \\\\\n+ \\,757,\\!185 \\\\\n+ \\,1,\\!652,\\!619 \\\\\n+ \\,8,\\!367,\\!780 \\\\\n\\hline\n\\end{align*}\n\\]\n\n**Step 1:** Add the millions place:\n\\[\n4,\\!402,\\!730 + 1,\\!256,\\!753 = 5,\\!659,\\!483\n\\]\n\\(5,\\!659,\\!483\\) is less than 10 million, so we move to the next place.\n\n**Step 2:** Add the hundred thousands place:\n\\[\n5,\\!659,\\!483 + 9,\\!380,\\!747 = 15,\\!039,\\!230\n\\]\n\n**Step 3:** Add the ten thousands place:\n\\[\n15,\\!039,\\!230 + 757,\\!185 = 15,\\!796,\\!415\n\\]\n\n**Step 4:** Add the thousands place:\n\\[\n15,\\!796,\\!415 + 757,\\!185 = 16,\\!553,\\!600\n\\]\n\n**Step 5:** Add the hundreds place:\n\\[\n16,\\!553,\\!600 + 30 + 70 + 40 = 16,\\!553,\\!600 + 140 = 16,\\!553,\\!740\n\\]\n\n**Step 6:** Add the tens and ones places:\nAll numbers have zeros in the tens and ones places, so they contribute nothing.\n\n**Final Answer:**\n\n\\[\n\\boxed{16553740}\n\\]",
"thought": "First, I need to add all the given numbers: 4,402,730 + 1,256,753 + 9,380,747 + 757,185 + 1,652,619 + 8,367,780.\n\nTo simplify the calculation, I'll group numbers by their place values to make addition easier.\n\nAdding all the millions together: 4,000,000 + 1,000,000 + 9,000,000 = 14,000,000.\n\nNext, adding all the hundred thousands: 400,000 + 200,000 + 300,000 = 900,000.\n\nThen, for the ten thousands place: 7,000 + 5,000 + 8,000 = 21,000.\n\nIn the thousands place: 700 + 600 + 700 = 2,000.\n\nFor the hundreds place: 30 + 70 + 40 = 140.\n\nFinally, in the tens and ones places, all numbers have zero, so they contribute nothing to the sum.\n\nNow, adding all these together: 14,000,000 + 900,000 + 21,000 + 2,000 + 140 = 14,923,140.",
"actual_answer": 25817814,
"generated_answer": 14923140,
"error": true,
"new_thought": "First, I need to add all the given numbers: 4,402,730 + 1,256,753 + 9,380,747 + 757,185 + 1,652,619 + 8,367,780.\n\nTo simplify the calculation, I'll group numbers by their place values to make addition easier.\n\nAdding all the millions together: 4,000,000 + 1,000,000 + 9,000,000 + 0,000,000 + 1,000,000 + 8,000,000 = 23,000,000.\n**Correction:** Your initial million addition was incorrect and incomplete. You only considered the millions digits from the first three numbers and calculated 4,000,000 + 1,000,000 + 9,000,000 = 14,000,000. You missed the millions digits from the remaining numbers. The correct sum of millions digits is 4 + 1 + 9 + 0 + 1 + 8 = 23, leading to 23,000,000.\n\nNext, adding all the hundred thousands: 400,000 + 200,000 + 300,000 + 700,000 + 600,000 + 300,000 = 2,500,000.\n**Correction:** Similarly, your initial hundred thousands addition was incorrect and incomplete. You only considered the hundred thousands digits from the first three numbers and calculated 400,000 + 200,000 + 300,000 = 900,000. The correct sum of hundred thousands digits is 4 + 2 + 3 + 7 + 6 + 3 = 25, leading to 2,500,000.\n\nThen, for the ten thousands place: 0,000 + 50,000 + 80,000 + 50,000 + 50,000 + 60,000 = 290,000.\n**Correction:** Your initial ten thousands addition was incorrect. You picked wrong digits and place value and calculated 7,000 + 5,000 + 8,000 = 21,000. The correct sum of ten thousands digits is 0 + 5 + 8 + 5 + 5 + 6 = 29, leading to 290,000.\n\nIn the thousands place: 2,000 + 6,000 + 0,000 + 7,000 + 2,000 + 7,000 = 24,000.\n**Correction:** Your initial thousands addition was incorrect. You picked wrong digits and place value and calculated 700 + 600 + 700 = 2,000. The correct sum of thousands digits is 2 + 6 + 0 + 7 + 2 + 7 = 24, leading to 24,000.\n\nFor the hundreds place: 700 + 700 + 700 + 100 + 600 + 700 = 3,500.\n**Correction:** Your initial hundreds addition was incorrect. You picked wrong digits and place value and calculated 30 + 70 + 40 = 140. The correct sum of hundreds digits is 7 + 7 + 7 + 1 + 6 + 7 = 35, leading to 3,500.\n\nFor the tens place: 30 + 50 + 40 + 80 + 10 + 80 = 290.\n**Correction:** Your initial assumption about tens and ones places was wrong. You incorrectly stated they contribute nothing. The correct sum of tens digits is 3 + 5 + 4 + 8 + 1 + 8 = 29, leading to 290.\n\nFor the ones place: 0 + 3 + 7 + 5 + 9 + 0 = 24.\n**Correction:** The correct sum of ones digits is 0 + 3 + 7 + 5 + 9 + 0 = 24.\n\nNow, adding all these together: 23,000,000 + 2,500,000 + 290,000 + 24,000 + 3,500 + 290 + 24 = 25,817,814.\n**Correction:** Your final summation using your initially calculated incorrect values was 14,000,000 + 900,000 + 21,000 + 2,000 + 140 = 14,923,140, which is incorrect. Using the correctly calculated place values, the sum is 25,817,814.\n\nThe final answer is $\\boxed{25817814}$",
"new_generated_answer": 25817814
}
Step 4: Creating SFT Pairs and Supervised Finetuning
Two models had to be finetuned: Deepseek R1 1.5B(a Qwen Distill) and Deepseek R1 8B(a Llama distill). So, from the JSON file, SFT pairs were created. for Qwen, I used the chat completion format. Here, the instruction was "Add". Input was the numbers followed by the original thought process and the response was formatted as the new thought process, followed by the actual answer.
f"""
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
add
### Input:
{entry["numbers"]}<think>{entry["thought"]}
### Response:
{entry["new_thought"]}</think><answer>{entry["actual_answer"]}</answer>"""
As for Llama, I used the user-assistant chat format. Similarly, the add instruction, followed by the numbers, the original thought process was given as the content for the user and the new thought process and the answer was given as a response by the assistant.
["user: add [4402730, 1256753, 9380747, 757185, 1652619, 8367780]\nassistant:\n<think>First, I need to add all the given numbers: 4,402,730 + 1,256,753 + 9,380,747 + 757,185 + 1,652,619 + 8,367,780.\n\nTo simplify the calculation, I'll group numbers by their place values to make addition easier.\n\nAdding all the millions together: 4,000,000 + 1,000,000 + 9,000,000 = 14,000,000.\n\nNext, adding all the hundred thousands: 400,000 + 200,000 + 300,000 = 900,000.\n\nThen, for the ten thousands place: 7,000 + 5,000 + 8,000 = 21,000.\n\nIn the thousands place: 700 + 600 + 700 = 2,000.\n\nFor the hundreds place: 30 + 70 + 40 = 140.\n\nFinally, in the tens and ones places, all numbers have zero, so they contribute nothing to the sum.\n\nNow, adding all these together: 14,000,000 + 900,000 + 21,000 + 2,000 + 140 = 14,923,140.",
"First, I'll align all the numbers vertically, ensuring that each digit is in its correct place value.\n\nNext, I'll start adding from the rightmost digit (the ones place), moving to the left, carrying over any excess as needed.\n\nAfter performing the addition for each place value, I will compile the final sum.</think>\n<answer>25817814</answer>"]
Step 5: Finetuning
The finetuning was performed and the loss was calculated only on the response tokens(all previous tokens were masked when calculating loss). This was done to ensure:
- model doesn't overfit to the input/prompt
- reduce training time
- we need the model to generate good accurate output, rather than replicating the input.
By masking the input and instruction tokens, we confine the loss to only the response, making sure that the response quality is top-notch.
Fine tuning for both the models converged pretty well. For the smaller 1.5B parameter model, only 40 epochs were used whereas for the larger model, 60 epochs were used. Convergence was stable in both the cases, as we can see from the training loss.
Deepseek R1 - Qwen 1.5 B model finetune
| Step | Training Loss |
|---|---|
| 1 | 0.147100 |
| 2 | 0.221900 |
| 3 | 0.196700 |
| 4 | 0.203500 |
| 5 | 0.206400 |
| 6 | 0.187100 |
| 7 | 0.146500 |
| 8 | 0.158200 |
| 9 | 0.170900 |
| 10 | 0.193800 |
| 11 | 0.191300 |
| 12 | 0.180200 |
| 13 | 0.228200 |
| 14 | 0.194200 |
| 15 | 0.145400 |
| 16 | 0.115500 |
| 17 | 0.169200 |
| 18 | 0.239100 |
| 19 | 0.221100 |
| 20 | 0.166000 |
| 21 | 0.219900 |
| 22 | 0.200700 |
| 23 | 0.170700 |
| 24 | 0.199800 |
| 25 | 0.228100 |
| 26 | 0.254000 |
| 27 | 0.131800 |
| 28 | 0.175800 |
| 29 | 0.165700 |
| 30 | 0.135700 |
| 31 | 0.170200 |
| 32 | 0.229500 |
| 33 | 0.127600 |
| 34 | 0.183000 |
| 35 | 0.148500 |
| 36 | 0.154500 |
| 37 | 0.157600 |
| 38 | 0.232700 |
| 39 | 0.236600 |
| 40 | 0.186300 |
Deepseek-R1-Llama-8B model finetune
| Step | Training Loss |
|---|---|
| 1 | 0.576800 |
| 2 | 0.704400 |
| 3 | 0.645300 |
| 4 | 0.603800 |
| 5 | 0.576800 |
| 6 | 0.411100 |
| 7 | 0.338900 |
| 8 | 0.371600 |
| 9 | 0.350600 |
| 10 | 0.388400 |
| 11 | 0.351000 |
| 12 | 0.322100 |
| 13 | 0.388700 |
| 14 | 0.329600 |
| 15 | 0.262900 |
| 16 | 0.217000 |
| 17 | 0.255300 |
| 18 | 0.361500 |
| 19 | 0.335200 |
| 20 | 0.256200 |
| 21 | 0.362600 |
| 22 | 0.298800 |
| 23 | 0.238200 |
| 24 | 0.288300 |
| 25 | 0.344400 |
| 26 | 0.355500 |
| 27 | 0.195700 |
| 28 | 0.271900 |
| 29 | 0.233600 |
| 30 | 0.207900 |
| 31 | 0.242400 |
| 32 | 0.297600 |
| 33 | 0.187600 |
| 34 | 0.241300 |
| 35 | 0.195400 |
| 36 | 0.223000 |
| 37 | 0.195000 |
| 38 | 0.324000 |
| 39 | 0.334300 |
| 40 | 0.264000 |
| 41 | 0.206600 |
| 42 | 0.180300 |
| 43 | 0.244700 |
| 44 | 0.168600 |
| 45 | 0.300500 |
| 46 | 0.271300 |
| 47 | 0.228800 |
| 48 | 0.272600 |
| 49 | 0.196500 |
| 50 | 0.209700 |
| 51 | 0.190700 |
| 52 | 0.237800 |
| 53 | 0.235600 |
| 54 | 0.174500 |
| 55 | 0.159000 |
| 56 | 0.282600 |
| 57 | 0.206500 |
| 58 | 0.168900 |
| 59 | 0.241000 |
| 60 | 0.230200 |
Step 6: Evaluating the finetuned model
I tested the model exhaustively on about 100 fresh set of additions and safe to say, the model struggled.
[!info] Before finetuning, the 1.5B parameter model's addition especially, was off by quite a few digits. For example, if the answer was 5 digits long, sometimes the model erroneously generated a 6 digit answer as well. However, after finetuning, the model's accuracy is off only by 1 digit, i.e., if the answer is 257817814, the model generated 257827814. That was a massive improvement.
Interpretation of the results:
A few observations were made:
- The single/double digit error noted in the responses generated by the fine-tuned 1.5B parameter model can be attributed to sub-word tokenization. As a result of this, a big number, i.e., 123456 is sometimes tokenized as 123 and 456, leading to unexpected results.
- As presented in the GoAT paper Llama CAN outperform GPT 4 on arithmetic tasks, but the size of the finetuning dataset needs to be of a higher order of magnitude.
- For the same training dataset size, the smaller model(1.5B) outperforms the larger one(8B) by a long shot.
Future directions:
- Create a bigger dataset to explore with Llama 8B model as it does not have the sub word tokenization issue with numbers that we faced with Qwen.
- Try a reinforcement learning based approach instead of supervised fine-tuning for addition/arithmetic tasks.