-
-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Jaerin Lee, Bong Gyun Kang, Kihoon Kim, Kyoung Mu Lee | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Grokfast Optimizer | ||
|
||
See https://github.com/ironjr/grokfast | ||
|
||
### Usage | ||
|
||
```yaml | ||
plugins: | ||
- axolotl.integrations.grokfast.GrokfastPlugin | ||
|
||
grokfast_alpha: 2.0 | ||
grokfast_lamb: 0.98 | ||
``` |
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Grokfast plugin for Axolotl | ||
""" | ||
import logging | ||
|
||
from transformers.trainer_callback import TrainerCallback | ||
|
||
from ..base import BasePlugin | ||
from .args import GrokfastArgs # pylint: disable=unused-import. # noqa: F401 | ||
from .optimizer import gradfilter_ema | ||
|
||
LOG = logging.getLogger("axolotl.integrations.grokfast") | ||
|
||
|
||
class GrokfastCallbackHandler(TrainerCallback): | ||
""" | ||
Transformer trainer callbacks for Grokfast | ||
""" | ||
|
||
def __init__(self, *args_, alpha=0.98, lamb=2.0, **kwargs): | ||
super().__init__(*args_, **kwargs) | ||
self.grads = None | ||
self.alpha = alpha | ||
self.lamb = lamb | ||
|
||
def on_train_begin(self, *args_, **kwargs): # pylint: disable=unused-argument | ||
self.grads = None | ||
|
||
def on_pre_optimizer_step( | ||
self, args_, state, control, **kwargs | ||
): # pylint: disable=unused-argument | ||
model = kwargs.pop("model") | ||
self.grads = gradfilter_ema(model, self.grads, alpha=self.alpha, lamb=self.lamb) | ||
return control | ||
|
||
|
||
class GrokfastPlugin(BasePlugin): | ||
""" | ||
Plugin for Grokfast optimizer integraton with Axolotl. | ||
""" | ||
|
||
def get_input_args(self): | ||
return "axolotl.integrations.grokfast.GrokfastArgs" | ||
|
||
def add_callbacks_post_trainer(self, cfg, trainer): | ||
LOG.info("Adding Grokfast callback to the trainer") | ||
callback = GrokfastCallbackHandler( | ||
alpha=cfg.grokfast_alpha, lamb=cfg.grokfast_lamb | ||
) | ||
return [callback] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
config args for grokfast plugin | ||
""" | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class GrokfastArgs(BaseModel): | ||
""" | ||
Input args for Grokfast optimizer. | ||
""" | ||
|
||
grokfast_alpha: Optional[float] = 0.98 | ||
grokfast_lamb: Optional[float] = 2.0 |
This file contains 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright: MIT License (c) 2024 Jaerin Lee, Bong Gyun Kang, Kihoon Kim, Kyoung Mu Lee | ||
# Reference: https://github.com/ironjr/grokfast | ||
|
||
# pylint: skip-file | ||
from collections import deque | ||
from typing import Dict, Literal, Optional | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
def gradfilter_ma( | ||
m: nn.Module, | ||
grads: Optional[Dict[str, deque]] = None, | ||
window_size: int = 100, | ||
lamb: float = 5.0, | ||
filter_type: Literal["mean", "sum"] = "mean", | ||
warmup: bool = True, | ||
trigger: bool = False, # For ablation study. | ||
) -> Dict[str, deque]: | ||
if grads is None: | ||
grads = { | ||
n: deque(maxlen=window_size) | ||
for n, p in m.named_parameters() | ||
if p.requires_grad and p.grad is not None | ||
} | ||
|
||
for n, p in m.named_parameters(): | ||
if p.requires_grad and p.grad is not None: | ||
grads[n].append(p.grad.data.detach()) # .cpu()) | ||
|
||
# Modify the gradients. | ||
if not warmup or len(grads[n]) == window_size and not trigger: | ||
if filter_type == "mean": | ||
avg = sum(grads[n]) / len(grads[n]) | ||
elif filter_type == "sum": | ||
avg = sum(grads[n]) | ||
else: | ||
raise ValueError(f"Unrecognized filter_type {filter_type}") | ||
p.grad.data = p.grad.data + avg * lamb | ||
|
||
return grads | ||
|
||
|
||
def gradfilter_ema( | ||
m: nn.Module, | ||
grads: Optional[Dict[str, torch.Tensor]] = None, | ||
alpha: float = 0.98, | ||
lamb: float = 2.0, | ||
) -> Dict[str, torch.Tensor]: | ||
if grads is None: | ||
grads = { | ||
n: p.grad.data.detach() | ||
for n, p in m.named_parameters() | ||
if p.requires_grad and p.grad is not None | ||
} | ||
|
||
for n, p in m.named_parameters(): | ||
if p.requires_grad and p.grad is not None: | ||
grads[n] = grads[n] * alpha + p.grad.data.detach() * (1 - alpha) | ||
p.grad.data = p.grad.data + grads[n] * lamb | ||
|
||
return grads |
This file contains 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