【ms-swift 大模型微调实战】

安装环境

  • pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  • pip install modelscope vllm ‘ms-swift[llm]’ -U

下载模型

  • modelscope download --model Qwen/Qwen2.5-7B-Instruct --local_dir ./Qwen2.5-7B-Instruct

微调

  • 实验环境:4 * A100# 显存占用:4 * 70GB
    NPROC_PER_NODE=4 CUDA_VISIBLE_DEVICES=0,1,2,3 swift sft \
        --model_type qwen2_5-72b-instruct \
        --model_id_or_path Qwen2.5-72B-Instruct \
        --dataset qwen2-pro-en#500 qwen2-pro-zh#500 self-cognition#500 \
        --logging_steps 5 \
        --learning_rate 1e-4 \
        --output_dir output \ 
        --lora_target_modules ALL \
        --model_name 小黄 'Xiao Huang' \
        --model_author 魔搭 ModelScope \
        --deepspeed default-zero3
  • 单卡A10/3090可运行的例子 (Qwen2.5-7B-Instruct)# 显存占用:24GB
  	CUDA_VISIBLE_DEVICES=0 swift sft \
    --model_type qwen2_5-7b-instruct \
    --model_id_or_path Qwen2.5-7B-Instruct \
    --dataset qwen2-pro-en#500 qwen2-pro-zh#500 self-cognition#500 \
    --logging_steps 5 \
    --max_length 2048 \
    --learning_rate 1e-4 \
    --output_dir output \
    --lora_target_modules ALL \
    --model_name 小黄 'Xiao Huang' \
    --model_author 魔搭 ModelScope

融合lora

CUDA_VISIBLE_DEVICES=0,1 swift export \    
        --ckpt_dir output/qwen2_5-72b-instruct/vx-xxx/checkpoint-xxx \    
        --merge_lora true

vLLM部署

vllm serve xxxxx-checkpoint-merged [opentional args]

参考

### 使用Swift对大型模型进行微调 对于使用 Swift 对大型模型进行微调的任务,当前主要依赖于 Apple 提供的工具链以及社区支持。Apple 的 Core ML 工具允许开发者将机器学习模型集成到 iOS 应用程序中,并提供了初步的支持用于训练和优化这些模型[^1]。 然而,在具体实现上,由于 Swift 主要应用于苹果生态系统内开发环境,针对大规模模型微调的工作通常先在其他平台完成(如 PyTorch 或 TensorFlow),再转换成 Core ML 格式以便部署至移动设备或 macOS 上运行。这涉及到几个关键步骤: - **准备数据集**:确保拥有高质量的数据集来调整预训练模型参数。 - **选择基础模型**:挑选适合特定任务需求的预训练模型作为起点。 - **迁移学习框架的选择**:虽然直接利用 Swift 进行端到端训练可能不是最优解法,但是可以借助 Metal Performance Shaders (MPS) 和 Create ML 来加速部分流程并简化操作[^2]。 为了更好地理解如何应用 Swift 实现这一过程,下面给出一段简单的代码片段展示如何加载已有的 CoreML 模型并通过 MPS 执行推理: ```swift import Foundation import Vision import CoreML import MetalPerformanceShaders // 加载CoreML模型 guard let modelURL = Bundle.main.url(forResource: "MyModel", withExtension: "mlmodelc") else { fatalError("Failed to load model file.") } do { let configuration = MLModelConfiguration() let model = try VNCoreMLModel(for: MyModel(configuration: configuration).model) // 创建请求处理对象 let requestHandler = VNImageRequestHandler(cgImage: inputCGImage, options: [:]) try requestHandler.perform([VNCoreMLRequest(model: model)]) } catch { print(error.localizedDescription) } ``` 值得注意的是,上述例子仅展示了基于现有 CoreML 模型执行预测的过程;而对于更复杂的微调工作,则建议首先在外部门户网站上完成必要的训练阶段后再导入至 Swift 中做进一步的应用层面上的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放飞自我的Coder

你的鼓励很棒棒哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值