
* toctree * not-doctested.txt * collapse sections * feedback * update * rewrite get started sections * fixes * fix * loading models * fix * customize models * share * fix link * contribute part 1 * contribute pt 2 * fix toctree * tokenization pt 1 * Add new model (#32615) * v1 - working version * fix * fix * fix * fix * rename to correct name * fix title * fixup * rename files * fix * add copied from on tests * rename to `FalconMamba` everywhere and fix bugs * fix quantization + accelerate * fix copies * add `torch.compile` support * fix tests * fix tests and add slow tests * copies on config * merge the latest changes * fix tests * add few lines about instruct * Apply suggestions from code review Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> * fix * fix tests --------- Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> * "to be not" -> "not to be" (#32636) * "to be not" -> "not to be" * Update sam.md * Update trainer.py * Update modeling_utils.py * Update test_modeling_utils.py * Update test_modeling_utils.py * fix hfoption tag * tokenization pt. 2 * image processor * fix toctree * backbones * feature extractor * fix file name * processor * update not-doctested * update * make style * fix toctree * revision * make fixup * fix toctree * fix * make style * fix hfoption tag * pipeline * pipeline gradio * pipeline web server * add pipeline * fix toctree * not-doctested * prompting * llm optims * fix toctree * fixes * cache * text generation * fix * chat pipeline * chat stuff * xla * torch.compile * cpu inference * toctree * gpu inference * agents and tools * gguf/tiktoken * finetune * toctree * trainer * trainer pt 2 * optims * optimizers * accelerate * parallelism * fsdp * update * distributed cpu * hardware training * gpu training * gpu training 2 * peft * distrib debug * deepspeed 1 * deepspeed 2 * chat toctree * quant pt 1 * quant pt 2 * fix toctree * fix * fix * quant pt 3 * quant pt 4 * serialization * torchscript * scripts * tpu * review * model addition timeline * modular * more reviews * reviews * fix toctree * reviews reviews * continue reviews * more reviews * modular transformers * more review * zamba2 * fix * all frameworks * pytorch * supported model frameworks * flashattention * rm check_table * not-doctested.txt * rm check_support_list.py * feedback * updates/feedback * review * feedback * fix * update * feedback * updates * update --------- Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com> Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
6.5 KiB
Optimizers
Transformers offers two native optimizers, AdamW and AdaFactor. It also provides integrations for more specialized optimizers. Install the library that offers the optimizer and drop it in the optim
parameter in [TrainingArguments
].
This guide will show you how to use these optimizers with [Trainer
] using [TrainingArguments
] shown below.
import torch
from transformers import TrainingArguments, AutoTokenizer, AutoModelForCausalLM, Trainer
args = TrainingArguments(
output_dir="./test-optimizer",
max_steps=1000,
per_device_train_batch_size=4,
logging_strategy="steps",
logging_steps=1,
learning_rate=2e-5,
save_strategy="no",
run_name="optimizer-name",
)
APOLLO
pip install apollo-torch
Approximated Gradient Scaling for Memory Efficient LLM Optimization (APOLLO) is a memory-efficient optimizer that allows full parameter learning for both pretraining and fine-tuning. It maintains AdamW-level performance with SGD-like memory efficiency. For extreme memory efficiency, you can use APOLLO-Mini, a rank 1 variant of APOLLO. APOLLO optimizers support:
- Ultra-low rank efficiency. You can use a much lower rank than GaLoRE, rank 1 is sufficient.
- Avoid expensive SVD computations. APOLLO leverages random projections to avoid training stalls.
Use the optim_target_modules
parameter to specify which layers to train.
import torch
from transformers import TrainingArguments
args = TrainingArguments(
output_dir="./test-apollo",
max_steps=100,
per_device_train_batch_size=2,
+ optim="apollo_adamw",
+ optim_target_modules=[r".*.attn.*", r".*.mlp.*"],
logging_strategy="steps",
logging_steps=1,
learning_rate=2e-5,
save_strategy="no",
run_name="apollo_adamw",
)
For additional training options, use optim_args
to define hyperparameters like rank
, scale
, and more. Refer to the table below for a complete list of available hyperparameters.
Tip
The
scale
parameter can be set ton/r
, wheren
is the original space dimension andr
is the low-rank space dimension. You could achieve a similar effect by adjusting the learning rate while keepingscale
at its default value.
parameter | description | APOLLO | APOLLO-Mini |
---|---|---|---|
rank | rank of the auxiliary sub-space for gradient scaling | 256 | 1 |
scale_type | how scaling factors are applied | channel (per-channel scaling) |
tensor (per-tensor scaling) |
scale | adjusts gradient updates to stabilize training | 1.0 | 128 |
update_proj_gap | steps before updating projection matrices | 200 | 200 |
proj | projection type | random |
random |
The example below enables the APOLLO-Mini optimizer.
from transformers import TrainingArguments
args = TrainingArguments(
output_dir="./test-apollo_mini",
max_steps=100,
per_device_train_batch_size=2,
optim="apollo_adamw",
optim_target_modules=[r".*.attn.*", r".*.mlp.*"],
optim_args="proj=random,rank=1,scale=128.0,scale_type=tensor,update_proj_gap=200",
)
GrokAdamW
pip install grokadamw
GrokAdamW is an optimizer designed to help models that benefit from grokking, a term used to describe delayed generalization because of slow-varying gradients. It is particularly useful for models requiring more advanced optimization techniques to achieve better performance and stability.
import torch
from transformers import TrainingArguments
args = TrainingArguments(
output_dir="./test-grokadamw",
max_steps=1000,
per_device_train_batch_size=4,
+ optim="grokadamw",
logging_strategy="steps",
logging_steps=1,
learning_rate=2e-5,
save_strategy="no",
run_name="grokadamw",
)
LOMO
pip install lomo-optim
Low-Memory Optimization (LOMO) is a family of optimizers, LOMO and AdaLomo, designed for low-memory full-parameter finetuning of LLMs. Both LOMO optimizers fuse the gradient computation and parameter update in one step to reduce memory usage. AdaLomo builds on top of LOMO by incorporating an adaptive learning rate for each parameter like the Adam optimizer.
Tip
It is recommended to use AdaLomo without
grad_norm
for better performance and higher throughput.
args = TrainingArguments(
output_dir="./test-lomo",
max_steps=1000,
per_device_train_batch_size=4,
+ optim="adalomo",
gradient_checkpointing=True,
gradient_checkpointing=True,
logging_strategy="steps",
logging_steps=1,
learning_rate=2e-6,
save_strategy="no",
run_name="adalomo",
)
Schedule Free
pip install schedulefree
Schedule Free optimizer (SFO) replaces the base optimizers momentum with a combination of averaging and interpolation. Unlike a traditional scheduler, SFO completely removes the need to anneal the learning rate.
SFO supports the RAdam (schedule_free_radam
), AdamW (schedule_free_adamw
) and SGD (schedule_free_sgd
) optimizers. The RAdam scheduler doesn't require warmup_steps
or warmup_ratio
.
By default, it is recommended to set lr_scheduler_type="constant"
. Other lr_scheduler_type
values may also work, but combining SFO optimizers with other learning rate schedules could affect SFOs intended behavior and performance.
args = TrainingArguments(
output_dir="./test-schedulefree",
max_steps=1000,
per_device_train_batch_size=4,
+ optim="schedule_free_radamw,
+ lr_scheduler_type="constant",
gradient_checkpointing=True,
logging_strategy="steps",
logging_steps=1,
learning_rate=2e-6,
save_strategy="no",
run_name="sfo",
)