-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev' into doremi
- Loading branch information
Showing
11 changed files
with
570 additions
and
21 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ model: | |
type: llama | ||
hidden_dim: 32 | ||
num_heads: 4 | ||
num_kv_heads: 4 | ||
num_layers: 2 | ||
trainer: | ||
tracker: | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,67 @@ | ||
import equinox as eqx | ||
import jax | ||
import jax.random as jrandom | ||
|
||
import haliax as hax | ||
|
||
from levanter.models.gpt2 import Gpt2Config | ||
from levanter.optim import AdamConfig | ||
|
||
|
||
def test_weight_decay_masking(): | ||
def tree_at_mask(params): | ||
# let's mask all leaves as False | ||
params = jax.tree_util.tree_map(lambda _: False, params) | ||
|
||
def apply_weight_decay(tree): | ||
# there is no weight decay performed in LayerNorms and bias | ||
nodes = [] | ||
|
||
# apply on embedding | ||
nodes.append(tree.embeddings.token_embeddings.array) | ||
nodes.append(tree.embeddings.position_embeddings.array) | ||
|
||
# apply on attention | ||
nodes.append(tree.transformer.blocks.stacked.attn.c_attn.weight.array) | ||
nodes.append(tree.transformer.blocks.stacked.attn.c_proj.weight.array) | ||
|
||
# apply on MLP | ||
nodes.append(tree.transformer.blocks.stacked.mlp.c_fc.weight.array) | ||
nodes.append(tree.transformer.blocks.stacked.mlp.c_proj.weight.array) | ||
|
||
return nodes | ||
|
||
# apply weight decay when necessary | ||
params = eqx.tree_at( | ||
where=apply_weight_decay, | ||
pytree=params, | ||
replace_fn=lambda _: True, | ||
) | ||
|
||
return params | ||
|
||
gpt_config = Gpt2Config() | ||
Vocab = hax.Axis("vocab", 100) | ||
model = gpt_config.build(Vocab, key=jrandom.PRNGKey(0)) | ||
string_list_config = AdamConfig( | ||
weight_decay_modules=[ | ||
"attn.c_attn.weight", | ||
"attn.c_proj.weight", | ||
"mlp.c_fc.weight", | ||
"mlp.c_proj.weight", | ||
"token_embeddings", | ||
"position_embeddings", | ||
] | ||
) | ||
regex_config = AdamConfig( | ||
weight_decay_modules=r".*attn.*weight|.*mlp.*weight|.*token_embeddings|.*position_embeddings", | ||
) | ||
# masking using `equinox.tree_at` | ||
true_mask = tree_at_mask(model) | ||
# masking using list of module path | ||
list_string_mask = string_list_config.build_weight_decay_mask()(model) | ||
|
||
regex_mask = regex_config.build_weight_decay_mask()(model) | ||
|
||
assert eqx.tree_equal(list_string_mask, true_mask) | ||
assert eqx.tree_equal(regex_mask, true_mask) |