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

Using Triton-based layer-norm #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
19 changes: 9 additions & 10 deletions src/models/sequence/long_conv_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
ColumnParallelLinear = None

try:
from flash_attn.ops.layer_norm import dropout_add_layer_norm
from flash_attn.ops.triton.layer_norm import layer_norm_fn
except ImportError:
dropout_add_layer_norm = None
layer_norm_fn = None

from src.utils import instantiate
import src.utils.registry as registry
Expand Down Expand Up @@ -301,8 +301,8 @@ def __init__(
# nn.Dropout probabilities are changed.
# This is for performance reason: we can fuse dropout + add + layer_norm.
self.fused_dropout_add_ln = fused_dropout_add_ln
if self.fused_dropout_add_ln and dropout_add_layer_norm is None:
raise ImportError("dropout_add_layer_norm is not installed")
if self.fused_dropout_add_ln and layer_norm_fn is None:
raise ImportError("Triton is not installed")

self.layers = nn.ModuleList(
[
Expand Down Expand Up @@ -384,15 +384,14 @@ def forward(self, input_ids, position_ids=None, inference_params=None):
hidden_states = self.ln_f(residual.to(dtype=self.ln_f.weight.dtype))
else:
# Set prenorm=False here since we don't need the residual
hidden_states = dropout_add_layer_norm(
hidden_states = layer_norm_fn(
hidden_states,
residual,
self.ln_f.weight,
self.ln_f.bias,
self.drop_f.p if self.training else 0.0,
self.ln_f.eps,
residual=residual,
eps=self.ln_f.eps,
dropout_p=self.drop_f.p if self.training else 0.0,
prenorm=False,
residual_in_fp32=self.residual_in_fp32,
)
return hidden_states

Expand Down Expand Up @@ -687,4 +686,4 @@ def shard_qkv_headdim(state_dict, key):
for name in ["kernel.kernel.C", "ssm_k_kernel.kernel.C"]:
if f"backbone.layers.{i}.mixer.{name}" in state_dict:
shard_dim(state_dict, f"backbone.layers.{i}.mixer.{name}", 1)
return state_dict
return state_dict