Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable QuickGelu Function for CLIP models #1408

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Changes from 2 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
24 changes: 20 additions & 4 deletions llmfoundry/models/layers/ffn.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@
}


def quickgelu_activation(input: torch.Tensor) -> torch.Tensor:
"""Applies GELU approximation that is fast but somewhat inaccurate.

Args:
input (torch.Tensor): Input tensor of shape(*), where * means any
number of dimensions

Returns:
torch.Tensor: Tensor with same shape as input tensor
"""
return input * torch.sigmoid(1.702 * input)


def resolve_ffn_act_fn(
config: Optional[dict] = None,
) -> Callable[[torch.Tensor], torch.Tensor]:
Expand All @@ -70,10 +83,13 @@ def resolve_ffn_act_fn(
config = _FFN_ACT_FN_DEFAULT
config = deepcopy(config)
name = config.pop('name')
if not hasattr(torch.nn.functional, name):
raise ValueError(f'Unrecognized activation function name ({name}).')
act = getattr(torch.nn.functional, name)
return partial(act, **config)
if name == 'quick_gelu':
return quickgelu_activation
else:
if not hasattr(torch.nn.functional, name):
raise ValueError(f'Unrecognized activation function name ({name}).')
act = getattr(torch.nn.functional, name)
return partial(act, **config)


_DEFAULT_ACT_FN = resolve_ffn_act_fn(_FFN_ACT_FN_DEFAULT)
Expand Down
Loading