File size: 12,778 Bytes
c7a1f78
 
f748564
c7a1f78
f748564
 
 
 
 
 
 
 
 
 
 
 
 
c7a1f78
 
f748564
c7a1f78
f748564
 
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
74ab58e
f748564
 
 
 
 
 
 
 
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
 
 
c7a1f78
f748564
 
c7a1f78
f748564
c7a1f78
f748564
 
 
 
 
c7a1f78
f748564
c7a1f78
f748564
 
c7a1f78
f748564
 
74ab58e
f748564
c7a1f78
f748564
 
 
 
 
 
c7a1f78
f748564
 
 
 
c7a1f78
f748564
 
c7a1f78
f748564
c7a1f78
74ab58e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78fb48b
 
 
 
 
 
 
f748564
939ee59
f748564
c092dc8
 
 
 
 
 
 
 
f748564
939ee59
f748564
c092dc8
 
 
 
f748564
 
 
c092dc8
 
 
 
 
f748564
 
 
c092dc8
 
 
 
 
 
 
 
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
 
 
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
 
c7a1f78
f748564
c7a1f78
f748564
c7a1f78
f748564
 
 
 
 
 
 
 
c7a1f78
f748564
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
---
base_model: Qwen/Qwen2.5-Coder-1.5B-Instruct
base_model_relation: finetune
library_name: peft
language:
- en
tags:
- code
- codeqwen
- chat
- qwen
- qwen-coder
license: gpl-3.0
datasets:
- nvidia/OpenCodeReasoning
pipeline_tag: text-generation
license_link: https://huggingface.co/bunyaminergen/Qwen2.5-Coder-1.5B-Instruct-Reasoning/blob/main/LICENSE
---

# Qwen2.5-Coder-1.5B-Instruct-Reasoning

The `Qwen2.5-Coder-1.5B-Instruct-Reasoning` model has been supervised fine-tuned (SFT) on the `nvidia/OpenCodeReasoning`
dataset to enhance its reasoning capabilities.

---

### TableofContents

- [Usage](#usage)
- [Comparison](#comparison)
- [Dataset](#dataset)
- [Training](#training)
- [License](#licence)
- [Links](#links)
- [Team](#team)
- [Contact](#contact)
- [Reference](#reference)
- [Citation](#citation)

---

### Usage

#### Hugging Face

```python
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer

base_model_name = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
adapter_repo = "bunyaminergen/Qwen2.5-Coder-1.5B-Instruct-Reasoning"

tokenizer = AutoTokenizer.from_pretrained(adapter_repo, trust_remote_code=True)

model = AutoModelForCausalLM.from_pretrained(
    base_model_name,
    device_map="auto",
    torch_dtype="auto",
)

model.resize_token_embeddings(len(tokenizer))

model = PeftModel.from_pretrained(model, adapter_repo)
model.eval()

messages = [
    {"role": "system", "content": "You are a helpful coding assistant."},
    {"role": "user", "content": "Please provide a Python script that checks whether a number is prime."}
]

prompt = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

outputs = model.generate(
    **inputs,
    max_new_tokens=2048
)

print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```

---

### Comparison

- Please pay attention to the `<think> </think>` special tokens in the [Reasoning Model](#reasoning-model). As you can
  see, the `Reasoning Model` can now _“think”_.

#### Base Model

`System:` You are a helpful coding assistant.

`User:` Please provide a Python script that checks whether a number is prime.

`Assistant:`

```python
def check_prime(num):
    # Check if the number is less than 2
    if num < 2:
        return False
    # Check for factors from 2 to the square root of the number
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    # If no factors were found, the number is prime
    return True

# Example usage
number = 7
if check_prime(number):
    print(f"{number} is a prime number.")
else:
    print(f"{number} is not a prime number.")
```

In this Python script, the function `check_prime` takes an integer as input and returns `True` if the number is prime,
otherwise it returns `False`. The function first checks if the number is less than 2, which means it cannot be prime.
Then, it iterates from 2 up to the square root of the number (inclusive). If any factor of the number is found within
this range, the function immediately returns `False`, indicating that the number is not prime. If no factors are found
after checking all possible divisors, the function concludes that the number is prime and returns `True`.

The example usage demonstrates how to call the `check_prime` function with the number 7. The output will indicate
whether 7 is a prime number or not.

#### Reasoning Model

`System:` You are a helpful coding assistant.

`User:` Please provide a Python script that checks whether a number is prime.

`Assistant:`
`<think>`
Okay, I need to write a Python program that checks if a given number is prime. Hmm, what's a prime number? Oh right, a
prime number has exactly two distinct positive divisors: 1 and itself. So for a number to be prime, it must not have any
divisors other than those two.

First, let's think about the steps involved. The first thing is to determine if there are any divisors other than 1 and
the number itself. How do I check that?

Well, for a number n, starting from 2 up to sqrt(n), check if any of them divide n. If any do, then it's not prime. But
wait, maybe even before checking up to the square root, some numbers might have factors beyond that. For example, take

9. The square root is 3. So checking 2,3,4 would catch 9's factors. Wait, but 3 divides 9, so the loop from 2 to sqrt(
   n) (which is 3 in this case) will check 2 and 3. So yes, checking up to the square root is sufficient because any
   composite number has at least one factor less than or equal to its square root.

So the plan is:

1. Read an integer from input.
2. Check if the number is less than 2. If so, it's not prime.
3. Otherwise, check from 2 up to the square root of the number. For each i, if the number mod i is zero, return False.
4. If none of the numbers in this range divide the number, return True.

Wait, but what about 0 and 1? Well, according to the problem statement, the input could be 0 or 1, which are not primes.
So we should handle those cases separately.

Let me outline the code structure.

Read the input number as an integer. Let's call it num.

Handle edge cases first:

- If num < 2: print "No" (since primes are >=2)
  Else:
  Compute sqrt_num = int(math.sqrt(num))
  Then loop from 2 to sqrt_num inclusive. For each i, check if num % i ==0. If any such i exists, return False. Else,
  after checking all, return True.

But how to compute the square root accurately in Python? Using math.isqrt for integers, which returns the floor of the
square root. That's better than using ** operator since it avoids floating point inaccuracies.

Wait, but what if the number is a perfect square? Like 9. Then math.isqrt(9) is 3. So in that case, the loop would check
up to 3. But when i reaches 3, 9%3 is 0, so it would return False, which is correct.

So the code would look something like this:

import math

n = int(input())
if n < 2:
print("No")
else:
sqrt_n = math.isqrt(n)
is_prime = True
for i in range(2, sqrt_n +1):
if n % i == 0:
is_prime = False
break
print("Yes" if is_prime else "No")

Wait, but wait, what about numbers like 25. The square root is 5. So the loop runs from 2 to 5. When i=5, 25%5 is 0, so
returns false. Correct.

This logic seems solid. Let me test some examples.

Test case 1: input 7. The loop runs up to 2. 2 to 2. Check 2. 7%2 is 1, so returns true. Output Yes.

Test case 2: input 6. sqrt(6) is ~2.449. So sqrt_n is 2. Loop from 2 to 2. Check 2. 6%2 is 0. So returns false. Output
No.

Another test case: 8. sqrt(8) is ~2.828. So sqrt_n is 2. Check 2. 8%2 is 0. So output No.

What about a prime number like 13. sqrt(13) is ~3.605. So loop up to 3. Check 2 and 3. 13%2 is 1, 13%3 is 1. So returns
true. Correct.

Edge case: 1. Output No.

Another edge case: 2. Output Yes.

What about a very large number, say 999983 (a prime). The code should check up to sqrt(999983) which is around 999.9. So
the loop runs 2 to 999. It will check all possible factors. Since it's a prime, none will divide it. So returns Yes.

So the code seems to handle all cases correctly.

I think that's the solution. Now, writing the code accordingly.
`</think>`

```python
import math

n = int(input())
if n < 2:
    print("No")
else:
    sqrt_n = math.isqrt(n)
    is_prime = True
    for i in range(2, sqrt_n + 1):
        if n % i == 0:
            is_prime = False
            break
    print("Yes" if is_prime else "No")
```

---

### Dataset

- [nvidia/OpenCodeReasoning](https://huggingface.co/datasets/nvidia/OpenCodeReasoning)

---

### Training

#### Base

| Parameter                   | Value                              |
|-----------------------------|------------------------------------|
| Base Model                  | `Qwen/Qwen2.5-Coder-1.5B-Instruct` |
| Fine-tuning Method          | QLoRA                              |
| Task Type                   | `CAUSAL_LM`                        |
| Number of Epochs            | `3`                                |
| Batch Size                  | `1`                                |
| Gradient Accumulation Steps | `1`                                |
| Effective Batch Size        | `1`                                |
| Learning Rate               | `2e-4`                             |
| LR Scheduler Type           | `cosine`                           |
| Warmup Ratio                | `0.05`                             |
| Precision                   | `FP16 Mixed Precision`             |
| Gradient Checkpointing      | `True`                             |
| Completion-Only Loss        | `True`                             |
| Packing                     | `False`                            |
| Max Sequence Length         | `8192 tokens`                      |
| Logging Steps               | every `10000` steps                |
| Save Checkpoint Steps       | every `10000` steps                |
| Output Directory            | `.model`                           |

#### PEFT/LoRA

| Parameter       | Value                                                                       |
|-----------------|-----------------------------------------------------------------------------|
| LoRA Rank (`r`) | `16`                                                                        |
| LoRA Alpha      | `32`                                                                        |
| LoRA Dropout    | `0.05`                                                                      |
| LoRA Bias       | `none`                                                                      |
| Task Type       | `CAUSAL_LM`                                                                 |
| Target Modules  | `q_proj`, `k_proj`, `v_proj`, `o_proj`, `gate_proj`, `up_proj`, `down_proj` |
| Modules to Save | `embed_tokens`, `lm_head`                                                   |

#### Model

| Parameter                 | Value                              |
|---------------------------|------------------------------------|
| Name                      | `Qwen/Qwen2.5-Coder-1.5B-Instruct` |
| Attention Implementation  | `flash_attention_2`                |
| load_in_4bit              | `true`                             |
| bnb_4bit_quant_type       | `nf4`                              |
| bnb_4bit_use_double_quant | `true`                             |

#### Dataset

| Parameter           | Value                      |
|---------------------|----------------------------|
| Dataset Name        | `nvidia/OpenCodeReasoning` |
| Split               | `split_0`                  |
| Number of Rows      | `8000`                     |
| Max Token Length    | `8192`                     |
| Shuffle             | `True`                     |
| Number of Processes | `4`                        |

#### Tokenizer

| Parameter                      | Value                       |
|--------------------------------|-----------------------------|
| Truncation                     | Enabled (`max_length=8192`) |
| Masked Language Modeling (MLM) | `False`                     |

#### Speeds, Sizes, Times

| Parameter            | Value                                                      |
|----------------------|------------------------------------------------------------|
| Total Training Time  | ~3.5 hours                                                 |
| Checkpoint Frequency | every `10000` steps                                        |
| Checkpoint Steps     | `checkpoint-10000`, `checkpoint-20000`, `checkpoint-24000` |

#### Compute Infrastructure

| Parameter    | Value                            |
|--------------|----------------------------------|
| GPU          | 1 × NVIDIA H100 SXM (80 GB VRAM) |
| RAM          | 125 GB                           |
| CPU          | 16 vCPU                          |
| OS           | Ubuntu 22.04                     |
| Frameworks   | PyTorch 2.4.0                    |
| CUDA Version | 12.4.1                           |

---

### Licence

- [LICENSE](LICENSE)

---

### Links

- [Github](https://github.com/bunyaminergen/)
- [Website](https://bunyaminergen.com)
- [Linkedin](https://www.linkedin.com/in/bunyaminergen)

---

### Team

- [Bunyamin Ergen](https://www.linkedin.com/in/bunyaminergen)

---

### Contact

- [Mail](mailto:info@bunyaminergen.com)

---

### Reference

- This model has been fine-tuned using Supervised Fine Tuning (SFT) method from the original
  model [Qwen/Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct).

---

### Citation

```bibtex
@software{       Qwen2.5-Coder-1.5B-Instruct-Reasoning,
  author       = {Bunyamin Ergen},
  title        = {{Qwen2.5-Coder-1.5B-Instruct-Reasoning}},
  year         = {2025},
  month        = {04},
}
```

---