1 Star 0 Fork 0

mustang2247/ms-swift

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

SWIFT (Scalable lightWeight Infrastructure for Fine-Tuning)



魔搭社区
中文  |  English  |  文档

📖 目录

📝 简介

SWIFT(Scalable lightWeight Infrastructure for Fine-Tuning)是一个可扩展的轻量级一站式训练、推理深度学习框架。它集成了各种高效的微调方法,如LoRA、QLoRA、阿里云自研的ResTuning-Bypass等,以及开箱即用的训练推理脚本,使开发者可以在单张商业级显卡上微调推理LLM&AIGC模型。此外,SWIFT与PEFT完全兼容,使开发者可以在ModelScope模型体系中使用PEFT的能力。

目前支持的方法:

  1. 🔥LoRA: LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS
  2. 🔥LoRA+: LoRA+: Efficient Low Rank Adaptation of Large Models
  3. 🔥LLaMA PRO: LLAMA PRO: Progressive LLaMA with Block Expansion
  4. 🔥SCEdit: SCEdit: Efficient and Controllable Image Diffusion Generation via Skip Connection Editing < arXiv | Project Page >
  5. 🔥NEFTune: Noisy Embeddings Improve Instruction Finetuning
  6. QA-LoRA:Quantization-Aware Low-Rank Adaptation of Large Language Models.
  7. LongLoRA: Efficient Fine-tuning of Long-Context Large Language Models
  8. ROME: Rank-One Editing of Encoder-Decoder Models
  9. Adapter: Parameter-Efficient Transfer Learning for NLP
  10. Prompt Tuning: Visual Prompt Tuning
  11. Side: Side-Tuning: A Baseline for Network Adaptation via Additive Side Networks
  12. Res-Tuning: Res-Tuning: A Flexible and Efficient Tuning Paradigm via Unbinding Tuner from Backbone < arXiv | Project Page | Usage >
  13. PEFT提供的tuners, 如IA3, AdaLoRA等

主要能力:

  1. 可以通过model-id使SWIFT或PEFT的方法使用ModelScope Hub中的模型
  2. 在单次训练或推理中可以使用多个tuners
  3. 支持调用activate_adapterdeactivate_adapterset_active_adapters来使部分tuner激活或失活,用户可以在推理时同时加载多个独立的tuners在不同线程中并行使用。
  4. 支持通过脚本方式和命令行方式开启训练和推理,同时支持Web-UI方式进行推理。
  5. 支持模型训练后的部署链路(vllm/chatglm.cpp/xinference),详情可以查看官方文档

用户可以查看 SWIFT官方文档 来了解详细信息。

🎉 新闻

  • 🔥2024.03.09: 支持MAMBA模型的训练和推理, 使用这个脚本来开始训练!.
  • 2024.03.09: 支持AQLM量化模型的训练和推理, 使用这个脚本开始训练!
  • 2024.03.06: 支持AWQ量化模型的训练和推理, 使用这个Qwen1.5-AWQ模型脚本开始训练, 并支持yi-9b的训练和推理.
  • 🔥2024.02.29: 支持LLaMA PRO, 使用这个脚本即可开始训练.
  • 🔥2024.02.29: 支持LoRA+, 使用这个脚本即可开始训练.
  • 2024.02.25: 支持swift export, 对模型进行AWQ/GPTQ量化导出, 以及推送ModelScope Hub. 具体可以查看文档: LLM量化文档.
  • 2024.02.22: 支持gemma系列: gemma-2b, gemma-2b-instruct, gemma-7b, gemma-7b-instruct.
  • 2024.02.16: 支持deepseek-math系列: deepseek-math-7b, deepseek-math-7b-instruct, deepseek-math-7b-chat.
  • 🔥2024.02.05: 支持Qwen1.5系列模型, 支持的所有Qwen1.5系列模型请查看模型列表. 提供了qwen1half-7b-chat, qwen1half-7b-chat-int8微调的脚本.
  • 2024.02.05: 支持扩散模型如SDXL, SD, ControlNet的训练, 同时也支持DreamBooth的训练, 详情可以查看对应的训练脚本.
  • 2024.02.01: 支持openbmb-minicpm系列: openbmb-minicpm-2b-sft-chat, openbmb-minicpm-2b-chat.
  • 🔥2024.02.01: 支持数据集打混来减少 灾难性遗忘问题. 使用--train_dataset_mix_ratio 2.0开启训练!同时我们也开源了通用知识数据集 ms-bench.
  • 🔥2024.02.01: 支持Agent训练!Agent训练算法源自这篇论文. 我们也增加了ms-agent这个优质的agent数据集. 使用这个脚本开启Agent训练!
  • 🔥2024.02.01: 支持在DPO训练中增加SFT loss来减少KL散度loss造成的生成重复问题.
  • 2024.02.01: 支持在训练中使用AdaLoRA和IA3两个adapter.
  • 2024.02.01: 支持在AnimateDiff训练中使用--merge_lora参数.
更多
  • 2024.01.30: 支持internlm-xcomposer2-7b-chat.
  • 🔥2024.01.30: 支持ZeRO-3, 只需要指定--deepspeed default-zero3即可.
  • 2024.01.29: 支持internlm2-math系列: internlm2-math-7b, internlm2-math-7b-chat, internlm2-math-20b, internlm2-math-20b-chat.
  • 🔥2024.01.26: 支持yi-vl-6b-chat, yi-vl-34b-chat.
  • 2024.01.24: 支持codefuse-codegeex2-6b-chat, codefuse-qwen-14b-chat.
  • 2024.01.23: 支持orion系列: orion-14b, orion-14b-chat.
  • 2024.01.20: 支持xverse-13b-256k, xverse-65b-v2, xverse-65b-chat.
  • 🔥2024.01.17: 支持internlm2系列: internlm2-7b-base, internlm2-7b, internlm2-7b-sft-chat, internlm2-7b-chat, internlm2-20b-base, internlm2-20b, internlm2-20b-sft-chat, internlm2-20b-chat.
  • 2024.01.15: 支持yuan系列: yuan2-2b-instruct, yuan2-2b-janus-instruct, yuan2-51b-instruct, yuan2-102b-instruct.
  • 🔥2024.01.12: 支持deepseek-moe系列: deepseek-moe-16b, deepseek-moe-16b-chat.
  • 🔥2024.01.04: 支持VLLM部署, 兼容OpenAI API样式, 具体可以查看VLLM推理加速与部署.
  • 2024.01.04: 更新Benchmark, 方便查看不同模型训练的速度和所需显存.
  • 🔥 2023.12.29: 支持web-ui进行sft训练和推理,安装ms-swift后使用swift web-ui开启
  • 🔥 2023.12.29: 支持 DPO RLHF(Reinforcement Learning from Human Feedback) 和三个用于此任务的数据集: AI-ModelScope/stack-exchange-paired 以及 AI-ModelScope/hh-rlhf 以及 AI-ModelScope/hh_rlhf_cn. 查看文档开启训练!
  • 🔥 2023.12.28: 支持SCEdit! 该tuner可显著降低U-Net中的显存占用,并支持低显存可控图像生成(取代ControlNet),阅读下面的章节来了解详细信息
  • 2023.12.23: 支持codegeex2-6b.
  • 2023.12.19: 支持phi2-3b.
  • 2023.12.18: 支持VLLM进行推理加速.
  • 2023.12.15: 支持deepseek, deepseek-coder系列: deepseek-7b, deepseek-7b-chat, deepseek-67b, deepseek-67b-chat, openbuddy-deepseek-67b-chat, deepseek-coder-1_3b, deepseek-coder-1_3b-instruct, deepseek-coder-6_7b, deepseek-coder-6_7b-instruct, deepseek-coder-33b, deepseek-coder-33b-instruct.
  • 2023.12.13: 支持mistral-7b-instruct-v2, mixtral-moe-7b, mixtral-moe-7b-instruct.
  • 2023.12.09: 支持freeze_parameters参数, 作为lora和全参数训练的折中方案. 对应的sh可以查看full_freeze_ddp. 支持disable_tqdm, lazy_tokenize, preprocess_num_proc参数, 具体可以查看命令行参数.
  • 2023.12.08: 支持sus-34b-chat, 支持yi-6b-200k, yi-34b-200k.
  • 2023.12.07: 支持Multi-Node DDP训练.
  • 2023.12.05: 支持模型: zephyr-7b-beta-chat, openbuddy-zephyr-7b-chat. 支持数据集: hc3-zh, hc3-en.
  • 🔥 2023.12.02: 自我认知微调最佳实践, 10分钟对大模型进行自我认知微调, 创建专属于自己的大模型.
  • 🔥 2023.11.30: 支持qwen-1_8b, qwen-72b, qwen-audio系列模型的训练的推理. 对应的sh脚本可以查看qwen_1_8b_chat, qwen_72b_chat, qwen_audio_chat
  • 🔥 2023.11.29: 支持AnimateDiff的训练和推理
  • 🔥 2023.11.24: 支持yi-34b-chat, codefuse-codellama-34b-chat模型. 对应的sh脚本可以查看yi_34b_chat, codefuse_codellama_34b_chat.
  • 🔥 2023.11.18: 支持tongyi-finance-14b系列模型: tongyi-finance-14b, tongyi-finance-14b-chat, tongyi-finance-14b-chat-int4. 对应的sh脚本可以查看tongyi_finance_14b_chat_int4.
  • 2023.11.16: 支持更多模型的flash attn支持: qwen系列, qwen-vl系列, llama系列, openbuddy系列, mistral系列, yi系列, ziya系列. 请使用use_flash_attn参数.
  • 🔥 2023.11.11: 支持NEFTune, 使用Swift.prepare_model(model, NEFTuneConfig())即可开启.
  • 🔥 2023.11.11: 支持命令行训练推理和Web-UI推理, 详情可以查看下方的使用Swift CLI运行章节.
  • 🔥 2023.11.11: 支持模型训练后的部署链路(vllm/chatglm.cpp/xinference),详情可以查看官方文档.
  • 🔥 2023.11.10: 支持bluelm系列模型: bluelm-7b, bluelm-7b-chat, bluelm-7b-32k, bluelm-7b-chat-32k. 对应的sh脚本可以查看bluelm_7b_chat.
  • 🔥 2023.11.08: 支持xverse-65b模型的训练和推理流程,脚本在xverse_65b.
  • 🔥 2023.11.07: 支持yi-6b, yi-34b模型的训练和推理流程,脚本在yi_6b, yi_34b.
  • 🔥 2023.10.30: 支持 QA-LoRALongLoRA两种新的tuners.
  • 🔥 2023.10.30: 支持使用ROME(Rank One Model Editing)来编辑模型,在无需训练的情况下即可给模型灌注新知识!
  • 2023.10.30: 支持skywork-13b系列模型: skywork-13b, skywork-13b-chat. 对应的sh脚本可以查看skywork_13b.
  • 🔥 2023.10.27: 支持chatglm3系列模型: chatglm3-6b-base, chatglm3-6b, chatglm3-6b-32k. 对应的sh脚本可以查看chatglm3_6b.
  • 🔥 2023.10.17: 支持int4, int8模型的SFT: qwen-7b-chat-int4, qwen-14b-chat-int4, qwen-vl-chat-int4, baichuan2-7b-chat-int4, baichuan2-13b-chat-int4, qwen-7b-chat-int8, qwen-14b-chat-int8.
  • 2023.10.15: 支持ziya2-13b系列模型: ziya2-13b, ziya2-13b-chat.
  • 2023.10.12: 支持mistral-7b系列模型: openbuddy-mistral-7b-chat, mistral-7b, mistral-7b-instruct.
  • 🔥 2023.10.07: 支持DeepSpeed ZeRO-2, 使得lora(不仅仅是qlora)可以在双卡A10上运行DDP.
  • 2023.10.04: 支持更多数学, 法律, SQL, 代码领域的数据集: blossom-math-zh, school-math-zh, text2sql-en, sql-create-context-en, lawyer-llama-zh, tigerbot-law-zh, leetcode-python-en.
  • 🔥 2023.09.25: 支持qwen-14b系列: qwen-14b, qwen-14b-chat.
  • 2023.09.18: 支持internlm-20b系列: internlm-20b, internlm-20b-chat.
  • 2023.09.12: 支持MP+DDP对全参数训练进行加速.
  • 2023.09.05: 支持openbuddy-llama2-70b-chat.
  • 2023.09.03: 支持baichuan2系列: baichuan2-7b, baichuan2-7b-chat, baichuan2-13b, baichuan2-13b-chat.

✨ 大模型训练推理

WEB UI训练推理

安装SWIFT之后,用如下方式启动界面训练推理:

swift web-ui

支持的环境变量:

WEBUI_SHARE=1 控制gradio是否是share状态 SWIFT_UI_LANG=en/zh 控制web-ui界面语言 WEBUI_SERVER server_name参数, web-ui host ip,0.0.0.0代表所有ip均可访问,127.0.0.1代表只允许本机访问 WEBUI_PORT web-ui的端口号

下面是一个web-ui的简单视频介绍:

Watch the video

简单使用

快速开始

你可以通过运行以下代码来测试环境是否安装正确.

# pip install ms-swift[llm] -U

# Experimental environment: A10, 3090, V100, ...
# 8GB GPU memory
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'

import torch

from swift.llm import (
    DatasetName, InferArguments, ModelType, SftArguments,
    infer_main, sft_main, app_ui_main, merge_lora
)

model_type = ModelType.qwen1half_0_5b
sft_args = SftArguments(
    model_type=model_type,
    train_dataset_sample=2000,
    dataset=[DatasetName.jd_sentiment_zh],
    output_dir='output')
result = sft_main(sft_args)
best_model_checkpoint = result['best_model_checkpoint']
print(f'best_model_checkpoint: {best_model_checkpoint}')
torch.cuda.empty_cache()

infer_args = InferArguments(
    ckpt_dir=best_model_checkpoint,
    load_dataset_config=True,
    val_dataset_sample=10)
merge_lora(infer_args, device_map='cpu')
result = infer_main(infer_args)
torch.cuda.empty_cache()

app_ui_main(infer_args)

训练脚本

你可以参考以下脚本来自定义属于你的训练脚本.

特性

  • 支持的SFT方法: lora, qlora, longlora, qalora, 全参数微调, 部分参数微调.
  • 支持的特性: 模型量化, DDP, 模型并行, gradient checkpointing, 支持推送ModelScope Hub, 自定义数据集, 多模态和Agent SFT, 多轮对话, DPO, 自我认知微调, ...
  • 支持的模型: [详细信息]
    • 多模态:
      • qwen-vl 系列: qwen-vl, qwen-vl-chat, qwen-vl-chat-int4.
      • qwen-audio 系列: qwen-audio, qwen-audio-chat.
      • yi-vl 系列: yi-vl-6b-chat, yi-vl-34b-chat.
      • cogagent 系列: cogagent-18b-chat, cogagent-18b-instruct.
      • internlm-xcomposer2 系列: internlm-xcomposer2-7b-chat.
    • 通用:
      • qwen 系列:
        • qwen-1_8b, qwen-1_8b-chat, qwen-1_8b-chat-int4, qwen-1_8b-chat-int8.
        • qwen-7b, qwen-7b-chat, qwen-7b-chat-int4, qwen-7b-chat-int8.
        • qwen-14b, qwen-14b-chat, qwen-14b-chat-int4, qwen-14b-chat-int8.
        • qwen-72b, qwen-72b-chat, qwen-72b-chat-int4, qwen-72b-chat-int8.
      • qwen1.5 系列:
        • qwen1half-0_5b, qwen1half-0_5b-chat, qwen1half-0_5b-chat-int4, qwen1half-0_5b-chat-int8, qwen1half-0_5b-chat-awq.
        • qwen1half-1_8b, qwen1half-1_8b-chat, qwen1half-1_8b-chat-int4, qwen1half-1_8b-chat-int8, qwen1half-1_8b-chat-awq.
        • qwen1half-4b, qwen1half-4b-chat, qwen1half-4b-chat-int4, qwen1half-4b-chat-int8, qwen1half-4b-chat-awq.
        • qwen1half-7b, qwen1half-7b-chat, qwen1half-7b-chat-int4, qwen1half-7b-chat-int8, qwen1half-7b-chat-awq.
        • qwen1half-14b, qwen1half-14b-chat, qwen1half-14b-chat-int4, qwen1half-14b-chat-int8, qwen1half-14b-chat-awq.
        • qwen1half-72b, qwen1half-72b-chat, qwen1half-72b-chat-int4, qwen1half-72b-chat-int8, qwen1half-72b-chat-awq.
      • chatglm 系列: chatglm2-6b, chatglm2-6b-32k, chatglm3-6b-base, chatglm3-6b, chatglm3-6b-32k.
      • llama 系列: llama2-7b, llama2-7b-chat, llama2-13b, llama2-13b-chat, llama2-70b, llama2-70b-chat.
      • yi 系列: yi-6b, yi-6b-200k, yi-6b-chat, yi-9b, yi-34b, yi-34b-200k, yi-34b-chat.
      • internlm 系列:
        • internlm-7b, internlm-7b-chat, internlm-7b-chat-8k, internlm-20b, internlm-20b-chat.
        • internlm2-1_8b, internlm2-1_8b-sft-chat, internlm2-1_8b-chat, internlm2-7b-base, internlm2-7b, internlm2-7b-sft-chat, internlm2-7b-chat, internlm2-20b-base, internlm2-20b, internlm2-20b-sft-chat, internlm2-20b-chat.
      • deepseek 系列: deepseek-7b, deepseek-7b-chat, deepseek-67b, deepseek-67b-chat, deepseek-moe-16b, deepseek-moe-16b-chat.
      • gemma 系列: gemma-2b, gemma-2b-instruct, gemma-7b, gemma-7b-instruct.
      • openbmb-minicpm 系列: openbmb-minicpm-2b-sft-chat, openbmb-minicpm-2b-chat.
      • openbuddy 系列: openbuddy-llama2-13b-chat, openbuddy-llama-65b-chat, openbuddy-llama2-70b-chat, openbuddy-mistral-7b-chat, openbuddy-zephyr-7b-chat, openbuddy-deepseek-67b-chat, openbuddy-mixtral-moe-7b-chat.
      • mistral 系列: mistral-7b, mistral-7b-instruct, mistral-7b-instruct-v2.
      • mixtral 系列: mixtral-moe-7b, mixtral-moe-7b-instruct.
      • baichuan 系列: baichuan-7b, baichuan-13b, baichuan-13b-chat, baichuan2-7b, baichuan2-7b-chat, baichuan2-13b, baichuan2-13b-chat, baichuan2-7b-chat-int4, baichuan2-13b-chat-int4.
      • yuan 系列: yuan2-2b-instruct, yuan2-2b-janus-instruct, yuan2-51b-instruct, yuan2-102b-instruct.
      • xverse 系列: xverse-7b, xverse-7b-chat, xverse-13b, xverse-13b-chat, xverse-65b, xverse-65b-v2, xverse-65b-chat, xverse-13b-256k.
      • orion 系列: orion-14b, orion-14b-chat.
      • bluelm 系列: bluelm-7b, bluelm-7b-chat, bluelm-7b-32k, bluelm-7b-chat-32k.
      • zephyr 系列: zephyr-7b-beta-chat.
      • ziya 系列: ziya2-13b, ziya2-13b-chat.
      • skywork 系列: skywork-13b, skywork-13b-chat.
      • other: polylm-13b, seqgpt-560m, sus-34b-chat.
    • 金融:
      • tongyi-finance 系列: tongyi-finance-14b, tongyi-finance-14b-chat, tongyi-finance-14b-chat-int4.
    • 代码:
      • codefuse 系列: codefuse-codellama-34b-chat, codefuse-codegeex2-6b-chat, codefuse-qwen-14b-chat.
      • deepseek-coder 系列: deepseek-coder-1_3b, deepseek-coder-1_3b-instruct, deepseek-coder-6_7b, deepseek-coder-6_7b-instruct, deepseek-coder-33b, deepseek-coder-33b-instruct.
      • codegeex2 系列: codegeex2-6b.
      • phi 系列: phi2-3b.
    • 数学:
      • internlm2-math 系列: internlm2-math-7b, internlm2-math-7b-chat, internlm2-math-20b, internlm2-math-20b-chat.
      • deepseek-math 系列: deepseek-math-7b, deepseek-math-7b-instruct, deepseek-math-7b-chat.
  • 支持的数据集: [详细信息]
    • NLP:
      • 通用: 🔥ms-bench, 🔥ms-bench-mini, 🔥alpaca-en(gpt4), 🔥alpaca-zh(gpt4), multi-alpaca-all, instinwild-en, instinwild-zh, cot-en, cot-zh, firefly-all-zh, instruct-en, gpt4all-en, sharegpt-en, sharegpt-zh, tulu-v2-sft-mixture, wikipedia-zh, open-orca, open-orca-gpt4, sharegpt-gpt4, 🔥sharegpt-gpt4-mini.
      • Agent: 🔥ms-agent, damo-mini-agent-zh, damo-agent-zh, agent-instruct-all-en.
      • RLHF: 🔥hh-rlhf-cn, stack-exchange-paired, hh-rlhf-harmless-base, hh-rlhf-helpful-base, hh-rlhf-helpful-online, hh-rlhf-helpful-rejection-sampled, hh-rlhf-red-team-attempts, hh-rlhf-cn-harmless-base-cn, hh-rlhf-cn-helpful-base-cn, hh-rlhf-cn-harmless-base-en, hh-rlhf-cn-helpful-base-en.
      • 代码: code-alpaca-en, 🔥leetcode-python-en, 🔥codefuse-python-en, 🔥codefuse-evol-instruction-zh.
      • 医疗: medical-en, medical-zh, medical-mini-zh, 🔥disc-med-sft-zh.
      • 法律: lawyer-llama-zh, tigerbot-law-zh, 🔥disc-law-sft-zh.
      • 数学: 🔥blossom-math-zh, school-math-zh, open-platypus-en.
      • SQL: text2sql-en, 🔥sql-create-context-en.
      • 文本生成: 🔥advertise-gen-zh, 🔥dureader-robust-zh.
      • 分类: cmnli-zh, 🔥cmnli-mini-zh, 🔥jd-sentiment-zh, 🔥hc3-zh, 🔥hc3-en.
      • AWQ: pileval.
      • 其他: finance-en, poetry-zh, webnovel-zh, generated-chat-zh, cls-fudan-news-zh, ner-jave-zh.
    • 多模态:
      • 视觉: coco-en, 🔥coco-mini-en, coco-mini-en-2, capcha-images.
      • 音频: aishell1-zh, 🔥aishell1-mini-zh.
    • 自定义数据集
  • 支持的对话模板:
    • 文本生成: default-generation, default-generation-bos, chatglm-generation, qwen-audio-generation.
    • 对话: default, qwen, qwen-audio, baichuan, chatglm2, chatglm3, llama, openbuddy, internlm, internlm2, internlm-xcomposer2, yi, yi-vl, yuan, xverse, ziya, skywork, bluelm, zephyr, sus, deepseek, deepseek-coder, codefuse-codellama, codefuse, cogagent-chat, cogagent-instruct, orion, openbmb, gemma, chatml.

🔥SCEdit

SCEdit由阿里巴巴通义实验室视觉智能团队(Alibaba TongYi Vision Intelligence Lab)所提出,是一个高效的生成式微调框架。该框架不仅支持文生图下游任务的微调能力,相比LoRA节省30%-50%的训练显存开销,实现快速迁移到特定的生成场景中;而且还可以直接扩展到可控图像生成任务中,仅需ControlNet条件生成7.9%的参数量并节省30%的显存开销,支持边缘图、深度图、分割图、姿态、颜色图、图像补全等条件生成任务。

我们使用了风格迁移数据集中的3D风格数据进行了训练,并使用相同的Prompt: A boy in a camouflage jacket with a scarf进行测试,具体的定性和定量的结果如下:

Method bs ep Target Module Param. (M) Mem. (MiB) 3D style
LoRA/r=64 1 50 q/k/v/out/mlp 23.94 (2.20%) 8440MiB img
SCEdit 1 50 up_blocks 19.68 (1.81%) 7556MiB img
LoRA/r=64 10 100 q/k/v/out/mlp 23.94 (2.20%) 26300MiB img
SCEdit 10 100 up_blocks 19.68 (1.81%) 18634MiB img
LoRA/r=64 30 200 q/k/v/out/mlp 23.94 (2.20%) 69554MiB img
SCEdit 30 200 up_blocks 19.68 (1.81%) 43350MiB img

使用SCEdit执行训练任务并复现上述结果:

# 先执行下面章节的安装步骤
cd examples/pytorch/multi_modal/notebook
python text_to_image_synthesis.py

🛠️ 安装

SWIFT在Python环境中运行。请确保您的Python版本高于3.8。

  • 方法1:使用pip命令安装SWIFT:
# 全量能力
pip install ms-swift[all] -U
# 仅使用LLM
pip install ms-swift[llm] -U
# 仅使用AIGC
pip install ms-swift[aigc] -U
# 仅使用adapters
pip install ms-swift -U
  • 方法2:通过源代码安装SWIFT(方便运行训练推理脚本),请运行以下命令:
git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .[llm]

SWIFT依赖torch>=1.13。

  • 方法3:在我们的Docker镜像中使用SWIFT
docker pull registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.8.0-py38-torch2.0.1-tf2.13.0-1.9.1

🚀 快速开始

SWIFT支持多个tuners,包括由PEFT提供的tuners。要使用这些tuners,只需调用:

from swift import Swift, LoRAConfig
config = LoRAConfig(...)
model = Swift.prepare_model(model, config, extra_state_keys=['...'])

上面的代码片段随机初始化了tuner。输入model是torch.nn.Module的一个实例,config是SwiftConfig或PeftConfig的子类实例。extra_state_keys是要训练并存储在输出目录中的额外模块权重(如linear head)。

您可以通过以下方式组合多个tuners:

from swift import Swift, LoRAConfig, PromptConfig
model = Swift.prepare_model(model, {'lora': LoRAConfig(...), 'prompt': PromptConfig(...)})

在微调之后,您可以调用save_pretrained和push_to_hub方法:

from swift import push_to_hub
model.save_pretrained('some-output-folder')
push_to_hub('my-group/some-repo-id-modelscope', 'some-output-folder', token='some-ms-token')

假设my-group/some-repo-id-modelscope是Hub中的model-id,而some-ms-token是用于上传的令牌。

使用model-id进行后续推理:

from swift import Swift
model = Swift.from_pretrained(model, 'my-group/some-repo-id-modelscope')

下面是一个可运行的示例:

import os
import tempfile

# 请通过`pip install modelscope`安装modelscope
from modelscope import Model

from swift import LoRAConfig, SwiftModel, Swift, push_to_hub

tmp_dir = tempfile.TemporaryDirectory().name
if not os.path.exists(tmp_dir):
    os.makedirs(tmp_dir)


model = Model.from_pretrained('modelscope/Llama-2-7b-ms', device_map='auto')
lora_config = LoRAConfig(target_modules=['q_proj', 'k_proj', 'v_proj'])
model: SwiftModel = Swift.prepare_model(model, lora_config)
# 在这里进行一些微调操作
model.save_pretrained(tmp_dir)

push_to_hub('my-group/swift_llama2', output_dir=tmp_dir)
model = Model.from_pretrained('modelscope/Llama-2-7b-ms', device_map='auto')
model = SwiftModel.from_pretrained(model, 'my-group/swift_llama2', device_map='auto')

这是一个使用transformers库实例化模型,并使用SWIFT进行高效微调的示例。

from swift import Swift, LoRAConfig, AdapterConfig, PromptConfig
from transformers import AutoModelForImageClassification

# 初始vit模型
model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")

# 初始化LoRA tuner配置
lora_config = LoRAConfig(
    r=10,  # LoRA模块的rank
    target_modules=['query', 'key', 'value'],  # 将要被替换的模块的模块名后缀
    merge_weights=False  # 是否合并权重
)

# 初始化adapter tuner配置
adapter_config = AdapterConfig(
    dim=768,  # hidden states的维度
    hidden_pos=0,  # 要传递到adapter的hidden state的位置
    target_modules=r'.*attention.output.dense$',  # 要使用正则表达式替换的模块
    adapter_length=10  # adapter长度
)

# 初始化prompt tuner配置
prompt_config = PromptConfig(
    dim=768,  # hidden states的维度
    target_modules=r'.*layer\.\d+$',  # 要使用正则表达式替换的模块
    embedding_pos=0,    # embedding张量的位置
    prompt_length=10,   # 提示符token的长度
    attach_front=False  # 是否将提示符附加在embedding前面
)

# 使用swift创建模型。在实践中,您可以使用其中任何一个调谐器或它们的组合。
model = Swift.prepare_model(model, {"lora_tuner": lora_config, "adapter_tuner": adapter_config, "prompt_tuner": prompt_config})

# 获取模型的可训练参数。
model.get_trainable_parameters()
# 'trainable params: 838,776 || all params: 87,406,432 || trainable%: 0.9596273189597764'

可以在SWIFT中使用PEFT提供的功能:

from swift import LoraConfig, Swift
from peft import TaskType
lora_config = LoraConfig(target_modules=['query', 'key', 'value'], task_type=TaskType.CAUSAL_LM)
model_wrapped = Swift.prepare_model(model, lora_config)

# 或者使用from_pretrained从modelscope hub中加载权重。
model_wrapped = Swift.from_pretrained(model, 'some-id-in-the-modelscope-modelhub')

Swift tuners和Peft tuners之间的保存策略略有不同。可以通过以下方式为Swift tuners命名:

model = Swift.prepare_model(model, {'default': LoRAConfig(...)})
model.save_pretrained('./output')

在output目录中将会得到以下类似的目录结构:

output
    |-- default
        |-- adapter_config.json
        |-- adapter_model.bin
    |-- adapter_config.json
    |-- adapter_model.bin

存储在output目录中的config/weights是extra_state_keys的配置和权重。这与Peft不同,Peft存储了default tuner的config/weights。

🔍 了解更多

License

本项目使用Apache License (Version 2.0)进行许可。

☎ 联系我们

您可以通过加我们的微信群, 来和我们联系和交流:

Star History

Star History Chart

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

暂无描述 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ecoteam/ms-swift.git
git@gitee.com:ecoteam/ms-swift.git
ecoteam
ms-swift
ms-swift
dev

搜索帮助