-
-
Notifications
You must be signed in to change notification settings - Fork 11k
[Misc] Add CustomOp interface for device portability #5255
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e510c0d
Add CustomOp Interface
WoosukKwon d9d43a6
Move activation
WoosukKwon 19bff1c
Move layernorm
WoosukKwon 8bff05a
Move RoPE
WoosukKwon af0d31e
Minor
WoosukKwon a631e7f
Fix
WoosukKwon a1486ff
Fix
WoosukKwon e135eae
Fix
WoosukKwon 31e4930
Merge branch 'main' into dispatcher
WoosukKwon 16bab8e
Revert model changes
WoosukKwon 41b9a2a
move back
WoosukKwon 7986c0f
forward_native
WoosukKwon 24e11d2
revert
WoosukKwon cdc62a2
Move dispatch to offline
WoosukKwon d1182e7
Add note
WoosukKwon 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import torch.nn as nn | ||
|
|
||
| from vllm.utils import is_cpu, is_hip | ||
|
|
||
|
|
||
| class CustomOp(nn.Module): | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__() | ||
| self._forward_method = self.dispatch_forward() | ||
|
|
||
| def forward(self, *args, **kwargs): | ||
| return self._forward_method(*args, **kwargs) | ||
|
|
||
| def forward_native(self, *args, **kwargs): | ||
| """PyTorch-native implementation of the forward method. | ||
|
|
||
| This method is optional. If implemented, it can be used with compilers | ||
| such as torch.compile or PyTorch XLA. Also, it can be used for testing | ||
| purposes. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def forward_cuda(self, *args, **kwargs): | ||
| raise NotImplementedError | ||
|
|
||
| def forward_hip(self, *args, **kwargs): | ||
| # By default, we assume that HIP ops are compatible with CUDA ops. | ||
| return self.forward_cuda(*args, **kwargs) | ||
|
|
||
| def forward_xpu(self, *args, **kwargs): | ||
| # By default, we assume that XPU ops are compatible with CUDA ops. | ||
| # NOTE(woosuk): This is a placeholder for future extensions. | ||
| return self.forward_cuda(*args, **kwargs) | ||
|
|
||
| def forward_cpu(self, *args, **kwargs): | ||
| # By default, we assume that CPU ops are compatible with CUDA ops. | ||
| return self.forward_cuda(*args, **kwargs) | ||
|
|
||
| def forward_tpu(self, *args, **kwargs): | ||
| # By default, we assume that TPU ops are compatible with the | ||
| # PyTorch-native implementation. | ||
| # NOTE(woosuk): This is a placeholder for future extensions. | ||
| return self.forward_native(*args, **kwargs) | ||
|
|
||
| def forward_gaudi(self, *args, **kwargs): | ||
| # By default, we assume that Gaudi ops are compatible with the | ||
| # PyTorch-native implementation. | ||
| # NOTE(woosuk): This is a placeholder for future extensions. | ||
| return self.forward_native(*args, **kwargs) | ||
|
|
||
| def dispatch_forward(self): | ||
| # NOTE(woosuk): Here we assume that vLLM was built for only one | ||
| # specific backend. Currently, we do not support dynamic dispatching. | ||
| if is_hip(): | ||
| return self.forward_hip | ||
| elif is_cpu(): | ||
| return self.forward_cpu | ||
| else: | ||
| return self.forward_cuda | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.