Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chipen

Verilog 专用 7B 代码生成模型 — 为芯片设计赋能的 AI

Chipen 是一个基于 Qwen2.5-Coder-7B-Instruct 微调的 Verilog 专用代码生成模型,支持中文指令理解和 EDA 工具调用(Agent 能力)。

核心能力

  • Verilog RTL 代码生成:根据自然语言描述生成可综合的 Verilog 代码
  • 中文理解:支持中文指令输入
  • EDA 工具调用:支持 compile、simulate、lint、synthesize 等工具调用
  • 错误修复:分析编译/仿真错误并自动修复

快速开始

环境要求

  • Python 3.10+
  • PyTorch 2.0+
  • CUDA GPU (16GB+ 显存) 或兼容的国产 GPU
  • iverilog (评测需要)

安装

git clone https://github.com/egg-rolls/Chipen.git
cd Chipen
pip install -r requirements.txt

使用微调后的模型

# 通过 Ollama 使用(推荐)
ollama create chipen -f Modelfile
ollama run chipen "设计一个4位计数器,带异步复位"

从零复现

如果你想从头训练模型,按以下步骤操作。

1. 下载基座模型

# 方式 A:ModelScope(国内推荐)
pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('Qwen/Qwen2.5-Coder-7B-Instruct', cache_dir='models/')"

# 方式 B:HuggingFace(需要网络)
python -c "from transformers import AutoTokenizer; AutoTokenizer.from_pretrained('Qwen/Qwen2.5-Coder-7B-Instruct')"

2. 生成训练数据

训练数据需要自行生成,因为涉及 LLM API 调用费用。

# 配置 API Key
cp configs/generate_config.template.yaml configs/generate_config.yaml
# 编辑 configs/generate_config.yaml,填入你的 DeepSeek API Key
# 或设置环境变量: export DEEPSEEK_API_KEY=your-key

# 生成代码生成数据
python scripts/generate_data.py --template fewshot --target 1000

# 生成 Agent 数据(工具调用、错误修复等)
python scripts/generate_agent_data.py --target 1000

# 合并并清洗数据
python scripts/merge_data.py --agent-ratio 0.3

数据生成成本估算(DeepSeek API):

  • 1000 条代码生成:~¥1
  • 1000 条 Agent 数据:~¥3
  • 总计约 ¥10-20 即可生成足够训练的数据

3. 训练

python scripts/train_qlora.py

训练输出保存在 output/chipen-7b-qlora/adapter/

关键训练参数(configs/train_config.yaml):

参数 说明
LoRA rank 32 适配器秩
Epochs 3 训练轮数
学习率 2e-4
Batch size 4 × 4 = 16 有效批大小
序列长度 2048 最大 token 数
预计时间 2-4 小时 V100 GPU

从已有 adapter 继续训练(增量训练):

python scripts/train_qlora.py --pretrained-adapter output/chipen-7b-qlora/adapter

4. 评测

# 使用内置评测集 (40 题)
python scripts/evaluate.py --adapter output/chipen-7b-qlora/adapter

# 使用 VerilogEval (需下载)
# 从 https://github.com/NVlabs/verilog-eval 下载到 data/verilogeval/
python scripts/setup_eval.py
python scripts/evaluate.py --adapter output/chipen-7b-qlora/adapter --problems data/verilogeval_both.json

5. 导出

# 导出为 GGUF 格式
python scripts/export_gguf.py

# 注册到 Ollama
cd output/chipen-7b-gguf
ollama create chipen -f Modelfile

项目结构

Chipen/
├── scripts/
│   ├── train_qlora.py           # QLoRA 训练
│   ├── evaluate.py              # 评测 (内置 40 题 + VerilogEval)
│   ├── export_gguf.py           # 导出 GGUF
│   ├── generate_data.py         # 代码生成数据
│   ├── generate_agent_data.py   # Agent 数据
│   ├── merge_data.py            # 数据合并清洗
│   └── setup_eval.py            # 下载 VerilogEval
├── configs/
│   ├── train_config.yaml        # 训练配置
│   └── generate_config.template.yaml  # 数据生成配置模板
├── data/
│   ├── raw/                     # 原始生成数据 (gitignore)
│   └── processed/               # 清洗后数据 (gitignore)
├── output/                      # 训练输出 (gitignore)
└── requirements.txt

数据集说明

训练数据由以下部分组成:

类型 来源 说明
代码生成 LLM 合成 (DeepSeek) 根据自然语言生成 Verilog
Agent 数据 LLM 合成 (DeepSeek) 工具调用、错误修复、任务规划

数据格式为 ChatML JSONL,每条数据包含 system/user/assistant 三元组。

注意:训练数据未包含在仓库中,需要自行生成。

模型权重

模型权重未包含在仓库中。训练完成后,adapter 保存在 output/chipen-7b-qlora/adapter/

如需分享模型权重,可以:

  1. 上传到 HuggingFace Hub
  2. 导出为 GGUF 格式分享

许可证

  • 基座模型 Qwen2.5-Coder-7B-Instruct:Apache 2.0
  • 本项目代码:Apache 2.0

致谢

About

fune train model for verilog

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages