基于LLaMA-Factory微调DeepSeek-R1-Distill-Qwen-7B最佳实践 步骤二:微调DeepSeek模型 1. 启动LLaMAFactory服务 在完成云主机开通后,即可通过ssh连接到到云主机命令行。模型及LLaMAFactory服务均已放置在/root目录下,且已完成配套环境安装,可直接运行。 plaintext 进入目录 cd /root/LLaMAFactory 启动webui服务 (也可通过nohup放到后台启动) llamafactorycli webui 在成功启动LLaMAFactory服务后,您将可以通过 访问到相关页面。 plaintext 访问服务 2. 配置基础大模型和微调方法 在成功进入LLaMAFactory页面后,您首先需要按照如图的内容进行页面语言,模型名称和模型路径的设置。 注意 模型预置在/root/DeepSeekR1DistillQwen7B目录下。 3. 配置微调数据集 在Train页面内,您需要配置用于微调训练的数据集。镜像内预置了alpaca格式的天翼云示例数据,您也可参考示例数据修改/root/LLaMAFactory/data/目录下的alpacazhctyun.json(微调数据集)和datasetinfo.json(数据集信息)两个文件,改用您自己的私有数据。 镜像内置两份数据集,单条内容示例如下: data/alpacazhctyun.json(天翼云文档数据集) plaintext { "instruction": "天翼云的通用型弹性云主机有哪些规格?", "input": "", "output": " 用户询问通用型云主机规格,首先需要明确通用型的定位是平衡计算和内存资源。根据产品文档,通用型以s7系列为代表,应当列举不同后缀规格及其配置差异,最后补充适用场景说明。 n通用型弹性云主机有多个规格,例如s7.small、s7.medium、s7.large、s7.xlarge等,每种规格提供不同的vCPU和内存配置,适合多种业务场景。" } data/alpacaenmedcalcbenchmax2k.json(医学计算数据集MedCalcBench) plaintext { "instruction": "A 16yearold female adolescent was referred to our hospital with severe hypertension (systolic pressure 178 mmHg), which was first detected 7 months prior to presentation during a routine annual physical ...", "input": "What is the patient's Creatinine Clearance using the CockroftGault Equation in terms of mL/min? You should use the patient's adjusted body weight in kg instead of the patient's actual body weight if the patient is ...", "output": " The formula for computing CockcroftGault is given by CrCl ((140 age) adjusted weight (gendercoefficient)) / (serum creatinine 72), where the gendercoefficient is 1 if male, and 0.85 if female..." } 4. 配置相关训练参数 在Train页面内,还有大量可修改的微调训练参数。如果您对他们还不够了解,可暂时不进行修改。其中,训练轮数与样本量级关联性较大,如果您的微调数据量很少,则可能需要设置较大的训练轮数,才能有效果。 5. 启动微调训练 点击最下方的开始按钮,即可基于上面选择的基础模型和微调数据集,启动模型微调训练。页面下方会显示实时的训练进度,训练日志和loss变化情况。 如果您的训练样本较多,单张A10显卡的24G显存很容易因为无法承载,而导致报错"CUDA out of memory"。此时,您需要将云主机变配到显存更大的机型规格,并开启DeepSpeed stage3进行模型参数分片,如下图所示。 同时,您还需要修改默认DeepSpeed配置中的部分参数,以保证训练正常进行。修改/root/LLaMAFactory/cache/dsz3config.json中的如下内容: plaintext "zerooptimization": { "stage": 3, "overlapcomm": true, "contiguousgradients": true, "subgroupsize": 1e9, "reducebucketsize": "auto", "stage3prefetchbucketsize": "auto", "stage3parampersistencethreshold": "auto", "stage3maxliveparameters": 1e4, //降低该参数以减少显存占用 "stage3maxreusedistance": 1e4, //降低该参数以减少显存占用 "stage3gather16bitweightsonmodelsave": true }