Qwen2.5 Coder 1.5B Instruct Reasoning
基于Qwen2.5-Coder-1.5B-Instruct微调的代码推理模型,增强了编程问题解决和逻辑推理能力
下载量 130
发布时间 : 4/27/2025
模型简介
该模型在nvidia/OpenCodeReasoning数据集上进行了监督微调,特别强化了代码生成和逻辑推理能力,适合作为编程助手使用
模型特点
增强的代码推理能力
通过特殊标记<think></think>展示推理过程,使代码生成更具逻辑性
编程问题解决
能够理解编程问题需求并生成正确的代码解决方案
高效微调
使用QLoRA方法进行高效微调,保持基础模型能力的同时增强特定功能
模型能力
代码生成
编程问题解答
逻辑推理
代码解释
算法实现
使用案例
编程教育
编程练习辅导
帮助学生理解编程概念并生成示例代码
提供带有详细解释的正确代码实现
开发辅助
算法实现
根据需求生成常见算法的实现代码
如质数检查、排序算法等标准实现
🚀 Qwen2.5-Coder-1.5B-Instruct-Reasoning
Qwen2.5-Coder-1.5B-Instruct-Reasoning
模型在 nvidia/OpenCodeReasoning
数据集上进行了监督微调(SFT),以增强其推理能力。
🚀 快速开始
Hugging Face使用示例
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))
✨ 主要特性
通过在 nvidia/OpenCodeReasoning
数据集上进行监督微调,模型增强了推理能力。在处理代码相关任务时,能够展现出更强大的逻辑推理和问题解决能力。
💻 使用示例
基础用法
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))
高级用法
在实际应用中,你可以根据具体需求调整输入的消息内容和生成参数,以满足不同的任务要求。例如,修改 max_new_tokens
参数来控制生成文本的长度。
📚 详细文档
对比
- 请关注 推理模型 中的
<think> </think>
特殊标记。可以看到,推理模型
现在能够进行 “思考”。
基础模型
`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.")
在这个Python脚本中,check_prime
函数接受一个整数作为输入,如果该数是质数则返回 True
,否则返回 False
。函数首先检查该数是否小于2,因为小于2的数不是质数。然后,它从2开始迭代到该数的平方根(包含)。如果在这个范围内找到该数的任何因数,则函数立即返回 False
,表示该数不是质数。如果在检查所有可能的除数后没有找到因数,则函数得出该数是质数并返回 True
。
示例用法展示了如何使用 check_prime
函数检查数字7是否为质数。输出将指示7是否为质数。
推理模型
`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")
数据集
训练
基础参数
参数 | 详情 |
---|---|
基础模型 | Qwen/Qwen2.5-Coder-1.5B-Instruct |
微调方法 | QLoRA |
任务类型 | CAUSAL_LM |
训练轮数 | 3 |
批次大小 | 1 |
梯度累积步数 | 1 |
有效批次大小 | 1 |
学习率 | 2e-4 |
学习率调度器类型 | cosine |
预热比例 | 0.05 |
精度 | FP16 混合精度 |
梯度检查点 | True |
仅完成损失 | True |
打包 | False |
最大序列长度 | 8192 tokens |
日志记录步数 | 每 10000 步 |
保存检查点步数 | 每 10000 步 |
输出目录 | .model |
PEFT/LoRA参数
参数 | 详情 |
---|---|
LoRA 秩 (r ) |
16 |
LoRA 阿尔法 | 32 |
LoRA 丢弃率 | 0.05 |
LoRA 偏置 | none |
任务类型 | CAUSAL_LM |
目标模块 | q_proj , k_proj , v_proj , o_proj , gate_proj , up_proj , down_proj |
保存模块 | embed_tokens , lm_head |
模型参数
参数 | 详情 |
---|---|
名称 | Qwen/Qwen2.5-Coder-1.5B-Instruct |
注意力实现 | flash_attention_2 |
以4位加载 | true |
4位量化类型 | nf4 |
4位双量化 | true |
数据集参数
参数 | 详情 |
---|---|
数据集名称 | nvidia/OpenCodeReasoning |
分割 | split_0 |
行数 | 8000 |
最大令牌长度 | 8192 |
打乱 | True |
进程数 | 4 |
分词器参数
参数 | 详情 |
---|---|
截断 | 启用 (max_length=8192 ) |
掩码语言模型 (MLM) | False |
速度、大小、时间
参数 | 详情 |
---|---|
总训练时间 | ~3.5 小时 |
检查点频率 | 每 10000 步 |
检查点步数 | checkpoint-10000 , checkpoint-20000 , checkpoint-24000 |
计算基础设施
参数 | 详情 |
---|---|
GPU | 1 × NVIDIA H100 SXM (80 GB VRAM) |
内存 | 125 GB |
CPU | 16 vCPU |
操作系统 | Ubuntu 22.04 |
框架 | PyTorch 2.4.0 |
CUDA 版本 | 12.4.1 |
🔧 技术细节
该模型基于 Qwen/Qwen2.5-Coder-1.5B-Instruct
基础模型,使用 QLoRA 方法进行微调。通过在 nvidia/OpenCodeReasoning
数据集上进行监督微调,模型学习到了更多的代码推理知识,从而增强了其推理能力。在训练过程中,采用了一系列的优化策略,如混合精度训练、梯度检查点等,以提高训练效率和模型性能。
📄 许可证
🔗 相关链接
👥 团队
📞 联系我们
📚 参考资料
- 该模型是在原始模型 Qwen/Qwen2.5-Coder-1.5B-Instruct 的基础上,使用监督微调(SFT)方法进行微调得到的。
📝 引用
@software{ Qwen2.5-Coder-1.5B-Instruct-Reasoning,
author = {Bunyamin Ergen},
title = {{Qwen2.5-Coder-1.5B-Instruct-Reasoning}},
year = {2025},
month = {04},
}
Phi 2 GGUF
其他
Phi-2是微软开发的一个小型但强大的语言模型,具有27亿参数,专注于高效推理和高质量文本生成。
大型语言模型 支持多种语言
P
TheBloke
41.5M
205
Roberta Large
MIT
基于掩码语言建模目标预训练的大型英语语言模型,采用改进的BERT训练方法
大型语言模型 英语
R
FacebookAI
19.4M
212
Distilbert Base Uncased
Apache-2.0
DistilBERT是BERT基础模型的蒸馏版本,在保持相近性能的同时更轻量高效,适用于序列分类、标记分类等自然语言处理任务。
大型语言模型 英语
D
distilbert
11.1M
669
Llama 3.1 8B Instruct GGUF
Meta Llama 3.1 8B Instruct 是一个多语言大语言模型,针对多语言对话用例进行了优化,在常见的行业基准测试中表现优异。
大型语言模型 英语
L
modularai
9.7M
4
Xlm Roberta Base
MIT
XLM-RoBERTa是基于100种语言的2.5TB过滤CommonCrawl数据预训练的多语言模型,采用掩码语言建模目标进行训练。
大型语言模型 支持多种语言
X
FacebookAI
9.6M
664
Roberta Base
MIT
基于Transformer架构的英语预训练模型,通过掩码语言建模目标在海量文本上训练,支持文本特征提取和下游任务微调
大型语言模型 英语
R
FacebookAI
9.3M
488
Opt 125m
其他
OPT是由Meta AI发布的开放预训练Transformer语言模型套件,参数量从1.25亿到1750亿,旨在对标GPT-3系列性能,同时促进大规模语言模型的开放研究。
大型语言模型 英语
O
facebook
6.3M
198
1
基于transformers库的预训练模型,适用于多种NLP任务
大型语言模型
Transformers

1
unslothai
6.2M
1
Llama 3.1 8B Instruct
Llama 3.1是Meta推出的多语言大语言模型系列,包含8B、70B和405B参数规模,支持8种语言和代码生成,优化了多语言对话场景。
大型语言模型
Transformers 支持多种语言

L
meta-llama
5.7M
3,898
T5 Base
Apache-2.0
T5基础版是由Google开发的文本到文本转换Transformer模型,参数规模2.2亿,支持多语言NLP任务。
大型语言模型 支持多种语言
T
google-t5
5.4M
702
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98
智启未来,您的人工智能解决方案智库
简体中文