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

Add SwiGLU for auto Llama #8038

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions paddlenlp/transformers/llama/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
except ImportError:
fused_rotary_position_embedding = None

try:
from paddle.incubate.nn.functional import swiglu
except ImportError:

def swiglu(x, y=None):
if y is None:
x, y = paddle.chunk(x, chunks=2, axis=-1)
return F.silu(x) * y

Check warning on line 42 in paddlenlp/transformers/llama/modeling_auto.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/transformers/llama/modeling_auto.py#L40-L42

Added lines #L40 - L42 were not covered by tests


from paddlenlp.transformers.conversion_utils import (
StateDictNameMapping,
init_name_mappings,
Expand Down Expand Up @@ -228,10 +238,10 @@

def forward(self, x):
if self.fuse_attention_ffn:
gate_out, up_out = paddle.chunk(self.gate_up_fused_proj(x), chunks=2, axis=-1)
out = self.down_proj(F.silu(gate_out) * up_out)
x = swiglu(self.gate_up_fused_proj(x))

Check warning on line 241 in paddlenlp/transformers/llama/modeling_auto.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/transformers/llama/modeling_auto.py#L241

Added line #L241 was not covered by tests
else:
out = self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))
x = swiglu(self.gate_proj(x), self.up_proj(x))
out = self.down_proj(x)

Check warning on line 244 in paddlenlp/transformers/llama/modeling_auto.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/transformers/llama/modeling_auto.py#L243-L244

Added lines #L243 - L244 were not covered by tests
return out


Expand Down
16 changes: 13 additions & 3 deletions paddlenlp/transformers/llama/modeling_auto_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
except ImportError:
fused_rotary_position_embedding = None

try:
from paddle.incubate.nn.functional import swiglu
except ImportError:

def swiglu(x, y=None):
if y is None:
x, y = paddle.chunk(x, chunks=2, axis=-1)
return F.silu(x) * y

Check warning on line 41 in paddlenlp/transformers/llama/modeling_auto_static.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/transformers/llama/modeling_auto_static.py#L39-L41

Added lines #L39 - L41 were not covered by tests


from paddlenlp.transformers.conversion_utils import (
StateDictNameMapping,
init_name_mappings,
Expand Down Expand Up @@ -242,10 +252,10 @@
fleet.auto.shard_tensor(self.down_proj.weight, *get_dist_attr(["mp", None], self.ipp))

if self.fuse_attention_ffn:
gate_out, up_out = paddle.chunk(self.gate_up_fused_proj(x), chunks=2, axis=-1)
out = self.down_proj(F.silu(gate_out) * up_out)
x = swiglu(self.gate_up_fused_proj(x))

Check warning on line 255 in paddlenlp/transformers/llama/modeling_auto_static.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/transformers/llama/modeling_auto_static.py#L255

Added line #L255 was not covered by tests
else:
out = self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))
x = swiglu(self.gate_proj(x), self.up_proj(x))
out = self.down_proj(x)

Check warning on line 258 in paddlenlp/transformers/llama/modeling_auto_static.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/transformers/llama/modeling_auto_static.py#L257-L258

Added lines #L257 - L258 were not covered by tests
return out


Expand Down
Loading