mirror of
https://github.com/RYDE-WORK/MiniCPM.git
synced 2026-01-19 21:03:39 +08:00
130 lines
3.4 KiB
Plaintext
130 lines
3.4 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# MiniCPM-2B 参数高效微调(LoRA)消费级单卡示例\n",
|
||
"\n",
|
||
"本 notebook 是一个使用 `AdvertiseGen` 数据集对 MiniCPM-2B 进行 LoRA 微调,使其具备专业的广告生成能力的代码示例。\n",
|
||
"\n",
|
||
"## 硬件需求\n",
|
||
"- 显存:12GB\n",
|
||
"- 显卡架构:安培架构(推荐)\n",
|
||
"- 内存:16GB"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 1. 准备数据集\n",
|
||
"\n",
|
||
"下载 AdvertiseGen 数据集\n",
|
||
"- [Google Drive](https://drive.google.com/file/d/13_vf0xRTQsyneRKdD1bZIr93vBGOczrk/view?usp=sharing)\n",
|
||
"- [Tsinghua Cloud](https://cloud.tsinghua.edu.cn/f/b3f119a008264b1cabd1/?dl=1)\n",
|
||
"\n",
|
||
"下载后的数据集格式为 `.tar.gz` 的压缩格式,接下来的操作中,假设该压缩包被置于 `finetune/data/`。\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 校验文件完整性\n",
|
||
"!md5sum data/AdvertiseGen.tar.gz "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 解压数据集\n",
|
||
"!tar xvf data/AdvertiseGen.tar.gz "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 转换为 ChatML 格式\n",
|
||
"import os\n",
|
||
"import shutil\n",
|
||
"import json\n",
|
||
"\n",
|
||
"input_dir = \"data/AdvertiseGen\"\n",
|
||
"output_dir = \"data/AdvertiseGenChatML\"\n",
|
||
"if os.path.exists(output_dir):\n",
|
||
" shutil.rmtree(output_dir)\n",
|
||
"os.makedirs(output_dir, exist_ok=True)\n",
|
||
"\n",
|
||
"for fn in [\"train.json\", \"dev.json\"]:\n",
|
||
" data_out_list = []\n",
|
||
" with open(os.path.join(input_dir, fn), \"r\") as f, open(os.path.join(output_dir, fn), \"w\") as fo:\n",
|
||
" for line in f:\n",
|
||
" if len(line.strip()) > 0:\n",
|
||
" data = json.loads(line)\n",
|
||
" data_out = {\n",
|
||
" \"messages\": [\n",
|
||
" {\n",
|
||
" \"role\": \"user\",\n",
|
||
" \"content\": data[\"content\"],\n",
|
||
" },\n",
|
||
" {\n",
|
||
" \"role\": \"assistant\",\n",
|
||
" \"content\": data[\"summary\"],\n",
|
||
" },\n",
|
||
" ]\n",
|
||
" }\n",
|
||
" data_out_list.append(data_out)\n",
|
||
" json.dump(data_out_list, fo, ensure_ascii=False, indent=4)\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 2. 使用 LoRA 进行微调\n",
|
||
"\n",
|
||
"命令行一键运行"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"!bash lora_finetune.sh"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "base",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.10.13"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|