RapidFire AI 与 TRL 集成的并发训练实验框架
RapidFire AI 是开源实验执行框架,与 TRL 集成后可在同一批 GPU 上并行比较多个训练配置,用于超参数、LoRA 设置与消融实验,官方称实验吞吐提升 16–24 倍且无需额外硬件。
# RapidFire AI 整合 RapidFire AI是一个开源实验执行框架,它与TRL集成,将"一次训练一个配置"转化为"一次训练一个配置". **实时, side-by-side 对相同 GPU (s) 上的许多配置进行比较** ——因此您可以在超参数,LoRA设置,即时方案,和调剂上进行斜拉 **16 – 没有额外硬件的24 X 更快**. 链接 : [GitHub](https://github.com/RapidFireAI/rapidfireai) · [文档](https://oss-docs.rapidfire.ai) · [在科拉布试试](http://tinyurl.com/rapidfireai-colab) ## 为什么使用RapidFire AI 与TRL? 当使用 TRL 进行微调或培训后,您通常需要: - 尝试不同的超参数配置 - 比较不同的 LoRA 设置 - 测试不同的快速方案 - 进行衰竭研究 | 情景:比较相同GPU(s)上的N培训配置 | 单是TRL | TRL + RapidFire AI | | --- | --- | --- | | 培训战略 | 按顺序运行 N 配置 | 同时运行 N 配置 | | 你什么时候能比较一下配置呢? | 跑完所有任务后 | 生活,从第一个块 | | 停止输家/ 克隆赢家中场训练 | 没有 | 是(互动控制操作) | ### 如何运作 RapidFire AI 受雇于 **基于适应性块的排程**: ``` GPU Timeline (Single GPU): Chunk 1: [Config A] → [Config B] → [Config C] → [Config D] Chunk 2: [Config A] → [Config B] → [Config C] → [Config D] Chunk 3: [Config A] → [Config B] → [Config C] → [Config D] ``` 这使得: - 同一数据子集的配置的早期比较 - 高效的 GPU 利用和尽量减少闲置时间 - 实时和自动化实验度量衡跟踪 - 动态控制飞行中的运行 激励更多的实验 ## 关键特性 - **16 - 24 → 较高的实验吞吐量** 与相继培训相比。 - **几乎没有更改代码** - 简单的滴入配置 API , 仅环绕现有的 TRL 和 PEFT 配置 API. - **交互式控制操作** - 实时控制以停止、恢复和克隆修改(不论是否温暖开始)训练在飞行中运行。 - **与 Fully 硬化数据并行集成( FSDP )** 用于通过硬化参数,梯度,以及跨越多个GPU的优化状态来训练不符合单一GPU的大型模型. - **完全兼容** 与变压器,PEFT,SFTtrainer,DPOTrainer,和GRPOTrainer. - **可插件实验跟踪**: MLflow(默认),TensorBoard,和Trackio,单独或组合启用. - **零设置 Google Colab 支持**:在免费的T4 GPU上为SFT,DPO和GRPO提供一击的教程笔记本. - **生产准备**: 已在生产环境中使用,有完整的工作实例。 ## 安装 ### 先决条件 - Python 3.12 .x (英语). - NVIDIA GPU 具有计算能力 7 .x 或 8.x(FSDP 需要多种GPU) - CUDA 工具包 11.8 + - PyTorch 2.8+ ### pip 安装 ```bash pip install rapidfireai ``` 安装后,通过Hugging Face认证,并初始化RapidFire AI : ```bash # Authenticate with Hugging Face hf auth login --token YOUR_TOKEN # Workaround for current issue: https://github.com/huggingface/xet-core/issues/527 pip uninstall -y hf-xet # Initialize RapidFire AI rapidfireai init # Start the RapidFire AI server rapidfireai start ``` 仪表板将在下列地点提供: `http://localhost:8853` 可以在其中实时监控实验. ## 快速启动: SFT 具有多重配置的训练 以下是一个完整的例子,说明如何同时训练多个 SFT 配置: ```python from rapidfireai import Experiment from rapidfireai.automl import List, RFGridSearch, RFModelConfig, RFLoraConfig, RFSFTConfig from datasets import load_dataset from transformers import AutoModelForCausalLM, AutoTokenizer # Load dataset dataset = load_dataset("bitext/Bitext-customer-support-llm-chatbot-training-dataset") train_dataset = dataset["train"].select(range(128)).shuffle(seed=42) eval_dataset = dataset["train"].select(range(100, 124)).shuffle(seed=42) # Define data formatting function def formatting_function(row): return { "prompt": [ {"role": "system", "content": "You are a helpful customer support assistant."}, {"role": "user", "content": row["instruction"]}, ], "completion": [ {"role": "assistant", "content": row["response"]} ] } # Initialize experiment experiment = Experiment(experiment_name="sft-customer-support") # Define multiple LoRA configurations to compare peft_configs = List([ RFLoraConfig(r=8, lora_alpha=16, lora_dropout=0.1, target_modules=["q_proj", "v_proj"], bias="none"), RFLoraConfig(r=32, lora_alpha=64, lora_dropout=0.1, target_modules=["q_proj", "k_proj", "v_proj", "o_proj"], bias="none") ]) # Define multiple training configurations # 2 base configs × 2 PEFT configs = 4 total training runs config_set = List([ RFModelConfig( model_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0", peft_config=peft_configs, training_args=RFSFTConfig( # Wraps TRL's SFTConfig learning_rate=1e-3, per_device_train_batch_size=4, max_steps=128, fp16=True, ), model_type="causal_lm", model_kwargs={"device_map": "auto", "dtype": "auto", "use_cache": False}, formatting_func=formatting_function, ), RFModelConfig( model_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0", peft_config=peft_configs, training_args=RFSFTConfig( learning_rate=1e-4, # Different learning rate per_device_train_batch_size=4, max_steps=128, fp16=True, ), model_type="causal_lm", model_kwargs={"device_map": "auto", "dtype": "auto", "use_cache": False}, formatting_func=formatting_function, ) ]) # Define model creation function def create_model(model_config): model = AutoModelForCausalLM.from_pretrained( model_config["model_name"], **model_config["model_kwargs"] ) tokenizer = AutoTokenizer.from_pretrained(model_config["model_name"]) return (model, tokenizer) # Create grid search over all configurations config_group = RFGridSearch(configs=config_set, trainer_type="SFT") # Run all 4 configurations concurrently with chunk-based scheduling experiment.run_fit(config_group, create_model, train_dataset, eval_dataset, num_chunks=4, seed=42) # End experiment experiment.end() ``` ### 执行期间发生的事 当运行此示例时 : 1. **配置扩展**: 2 基础配置 × 2 PEFT 配置 = 4 总训练运行 2. **基于 Chunk 的排程**: 培训数据分为块, 所有 4 配置列车同时进行 3. **GPU 盗版**: 模型在基于块边界的 GPU 内存中互换 4. **实时跟踪**: 仪表板上可见的所有度量标准 `http://localhost:8853` 5. **交互式控制**: 停止、恢复或克隆修改仪表板上的任何配置 这才对 **16 - 24 × 较高的吞吐量** 相较于按顺序训练每个配置! ## 支持的 TRL 训练员 ### SFTtrainer 服务器 使用( U) `RFSFTConfig` 即时替换 `SFTConfig`: ```python from rapidfireai.automl import RFSFTConfig training_args = RFSFTConfig( learning_rate=5e-5, per_device_train_batch_size=4, num_train_epochs=3, max_length = 512, # ... all other SFTConfig parameters supported ) ``` **示例笔记本**: [客户支持的SFT](https://github.com/RapidFireAI/rapidfireai/blob/main/tutorial_notebooks/fine-tuning/rf-tutorial-sft-chatqa-lite.ipynb) ### DPOTrainer 软件编辑器 使用( U) `RFDPOConfig` 即时替换 `DPOConfig`: ```python from rapidfireai.automl import RFDPOConfig training_args = RFDPOConfig( beta=0.1, loss_type="sigmoid", max_length=1024, learning_rate=5e-4, # ... all other DPOConfig parameters supported ) ``` **示例笔记本**: [对齐的 DPO](https://github.com/RapidFireAI/rapidfireai/blob/main/tutorial_notebooks/post-training/rf-tutorial-dpo-alignment-lite.ipynb) ### GRPOTrainer (菲律宾语) 使用( U) `RFGRPOConfig` 即时替换 `GRPOConfig`: ```python from rapidfireai.automl import RFGRPOConfig training_args = RFGRPOConfig( learning_rate=5e-6, num_generations=8, max_completion_length=256, # ... all other GRPOConfig parameters supported ) ``` **示例笔记本**: [数学原因的GRPO](https://github.com/RapidFireAI/rapidfireai/blob/main/tutorial_notebooks/post-training/rf-tutorial-grpo-mathreasoning-lite.ipynb) ## 核心概念 ### 以春克为基础的同时期培训 RapidFire AI 将训练数据分为块和副块,在配置之间: ``` GPU Timeline (Single GPU): Chunk 1: [Config A] → [Config B] → [Config C] → [Config D] Chunk 2: [Config A] → [Config B] → [Config C] → [Config D] Chunk 3: [Config A] → [Config B] → [Config C] → [Config D] ... ``` 这种方法可以最大限度地利用GPU,并能够及早比较配置,同时通过自动检查来保持训练稳定性. ### 交互式控制操作( IC Ops) 通过RapidFire AI仪表板,可以动态控制运行实验: - **停下来**: 暂停配置( 自动选中) - **继续**: 从最后一个检查站继续 - **克隆修改**: 复制带有修改的配置( 新运行从零开始) - **用温暖的启动来修改克隆**: 克隆修改和初始化从父母的重量 - **删除**: 删除失败或不必要的运行 这样可以进行适应性实验, 从而可以停止早期性能不佳的配置 和克隆有希望的配置, 使用微调的超参数。 ### 多功能实验 使用( U) `RFGridSearch` 或 时 间 `RFRandomSearch` 自动生成配置组合 : ```python # Grid search: tests all combinations config_group = RFGridSearch(configs=config_list, trainer_type="SFT") # Random search: samples N configurations config_group = RFRandomSearch(configs=config_list, trainer_type="DPO", num_samples=10) ``` ## 高级特性 ### PEFT / LoRA 整合 完全支持参数效率微调: ```python from rapidfireai.automl import RFLoraConfig from peft import TaskType lora_config = RFLoraConfig( task_type=TaskType.CAUSAL_LM, r=64, lora_alpha=64, lora_dropout=0.1, target_modules=["q_proj", "k_proj", "v_proj", "o_proj"], bias="none" ) ``` ### 自定义奖励函数( GRPO ) 为 GRPO 培训定义多重奖励功能 : ```python def correctness_reward(prompts, completions, answer, **kwargs): """Reward for correct answers""" responses = [completion[0]['content'] for completion in completions] extracted = [extract_answer(r) for r in responses] return [2.0 if r == a else 0.0 for r, a in zip(extracted, answer)] def format_reward(completions, **kwargs): """Reward for proper formatting""" import re pattern = r"<reasoning>.*?</reasoning>\s*<answer>.*?</answer>" responses = [completion[0]["content"] for completion in completions] matches = [re.match(pattern, r) for r in responses] return [0.5 if match else 0.0 for match in matches] # Use in model config config = RFModelConfig( reward_funcs=[correctness_reward, format_reward], # ... other parameters ) ``` ### 多类 GPU 支持 RapidFire AI 自动检测并使用所有可用的 GPU. 默认情况下,调度器会在 GPU 之间分配独立配置( 数据- 并行跨配置) , 因此运行不需要特殊设置 。 `N` 配置打开 `N` 和GPU同时使用. 对于不符合一个单一的GPU的模型,RapidFire AI也支持. **Fully Sharded Data 并行( FSDP ) (中文(简体) ).** 将单个配置在多个 GPU 上硬化 – 见下一节 。 ### 多种类型GPU培训FSDP 当一个型号对一个单一的GPU来说太大时,直接通过训练的格子启用FSDP. `RFSFTConfig` 或 时 间 `RFDPOConfig` - 一样 `fsdp` 财务报告和财务报告 `fsdp_config` 由 Hugging Face 曝光的字段 `TrainingArguments`: ```python from rapidfireai.automl import RFModelConfig, RFSFTConfig, RFLoraConfig model_config = RFModelConfig( model_name="meta-llama/Llama-3.1-8B-Instruct", peft_config=RFLoraConfig( r=16, lora_alpha=32, lora_dropout=0.05, target_modules=["q_proj", "k_proj", "v_proj", "o_proj"], bias="none", ), training_args=RFSFTConfig( learning_rate=2e-4, per_device_train_batch_size=1, gradient_accumulation_steps=8, fsdp="full_shard auto_wrap", fsdp_config={ "sharding_strategy": "FULL_SHARD", "auto_wrap_policy": "TRANSFORMER_BASED_WRAP", "backward_prefetch": "backward_pre", "forward_prefetch": True, "use_orig_params": False, "cpu_ram_efficient_loading": True, "offload_params": True, "sync_module_states": True, "limit_all_gathers": True, }, ), model_type="causal_lm", model_kwargs={"dtype": "auto"}, ) ``` 要点: - FSDP透明地与RapidFire AI基于块的调度,IC Ops(停止/恢复/克隆-修改是否温暖启动),以及所有支持的度量追踪后端. - FSDP与PEFT / LoRA完全兼容——LoRA适配器重量在保存检查站时被高效地跨越硬体收集. - FSDP编曲有网格搜索和随机搜索:每个扩展的配置都得到自己的硬化训练运行. **示例笔记本**: - [SFT与 FSDP(升,小型号)](https://github.com/RapidFireAI/rapidfireai/blob/main/tutorial_notebooks/fine-tuning/rf-tutorial-sft-chatqa-fsdp-lite.ipynb) - [SFT与FSDP(大型号)](https://github.com/RapidFireAI/rapidfireai/blob/main/tutorial_notebooks/fine-tuning/rf-tutorial-sft-chatqa-fsdp-large.ipynb) - [使用 DPO 的 FSDP](https://github.com/RapidFireAI/rapidfireai/blob/main/tutorial_notebooks/post-training/rf-tutorial-dpo-alignment-fsdp-lite.ipynb) ### 实验跟踪后端 RapidFire AI支持三个可以单独或一起使用的计量日志后端: **ML 流量** (本地安装的默认值), **TensorBoard** (Google Colab的默认),和 **轨迹**. 在服务器启动时选择一个或多个后端 `--tracking-backends` 旗帜 : ```bash # MLflow only (default on local installs) rapidfireai start --tracking-backends mlflow # TensorBoard only rapidfireai start --tracking-backends tensorboard # Any combination rapidfireai start --tracking-backends mlflow tensorboard trackio ``` 也有等效的环境变量: - `RF_MLFLOW_ENABLED` (默认) `true`,或 `false` (科拉布语) - `RF_TENSORBOARD_ENABLED` (默认) `false`,或 `true` (科拉布语) - `RF_TRACKIO_ENABLED` (默认) `false`) 所有三个后端都得到相同的度量衡(损失,评价分数,学习率等),并尊重IC Ops运行的生命周期事件,因此可以使用Trackio来进行轻量级共享,同时使用MLflow作为完整的本地仪表板. ### 在 Google Colab 中运行 RapidFire AI运行于免费的Google Colab T4 GPU上,带有用于SFT,DPO,GRPO,以及RAG/上下文工程工作流程的教程笔记本. Colab中,TensorBoard是默认的跟踪后端(MLflow为简便而禁用),通常的. `rapidfireai init` / `rapidfireai start` 命令直接从笔记本单元格中运行——不需要终端访问。 开始 : [Google Colab的RapidFire AI 请检查date=中的日期值 (帮助) 互联网档案馆的存檔,存档日期2013-09-02.](http://tinyurl.com/rapidfireai-colab). ## 最佳做法 ### tuning Chunk 颗粒度 那个... `num_chunks` 参数控制互换频率 : ```python # Fewer chunks = less overhead, less frequent comparison experiment.run_fit(..., num_chunks=2) # More chunks = more overhead, more frequent comparison experiment.run_fit(..., num_chunks=16) ``` **拇指规则**: 开始于 `num_chunks=4` ,并根据数据集大小和配置数量进行调整。 ### 内存管理 对于大型型号,使用量化: ```python from transformers import BitsAndBytesConfig import torch bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", ) model_kwargs = { "quantization_config": bnb_config, "device_map": "auto", } ``` ## 业绩基准 根据内部基准,比较了相继的RapidFire AI 并行培训: | 假设 | 顺序时间 | RapidFire AI 时间 | 加速 | |----------|----------------|-------------------|---------| | 4 配置 1 GPU | 120 分钟 | 7.5 分钟 | 16× | | 8 配置 1 GPU | 240 分钟 | 12 分钟 | 20× | | 4 配置, 2 GPU | 60 分钟 | 4 分钟 | 15× | | 8 配置, 4 GPU | 60 分钟 | 3 分钟 | 20× | *在NVIDIA A100 40GB上与TinyLlama-1.1B和Llama-3 2.-1B模型执行的基准* ## 解决问题 关于解决问题的指南,请参看 [RapidFire AI 解决问题指南](https://oss-docs.rapidfire.ai/en/latest/troubleshooting.html). ## 追加资源 - **Colab 笔记本**: [Google Colab的RapidFire AI 请检查date=中的日期值 (帮助) 互联网档案馆的存檔,存档日期2013-09-02.](http://tinyurl.com/rapidfireai-colab) - **文档**: [(原始内容存档于2018-03-27). oss-docs.rapidfire.ai.](https://oss-docs.rapidfire.ai) - **GitHub**: [RapidFireAI/rapidfireai](https://github.com/RapidFireAI/rapidfireai) - **PyPI**: [pypi.org/project/rapidfirei (英语).](https://pypi.org/project/rapidfireai/) - **调色板**: [加入我们的Discord](https://discord.gg/6vSTtncKNN) - **教学笔记簿**: [GitHub 仓库](https://github.com/RapidFireAI/rapidfireai/tree/main/tutorial_notebooks) 更多关于RapidFire AI在他们的 [官方仓库](https://github.com/RapidFireAI/rapidfireai) 财务报告和财务报告 [文档](https://oss-docs.rapidfire.ai).
查看原始来源