-
Notifications
You must be signed in to change notification settings - Fork 429
[main] mlp weight prefetch in Qwen Dense Models #2762
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
Closed
+426
−13
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import torch | ||
import torch.nn.functional as F | ||
import torch_npu | ||
from vllm.utils import direct_register_custom_op | ||
from vllm.distributed import (tensor_model_parallel_all_gather, | ||
tensor_model_parallel_reduce_scatter, | ||
tensor_model_parallel_all_reduce, | ||
get_tensor_model_parallel_rank, | ||
get_tensor_model_parallel_world_size) | ||
from vllm.forward_context import get_forward_context | ||
import vllm_ascend.envs as envs_ascend | ||
|
||
|
||
def _maybe_chunk_residual_impl(x: torch.Tensor, residual: torch.Tensor) -> torch.Tensor: | ||
if x.size(0) != residual.size(0): | ||
flashcomm_v1_enabled = get_forward_context().flashcomm_v1_enabled | ||
assert flashcomm_v1_enabled is True, ( | ||
"Currently, this situation only occurs " | ||
"when flashcomm_v1 is enabled" | ||
) | ||
pad_size = get_forward_context().pad_size | ||
if pad_size > 0: | ||
residual = F.pad(residual, (0, 0, 0, pad_size)) | ||
tp_size = get_tensor_model_parallel_world_size() | ||
tp_rank = get_tensor_model_parallel_rank() | ||
residual = torch.chunk(residual, tp_size, dim=0)[tp_rank] | ||
|
||
return residual | ||
|
||
|
||
def _maybe_all_gather_and_maybe_unpad_impl(x: torch.Tensor, label: bool) -> torch.Tensor: | ||
flashcomm_v1_enabled = get_forward_context().flashcomm_v1_enabled | ||
if flashcomm_v1_enabled and label: | ||
x = tensor_model_parallel_all_gather(x, 0) | ||
pad_size = get_forward_context().pad_size | ||
if pad_size > 0: | ||
x = x[:-pad_size, :] | ||
return x | ||
|
||
|
||
def _maybe_pad_and_reduce_impl(x: torch.Tensor) -> torch.Tensor: | ||
flashcomm_v1_enabled = get_forward_context().flashcomm_v1_enabled | ||
if flashcomm_v1_enabled: | ||
pad_size = get_forward_context().pad_size | ||
if pad_size > 0: | ||
x = F.pad(x, (0, 0, 0, pad_size)) | ||
return tensor_model_parallel_reduce_scatter(x, 0) | ||
else: | ||
return tensor_model_parallel_all_reduce(x) | ||
|
||
|
||
def _maybe_prefetch_mlp_gate_up_proj_impl(x_dependency: torch.Tensor, prefix: str) -> None: | ||
forward_context = get_forward_context() | ||
if not forward_context.prefetch_mlp_enabled: | ||
return | ||
prefetch_model = forward_context.prefetch_model | ||
prefetch_stream = forward_context.prefetch_stream | ||
layer_idx = int(prefix.split('.')[2]) | ||
|
||
# start point of gate_up_proj weight prefetch | ||
if prefix.split('.')[-2] == "self_attn": | ||
forward_context.prefetch_mlp_gate_up_proj = True | ||
if forward_context.prefetch_mlp_gate_up_proj: | ||
prefetch_stream.wait_stream(torch.npu.current_stream()) | ||
|
||
with torch.npu.stream(prefetch_stream): | ||
MLP_GATE_UP_PREFETCH_SIZE = envs_ascend.MLP_GATE_UP_PREFETCH_SIZE | ||
torch_npu.npu_prefetch(prefetch_model.model.layers[layer_idx].mlp.gate_up_proj.weight, \ | ||
x_dependency, MLP_GATE_UP_PREFETCH_SIZE) | ||
return | ||
|
||
|
||
def _maybe_prefetch_mlp_gate_up_proj_impl_fake(x_dependency: torch.Tensor, prefix: str) -> None: | ||
return | ||
|
||
|
||
def _maybe_prefetch_mlp_down_proj_impl(x_dependency: torch.Tensor) -> None: | ||
forward_context = get_forward_context() | ||
if not forward_context.prefetch_mlp_enabled: | ||
return | ||
forward_context.prefetch_mlp_down_proj = True | ||
prefetch_model = forward_context.prefetch_model | ||
prefetch_stream = forward_context.prefetch_stream | ||
layer_idx = forward_context.layer_idx | ||
|
||
# start point of down_proj weight prefetch | ||
prefetch_stream.wait_stream(torch.npu.current_stream()) | ||
|
||
with torch.npu.stream(prefetch_stream): | ||
MLP_DOWN_PREFETCH_SIZE = envs_ascend.MLP_DOWN_PREFETCH_SIZE | ||
torch_npu.npu_prefetch(prefetch_model.model.layers[layer_idx].mlp.down_proj.weight, \ | ||
x_dependency, MLP_DOWN_PREFETCH_SIZE) | ||
forward_context.layer_idx += 1 | ||
return | ||
|
||
|
||
def _maybe_prefetch_mlp_down_proj_impl_fake(x_dependency: torch.Tensor) -> None: | ||
return | ||
|
||
|
||
def _maybe_wait_prefetch_done_impl(x: torch.Tensor) -> None: | ||
forward_context = get_forward_context() | ||
if not forward_context.prefetch_mlp_enabled: | ||
return | ||
if forward_context.prefetch_mlp_gate_up_proj or \ | ||
forward_context.prefetch_mlp_down_proj: | ||
prefetch_stream = get_forward_context().prefetch_stream | ||
# wait until prefetch done | ||
torch.npu.current_stream().wait_stream(prefetch_stream) | ||
forward_context.prefetch_mlp_gate_up_proj = False | ||
forward_context.prefetch_mlp_down_proj = False | ||
return | ||
|
||
|
||
def _maybe_wait_prefetch_done_impl_fake(x: torch.Tensor) -> None: | ||
return | ||
|
||
|
||
direct_register_custom_op( | ||
op_name="maybe_chunk_residual", | ||
op_func=_maybe_chunk_residual_impl, | ||
fake_impl=lambda x, residual: residual, | ||
mutates_args=[], | ||
dispatch_key="PrivateUse1" | ||
) | ||
|
||
|
||
direct_register_custom_op( | ||
op_name="maybe_all_gather_and_maybe_unpad", | ||
op_func=_maybe_all_gather_and_maybe_unpad_impl, | ||
fake_impl=lambda x, label: x, | ||
mutates_args=[], | ||
dispatch_key="PrivateUse1" | ||
) | ||
|
||
|
||
direct_register_custom_op( | ||
op_name="maybe_pad_and_reduce", | ||
op_func=_maybe_pad_and_reduce_impl, | ||
fake_impl=lambda x: x, | ||
mutates_args=[], | ||
dispatch_key="PrivateUse1" | ||
) | ||
|
||
|
||
direct_register_custom_op( | ||
op_name="maybe_prefetch_mlp_gate_up_proj", | ||
op_func=_maybe_prefetch_mlp_gate_up_proj_impl, | ||
fake_impl=_maybe_prefetch_mlp_gate_up_proj_impl_fake, | ||
mutates_args=[], | ||
dispatch_key="PrivateUse1" | ||
) | ||
|
||
|
||
direct_register_custom_op( | ||
op_name="maybe_prefetch_mlp_down_proj", | ||
op_func=_maybe_prefetch_mlp_down_proj_impl, | ||
fake_impl=_maybe_prefetch_mlp_down_proj_impl_fake, | ||
mutates_args=[], | ||
dispatch_key="PrivateUse1" | ||
) | ||
|
||
|
||
direct_register_custom_op( | ||
op_name="maybe_wait_prefetch_done", | ||
op_func=_maybe_wait_prefetch_done_impl, | ||
fake_impl=_maybe_wait_prefetch_done_impl_fake, | ||
mutates_args=[], | ||
dispatch_key="PrivateUse1" | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value
1000
is a magic number. It's used as a threshold to enable theflashcomm_v1
optimization. This makes the code harder to understand and maintain. It should be defined as a named constant with a comment explaining its purpose and how this value was determined. This would improve readability and make it easier to tune this threshold in the future.