Adafactor
The HuggingFace Adafactor optimizer implementation.
This implementation (including the documentation below) is taken from HuggingFace transformers library, which itself is adapted from the original fairseq implementation, which is based on the description in the paper Adafactor: Adaptive Learning Rates with Sublinear Memory Cost.
Note that this implementation differs from the PyTorch torch.optim.Adafactor one, which intentionally deviates from the original paper (the documentation of the PyTorch implementation describes these differences). However, in our experiments, the PyTorch implementation has been consistently outperformed by the original (i.e., this) implementation.
minnt.optimizers.Adafactor
Bases: Optimizer
AdaFactor pytorch implementation can be used as a drop in replacement for Adam original fairseq code: https://github.com/pytorch/fairseq/blob/master/fairseq/optim/adafactor.py
Paper: Adafactor: Adaptive Learning Rates with Sublinear Memory Cost https://huggingface.co/papers/1804.04235
Note that this optimizer internally adjusts the learning rate depending on the scale_parameter, relative_step
and warmup_init options. To use a manual (external) learning rate schedule you should set scale_parameter=False
and relative_step=False.
Parameters:
-
params(Iterable[Parameter]) –Iterable of parameters to optimize or dictionaries defining parameter groups.
-
lr(float | None, default:None) –The external learning rate.
-
eps(tuple[float, float], default:(1e-30, 0.001)) –Regularization constants for square gradient and parameter scale respectively
-
clip_threshold(float, default:1.0) –Threshold of root mean square of final gradient update
-
decay_rate(float, default:-0.8) –Coefficient used to compute running averages of square
-
beta1(float | None, default:None) –Coefficient used for computing running averages of gradient
-
weight_decay(float, default:0.0) –Weight decay (L2 penalty)
-
scale_parameter(bool, default:True) –If True, learning rate is scaled by root mean square
-
relative_step(bool, default:True) –If True, time-dependent learning rate is computed instead of external learning rate
-
warmup_init(bool, default:False) –Time-dependent learning rate computation depends on whether warm-up initialization is being used
This implementation handles low-precision (FP16, bfloat) values, but we have not thoroughly tested.
Recommended T5 finetuning settings (https://discuss.huggingface.co/t/t5-finetuning-tips/684/3):
- Training without LR warmup or clip_threshold is not recommended.
- use scheduled LR warm-up to fixed LR
- use clip_threshold=1.0 (https://huggingface.co/papers/1804.04235)
- Disable relative updates
- Use scale_parameter=False
- Additional optimizer operations like gradient clipping should not be used alongside Adafactor
Example:
Adafactor(model.parameters(), scale_parameter=False, relative_step=False, warmup_init=False, lr=1e-3)
Others reported the following combination to work well:
Adafactor(model.parameters(), scale_parameter=True, relative_step=True, warmup_init=True, lr=None)
When using lr=None with [Trainer] you will most likely need to use [~optimization.AdafactorSchedule]
scheduler as following:
from transformers.optimization import Adafactor, AdafactorSchedule
optimizer = Adafactor(model.parameters(), scale_parameter=True, relative_step=True, warmup_init=True, lr=None)
lr_scheduler = AdafactorSchedule(optimizer)
trainer = Trainer(..., optimizers=(optimizer, lr_scheduler))
Usage:
# replace AdamW with Adafactor
optimizer = Adafactor(
model.parameters(),
lr=1e-3,
eps=(1e-30, 1e-3),
clip_threshold=1.0,
decay_rate=-0.8,
beta1=None,
weight_decay=0.0,
relative_step=False,
scale_parameter=False,
warmup_init=False,
)
Source code in minnt/optimizers/adafactor.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
step
step(closure=None)
Performs a single optimization step
Parameters:
-
closure(callable, default:None) –A closure that reevaluates the model and returns the loss.
Source code in minnt/optimizers/adafactor.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |