掘金 人工智能 20小时前
第一次免费使用A800显卡80GB显存微调Ernie大模型
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何利用AI Studio提供的免费A800 80GB显存资源,结合ERNIEKit工具对ERNIE大模型进行微调。文章详细讲解了ERNIEKit的安装、模型下载(包括aistudio-sdk和huggingface两种方式)、不同模型和训练方法的介绍,以及数据准备、监督微调(SFT)和直接偏好优化(DPO)等核心训练流程。此外,还涵盖了LoRA权重合并和使用FastDeploy进行模型部署及测试的步骤,为用户提供了一个完整的实践指南,让爱好者也能轻松上手大模型微调。

💡 **AI Studio提供免费80GB显存A800资源**:文章强调了AI Studio平台近期活动,为用户提供了免费使用A800 80GB显存的宝贵机会,这对于普通爱好者来说是接触和实验大型模型微调的绝佳途径。

🛠️ **ERNIEKit助力模型微调与部署**:ERNIEKit是专门为ERNIE模型设计的工具,支持模型微调和快捷部署。通过GitHub仓库和详细的安装指南,用户可以轻松配置环境并开始使用。

📚 **模型下载与训练方法多样**:文章提供了通过aistudio-sdk和huggingface两种方式下载ERNIE模型的途径,并详细列出了ERNIE-4.5系列模型支持的监督微调(SFT)和直接偏好优化(DPO)等多种训练方法,以及对应的硬件资源需求和训练脚本。

📊 **SFT与DPO的原理与应用**:文章解释了监督式微调(SFT)通过标注数据优化模型性能,适用于精度要求高的任务;而直接偏好优化(DPO)作为一种对齐技术,旨在解决模型“难以控制”的问题,相比RLHF具有更高的稳定性和更低的计算开销。

🚀 **模型部署与测试流程**:完成微调后,文章介绍了如何使用ERNIEKit进行权重合并和部署,包括erniekit server/chat命令以及更专业的FastDeploy集成CLI工具。最后,通过OpenAI库的示例代码演示了如何测试部署好的模型,确保其正常运行。

话说大模型出来后,大部分工作都是围绕调用展开,像微调等工作不是我等爱好者可以用的,那么时至今日,aisutdio上有有免费使用A800 80GB的机会,你会用吗?话不多说,现在开始。

1.ERNIEKit v2.0介绍

- 下载地址 https://github.com/PaddlePaddle/ERNIE- 安装
# !git clone https://gitclone.com/github.com/PaddlePaddle/ERNIE%cd ERNIE!python -m pip install -r requirements/gpu/requirements.txt!python -m pip install -e . # 推荐使用可编辑模式安装

2.aistudio介绍

aistudio是paddlepaddle框架的开源社区,可以进行各类学习。

3.下载模型

下载有下面2个路径:

# 首先请先安装aistudio-sdk库!pip install --upgrade aistudio-sdk# 使用aistudio cli下载模型(推荐)!aistudio download --model PaddlePaddle/ERNIE-4.5-0.3B-Paddle --local_dir baidu/ERNIE-4.5-0.3B-Paddle# 从huggingface下载模型(需换源)# !huggingface-cli download baidu/ERNIE-4.5-0.3B-Paddle --local-dir baidu/ERNIE-4.5-0.3B-Paddle

4.模型介绍及训练脚本

ERNIEKit支持以下模型的训练。开始训练前请确保:环境配置完成和硬件资源满足最低要求

ModelPost-Training MethodSeq LengthMin ResourcesRecommended Config
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BSFT8K96x80G A/H GPUsrun_sft_8k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BSFT32K112x80G A/H GPUsrun_sft_32k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BSFT(FP8)8K16x80G H GPUs + 2TB CPU RAMrun_sft_fp8_8k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BSFT(FP8)32K16x80G H GPUs + 2TB CPU RAMrun_sft_fp8_32k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BSFT-LoRA(wint4/8)8K4x80G A/H GPUsrun_sft_wint8mix_lora_8k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BSFT-LoRA(wint4/8)32K8x80G A/H GPUsrun_sft_wint8mix_lora_32k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BDPO8K112x80G A/H GPUsrun_dpo_8k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BDPO32K112x80G A/H GPUsrun_dpo_32k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BDPO-LoRA8K16x80G A/H GPUsrun_dpo_lora_8k.sh
ERNIE-4.5-300B-A47B-Base/ERNIE-4.5-300B-A47BDPO-LoRA32K16x80G A/H GPUsrun_dpo_lora_32k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BSFT8K8x80G A/H GPUsrun_sft_8k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BSFT32K8x80G A/H GPUsrun_sft_32k.sh
ERNIE-4.5-21B-A3B-B base/ERNIE-4.5-21B-A3BSFT128K8x80G A/H GPUsrun_sft_128k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BSFT-LoRA(wint4/8)8K2x80G A/H GPUsrun_sft_wint8mix_lora_8k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BSFT-LoRA(wint4/8)32K2x80G A/H GPUsrun_sft_wint8mix_lora_32k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BDPO8K8x80G A/H GPUsrun_dpo_8k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BDPO32K8x80G A/H GPUsrun_dpo_32k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BDPO128K8x80G A/H GPUsrun_dpo_128k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BDPO-LoRA8K2x80G A/H GPUsrun_dpo_lora_8k.sh
ERNIE-4.5-21B-A3B-Base/ERNIE-4.5-21B-A3BDPO-LoRA32K2x80G A/H GPUsrun_dpo_lora_32k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BSFT8K1x80G A/H GPUrun_sft_8k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BSFT32K1x80G A/H GPUrun_sft_32k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BSFT128K1x80G A/H GPUrun_sft_128k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BSFT-LoRA(wint4/8)8K1x80G A/H GPUrun_sft_wint8mix_lora_8k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BSFT-LoRA(wint4/8)32K1x80G A/H GPUrun_sft_wint8mix_lora_32k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BDPO8K1x80G A/H GPUrun_dpo_8k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BDPO32K1x80G A/H GPUrun_dpo_32k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BDPO128K1x80G A/H GPUrun_dpo_128k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BDPO-LoRA8K1x80G A/H GPUrun_dpo_lora_8k.sh
ERNIE-4.5-0.3B-Base/ERNIE-4.5-0.3BDPO-LoRA32K1x80G A/H GPUrun_dpo_lora_32k.sh

4.数据准备

ERNIEKit支持 alpaca 和 erniekit 两种数据集格式。详细格式规范请参考:数据集指南。提供了 erniekit 格式的示例数据集(sft-train.jsonl)用于快速上手。

5.监督史微调SFT

监督式微调通过标注数据集优化预训练语言模型,提升任务特定性能和指令遵循能力。这种参数更新方法具有以下特点:

6.直接偏好优化(DPO)

对齐训练是确保大语言模型行为与人类意图、价值观或特定目标一致的关键技术。其核心目标是解决预训练模型"强大但难以控制"的问题,使模型输出更安全、可靠,更符合人类期望。

直接偏好优化(DPO)是实现人类偏好对齐的代表性方法,直接在标注偏好数据上微调模型参数。相比RLHF,DPO训练稳定性更高、计算开销更低,已成为主流的偏好对齐方法。示例1:全参数直接偏好优化

# 首先请先安装aistudio-sdk库# !pip install --upgrade aistudio-sdk# 使用aistudio cli下载模型(推荐)# !aistudio download --model PaddlePaddle/ERNIE-4.5-0.3B-Paddle --local_dir baidu/ERNIE-4.5-0.3B-Paddle# 从huggingface下载模型(需换源)# !huggingface-cli download baidu/ERNIE-4.5-0.3B-Paddle --local-dir baidu/ERNIE-4.5-0.3B-Paddle# 8K序列长度DPO训练# !erniekit train examples/configs/ERNIE-4.5-0.3B/dpo/run_dpo_8k.yaml# 32K序列长度DPO训练# !erniekit train examples/configs/ERNIE-4.5-0.3B/dpo/run_dpo_32k.yaml

7.权重合并

LoRA微调完成后,需要将LoRA权重与主模型权重合并。多机训练场景下需要注意:⚠️每台机器存储部分模型参数(检查点)⚠️必须同步所有机器的参数文件 后再进行LoRA权重合并或部署

训练完成后合并LoRA参数到基座模型:

# 权重合并%cd ~/ERNIE!erniekit export examples/configs/ERNIE-4.5-0.3B/run_export.yaml lora=True

8.模型部署

训练完成的ERNIEKit权重可通过FastDeploy集成的CLI工具直接部署。以下是ERNIE-4.5-0.3B的部署示例(在终端中运行):

# 启动服务 erniekit server examples/configs/ERNIE-4.5-0.3B/run_chat.yaml# 开始对话 erniekit chat examples/configs/ERNIE-4.5-0.3B/run_chat.yaml
# 1: FastDeploy完整启动代码import subprocessimport timeimport requestsimport threadingdef start_fastdeploy():    cmd = [        "python", "-m", "fastdeploy.entrypoints.openai.api_server",        "--model", "output/export",        "--port", "8180",        "--metrics-port", "8181",         "--engine-worker-queue-port", "8182",        "--max-model-len", "32768",        "--max-num-seqs", "32"    ]        print("🚀 启动FastDeploy服务...")    print("-" * 50)        process = subprocess.Popen(        cmd,        stdout=subprocess.PIPE,        stderr=subprocess.STDOUT,        universal_newlines=True,        bufsize=1    )        print(f"📝 PID: {process.pid}")        service_ready = False        def monitor_logs():        nonlocal service_ready        try:            while True:                output = process.stdout.readline()                if output == '' and process.poll() is not None:                    break                if output:                    line = output.strip()                    print(f"[日志] {line}")                                        if "Loading Weights:" in line and "100%" in line:                        print("✅ 权重加载完成")                    elif "Loading Layers:" in line and "100%" in line:                        print("✅ 层加载完成")                    elif "Worker processes are launched" in line:                        print("✅ 工作进程启动")                    elif "Uvicorn running on" in line:                        print("🎉 服务启动完成!")                        service_ready = True                        break        except Exception as e:            print(f"日志监控错误: {e}")        log_thread = threading.Thread(target=monitor_logs, daemon=True)    log_thread.start()        start_time = time.time()    while time.time() - start_time < 120:        if service_ready:            break        if process.poll() is not None:            print("❌ 进程退出")            return None        time.sleep(1)        if not service_ready:        print("❌ 启动超时")        process.terminate()        return None        print("-" * 50)    return processdef test_model():    try:        import openai                print("🔌 测试模型连接...")                client = openai.Client(base_url="http://localhost:8180/v1", api_key="null")                response = client.chat.completions.create(            model="null",            messages=[                {"role": "system", "content": "你是一个有用的AI助手。"},                {"role": "user", "content": "你好"}            ],            max_tokens=50,            stream=False        )                print("✅ 模型测试成功!")        print(f"🤖 回复: {response.choices[0].message.content}")        return True            except Exception as e:        print(f"❌ 测试失败: {e}")        return Falsedef check_service():    try:        response = requests.get("http://localhost:8180/v1/models", timeout=3)        return response.status_code == 200    except:        return Falsedef setup_service():    print("=== ERNIE-4.5-0.3B-Paddle 服务启动 ===")        if check_service():        print("✅ 发现运行中的服务")        if test_model():            print("🎉 服务已就绪!")            return True        print("⚠️ 服务异常,重新启动")        process = start_fastdeploy()        if process is None:        print("❌ 启动失败")        return False        if test_model():        print("🎊 启动成功!现在可以运行知识图谱代码")        return True    else:        print("❌ 启动但连接失败")        return Falseif __name__ == "__main__" or True:    setup_service()

9.测试

# 2:测试模型import openaihost = "0.0.0.0"port = "8180"client = openai.Client(base_url=f"http://{host}:{port}/v1", api_key="null")response = client.chat.completions.create(    model="null",    messages=[        {"role": "system", "content": "你好"},        {"role": "user", "content": "请为我并编写水仙花数的python代码"},    ],    stream=True,)for chunk in response:    if chunk.choices[0].delta:        print(chunk.choices[0].delta.content, end='')print('\n')

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

ERNIEKit AI Studio 大模型微调 A800 ERNIE
相关文章