摘要:而操作系统方面,由于绝大多数工业场景下微调会涉及多卡微调,目前只有Linux系统对DeepSpeed和其他多卡并行加速库支持较好,因此绝大多数工业场景下都会使用Ubuntu操作系统或 CentOS操作系统。本次以Ubuntu系统为例来进行高效微调,CentOS
大模型微调属于大模型进阶类技术,不同于普通的模型对话或搭建基础应用,微调往往需要一定的软硬件条件支持。
大模型微调所需硬件一览尺寸与显存对比图
接下来将以8B模型为例进行高效微调,最低INT4情况下仅需7G显存即可运行。
操作系统选择而操作系统方面,由于绝大多数工业场景下微调会涉及多卡微调,目前只有Linux系统对DeepSpeed和其他多卡并行加速库支持较好,因此绝大多数工业场景下都会使用Ubuntu操作系统或 CentOS操作系统。本次以Ubuntu系统为例来进行高效微调,CentOS操作系统也可以。
若无相关软件环境,相关代码也可以在Windows下运行(本节微调示例不涉及多卡并行)。但若想体验更加真实的工业场景下的微调流程,也可以考虑在AutoDL(算力租赁网站)上租赁显卡并配置Ubuntu 服务器来完成操作。最小化实现微调效果,仅需单卡3090运行两小时即可得到结果,仅需不到5元即可 完成训练:
算力租赁
首先你需要安装Python环境、Anaconda环境;这些在我们租赁的服务器都有,如果是用自己的电脑微调,则需要安装这两个环境,都很简单;下载之后一路点击Next即可。如果有问题参考我之前的文章,还有问题不会安装,请留言我可以做个安装教程。如果机器有以上两个环境即可进入下一步:
(base) root@autodl-container-ce1442a1a8-208edcc9:~# python --versionPython 3.11.10# 创建虚拟环境(base) root@autodl-container-ce1442a1a8-208edcc9:~# conda create --name deepseekr1 python=3.11.10# 中间省略,提示输入 y 即可。Proceed ([y]/n)? y# 中间省略,根据提示执行下面的命令激活虚拟环境(base) root@autodl-container-ce1442a1a8-208edcc9:~# conda activate deepseekr1(deepseekr1) root@autodl-container-ce1442a1a8-208edcc9:~#unsloth是推理、微调一体式框架,在开始安装之前可以执行如下命令开启服务器下载加速:
source /etc/network_turbo以上命令是开启学术加速,就是提高下载速度,下面开始安装:
安装效果图
在大规模模型训练中,我们往往需要监控和分析大量的训练数据,而WandB可以帮助我们实现这一 目标。它提供了以下几个重要的功能:
实时可视化:WandB可以实时展示训练过程中关键指标的变化,如损失函数、学习率、训练时间 等。通过这些可视化数据,我们能够直观地了解模型的训练进展,快速发现训练中的异常或瓶颈。
自动记录与日志管理:WandB会自动记录每次实验的参数、代码、输出结果,确保实验结果的可追 溯性。无论是超参数的设置,还是模型的架构调整, WandB都能够帮助我们完整保留实验记录,方便后 期对比与调优。
支持中断与恢复训练:在长时间的预训练任务中,系统中断或需要暂停是常见的情况。通过WandB 的checkpoint功能,我们可以随时恢复训练,从上次中断的地方继续进行,避免数据和时间的浪费。
多实验对比:当我们尝试不同的模型配置或超参数时, WandB允许我们在多个实验之间轻松进行对 比分析,帮助我们选择最优的模型配置。
团队协作:WandB还支持团队协作,多个成员可以共同查看实验结果,协同调试模型。这对研究和 项目开发中团队的合作非常有帮助。
注册账号
完善信息
API key信息
然后即可在令行中输入如下代码安装wandb:
pip install wandb安装效果
接下来在unsloth微调前,我们即可设置wandb进行微调记录,并可在对应网站上观察到训练过程。
本次实验我将同时介绍DeepSeek R1 Qwen蒸馏模型和LLama蒸馏模型的高效微调流程,因此需 要提前进行模型下载。考虑到国内网络环境,可以使用ModelScope进行下载,这里首先需要安装 ModelScope:
pip install modelscope7B模型
然后创建DeepSeek-R1-Distill-Qwen-7B文件夹,用于保存模型权重:
mkdir ./DeepSeek-R1-Distill-Qwen-7B(deepseekr1) root@autodl-container-ce1442a1a8-208edcc9:~/autodl-tmp# mkdir ./DeepSeek-R1-Distill-Qwen-7B(deepseekr1) root@autodl-container-ce1442a1a8-208edcc9:~/autodl-tmp# lltotal 20drwxr-xr-x 6 root root 150 Feb 16 23:46 ./drwx------ 1 root root 4096 Feb 16 23:36 ../drwxr-xr-x 2 root root 75 Feb 16 23:46 .autodl/drwxr-xr-x 4 root root 4096 Dec 16 17:48 chatglm3-6b/drwxr-xr-x 2 root root 10 Feb 16 23:46 DeepSeek-R1-Distill-Qwen-7B/-rw------- 1 root root 92 Dec 15 22:47 .msc-rw-r--r-- 1 root root 4478 Dec 15 22:47 README.mddrwxr-xr-x 2 root root 10 Dec 15 22:47 ._____temp/(deepseekr1) root@autodl-container-ce1442a1a8-208edcc9:~/autodl-tmp#然后即可使用如下命令进行模型下载:
modelscope download --model deepseek-ai/DeepSeek-R1-Distill-Qwen-7B --local_dir /root/autodl-tmp/DeepSeek-R1-Distill-Qwen-7B下载流程
下载完成后模型完整项目如下:
下载完成
ModelScope模型地址: https://www.modelscope.cn/models/deepseek-ai/DeepSeek-R1-Distill-Llama-8B
8B模型
然后创建DeepSeek-R1-Distill-Llama-8B文件夹,用于保存模型权重,即可使用如下命令进行模型下载:
mkdir ./DeepSeek-R1-Distill-Llama-8B
modelscope download --model deepseek-ai/DeepSeek-R1-Distill-Llama-8B --local_dir ./DeepSeek-R1-Distill-Llama-8B
下载完成后模型完整项目如下:
8B模型权重文件
两个模型下载完成后文件结构如下所示:
模型截图
DeepSeek R1及其蒸馏模型,推理过程的具体体现就是在回复内容中,会同时包含推理部分内容和 最终回复部分内容,并且其推理部分内容会通过(一种在模型训练过程中注入的特殊标记)来进行区分。
推理和回复
也就是说, DeepSeek R1模型组的回复格式是一种非常特殊的格式,即包含think部分内容,也包含 response部分内容。
因此,在围绕DeepSeek R1模型组进行微调的时候,微调数据集的回复部分文本也需要是包含推理 和最终回复两部分内容,才能使得DeepSeek R1模型组在保持既定回复风格的同时,强化模型能力,反 之则会导致指令消融问题(模型回复不再包含think部分)。
此时think部分和最终回复,共同构成有监督微调的标签。
而这种同时包含思考和结果的数据集,在推理模型大行其道的当下也并不少见,例如非常著名的数 学问答数据集NuminaMath CoT,就同时包含数学问题、问题的解题思路(也就是think部分)和问题最 终的答案。而该数据集也是可以用于推理模型微调的数据集。
数据集
NuminaMath CoT数据集地址: https://huggingface.co/datasets/AI-MO/NuminaMath-CoT除了NuminaMath CoT数据集外,还有APPs(编程数据集)、 TACO(编程数据集)、long_form_thought_data_5k (通用问答数据集)等,都是CoT数据集,均可用于推理模型微调。
本次微调选取2024年12月31号最新发布的一个包含推理过程的医学数据集:由深圳大数据研究院发布的HuatuoGPT-o1模型的微调数据集——medical-o1-reasoning-SFT。
medical-o1-reasoning-SFT地址:https://huggingface.co/datasets/FreedomIntelligence/medical-o1-reasoning-SFT医疗数据集
数据集总共包含25400条数据,均为医学领域疾病诊断数据集,且不乏一些疑难杂症的推理和判断,数据集整体质量较高,推理过程严谨准确,非常适合进行医疗领域模型微调,可以极大程度提高模 型对于病理的推理过程,并在这个过程中完成一些医疗知识的灌注。
例如一种一条数据集内容如下:
Question:A 45-year-old man with a history of alcohol use, who has been abstinent for the past 10 years, presents with sudden onset dysarthria, shuffling gait, and intention tremors. Given this clinical presentation and history, what is the most likely diagnosis?一位45岁的男性,有饮酒史,过去10年一直戒酒,现因突然出现构音困难、步态蹒跚和意向 性震颤就诊。根据这一临床表现和病史,最可能的诊断是什么?
Complex_CoT:Alright, let’s break this down. We have a 45-year-old man here, who suddenly starts showing some pretty specific symptoms: dysarthria, shuffling gait, and those intention tremors. This suggests something's going wrong with motor control, probably involving the cerebellum or its connections. Now, what's intriguing is that he's had a history of alcohol use, but he's been off it for the past 10 years. Alcohol can do a number on the cerebellum, leading to degeneration,and apparently, the effects can hang around or even appear long after one stops drinking. At first glance, these symptoms look like they could be some kind of chronic degeneration,maybe something like alcoholic cerebellar degeneration, but hold on. This looks different. The symptoms just came on all of a sudden. Chronic degenerations typically have a more gradual onset. Okay, let’s reconsider this sudden nature. It’s making me think of something more acute, more rapid onset. Hmm, if we dig back into his history of drinking,there might have been some damagedone, leading to nutritional deficiencies, like a lack of thiamine. Wernicke’s encephalopathy is a classic possibility here. That condition pops up due to thiamine deficiency, often after a history of alcohol use. It’s known for causing ataxia,
confusion, and eye movement issues. However, he’s not showing the full triad of symptoms; there's no mention of confusion or eye problems, so maybe it doesn’t fit perfectly. Oh, wait a second, maybe we're missing something simpler. Given the acute nature of the symptoms,maybe this is more inline with something like a stroke. Sudden onset can definitely suggest a vascular event. With his alcohol history, he’s at risk for things like high blood pressure, which can increase stroke risk. In particular, lacunar strokes can mess with motor coordination,speech, the works. These are small, but significant enough to cause these exact symptoms: dysarthria, tremors, and a shuffling gait. But hang on, what if there’s another angle we’re missing? Chronic alcohol effects might still play a role here, justin a different way. There’s a condition called acquired hepatocerebral degeneration. This can happen in people with a history of liver disease due to alcohol, even years after they’ve quit. It can result in symptoms like these because of how substances get deposited in the brain. Linking this back to our patient, his history with alcohol could’ve led to some liver issues, and now, here we are with these symptoms showing up suddenly. Makes sense, right? So, considering everything,
acquired hepatocerebral degeneration seems to fit quite nicely. That’s probably our most fitting diagnosis for this situation.
好的,让我们一步一步分析这个问题。我们有一位45岁的男性,突然出现了几个相当具体的 症状:构音困难、步态蹒跚和意向性震颤。这提示着运动控制可能出现了问题,很可能是小 脑或其连接受到了影响。现在有趣的是,他有饮酒史,但过去10年一直戒酒。酒精对小脑有 很大的影响,可能导致退化,而且显然这种影响可能会持续很长时间,甚至在戒酒后仍然会 出现。乍一看,这些症状看起来像是某种慢性退行性病变,也许像是酒精性小脑退行性病变,但稍等一下,这看起来有所不同。症状突然出现。慢性退行性病变通常是逐渐开始的。 好吧,让我们重新考虑一下这种突然的性质。这让我想到一些更急性的、起病迅速的疾病。 嗯,如果我们回顾他的饮酒史,可能存在一些损伤,导致营养缺乏,比如缺乏硫胺素。维尔 尼克脑病就是一个经典的可能性。这个病状是由于硫胺素缺乏引起的,通常发生在有饮酒史 的人群中。它的典型症状包括共济失调、意识混乱和眼球运动问题。然而,他并没有表现出 完整的三联症;没有提到混乱或眼部问题,所以这可能不完全符合。哦,等一下,也许我们 漏掉了更简单的情况。考虑到症状的急性性质,也许这更像是中风。突然发作肯定可以提示 血管事件。考虑到他的饮酒史,他有高血压的风险,这会增加中风的风险。特别是,腔隙性 中风可能会影响运动协调、言语等。这些小的中风虽然很小,但足够显著,能引发这些确切 的症状:构音困难、震颤和蹒跚步态。但等等,是否还有其他角度我们没考虑到?慢性酒精 影响可能仍然起着作用,只是以不同的方式。有一种叫做获得性肝脑病的疾病。这种情况可 能发生在有酒精引起的肝病史的人身上,即使他们已经戒酒多年。它可能会导致类似的症状,因为体内的物质沉积在大脑中。回到我们的患者,他的酒精史可能导致了一些肝脏问题,而现在这些症状突然出现了。这是有道理的,对吧?所以,综合考虑所有因素,获得性 肝脑病似乎非常符合。这个诊断可能是我们最合适的结论。
. Response :Considering the clinical presentation of sudden onset dysarthria, shuffling gait, and intention tremors in a 45-year-old man with a history of alcohol use who has been abstinent for the past 10 years, the most likely diagnosis is acquired hepatocerebral degeneration. This condition is associated with chronic liver disease, which can often be a consequence of long-term alcohol use. Despite the patient's abstinence from alcohol for a decade, previous alcohol use may have led to underlying liver dysfunction. This dysfunction, even if subclinical, can cause encephalopathy due to the accumulation of neurotoxic substances that affect the brain. The sudden onset of these neurological symptoms aligns with how acquired hepatocerebral degeneration can manifest, making it a probable diagnosis in this scenario.
考虑到这位45岁男性突发构音困难、蹒跚步态和意向性震颤的临床表现,以及他有饮酒史且 过去10年未再饮酒,最可能的诊断是获得性肝脑病。该病与慢性肝病相关,而慢性肝病通常 是长期饮酒的后果。尽管患者已经戒酒十年,但之前的饮酒可能导致了潜在的肝功能障碍。 即使这些障碍是亚临床的,它也能通过神经毒性物质的积聚引起脑病。症状的突然出现与获 得性肝脑病的表现方式相符,因此它是最符合的诊断。
至此准备工作完成,第三步开展正式微调阶段,下一篇见。
来源:通往AI的地铁