-
Notifications
You must be signed in to change notification settings - Fork 2.8k
docs: Add PEFT subsection to reducing memory usage guide #4430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16c4c1a
docs: Expand speeding up training guide with acceleration methods
behroozazarkhalili 3328778
docs: Add PEFT subsection to reducing memory usage guide
behroozazarkhalili 289e8aa
Merge branch 'main' into docs/add-peft-to-reducing-memory
behroozazarkhalili 6087488
Merge branch 'main' into docs/add-peft-to-reducing-memory
behroozazarkhalili dce96c7
Merge branch 'main' into docs/add-peft-to-reducing-memory
sergiopaniego 2eb6571
Merge branch 'main' into docs/add-peft-to-reducing-memory
behroozazarkhalili File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| # Speeding Up Training | ||
|
|
||
| > [!WARNING] | ||
| > Section under construction. Feel free to contribute! | ||
| This guide covers various methods to accelerate training in TRL. Each technique includes minimal examples with links to more comprehensive documentation. | ||
|
|
||
| ## vLLM for fast generation in online methods | ||
|
|
||
|
|
@@ -95,3 +94,80 @@ You can customize the server configuration by passing additional arguments. For | |
|
|
||
| </hfoption> | ||
| </hfoptions> | ||
|
|
||
| ## Flash Attention 2 for faster attention computation | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is out of the scope of the PR, right? |
||
|
|
||
| Flash Attention 2 is an optimized implementation of the attention mechanism that can significantly speed up training while reducing memory usage. It's particularly effective for long sequences. | ||
|
|
||
| To enable Flash Attention 2, pass `attn_implementation="flash_attention_2"` in the model initialization arguments: | ||
|
|
||
| ```python | ||
| from trl import SFTConfig | ||
|
|
||
| training_args = SFTConfig( | ||
| ..., | ||
| model_init_kwargs={"attn_implementation": "flash_attention_2"} | ||
| ) | ||
| ``` | ||
|
|
||
| Flash Attention 2 works across all TRL trainers. For padding-free batching with Flash Attention, see [Reducing Memory Usage](reducing_memory_usage#padding-free). | ||
|
|
||
| ## PEFT for parameter-efficient training | ||
|
|
||
| PEFT (Parameter-Efficient Fine-Tuning) methods like LoRA significantly reduce memory usage and training time by only training a small number of adapter parameters instead of the full model. | ||
|
|
||
| ```python | ||
| from peft import LoraConfig | ||
| from trl import SFTConfig, SFTTrainer | ||
|
|
||
| peft_config = LoraConfig( | ||
| r=16, | ||
| lora_alpha=32, | ||
| lora_dropout=0.05, | ||
| target_modules=["q_proj", "v_proj"], | ||
| ) | ||
|
|
||
| trainer = SFTTrainer( | ||
| model="Qwen/Qwen2.5-0.5B", | ||
| peft_config=peft_config, | ||
| args=training_args, | ||
| ) | ||
| ``` | ||
|
|
||
| For more details, see [PEFT Integration](peft_integration). | ||
|
|
||
| ## Liger Kernel for memory optimization | ||
|
|
||
| Liger Kernel is a collection of Triton kernels designed for LLM training that can increase throughput by 20% and reduce memory usage by 60%. | ||
|
|
||
| ```python | ||
| from trl import DPOConfig | ||
|
|
||
| training_args = DPOConfig(..., use_liger_kernel=True) | ||
| ``` | ||
|
|
||
| Liger Kernel is supported across multiple trainers (SFT, DPO, GRPO, KTO, GKD). For more information, see [Liger Kernel Integration](liger_kernel_integration). | ||
|
|
||
| ## Gradient checkpointing for memory savings | ||
|
|
||
| Gradient checkpointing trades compute for memory by not storing all intermediate activations during the forward pass, recomputing them during the backward pass instead. | ||
|
|
||
| ```python | ||
| from trl import SFTConfig | ||
|
|
||
| training_args = SFTConfig(..., gradient_checkpointing=True) | ||
| ``` | ||
|
|
||
| Gradient checkpointing is available across all TRL trainers. For more memory optimization techniques, see the [Transformers Performance Guide](https://huggingface.co/docs/transformers/perf_train_gpu_one#gradient-checkpointing). | ||
|
|
||
| ## Mixed precision training | ||
|
|
||
| Mixed precision training using bf16 or fp16 can speed up training and reduce memory usage with minimal impact on model quality. | ||
|
|
||
| ```python | ||
| from trl import SFTConfig | ||
|
|
||
| training_args = SFTConfig(..., bf16=True) # or fp16=True for older GPUs | ||
| ``` | ||
|
|
||
| Use `bf16=True` for Ampere GPUs (A100, RTX 30xx) or newer, and `fp16=True` for older GPUs. Mixed precision training is supported across all TRL trainers. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LoraConfig()training_argsis used but never defined. I think you can simply drop it