Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions megatron/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ def _add_network_size_args(parser):
default=PositionEmbeddingType.absolute,
help='Define position embedding type ("absolute" | "rotary"). "absolute" by default.'
)
group.add_argument('--glu-activation', type=str,
choices=["liglu", "geglu", "reglu", "swiglu"],
Comment thread
jaketae marked this conversation as resolved.
Outdated
help='GLU activations to use.'
)

return parser

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class _GLUBaseModule(nn.Module):
def __init__(self, activation_fn):
super().__init__()
self.activation_fn = activation_fn

def forward(self, x):
# dim=-1 breaks in jit for pt<1.10
x1, x2 = x.chunk(2, dim=(x.ndim-1))
x1, x2 = x.chunk(2, dim=(x.ndim - 1))
return x1 * self.activation_fn(x2)


Expand Down Expand Up @@ -38,3 +38,11 @@ def __init__(self):
geglu = torch.jit.script(GEGLU())
reglu = torch.jit.script(ReGLU())
swiglu = torch.jit.script(SwiGLU())


GLU_ACTIVATIONS = {
"gegelu": geglu,
"liglu": liglu,
"reglu": reglu,
"swiglu": swiglu,
}
5 changes: 4 additions & 1 deletion megatron/model/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import deepspeed

from .glu_activations import GLU_ACTIVATIONS
from .positional_embeddings import RotaryEmbedding, apply_rotary_pos_emb_torch, apply_rotary_pos_emb

# flags required to enable jit fusion kernels
Expand Down Expand Up @@ -76,7 +77,9 @@ def __init__(self, init_method, output_layer_init_method):

self.bias_gelu_fusion = args.bias_gelu_fusion
self.activation_func = F.gelu
if args.openai_gelu:
if args.glu_activation:
self.activation_func = GLU_ACTIVATIONS[args.glu_activation]
elif args.openai_gelu:
self.activation_func = openai_gelu
elif args.onnx_safe:
self.activation_func = erf_gelu
Expand Down
2 changes: 1 addition & 1 deletion tests/test_activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch
from torch.nn import functional as F

from megatron.model.activations import liglu, geglu, reglu, swiglu
from megatron.model.glu_activations import liglu, geglu, reglu, swiglu
from megatron.testing_utils import set_seed, torch_assert_equal


Expand Down