Axolotl 是一款旨在简化各种人工智能模型微调的工具,为多种配置和架构提供支持。
特征:
目录
| Axolotl 提供了一个用于微调的统一存储库 继续问蝾螈问题!! |
FP16/FP32 | 洛拉 | 格洛拉 | 总表 | 带 flash attn 的 gptq | 闪光注意事项 | xformers 收件人 | |
---|---|---|---|---|---|---|---|
骆驼 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
米斯特拉尔 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
混合MoE | ✅ | ✅ | ✅ | ❓ | ❓ | ❓ | ❓ |
混合8X22 | ✅ | ✅ | ✅ | ❓ | ❓ | ❓ | ❓ |
皮提亚 | ✅ | ✅ | ✅ | ❓ | |||
大脑 | ✅ | ✅ | ✅ | ❓ | |||
BTLM | ✅ | ✅ | ✅ | ❓ | |||
MPT | ✅ | ❓ | ❓ | ||||
鹘 | ✅ | ✅ | ✅ | ❓ | |||
gpt-j | ✅ | ✅ | ✅ | ❓ | ❓ | ||
XGen | ✅ | ❓ | ✅ | ❓ | ❓ | ❓ | ✅ |
菲 | ✅ | ✅ | ✅ | ❓ | ❓ | ❓ | ❓ |
RWKV | ✅ | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ |
奎文 | ✅ | ✅ | ✅ | ❓ | ❓ | ❓ | ❓ |
芽 | ✅ | ✅ | ✅ | ❓ | ❓ | ✅ | ❓ |
詹巴 | ✅ | ✅ | ✅ | ❓ | ❓ | ✅ | ❓ |
✅:支持 :不支持 ❓:未经测试
只需几个步骤即可开始使用蝾螈!本快速入门指南将引导您完成设置和运行基本的微调任务。
要求:Nvidia GPU(Ampere 架构或bf16
和 Flash Attention 的更新版本)、Python >=3.10 和 PyTorch >=2.3.1。
git clone https://github.com/axolotl-ai-cloud/axolotl
cd axolotl
pip3 install packaging ninja
pip3 install -e ' .[flash-attn,deepspeed] '
# preprocess datasets - optional but recommended
CUDA_VISIBLE_DEVICES= " " python -m axolotl.cli.preprocess examples/openllama-3b/lora.yml
# finetune lora
accelerate launch -m axolotl.cli.train examples/openllama-3b/lora.yml
# inference
accelerate launch -m axolotl.cli.inference examples/openllama-3b/lora.yml
--lora_model_dir= " ./outputs/lora-out "
# gradio
accelerate launch -m axolotl.cli.inference examples/openllama-3b/lora.yml
--lora_model_dir= " ./outputs/lora-out " --gradio
# remote yaml files - the yaml config can be hosted on a public URL
# Note: the yaml config must directly link to the **raw** yaml
accelerate launch -m axolotl.cli.train https://raw.githubusercontent.com/axolotl-ai-cloud/axolotl/main/examples/openllama-3b/lora.yml
docker run --gpus ' "all" ' --rm -it axolotlai/axolotl:main-latest
或者在当前文件上运行以进行开发:
docker compose up -d
提示
如果您想调试 axolotl 或者更喜欢使用 Docker 作为开发环境,请参阅调试指南中有关 Docker 的部分。
一个更强大的 Docker 运行命令是这样的:
docker run --privileged --gpus ' "all" ' --shm-size 10g --rm -it --name axolotl --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --mount type=bind,src= " ${PWD} " ,target=/workspace/axolotl -v ${HOME} /.cache/huggingface:/root/.cache/huggingface axolotlai/axolotl:main-latest
它还:
--ipc
和--ulimit
args 防止运行时出现内存问题,例如 deepspeed(例如,您可能会遇到 SIGBUS/signal 7 错误)。--mount
/ -v
args 保存下载的 HF 数据(模型等)以及对 axolotl 代码的修改。--name
参数只是让在 vscode ( Dev Containers: Attach to Running Container...
) 或终端中引用容器变得更容易。--privileged
标志为容器提供所有功能。--shm-size 10g
参数增加共享内存大小。如果您在使用 deepspeed 时看到exitcode: -7
错误,请使用此选项。更多信息请访问 nvidia 网站
安装 python >= 3.10
安装 pytorch 稳定版 https://pytorch.org/get-started/locally/
安装 Axolotl 以及 python 依赖项
pip3 install packaging
pip3 install -e ' .[flash-attn,deepspeed] '
(可选)登录 Huggingface 以使用门控模型/数据集。
huggingface-cli login
在 Huggingface.co/settings/tokens 获取令牌
对于支持 docker 镜像的云 GPU 提供商,请使用axolotlai/axolotl-cloud:main-latest
sudo apt update
sudo apt install -y python3.10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
sudo update-alternatives --config python # pick 3.10 if given option
python -V # should be 3.10
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
安装 Pytorch https://pytorch.org/get-started/locally/
按照快速入门上的说明进行操作。
跑步
pip3 install protobuf==3.20.3
pip3 install -U --ignore-installed requests Pillow psutil scipy
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu: $LD_LIBRARY_PATH
使用安装了 cuda 和 pytorch 的 Deeplearning Linux 操作系统。然后按照快速入门上的说明进行操作。
确保运行以下命令来卸载 xla。
pip uninstall -y torch_xla[tpu]
请使用 WSL 或 Docker!
使用以下方法代替快速入门中的安装方法。
pip3 install -e '.'
更多信息:mac.md
请使用此示例笔记本。
要在 7 个以上云(GCP、AWS、Azure、OCI 等)上的 GPU 实例(按需实例和现货实例)上启动,您可以使用 SkyPilot:
pip install " skypilot-nightly[gcp,aws,azure,oci,lambda,kubernetes,ibm,scp] " # choose your clouds
sky check
获取使用 Axolotl 微调mistralai/Mistral-7B-v0.1
的示例 YAML:
git clone https://github.com/skypilot-org/skypilot.git
cd skypilot/llm/axolotl
使用一个命令来启动:
# On-demand
HF_TOKEN=xx sky launch axolotl.yaml --env HF_TOKEN
# Managed spot (auto-recovery on preemption)
HF_TOKEN=xx BUCKET= < unique-name > sky spot launch axolotl-spot.yaml --env HF_TOKEN --env BUCKET
要在公共云(GCP、AWS、Azure、Lambda Labs、TensorDock、Vast.ai 和 CUDO)上的 GPU 实例(按需实例和现货实例)上启动,您可以使用 dstack。
在 YAML 中编写职位描述如下:
# dstack.yaml
type : task
image : axolotlai/axolotl-cloud:main-latest
env :
- HUGGING_FACE_HUB_TOKEN
- WANDB_API_KEY
commands :
- accelerate launch -m axolotl.cli.train config.yaml
ports :
- 6006
resources :
gpu :
memory : 24GB..
count : 2
然后,只需使用dstack run
命令运行作业即可。如果您想要现货实例,请附加--spot
选项。 dstack run
命令将向您显示跨多云服务价格最便宜的实例:
pip install dstack
HUGGING_FACE_HUB_TOKEN=xxx WANDB_API_KEY=xxx dstack run . -f dstack.yaml # --spot
有关更详细的用例,请参阅官方 dstack 文档以及官方存储库上 axolotl 示例的详细说明。
Axolotl 支持多种数据集格式。建议使用 JSONL。 JSONL 的架构取决于您希望使用的任务和提示模板。除了 JSONL,您还可以使用 HuggingFace 数据集,其中每个 JSONL 字段都有列。
有关如何使用不同数据集格式的更多信息,请参阅文档。
请参阅示例以快速入门。建议根据您的需要进行复制和修改。最重要的选项是:
模型
base_model : ./llama-7b-hf # local or huggingface repo
注意:代码将加载正确的架构。
数据集
datasets :
# huggingface repo
- path : vicgalle/alpaca-gpt4
type : alpaca
# huggingface repo with specific configuration/subset
- path : EleutherAI/pile
name : enron_emails
type : completion # format from earlier
field : text # Optional[str] default: text, field to use for completion data
# huggingface repo with multiple named configurations/subsets
- path : bigcode/commitpackft
name :
- ruby
- python
- typescript
type : ... # unimplemented custom format
# chat_template https://axolotl-ai-cloud.github.io/axolotl/docs/dataset-formats/conversation.html#chat_template
- path : ...
type : chat_template
chat_template : chatml # defaults to tokenizer's chat_template
# local
- path : data.jsonl # or json
ds_type : json # see other options below
type : alpaca
# dataset with splits, but no train split
- path : knowrohit07/know_sql
type : context_qa.load_v2
train_on_split : validation
# loading from s3 or gcs
# s3 creds will be loaded from the system default and gcs only supports public access
- path : s3://path_to_ds # Accepts folder with arrow/parquet or file path like above. Supports s3, gcs.
...
# Loading Data From a Public URL
# - The file format is `json` (which includes `jsonl`) by default. For different formats, adjust the `ds_type` option accordingly.
- path : https://some.url.com/yourdata.jsonl # The URL should be a direct link to the file you wish to load. URLs must use HTTPS protocol, not HTTP.
ds_type : json # this is the default, see other options below.
加载中
load_in_4bit : true
load_in_8bit : true
bf16 : auto # require >=ampere, auto will detect if your GPU supports this and choose automatically.
fp16 : # leave empty to use fp16 when bf16 is 'auto'. set to false if you want to fallback to fp32
tf32 : true # require >=ampere
bfloat16 : true # require >=ampere, use instead of bf16 when you don't want AMP (automatic mixed precision)
float16 : true # use instead of fp16 when you don't want AMP
注意:Repo 不进行 4 位量化。
洛拉
adapter : lora # 'qlora' or leave blank for full finetune
lora_r : 8
lora_alpha : 16
lora_dropout : 0.05
lora_target_modules :
- q_proj
- v_proj
有关所有配置选项,请参阅这些文档。
跑步
accelerate launch -m axolotl.cli.train your_config.yml
提示
您还可以引用托管在公共 URL 上的配置文件,例如accelerate launch -m axolotl.cli.train https://yourdomain.com/your_config.yml
在微调之前,您可以选择使用以下内容对数据集进行预标记。建议对于大型数据集使用此方法。
dataset_prepared_path:
设置为本地文件夹,用于保存和加载预标记化数据集。push_dataset_to_hub: hf_user/repo
将其推送到Huggingface。--debug
查看预处理的示例。 python -m axolotl.cli.preprocess your_config.yml
以下是 axolotl 中可用于使用多个 GPU 进行训练的选项。请注意,DeepSpeed 是目前推荐的多 GPU 选项,因为 FSDP 可能会遇到丢失不稳定的情况。
Deepspeed 是一款适用于多 GPU 系统的优化套件,可让您训练比 GPU VRAM 通常能够容纳的模型大得多的模型。有关 deepspeed 各种优化类型的更多信息,请访问 https://huggingface.co/docs/accelerate/main/en/usage_guides/deepspeed#what-is-integrated
我们为 ZeRO 第 1、2 和 3 阶段提供了几种默认的 deepspeed JSON 配置。
deepspeed : deepspeed_configs/zero1.json
accelerate launch -m axolotl.cli.train examples/llama-2/config.yml --deepspeed deepspeed_configs/zero1.json
fsdp :
- full_shard
- auto_wrap
fsdp_config :
fsdp_offload_params : true
fsdp_state_dict_type : FULL_STATE_DICT
fsdp_transformer_layer_cls_to_wrap : LlamaDecoderLayer
Axolotl 支持使用 FSDP 和 QLoRA 进行训练,请参阅这些文档以获取更多信息。
确保您的WANDB_API_KEY
环境变量已设置(推荐),或者您使用wandb login
登录到 wandb 。
wandb_mode :
wandb_project :
wandb_entity :
wandb_watch :
wandb_name :
wandb_log_model :
确保您的COMET_API_KEY
环境变量已设置(推荐),或者您使用comet login
登录到 wandb 。
use_comet :
comet_api_key :
comet_workspace :
comet_project_name :
comet_experiment_key :
comet_mode :
comet_online :
comet_experiment_config :
在记号生成器的词汇表中包含特殊的记号(例如分隔符、序列结尾、序列开头)非常重要。这将帮助您避免标记化问题并帮助您更好地训练模型。你可以在 axolotl 中这样做:
special_tokens :
bos_token : " <s> "
eos_token : " </s> "
unk_token : " <unk> "
tokens : # these are delimiters
- " <|im_start|> "
- " <|im_end|> "
当您将这些标记包含在 axolotl 配置中时,axolotl 会将这些标记添加到标记生成器的词汇表中。
Liger Kernel:用于 LLM 培训的高效 Triton 内核
https://github.com/linkedin/Liger-Kernel
Liger(LinkedIn GPU Efficient Runtime)内核是专门为 LLM 训练设计的 Triton 内核的集合。可有效提升多GPU训练吞吐量20%,内存占用降低60%。 Liger Kernel 组合良好,并且与 FSDP 和 Deepspeed 兼容。
plugins :
- axolotl.integrations.liger.LigerPlugin
liger_rope : true
liger_rms_norm : true
liger_glu_activation : true
liger_layer_norm : true
liger_fused_linear_cross_entropy : true
Axolotl 允许您在交互式终端游乐场中加载模型以进行快速实验。配置文件与用于训练的配置文件相同。
根据训练的模型类型,将适当的标志传递给推理命令:
python -m axolotl.cli.inference examples/your_config.yml --lora_model_dir= " ./lora-output-dir "
python -m axolotl.cli.inference examples/your_config.yml --base_model= " ./completed-model "
cat /tmp/prompt.txt | python -m axolotl.cli.inference examples/your_config.yml
--base_model= " ./completed-model " --prompter=None --load_in_8bit=True
-- 使用 gradio 托管
python -m axolotl.cli.inference examples/your_config.yml --gradio
如果您已打开并收到类似于以下内容的错误,请使用--sample_packing False
:
RuntimeError: 堆栈期望每个张量大小相等,但在条目 0 处得到 [1, 32, 1, 128],在条目 1 处得到 [1, 32, 8, 128]
以下命令会将您的 LORA 适配器与您的基本模型合并。您可以选择传递参数--lora_model_dir
来指定保存 LORA 适配器的目录,否则,这将从 axolotl 配置文件中的output_dir
推断出来。合并后的模型保存在子目录{lora_model_dir}/merged
中。
python3 -m axolotl.cli.merge_lora your_config.yml --lora_model_dir= " ./completed-model "
您可能需要使用gpu_memory_limit
和/或lora_on_cpu
配置选项以避免内存不足。如果你仍然用完 CUDA 内存,你可以尝试合并到系统 RAM 中
CUDA_VISIBLE_DEVICES= " " python3 -m axolotl.cli.merge_lora ...
尽管这会非常慢,但建议使用上面的配置选项。
另请参阅常见问题解答和调试指南。
如果您遇到“Cuda 内存不足”错误,则意味着您的 GPU 在训练过程中内存不足。解决方法如下:
请减少以下任何一项
micro_batch_size
eval_batch_size
gradient_accumulation_steps
sequence_len
如果它没有帮助,请尝试在命令中不使用 deepspeed 和加速(将“accelerate launch”替换为“python”)来运行。
使用 adamw_bnb_8bit 也可能会节省一些内存。
failed (exitcode: -9)
通常意味着您的系统已耗尽系统内存。同样,您应该考虑减少与用完 VRAM 时相同的设置。此外,考虑升级系统 RAM,这应该比 GPU 升级更简单。
运行时错误:预期标量类型 Float 但发现 Half
尝试设置fp16: true
NotImplementedError:找不到
memory_efficient_attention_forward
的运算符...
尝试关闭 xformers。
加速配置丢失
忽略它是安全的。
NCCL 训练期间超时
请参阅 NCCL 指南。
对于许多格式,Axolotl 通过在标记字符串后连接标记 id 来构造提示。连接 token id 而不是对字符串进行操作的原因是为了保持对注意力掩码的精确计算。
如果您解码由 axolotl 构造的提示,您可能会看到您不希望看到的标记之间有空格(或缺少空格),特别是在分隔符和特殊标记周围。当您开始使用新格式时,您应该始终执行以下操作:
python -m axolotl.cli.preprocess your_config.yml --debug
实现一些数据,然后使用模型的标记生成器解码前几行。训练和推理过程中的提示不一致可能会导致模型表现非常差,因此值得检查这一点。请参阅此博客文章了解具体示例。
请参阅此调试指南,了解有关调试 Axolotl 的提示,以及使用 VSCode 进行调试的示例配置。
加入我们的 Discord 服务器,我们的社区成员可以为您提供帮助。
需要专门的支持吗?请通过 ✉️[email protected] 联系我们以获得专门的支持选项。
用蝾螈打造一些很酷的东西?考虑在您的模型卡上添加徽章。
[ < img src = " https://raw.githubusercontent.com/axolotl-ai-cloud/axolotl/main/image/axolotl-badge-web.png " alt = " Built with Axolotl " width = " 200 " height = " 32 " /> ] ( https://github.com/axolotl-ai-cloud/axolotl )
查看一些使用 Axolotl 构建的项目和模型!您有想要添加到我们的社区展示中的模型吗?与您的模型建立 PR。
开放获取人工智能集体
袖珍文档实验室
请阅读贡献指南
虫子?请检查未解决的问题,否则创建一个新问题。
非常欢迎PR !
请运行快速入门说明,然后运行以下命令来设置环境:
pip3 install -r requirements-dev.txt -r requirements-tests.txt
pre-commit install
# test
pytest tests/
# optional: run against all files
pre-commit run --all-files
感谢迄今为止所有的贡献者。通过为 Axolotl 做出贡献,帮助推动开源人工智能的进步。
OpenAccess AI Collective 由 winglian、NanoCode012、tmm1、mhenrichsen、casper-hansen、hamelsmu 等志愿者贡献者运营,他们通过修复错误、回答社区问题和实现新功能来帮助我们加速前进。 Axolotl 需要赞助商捐赠运行我们的单元和集成测试、解决社区问题以及提供赏金所需的计算资源。如果您喜欢 axolotl,请考虑通过 GitHub Sponsors、Ko-fi 赞助该项目,或直接联系 [email protected]。