Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
626c1b8a
"git@developer.sourcefind.cn:chenpangpang/transformers.git" did not exist on "b7345d22d0b59ccfda8df840a918af33cf95a189"
Unverified
Commit
626c1b8a
authored
Apr 17, 2023
by
fpgaminer
Committed by
GitHub
Apr 17, 2023
Browse files
improve(llama): Faster apply_rotary_pos_emb (#22785)
parent
abbc96a2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
4 deletions
+5
-4
src/transformers/models/llama/modeling_llama.py
src/transformers/models/llama/modeling_llama.py
+5
-4
No files found.
src/transformers/models/llama/modeling_llama.py
View file @
626c1b8a
...
@@ -131,10 +131,11 @@ def rotate_half(x):
...
@@ -131,10 +131,11 @@ def rotate_half(x):
def
apply_rotary_pos_emb
(
q
,
k
,
cos
,
sin
,
position_ids
):
def
apply_rotary_pos_emb
(
q
,
k
,
cos
,
sin
,
position_ids
):
gather_indices
=
position_ids
[:,
None
,
:,
None
]
# [bs, 1, seq_len, 1]
# The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
gather_indices
=
gather_indices
.
repeat
(
1
,
cos
.
shape
[
1
],
1
,
cos
.
shape
[
3
])
cos
=
cos
.
squeeze
((
0
,
1
))
# [seq_len, dim]
cos
=
torch
.
gather
(
cos
.
repeat
(
gather_indices
.
shape
[
0
],
1
,
1
,
1
),
2
,
gather_indices
)
sin
=
sin
.
squeeze
((
0
,
1
))
# [seq_len, dim]
sin
=
torch
.
gather
(
sin
.
repeat
(
gather_indices
.
shape
[
0
],
1
,
1
,
1
),
2
,
gather_indices
)
cos
=
cos
[
position_ids
].
unsqueeze
(
1
)
# [bs, 1, seq_len, dim]
sin
=
sin
[
position_ids
].
unsqueeze
(
1
)
# [bs, 1, seq_len, dim]
q_embed
=
(
q
*
cos
)
+
(
rotate_half
(
q
)
*
sin
)
q_embed
=
(
q
*
cos
)
+
(
rotate_half
(
q
)
*
sin
)
k_embed
=
(
k
*
cos
)
+
(
rotate_half
(
k
)
*
sin
)
k_embed
=
(
k
*
cos
)
+
(
rotate_half
(
k
)
*
sin
)
return
q_embed
,
k_embed
return
q_embed
,
k_embed
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment