Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
bd6028d6
Unverified
Commit
bd6028d6
authored
Apr 12, 2025
by
Michael Goin
Committed by
GitHub
Apr 12, 2025
Browse files
Optimized topk for topk=1 (Llama-4) (#16512)
Signed-off-by:
mgoin
<
mgoin64@gmail.com
>
parent
802329de
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
2 deletions
+11
-2
vllm/model_executor/models/llama4.py
vllm/model_executor/models/llama4.py
+2
-2
vllm/model_executor/models/utils.py
vllm/model_executor/models/utils.py
+9
-0
No files found.
vllm/model_executor/models/llama4.py
View file @
bd6028d6
...
@@ -37,7 +37,7 @@ from vllm.model_executor.layers.rotary_embedding import get_rope
...
@@ -37,7 +37,7 @@ from vllm.model_executor.layers.rotary_embedding import get_rope
from
vllm.model_executor.model_loader.weight_utils
import
default_weight_loader
from
vllm.model_executor.model_loader.weight_utils
import
default_weight_loader
from
.llama
import
LlamaForCausalLM
,
LlamaMLP
,
LlamaModel
from
.llama
import
LlamaForCausalLM
,
LlamaMLP
,
LlamaModel
from
.utils
import
(
AutoWeightsLoader
,
extract_layer_index
,
from
.utils
import
(
AutoWeightsLoader
,
extract_layer_index
,
fast_topk
,
is_pp_missing_parameter
)
is_pp_missing_parameter
)
...
@@ -50,7 +50,7 @@ class Llama4MoE(nn.Module):
...
@@ -50,7 +50,7 @@ class Llama4MoE(nn.Module):
topk
:
int
,
topk
:
int
,
renormalize
:
bool
,
renormalize
:
bool
,
)
->
Tuple
[
torch
.
Tensor
,
torch
.
Tensor
]:
)
->
Tuple
[
torch
.
Tensor
,
torch
.
Tensor
]:
router_scores
,
router_indices
=
torch
.
topk
(
gating_output
,
topk
,
dim
=-
1
)
router_scores
,
router_indices
=
fast_
topk
(
gating_output
,
topk
,
dim
=-
1
)
router_scores
=
torch
.
sigmoid
(
router_scores
.
float
()).
to
(
router_scores
=
torch
.
sigmoid
(
router_scores
.
float
()).
to
(
hidden_states
.
dtype
)
hidden_states
.
dtype
)
return
(
router_scores
,
router_indices
.
to
(
torch
.
int32
))
return
(
router_scores
,
router_indices
.
to
(
torch
.
int32
))
...
...
vllm/model_executor/models/utils.py
View file @
bd6028d6
...
@@ -703,3 +703,12 @@ def cast_overflow_tensors(
...
@@ -703,3 +703,12 @@ def cast_overflow_tensors(
clamp_value
=
torch
.
finfo
(
tensors
.
dtype
).
max
-
offset
clamp_value
=
torch
.
finfo
(
tensors
.
dtype
).
max
-
offset
tensors
=
torch
.
clamp
(
tensors
,
min
=-
clamp_value
,
max
=
clamp_value
)
tensors
=
torch
.
clamp
(
tensors
,
min
=-
clamp_value
,
max
=
clamp_value
)
return
tensors
return
tensors
def
fast_topk
(
values
,
topk
,
dim
):
if
topk
==
1
:
# Use max along the specified dimension to get both value and index
return
torch
.
max
(
values
,
dim
=
dim
,
keepdim
=
True
)
else
:
# Use topk for efficiency with larger k values
return
torch
.
topk
(
values
,
topk
,
dim
=
dim
)
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