与InstructLab交互的CLI
项目描述
InstructLab 🐶 (ilab
)
📖 内容
- 欢迎使用InstructLab CLI
- ❓ 什么是
ilab
- 📋 要求
- ✅ 入门
- 💻 创建新的知识或技能并训练模型
- 📣 与新模型进行聊天(这次是必需的)
- 🚀 升级InstructLab到最新版本
- 🎁 提交您的新知识或技能
- 📬 贡献
欢迎使用InstructLab CLI
InstructLab 🐶 使用一种基于合成数据对齐的大语言模型(LLM)的新方法。InstructLab 🐶 中的 “lab” 代表 “Large-Scale Alignment for ChatBots” (LAB) [1]。
[1] Shivchander Sudalairaj*, Abhishek Bhandwaldar*, Aldo Pareja*, Kai Xu, David D. Cox, Akash Srivastava*. "LAB: Large-Scale Alignment for ChatBots", arXiv预印本 arXiv: 2403.01081, 2024. (* 表示贡献相等)
❓ 什么是ilab
ilab
是一个命令行界面(CLI)工具,允许您执行以下操作
- 下载预训练的大语言模型(LLM)。
- 与LLM聊天。
要将新知识和技能添加到预训练的LLM中,请向同伴的 分类法 仓库添加信息。
在您已将知识和技能添加到分类法后,您可以执行以下操作
- 使用
ilab
生成基于您本地taxonomy
仓库中更改的新合成训练数据。 - 使用新训练数据重新训练LLM。
- 与新训练的LLM聊天,查看结果。
graph TD;
download-->chat
chat[Chat with the LLM]-->add
add[Add new knowledge<br/>or skill to taxonomy]-->generate[generate new<br/>synthetic training data]
generate-->train
train[Re-train]-->|Chat with<br/>the re-trained LLM<br/>to see the results|chat
有关完整工作流程的概述,请参阅工作流程图。
[!IMPORTANT] 我们已优化InstructLab,以便社区成员使用通用硬件执行这些步骤。然而,在笔记本电脑上运行InstructLab将提供合成数据生成(使用
ilab data generate
命令)和模型指令调整(使用ilab model train
命令,该命令使用QLoRA)的低保真近似。为了获得更高的质量,请使用更复杂的硬件,并将InstructLab配置为使用更大的教师模型,例如Mixtral。
📋 要求
- 🍎 Apple M1/M2/M3 Mac或🐧 Linux系统(在Fedora上进行了测试)。我们预计未来将支持更多操作系统。
- C++编译器
- Python 3.10或Python 3.11
- 大约60GB磁盘空间(整个过程)
注意:Python 3.12目前不支持,因为一些依赖项尚未在Python 3.12上工作。
注意:在macOS上安装
ilab
CLI时,您可能需要运行xcode-select --install
命令,安装先前列出的所需包。
✅ 入门
🧰 安装ilab
-
在Fedora Linux上安装时,通过运行以下命令安装C++、Python 3.10或3.11和其他必需工具
sudo dnf install gcc gcc-c++ make git python3.11 python3.11-devel
如果您正在macOS上运行,此安装不是必需的,您可以直接从以下步骤开始。
-
创建一个名为
instructlab
的新目录来存储ilab
CLI运行时所需的文件,并通过运行以下命令进入目录mkdir instructlab cd instructlab
注意:本文档中的以下步骤使用Python venv进行虚拟环境。但是,如果您使用pyenv或Conda Miniforge等其他工具来管理机器上的Python环境,请继续使用该工具。否则,您可能会遇到在venv中未找到已安装的包的问题。
-
您可以通过以下说明中选择几种本地安装
ilab
CLI的方法之一。选择您首选的安装方法,然后安装ilab
并激活您的venv
环境。注意:⏳ 下一步骤中显示的
python3
二进制文件是您在上一步骤中安装的 Python 版本。该命令也可以是python3.11
或python3.10
而不是python3
。您可以通过python3 -V
检查 Python 的版本。注意:⏳ 根据您的网络连接,
pip install
可能需要一些时间。如果安装失败并出现错误不支持指令 `vpdpbusd'
,请将-C cmake.args="-DLLAMA_NATIVE=off"
追加到pip install
命令中。有关如何在 AMD ROCm、Apple Metal Performance Shaders (MPS) 和 Nvidia CUDA 上启用硬件加速进行交互和训练的说明,请参阅GPU 加速文档。
使用没有 CUDA 绑定和无 GPU 加速的 PyTorch 进行安装
python3 -m venv --upgrade-deps venv source venv/bin/activate pip install instructlab
注意:Intel Macs 的附加构建参数
如果您有一台 Intel CPU 的 Mac,您必须将
CMAKE_ARGS="-DLLAMA_METAL=off"
前缀添加到pip install
命令中,以确保构建过程中不包含 Apple M 系列GPU 支持。(venv) $ CMAKE_ARGS="-DLLAMA_METAL=off" pip install ...
使用AMD ROCm安装
python3 -m venv --upgrade-deps venv source venv/bin/activate pip cache remove llama_cpp_python pip install 'instructlab[rocm]' \ --extra-index-url https://download.pytorch.org/whl/rocm6.0 \ -C cmake.args="-DLLAMA_HIPBLAS=on" \ -C cmake.args="-DAMDGPU_TARGETS=all" \ -C cmake.args="-DCMAKE_C_COMPILER=/opt/rocm/llvm/bin/clang" \ -C cmake.args="-DCMAKE_CXX_COMPILER=/opt/rocm/llvm/bin/clang++" \ -C cmake.args="-DCMAKE_PREFIX_PATH=/opt/rocm" \ -C cmake.args="-DLLAMA_NATIVE=off"
在 Fedora 40+ 上使用
-DCMAKE_C_COMPILER=clang-17
和-DCMAKE_CXX_COMPILER=clang++-17
。在M1/M2/M3 Macs上使用Apple Metal安装
注意:请确保您的系统 Python 构建是
Mach-O 64-bit executable arm64
,使用file -b $(command -v python)
检查,或者如果您已通过 pyenv 配置系统,请使用file -b $(pyenv which python)
命令。注意:此外,请确保
python3 -c 'import platform; print(platform.machine())'
和arch
都返回arm64
。如果arch
返回i386
或python3
返回x86_64
,则表示您的终端或 Python 正在运行在 Intel 模拟模式下,这不适合 Apple Silicon。如果您已从基于 Intel 的 Mac 同步数据到 Apple Silicon Mac(M1/M2/M3),这一点尤为重要,因为您可能需要审查和更新您的环境,特别是之前在 Intel 下安装的工具,如Homebrew
或Bash
。python3 -m venv --upgrade-deps venv source venv/bin/activate pip cache remove llama_cpp_python pip install 'instructlab[mps]'
使用Nvidia CUDA安装
为了获得最佳的 CUDA 体验,安装 vLLM 以服务 Safetensors 格式的模型是必要的。
python3 -m venv --upgrade-deps venv source venv/bin/activate pip cache remove llama_cpp_python pip install 'instructlab[cuda]' \ -C cmake.args="-DLLAMA_CUDA=on" \ -C cmake.args="-DLLAMA_NATIVE=off" pip install vllm@git+https://github.com/opendatahub-io/vllm@2024.08.01
-
从您的
venv
环境中,通过运行ilab
命令来验证ilab
是否正确安装。ilab
ilab
命令的示例输出(venv) $ ilab Usage: ilab [OPTIONS] COMMAND [ARGS]... CLI for interacting with InstructLab. If this is your first time running InstructLab, it's best to start with `ilab config init` to create the environment. Options: --config PATH Path to a configuration file. [default: /home/user/.config/instructlab/config.yaml] -v, --verbose Enable debug logging (repeat for even more verbosity) --version Show the version and exit. --help Show this message and exit. Commands: config Command Group for Interacting with the Config of InstructLab. data Command Group for Interacting with the Data generated by... model Command Group for Interacting with the Models in InstructLab. system Command group for all system-related command calls taxonomy Command Group for Interacting with the Taxonomy of InstructLab. Aliases: chat model chat convert model convert diff taxonomy diff download model download evaluate model evaluate generate data generate init config init list model model_list serve model serve sysinfo system info test model test train model train
重要:每个
ilab
命令都需要在您的 Python 虚拟环境中运行。您可以通过运行source venv/bin/activate
命令进入 Python 环境。 -
可选:您可以为
ilab
命令启用缩进完成。Bash (版本 4.4 或更高)
使用以下命令在
bash
中启用缩进完成eval "$(_ILAB_COMPLETE=bash_source ilab)"
要使此功能在每次打开新 shell 时自动启用,您可以保存完成脚本并将其从
~/.bashrc
中源。_ILAB_COMPLETE=bash_source ilab > ~/.ilab-complete.bash echo ". ~/.ilab-complete.bash" >> ~/.bashrc
Zsh
使用以下命令在
zsh
中启用缩进完成eval "$(_ILAB_COMPLETE=zsh_source ilab)"
要使此功能在每次打开新 shell 时自动启用,您可以保存完成脚本并将其从
~/.zshrc
中源。_ILAB_COMPLETE=zsh_source ilab > ~/.ilab-complete.zsh echo ". ~/.ilab-complete.zsh" >> ~/.zshrc
Fish
使用以下命令在
fish
中启用缩进完成_ILAB_COMPLETE=fish_source ilab | source
要使此功能在每次打开新 shell 时自动启用,您可以保存完成脚本并将其从
~/.bashrc
中源。_ILAB_COMPLETE=fish_source ilab > ~/.config/fish/completions/ilab.fish
🏗️ 初始化ilab
-
通过运行以下命令初始化
ilab
ilab config init
示例输出
Welcome to InstructLab CLI. This guide will help you set up your environment. Please provide the following values to initiate the environment [press Enter for defaults]: Path to taxonomy repo [taxonomy]: <ENTER>
-
当界面提示时,按 Enter 添加新的默认
config.yaml
文件。 -
当提示时,通过键入 y 将
https://github.com/instructlab/taxonomy.git
仓库克隆到当前目录。可选:如果您想指向现有的本地克隆的
taxonomy
仓库,可以交互式地传递路径或使用--taxonomy-path
标志。初始化
ilab
后的示例输出(venv) $ ilab config init Welcome to InstructLab CLI. This guide will help you set up your environment. Please provide the following values to initiate the environment [press Enter for defaults]: Path to taxonomy repo [taxonomy]: <ENTER> `taxonomy` seems to not exists or is empty. Should I clone https://github.com/instructlab/taxonomy.git for you? [y/N]: y Cloning https://github.com/instructlab/taxonomy.git...
ilab
将使用默认配置文件,除非明确指定。您可以使用--config
参数覆盖此行为,对于任何ilab
命令。 -
当提示时,请提供您默认模型的路径。否则,将使用量化后的默认Merlinite模型 - 您可以使用
ilab model download
下载此模型(见下文)。(venv) $ ilab config init Welcome to InstructLab CLI. This guide will help you set up your environment. Please provide the following values to initiate the environment [press Enter for defaults]: Path to taxonomy repo [taxonomy]: <ENTER> `taxonomy` seems to not exists or is empty. Should I clone https://github.com/instructlab/taxonomy.git for you? [y/N]: y Cloning https://github.com/instructlab/taxonomy.git... Path to your model [/home/user/.cache/instructlab/models/merlinite-7b-lab-Q4_K_M.gguf]: <ENTER>
-
当提示时,请选择一个训练配置文件。训练配置文件是针对GPU的特定配置文件,可以启用加速训练行为。如果您使用的是MacOS或没有专用GPU的Linux机器,请按Enter键选择
无配置(CPU,Apple Metal,AMD ROCm)
。您可以使用各种标志与单独的ilab
命令一起使用,以便在适用的情况下利用您的GPU。Welcome to InstructLab CLI. This guide will help you to setup your environment. Please provide the following values to initiate the environment [press Enter for defaults]: Path to taxonomy repo [/home/user/.local/share/instructlab/taxonomy]: Path to your model [/home/user/.cache/instructlab/models/merlinite-7b-lab-Q4_K_M.gguf]: Generating `/home/user/.config/instructlab/config.yaml` and `/home/user/.local/share/instructlab/internal/train_configuration/profiles`... Please choose a train profile to use. Train profiles assist with the complexity of configuring specific GPU hardware with the InstructLab Training library. You can still take advantage of hardware acceleration for training even if your hardware is not listed. [0] No profile (CPU, Apple Metal, AMD ROCm) [1] Nvidia A100/H100 x2 (A100_H100_x2.yaml) [2] Nvidia A100/H100 x4 (A100_H100_x4.yaml) [3] Nvidia A100/H100 x8 (A100_H100_x8.yaml) [4] Nvidia L40 x4 (L40_x4.yaml) [5] Nvidia L40 x8 (L40_x8.yaml) [6] Nvidia L4 x8 (L4_x8.yaml) Enter the number of your choice [hit enter for no profile] [0]: No profile selected - any hardware acceleration for training must be configured manually. Initialization completed successfully, you're ready to start using `ilab`. Enjoy!
根据GPU类型和GPU数量列出GPU配置文件。如果您有与上述配置文件中任何一个类似的vRAM数量的GPU配置,不妨尝试一下!
ilab
目录结构在初始化系统后
在Linux系统上运行ilab config init
后,您的目录将如下所示
├─ ~/.cache/instructlab/models/ (1)
├─ ~/.local/share/instructlab/datasets (2)
├─ ~/.local/share/instructlab/taxonomy (3)
├─ ~/.local/share/instructlab/checkpoints (4)
~/.cache/instructlab/models/
:包含所有下载的大型语言模型,包括您使用ilab生成的模型的保存输出。~/.local/share/instructlab/datasets/
:包含SDG阶段输出的数据,基于对分类存储库的修改。~/.local/share/instructlab/taxonomy/
:包含技能和知识数据。~/.local/share/instructlab/checkpoints/
:包含训练过程的输出。
📥 下载模型
-
运行
ilab model download
命令。ilab model download
ilab model download
从HuggingFace下载模型的紧凑预训练版本(约4.4G)(venv) $ ilab model download Downloading model from Hugging Face: instructlab/merlinite-7b-lab-GGUF@main to /home/user/.cache/instructlab/models... ... INFO 2024-08-01 15:05:48,464 huggingface_hub.file_download:1893: Download complete. Moving file to /home/user/.cache/instructlab/models/merlinite-7b-lab-Q4_K_M.gguf
注意⏳:此命令可能需要几分钟或立即完成,具体取决于您的互联网连接或模型是否已缓存。如果您连接到Hugging Face时遇到问题,请参阅Hugging Face讨论论坛获取更多详细信息。
从Hugging Face存储库下载特定模型
-
指定存储库、模型,如果需要,请指定Hugging Face令牌。有关Hugging Face令牌的更多信息,请参阅此处
HF_TOKEN=<YOUR HUGGINGFACE TOKEN GOES HERE> ilab model download --repository=TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF --filename=mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf
下载整个Hugging Face存储库(Safetensors模型)
-
指定存储库,如果需要,请指定Hugging Face令牌。例如
HF_TOKEN=<YOUR HUGGINGFACE TOKEN GOES HERE> ilab model download --repository=instructlab/granite-7b-lab
这类模型适用于具有GPU的系统或希望使用vLLM提供模型的任何人。InstructLab在HuggingFace上提供Granite模型的Safetensor版本。
列出已下载的模型
-
所有已下载的模型都可以使用
ilab model list
查看。ilab model list
ilab model download
后的ilab model list
的示例输出(venv) $ ilab model list +------------------------------+---------------------+--------+ | Model Name | Last Modified | Size | +------------------------------+---------------------+--------+ | merlinite-7b-lab-Q4_K_M.gguf | 2024-08-01 15:05:48 | 4.1 GB | +------------------------------+---------------------+--------+
🍴 提供模型服务
-
通过运行以下命令提供服务
ilab model serve
-
提供非默认模型(例如,Mixtral-8x7B-Instruct-v0.1)
ilab model serve --model-path models/mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf
-
一旦模型提供服务并准备就绪,您将看到以下输出
(venv) $ ilab model serve INFO 2024-03-02 02:21:11,352 lab.py:201 Using model 'models/ggml-merlinite-7b-lab-Q4_K_M.gguf' with -1 gpu-layers and 4096 max context size. Starting server process After application startup complete see http://127.0.0.1:8000/docs for API. Press CTRL+C to shut down the server.
注意:如果多个
ilab
客户端同时尝试连接到同一InstructLab服务器,则第一个将连接到服务器,而其他客户端将启动自己的临时服务器。这将在主机机器上需要额外的资源。 -
提供非默认Safetensors模型(例如,granite-7b-lab)。注意:这需要GPU。
确保vllm已安装
pip show vllm
如果没有,请运行
pip install vllm@git+https://github.com/opendatahub-io/vllm@2024.08.01
ilab model serve --model-path ~/.cache/instructlab/models/instructlab/granite-7b-lab
📣 与模型聊天(可选)
由于您在一个终端窗口中提供服务模型,您将不得不创建一个新的窗口并重新激活您的Python虚拟环境来运行ilab model chat
命令
source venv/bin/activate
ilab model chat
与非默认模型(例如,Mixtral-8x7B-Instruct-v0.1)进行聊天
source venv/bin/activate
ilab model chat --model models/mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf
请注意,使用--model
需要现有服务器上有该模型。如果没有,您必须退出服务器。ilab model chat
中的--model
具有在端口上没有运行该模型的情况下代表您启动服务器的功能。
在您开始向模型添加新技能和知识之前,您可以通过询问类似“加拿大首都是哪里?”的问题来检查其基线性能。
注意:要使用新技能或知识,模型需要用生成的合成数据进行训练。
(venv) $ ilab model chat
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────── system ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Welcome to InstructLab Chat w/ GGML-MERLINITE-7B-lab-Q4_K_M (type /h for help) │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
>>> what is the capital of Canada? [S][default]
╭────────────────────────────────────────────────────────────────────────────────────────────────────── ggml-merlinite-7b-lab-Q4_K_M ───────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ The capital city of Canada is Ottawa. It is located in the province of Ontario, on the southern banks of the Ottawa River in the eastern portion of southern Ontario. The city serves as the political center for Canada, as it is home to │
│ Parliament Hill, which houses the House of Commons, Senate, Supreme Court, and Cabinet of Canada. Ottawa has a rich history and cultural significance, making it an essential part of Canada's identity. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── elapsed 12.008 seconds ─╯
>>> [S][default]
💻 创建新的知识或技能并训练模型
🎁 贡献知识或组合技能
- 将新知识或组合技能贡献到您本地的分类法仓库中。
详细的贡献说明可以在分类法仓库中找到。
[!重要] 模型可以处理的问题/答案对内容存在限制。因此,请将最多约2300字添加到您的问答种子示例对的
qna.yaml
文件中。
📜 列出并验证您的新数据
您可以使用ilab taxonomy diff
命令来确保ilab
已注册您的新知识或技能,并且您的贡献格式正确。此命令显示您分类法树中的任何新或修改的YAML文件。例如,以下是在freeform writing子目录中添加名为foo-lang
的新技能后的有效组合技能贡献的预期结果
(venv) $ ilab taxonomy diff
compositional_skills/writing/freeform/foo-lang/qna.yaml
Taxonomy in $HOME/.local/share/instructlab/taxonomy is valid :)
您还可以通过使用--taxonomy-base=empty
参数与空基础进行diff来验证您的整个分类法。
(venv) $ ilab taxonomy diff --taxonomy-base=empty
compositional_skills/general/tables/empty/qna.yaml
compositional_skills/general/tables/editing/add_remove/qna.yaml
...
Taxonomy in $HOME/.local/share/instructlab/taxonomy is valid :)
🚀 生成合成数据集
在遵循这些说明之前,请确保您要添加技能或知识的现有模型仍在运行。或者,如果您提供完整的模型路径通过--model
,ilab data generate
可以为您启动服务器。
-
要基于分类法仓库中您新添加的知识或技能集生成合成数据集,请运行以下命令
带有GPU加速
ilab data generate --pipeline full --gpus <NUM_OF_GPUS>
没有GPU加速
ilab data generate --pipeline simple
使用非默认模型(例如Mixtral-8x7B-Instruct-v0.1)生成数据,请运行以下命令
ilab data generate --model ~/.cache/instructlab/models/mistralai/mixtral-8x7b-instruct-v0.1 --pipeline full --gpus 4
注意:⏳这可能需要从15分钟到1小时以上,具体取决于您的计算资源。
ilab data generate
的示例输出(venv) $ ilab data generate INFO 2024-07-30 19:57:44,093 numexpr.utils:161: NumExpr defaulting to 8 threads. INFO 2024-07-30 19:57:44,452 datasets:58: PyTorch version 2.3.1 available. Generating synthetic data using 'simple' pipeline, '$HOME/.cache/instructlab/models/mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf' model, './taxonomy' taxonomy, against http://localhost:8000/v1 server INFO 2024-07-30 19:57:45,084 instructlab.sdg:375: Synthesizing new instructions. If you aren't satisfied with the generated instructions, interrupt training (Ctrl-C) and try adjusting your YAML files. Adding more examples may help. INFO 2024-07-30 19:57:45,090 instructlab.sdg.pipeline:153: Running pipeline single-threaded INFO 2024-07-30 19:57:47,820 instructlab.sdg.llmblock:51: LLM server supports batched inputs: False INFO 2024-07-30 19:57:47,820 instructlab.sdg.pipeline:197: Running block: gen_skill_freeform INFO 2024-07-30 19:57:47,820 instructlab.sdg.pipeline:198: Dataset({ features: ['task_description', 'seed_question', 'seed_response'], num_rows: 5 }) INFO 2024-07-30 20:02:16,455 instructlab.sdg:411: Generated 1 samples ...
合成数据集将在新创建的datasets目录中的两个文件中:
~/.local/share/instructlab/datasets
。这些文件将被命名为skills_train_msgs_*.jsonl
和knowledge_train_msgs_*.jsonl
。 -
通过运行
ls datasets
命令验证文件是否已创建。注意:您必须位于您的XDG_DATA_HOME/instructlab
目录中。(venv) $ ls datasets/ node_datasets_2024-08-12T20_31_15 test_mixtral-8x7b-instruct-v0-1_2024-08-12T20_23_06.jsonl knowledge_recipe_2024-08-12T20_31_15.yaml node_datasets_2024-08-13T19_51_48 test_mixtral-8x7b-instruct-v0-1_2024-08-12T20_31_15.jsonl knowledge_recipe_2024-08-13T19_51_48.yaml skills_recipe_2024-08-12T20_31_15.yaml test_mixtral-8x7b-instruct-v0-1_2024-08-13T19_47_59.jsonl knowledge_train_msgs_2024-08-12T20_31_15.jsonl skills_recipe_2024-08-13T19_51_48.yaml test_mixtral-8x7b-instruct-v0-1_2024-08-13T19_51_48.jsonl knowledge_train_msgs_2024-08-13T19_51_48.jsonl skills_train_msgs_2024-08-12T20_31_15.jsonl train_mixtral-8x7b-instruct-v0-1_2024-08-12T20_31_15.jsonl messages_mixtral-8x7b-instruct-v0-1_2024-08-12T20_31_15.jsonl skills_train_msgs_2024-08-13T19_51_48.jsonl train_mixtral-8x7b-instruct-v0-1_2024-08-13T19_51_48.jsonl messages_mixtral-8x7b-instruct-v0-1_2024-08-13T19_51_48.jsonl test_mixtral-8x7b-instruct-v0-1_2024-08-12T20_13_21.jsonl
可选:您还可以通过使用OpenAI兼容的API(例如,由
ilab model serve
生成的)或任何远程或本地托管的大型语言模型(例如,通过ollama
、LM Studio
等)来运行生成步骤。运行以下命令ilab data generate --endpoint-url http://localhost:8000/v1
请注意,您也可以使用--taxonomy-base=empty
选项根据分类法仓库的全部内容生成合成数据集。
ilab data generate --taxonomy-base=empty
👩🏫 训练模型
有许多选项可以训练模型,以使用增强合成数据的数据集。
注意:每个
ilab
命令都需要在Python虚拟环境中运行。
ilab模型训练管道
ilab model train
有三个管道:simple
、full
和accelerated
。默认是accelerated
。
simple
使用Linux上的SFT训练师和MacOS上的MLX。这种类型的训练大约需要一小时,生成的模型精度最低,但应该可以指示您的数据是否被训练过程选中。full
使用Granite系列模型的自定义训练循环和数据处理函数。此循环针对CPU和MPS功能进行了优化。请在Linux上使用--pipeline=full
与--device=cpu
(或MacOS上的--device=mps
)。您也可以在MacOS机器上使用--device=cpu
。但是,MPS针对这些系统进行了优化以获得更好的性能。accelerated
使用支持GPU加速和分布式训练的instructlab-training库。该full
循环和数据处理函数直接从该库中提取或基于该库的工作。
本地训练模型
ilab model train
注意: ⏳ 此步骤可能需要 数小时 才能完成,具体取决于您的计算资源。请先停止
ilab模型聊天
和ilab模型服务
以释放资源。
如果您在Linux上使用 ilab模型训练 --pipeline=simple
ilab模型训练
输出一个新的模型,可在 models
目录下提供,命名为 ggml-model-f16.gguf
。
如果您在MacOS上使用 ilab模型训练 --pipeline=simple
ilab模型训练
输出一个新的模型,保存在名为 <model_name>-mlx-q
的目录中。
如果您在Linux或MacOS上使用 ilab模型训练 --pipeline=full
ilab模型训练
在 ~/.local/share/instructlab/checkpoints/hf_format
目录中输出新的 .bin
和 .gguf
模型。在每个 samples_*
目录中,您将找到两个GGUF模型。一个是量化模型,另一个是全精度模型,量化模型具有Q4-M-K后缀。
如果您在Linux上使用 ilab模型训练 --pipeline=accelerated
ilab模型训练
输出新的模型,可保存在 ~/.local/share/instructlab/checkpoints
目录中。这些模型可以通过 ilab模型评估
运行以选择最佳模型。
如果您在Linux上使用 ilab模型训练 --strategy lab-multiphase
ilab模型训练
输出新的模型,可保存在 ~/.local/share/instructlab/phased/phase1/checkpoints
和 ~/.local/share/instructlab/phased/phase2/checkpoints
中。第一阶段与知识训练相关联,第二阶段与技能训练相关联。
在运行多阶段训练时,每个阶段都会进行评估,我们将告诉您哪个检查点在此文件夹中表现最佳。
在M系列Mac或Linux上使用完整管道本地训练模型
要使用我们的完整管道和MPS在您的M系列Mac上本地训练模型,或在您的Linux笔记本电脑/台式机上使用CPU
ilab model train --pipeline full --device mps
ilab model train --pipeline full --device cpu
注意: ⏳ 此过程将花费一些时间才能完成。如果您运行大约8个时期,则可能需要数小时。
ilab模型训练
为每个时期输出一个目录,其结构如下所示
$ ls ~/.local/share/instructlab/checkpoints/hf_format/samples_0/
added_tokens.json pytorch_model.bin special_tokens_map.json tokenizer.json
config.json pytorch_model.gguf pytorch_model-Q4_K_M.gguf tokenizer_config.json tokenizer.model
此整个文件夹可以在支持vLLM的系统中使用.bin模型提供。然而,在大多数笔记本电脑上,您可以提供全精度gguf: pytorch_model.gguf
或4位量化模型: pytorch_model-Q4_K_M.gguf
。
在M系列Mac或Linux上使用简单管道本地训练模型
要使用我们的简单管道和MLX在您的M系列Mac上本地训练模型,或在您的Linux笔记本电脑/台式机上使用SFT Trainer
ilab model train --pipeline simple
注意: ⏳ 此过程将花费一些时间才能完成(时间可能因硬件和
ilab数据生成
的输出而异,但大约为5到15分钟)
在Mac上,ilab模型训练
输出一个新的模型,保存在名为 <model_name>-mlx-q
的目录中,称为 adapters.npz
(以Numpy压缩数组格式)。例如
(venv) $ ls instructlab-merlinite-7b-lab-mlx-q
adapters-010.npz adapters-050.npz adapters-090.npz config.json tokenizer.model
adapters-020.npz adapters-060.npz adapters-100.npz model.safetensors tokenizer_config.json
adapters-030.npz adapters-070.npz adapters.npz special_tokens_map.json
adapters-040.npz adapters-080.npz added_tokens.json tokenizer.json
在Linux上,ilab模型训练
输出一个新的模型,可在 models
目录下提供,命名为 ggml-model-f16.gguf
。
使用GPU加速本地训练模型
此版本ilab模型训练
输出新的模型,可保存在 ~/.local/share/instructlab/checkpoints
目录中。这些模型可以通过 ilab模型评估
运行以选择最佳模型。
ilab model train --pipeline accelerated --device cuda
支持使用Nvidia CUDA或AMD ROCm进行GPU加速的实验性训练。有关详细信息,请参阅GPU加速文档。目前,硬件加速需要数据中心GPU或至少有18GB空闲内存的高端消费级GPU。
使用多阶段训练和GPU加速本地训练模型
ilab模型训练
支持多阶段训练。这导致以下工作流程
- 我们在知识上训练模型
- 评估训练好的模型以找到最佳检查点
- 我们在技能上训练模型
- 我们评估模型以找到最佳的整体检查点
ilab model train --strategy lab-multiphase --phased-phase1-data <knowledge train messages jsonl> --phased-phase2-data <skills train messages jsonl> -y
此命令接受您数据目录下的两个 .jsonl
文件,一个是知识 jsonl,另一个是技能 jsonl。使用 -y
标志将跳过询问用户是否确定要运行多阶段训练的交互式提示。
注意:根据数据大小和您运行的训练轮数,此命令可能需要3小时或更长时间。
在云端训练模型
请按照训练中的说明进行操作。
⏳ 每个平台的大致耗时
- Google Colab:使用 T4 GPU 需要 5-10分钟
- Kaggle:使用 P100 GPU 需要 ~30分钟
完成后,您可以直接在 Google Colab 或 Kaggle 笔记本中操作您的模型。云上训练的模型将保存在云上。模型还可以下载并在本地提供服务。
📜 测试新训练的模型
-
运行以下命令来测试模型
ilab model test
命令的输出将包含模型训练前后的一系列输出。
🧪 评估新训练的模型
您可以使用 ilab model evaluate
命令使用几个基准来评估您正在训练的模型。目前支持四种基准。
基准 | 度量 | 全名 | 描述 | 参考 |
---|---|---|---|---|
MMLU | 知识 | 大规模多任务语言理解 | 将模型与标准化的知识数据集进行比较,并根据模型的表现生成分数 | 大规模多任务语言理解度量 |
MMLUBranch | 知识 | N/A | 将您的知识贡献与基础模型进行比较,并根据性能差异生成分数 | N/A |
MTBench | 技能 | 多轮基准 | 将模型应用其知识的能力与裁判模型进行比较,并根据模型的表现生成分数 | MT-Bench(多轮基准) |
MTBenchBranch | 技能 | N/A | 将您的技能贡献与裁判模型进行比较,并根据性能差异生成分数 | N/A |
[!NOTE] MTBench 和 MTBenchBranch 默认使用 prometheus-8x7b-v2.0 作为裁判模型。虽然您不需要使用此模型作为您的裁判,但如果您有必要的硬件资源,强烈建议这样做。您可以通过
ilab model download
下载它。
运行 MMLU
以下是在本地模型上以最小任务运行 MMLU 的示例
$ export INSTRUCTLAB_EVAL_MMLU_MIN_TASKS=true # don't set this if you want to run full MMLU
$ export ILAB_MODELS_DIR=$HOME/.local/share/instructlab/models
$ ilab model evaluate --benchmark mmlu --model $ILAB_MODELS_DIR/instructlab/granite-7b-lab
...
# KNOWLEDGE EVALUATION REPORT
## MODEL
/home/example-user/.local/share/instructlab/models/instructlab/granite-7b-lab
### AVERAGE:
0.45 (across 3)
### SCORES:
mmlu_abstract_algebra - 0.35
mmlu_anatomy - 0.44
mmlu_astronomy - 0.55
以下是在 Hugging Face 模型上以最小任务运行 MMLU 的示例
$ export INSTRUCTLAB_EVAL_MMLU_MIN_TASKS=true # don't set this if you want to run full MMLU
$ ilab model evaluate --benchmark mmlu --model instructlab/granite-7b-lab
...
# KNOWLEDGE EVALUATION REPORT
## MODEL
instructlab/granite-7b-lab
### AVERAGE:
0.45 (across 3)
### SCORES:
mmlu_abstract_algebra - 0.35
mmlu_anatomy - 0.44
mmlu_astronomy - 0.55
[!NOTE] 目前,MMLU 只能在本地或 Hugging Face 上的 safetensors 模型目录上运行,GGUFs 目前不支持。
运行 MMLUBranch
以下是在本地 safetensors 模型目录上运行 MMLUBranch 的示例
$ export ILAB_MODELS_DIR=$HOME/.local/share/instructlab/models
$ ilab model evaluate --benchmark mmlu_branch --model $ILAB_MODELS_DIR/instructlab/granite-7b-lab --base-model $ILAB_MODELS_DIR/instructlab/granite-7b-lab
...
# KNOWLEDGE EVALUATION REPORT
## BASE MODEL
/home/example-user/.local/share/instructlab/models/instructlab/granite-7b-lab
## MODEL
/home/example-user/.local/share/instructlab/models/instructlab/granite-7b-lab
### AVERAGE:
+0.0 (across 1)
### NO CHANGE:
1. tonsils
以下是在 Hugging Face 模型上运行 MMLUBranch 的示例
$ ilab model evaluate --benchmark mmlu_branch --model instructlab/granite-7b-lab --base-model instructlab/granite-7b-lab
...
# KNOWLEDGE EVALUATION REPORT
## BASE MODEL
instructlab/granite-7b-lab
## MODEL
instructlab/granite-7b-lab
### AVERAGE:
+0.0 (across 1)
### NO CHANGE:
1. tonsils
[!TIP] 您可以混合运行本地模型和 Hugging Face 上的远程模型,只要存在 safetensors 模型即可。
运行 MTBench
以下是在本地 safetensors 模型目录上运行 MTBench 的示例
$ export ILAB_MODELS_DIR=$HOME/.local/share/instructlab/models
$ ilab model evaluate --benchmark mt_bench --model $ILAB_MODELS_DIR/instructlab/granite-7b-lab --judge-model $ILAB_MODELS_DIR/instructlab/granite-7b-lab
...
# SKILL EVALUATION REPORT
## MODEL
/home/example-user/.local/share/instructlab/models/instructlab/granite-7b-lab
### AVERAGE:
8.07 (across 91)
### TURN ONE:
8.64
### TURN TWO:
7.19
### ERROR RATE:
0.43
以下是在本地 GGUF 模型上运行 MTBench 的示例
$ export ILAB_MODELS_DIR=$HOME/.local/share/instructlab/models
$ ilab model evaluate --benchmark mt_bench --model $ILAB_MODELS_DIR/granite-7b-lab-Q4_K_M.gguf --judge-model $ILAB_MODELS_DIR/granite-7b-lab-Q4_K_M.gguf
...
# SKILL EVALUATION REPORT
## MODEL
/home/example/.local/share/instructlab/models/granite-7b-lab-Q4_K_M.gguf
### AVERAGE:
5.0 (across 1)
### TURN ONE:
5.0
### TURN TWO:
N/A
### ERROR RATE:
0.99
[!NOTE] 目前,MTBench 必须与本地模型一起使用。不支持直接从 Hugging Face 使用模型而不下载。
运行 MTBenchBranch
以下是在本地 safetensors 模型目录上运行 MTBenchBranch 的示例
$ export ILAB_MODELS_DIR=$HOME/.local/share/instructlab/models
$ export ILAB_TAXONOMY_DIR=$HOME/.local/share/instructlab/taxonomy
$ ilab model evaluate --benchmark mt_bench_branch \
--model $ILAB_MODELS_DIR/instructlab/granite-7b-lab \
--judge-model $ILAB_MODELS_DIR/instructlab/granite-7b-lab \
--base-model $ILAB_MODELS_DIR/instructlab/granite-7b-lab \
--taxonomy-path $ILAB_TAXONOMY_DIR \
--branch rc \
--base-branch main
...
# SKILL EVALUATION REPORT
## BASE MODEL
/home/example/.local/share/instructlab/models/instructlab/granite-7b-lab
## MODEL
/home/example/.local/share/instructlab/models/instructlab/granite-7b-lab
### IMPROVEMENTS:
1. compositional_skills/extraction/receipt/markdown/qna.yaml (+4.0)
2. compositional_skills/STEM/science/units_conversion/temperature_conversion/qna.yaml (+3.0)
3. compositional_skills/extraction/commercial_lease_agreement/bullet_points/qna.yaml (+3.0)
...
### REGRESSIONS:
1. compositional_skills/extraction/abstractive/title/qna.yaml (-5.0)
2. compositional_skills/extraction/receipt/bullet_points/qna.yaml (-4.5)
3. compositional_skills/writing/grounded/summarization/wiki_insights/one_line/qna.yaml (-4.0)
...
### NO CHANGE:
1. compositional_skills/STEM/math/reasoning/qna.yaml
2. compositional_skills/extraction/commercial_lease_agreement/csv/qna.yaml
3. compositional_skills/roleplay/explain_like_i_am/graduate/qna.yaml
...
### NEW:
1. compositional_skills/linguistics/organize_lists/qna.yaml
2. compositional_skills/extraction/invoice/plain_text/qna.yaml
3. compositional_skills/writing/grounded/summarization/wiki_insights/concise/qna.yaml
...
### ERROR RATE:
0.32
以下是在本地 GGUF 模型上运行 MTBenchBranch 的示例
$ export ILAB_MODELS_DIR=$HOME/.local/share/instructlab/models
$ export ILAB_TAXONOMY_DIR=$HOME/.local/share/instructlab/taxonomy
$ ilab model evaluate --benchmark mt_bench_branch --model $ILAB_MODELS_DIR/granite-7b-lab-Q4_K_M.gguf --judge-model $ILAB_MODELS_DIR/granite-7b-lab-Q4_K_M.gguf --base-model $ILAB_MODELS_DIR/granite-7b-lab-Q4_K_M.gguf --taxonomy-path $ILAB_TAXONOMY_DIR --branch rc --base-branch main
...
# SKILL EVALUATION REPORT
## BASE MODEL
/home/ec2-user/.local/share/instructlab/models/granite-7b-lab-Q4_K_M.gguf
## MODEL
/home/ec2-user/.local/share/instructlab/models/granite-7b-lab-Q4_K_M.gguf
### NO CHANGE:
1. compositional_skills/STEM/math/distance_conversion/qna.yaml
### NEW:
1. compositional_skills/linguistics/organize_lists/qna.yaml
2. compositional_skills/extraction/annual_report/reasoning/qna.yaml
3. compositional_skills/extraction/email/plain_text/qna.yaml
4. compositional_skills/extraction/technical_paper/tables/bullet_points/qna.yaml
5. compositional_skills/extraction/technical_paper/abstract/reasoning/qna.yaml
### ERROR RATE:
0.98
[!NOTE] 目前,MTBenchBranch 必须与本地模型一起使用。不支持直接从 Hugging Face 使用模型而不下载。
🍴 提供服务的新训练模型
-
通过在运行服务器的终端中输入
ctrl+c
键来停止您运行的服务器。重要:
-
🍎 本步骤目前仅适用于搭载 M 系列芯片的 macOS(目前如此)。
-
在提供新训练的模型之前,您必须将其转换为与
ilab
命令行界面兼容。使用ilab model convert
命令将新模型转换为量化后的 GGUF 格式,这是服务器在ilab model serve
命令中托管模型所需的。
-
-
运行以下命令以转换新训练的模型:
ilab model convert
-
通过使用
ilab model serve
命令并使用--model-path
参数指定您的模型,在本地提供新训练的模型。ilab model serve --model-path <new model path>
您应该选择哪个模型来提供?运行
ilab model convert
命令后,将生成一些文件和一个目录。您希望提供的模型以.gguf
扩展名结尾,并存在于以trained
为后缀的目录中。例如:instructlab-merlinite-7b-lab-trained/instructlab-merlinite-7b-lab-Q4_K_M.gguf
。
📣 与新模型进行聊天(这次是必需的)
-
通过运行以下命令使用聊天界面尝试微调后的模型,并查看结果是否优于未训练模型的聊天版本:
ilab model chat -m <New model path>
如果您对优化模型响应质量感兴趣,请参阅
TROUBLESHOOTING.md
🚀 升级InstructLab到最新版本
-
要升级 InstructLab 到最新版本,请使用以下命令:
pip install instructlab --upgrade
🎁 提交您的新知识或技能
当然,最后一步是,如果您改进了模型,请在 taxonomy 存储库 中提交 pull-request,包括您改进数据(例如 qna.yaml
)的文件。
📬 贡献
查看我们的 贡献指南 了解如何贡献。
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。
源分布
构建分布
instructlab-0.19.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cd4cb887050b286e1440045817765a0aa1ec99fbb61f878aebf86eee33d81716 |
|
MD5 | de03e249767b160b4d4087434800bd8d |
|
BLAKE2b-256 | de7155aa0c240f096fad8acb8dc01f959962b38f50a10b5114327a9260f55344 |
instructlab-0.19.1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 04d28e42af02b7535899b225e9c20cb01d8097da8ecfbfa01634adb756819c92 |
|
MD5 | 8240e495b68dd025f67f52a3c595b10c |
|
BLAKE2b-256 | 83d33125cc42f9239891c9267e2ee0cfa5ccce4b809854b8d40219de7b9f68c3 |