-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Compile] Conditional compilation. Introduce compile_ranges #24252
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
Draft
ilmarkov
wants to merge
4
commits into
vllm-project:main
Choose a base branch
from
neuralmagic:imarkov/conditional_compilation_ranges
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
import torch | ||
from torch import nn | ||
from torch.library import Library | ||
|
||
from vllm.compilation.counter import compilation_counter | ||
from vllm.compilation.decorators import support_torch_compile | ||
from vllm.config import (CompilationConfig, CompilationLevel, VllmConfig, | ||
set_current_vllm_config) | ||
from vllm.forward_context import set_forward_context | ||
from vllm.utils import direct_register_custom_op | ||
|
||
# create a library to hold the custom op | ||
silly_lib = Library("silly", "FRAGMENT") # noqa | ||
|
||
BATCH_SIZE = 64 | ||
MLP_SIZE = 128 | ||
|
||
|
||
def silly_attention(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, | ||
out: torch.Tensor) -> None: | ||
out.copy_(q) | ||
out += k | ||
out += v | ||
|
||
|
||
def silly_attention_fake(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, | ||
out: torch.Tensor) -> None: | ||
return | ||
|
||
|
||
direct_register_custom_op( | ||
op_name="attention", | ||
op_func=silly_attention, | ||
mutates_args=["out"], | ||
fake_impl=silly_attention_fake, | ||
target_lib=silly_lib, | ||
) | ||
|
||
|
||
@support_torch_compile | ||
class TestModel(nn.Module): | ||
|
||
def __init__(self, | ||
*, | ||
vllm_config: VllmConfig, | ||
prefix: str = '', | ||
**kwargs) -> None: | ||
super().__init__() | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
x = x + x | ||
attn_output = torch.empty_like(x) | ||
torch.ops.silly.attention(x, x, x, attn_output) | ||
x = attn_output | ||
x = x * 3 | ||
return x | ||
|
||
|
||
@torch.inference_mode | ||
def run_model(vllm_config: VllmConfig, model: nn.Module, | ||
batch_sizes: list[int]): | ||
with set_forward_context({}, vllm_config=vllm_config): | ||
model(torch.randn(BATCH_SIZE, MLP_SIZE).cuda()) | ||
for batch_size in batch_sizes: | ||
model(torch.randn(batch_size, MLP_SIZE).cuda()) | ||
|
||
|
||
def test_compile_ranges(): | ||
vllm_config = VllmConfig(compilation_config=CompilationConfig( | ||
level=CompilationLevel.PIECEWISE, | ||
compile_ranges_split_points=[8, 32], | ||
)) | ||
|
||
with set_current_vllm_config(vllm_config): | ||
model = TestModel(vllm_config=vllm_config, prefix='').eval().cuda() | ||
batch_sizes = [1, 16, 48] | ||
# A has support_torch_compile | ||
with compilation_counter.expect( | ||
num_graphs_seen=1, | ||
num_piecewise_graphs_seen=1, | ||
num_backend_compilations=4, | ||
# num_cudagraph_sizes * num_piecewise_capturable_graphs_seen | ||
): | ||
run_model(vllm_config, model, batch_sizes) |
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.
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.
ah it seems users can still specify self.compile_sizes in addition to this .
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.
Yeah, empty means single compile size.