-
Notifications
You must be signed in to change notification settings - Fork 86
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
9 changed files
with
146 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
data: !include data/openwebtext_source.yaml | ||
model: | ||
type: gpt2 | ||
hidden_dim: 768 | ||
num_heads: 12 | ||
num_layers: 12 | ||
seq_len: 1024 | ||
gradient_checkpointing: true | ||
scale_attn_by_inverse_layer_idx: true | ||
trainer: | ||
tracker: | ||
- type: wandb | ||
project: "levanter" | ||
tags: [ "openwebtext", "gpt2", "itest"] | ||
|
||
mp: p=f32,c=bfloat16 | ||
model_averaging: | ||
type: ema | ||
beta: 0.995 | ||
|
||
model_axis_size: 1 | ||
per_device_parallelism: -1 | ||
|
||
train_batch_size: 256 | ||
num_train_steps: 20000 | ||
|
||
# tensor_parallel_axes: ["position", "key_position"] | ||
# tensor_parallel_axes: ["heads", "mlp"] | ||
optimizer: | ||
learning_rate: 1E-3 | ||
weight_decay: 0.1 | ||
warmup: 0.01 | ||
decay: 200 # no decay b/c EMA | ||
lr_schedule: inv |
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
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,59 @@ | ||
import abc | ||
import dataclasses | ||
from typing import Generic, TypeVar | ||
|
||
import draccus | ||
import equinox as eqx | ||
import optax | ||
|
||
|
||
S = TypeVar("S") | ||
M = TypeVar("M") | ||
|
||
|
||
class ModelAveraging(eqx.Module, Generic[M]): | ||
""" | ||
This is the interface for model averaging algorithms. Model averaging algorithms are used to average | ||
the parameters of a model over multiple training steps. This is useful for improving generalization | ||
""" | ||
|
||
@abc.abstractmethod | ||
def update(self: S, model: M, step: int) -> S: | ||
pass | ||
|
||
@property | ||
@abc.abstractmethod | ||
def model_params(self) -> M: | ||
pass | ||
|
||
|
||
class EmaModelAveraging(ModelAveraging[M]): | ||
""" | ||
Exponential moving average model averaging | ||
""" | ||
|
||
model: M | ||
beta: float = eqx.static_field() | ||
|
||
def update(self: S, new_model: M, step: int) -> S: | ||
del step | ||
return dataclasses.replace(self, model=optax.incremental_update(new_model, self.model, self.beta)) # type: ignore | ||
|
||
@property | ||
def model_params(self) -> M: | ||
return self.model | ||
|
||
|
||
class ModelAveragingConfig(abc.ABC, draccus.ChoiceRegistry, Generic[M]): | ||
@abc.abstractmethod | ||
def create(self, model: M) -> ModelAveraging[M]: | ||
pass | ||
|
||
|
||
@ModelAveragingConfig.register_subclass("ema") | ||
@dataclasses.dataclass | ||
class EmaModelAveragingConfig(ModelAveragingConfig[M]): | ||
beta: float = 0.999 | ||
|
||
def create(self, model: M) -> EmaModelAveraging[M]: | ||
return EmaModelAveraging(model=model, beta=self.beta) |
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