-
Notifications
You must be signed in to change notification settings - Fork 361
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
feat: Support flashinfer.rmsnorm #3424
Open
bowang007
wants to merge
3
commits into
main
Choose a base branch
from
flashinfer_rmsnorm
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.
Open
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 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 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import itertools | ||
import logging | ||
from types import FunctionType | ||
from typing import Any, Callable, Tuple | ||
|
@@ -130,16 +131,25 @@ def _generic_plugin_desc(*args: Any, **kwargs: Any) -> Tuple[trtp.TensorDesc]: | |
output = torch_op(*fake_args, **kwargs) | ||
|
||
# We assume that number of dimensions are the same in torch op | ||
shape_calc_fns = [None] * args[0].ndim | ||
for i in range(args[0].ndim): | ||
input_node_expr = [syms_arg[i].node.expr for syms_arg in syms_args] | ||
shape_calc_fns = [None] * output.ndim | ||
|
||
for i in range(output.ndim): | ||
input_node_expr = input_node_expr = list( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to be a typo here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 112 and 125 are duplicated? |
||
itertools.chain.from_iterable( | ||
[sym.node.expr for sym in syms_arg] for syms_arg in syms_args | ||
) | ||
) | ||
|
||
shape_calc_fns[i] = lambdify( | ||
tuple(input_node_expr), output.shape[i].node.expr, "math" | ||
) | ||
|
||
out_desc = tensor_args[0].like() | ||
for i in range(out_desc.ndim): | ||
input_shape_expr = [tensor_arg.shape_expr[i] for tensor_arg in tensor_args] | ||
input_shape_expr = list( | ||
itertools.chain.from_iterable(arg.shape_expr for arg in tensor_args) | ||
) | ||
|
||
if output.shape[i].node.expr is None: | ||
raise ValueError(f"output.shape[{i}].node.expr cannot be None") | ||
out_desc.shape_expr[i] = shape_calc_fns[i](*input_shape_expr) # type: ignore[misc] | ||
|
This file contains 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
50 changes: 50 additions & 0 deletions
50
tests/py/dynamo/automatic_plugin/test_flashinfer_rmsnorm.py
This file contains 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,50 @@ | ||
import flashinfer | ||
bowang007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import torch | ||
import torch.nn as nn | ||
import torch_tensorrt | ||
from parameterized import parameterized | ||
from torch.testing._internal.common_utils import run_tests | ||
from torch_tensorrt._enums import dtype | ||
|
||
from ..conversion.harness import DispatchTestCase | ||
|
||
|
||
@torch.library.custom_op("flashinfer::rmsnorm", mutates_args=()) # type: ignore[misc] | ||
def flashinfer_rmsnorm( | ||
input: torch.Tensor, weight: torch.Tensor, eps: float = 1e-6 | ||
) -> torch.Tensor: | ||
return flashinfer.norm.rmsnorm(input, weight) | ||
bowang007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@torch.library.register_fake("flashinfer::rmsnorm") | ||
def _(input: torch.Tensor, weight: torch.Tensor, b: float = 1e-6) -> torch.Tensor: | ||
return input | ||
|
||
|
||
torch_tensorrt.dynamo.conversion.plugins.custom_op( | ||
"flashinfer::rmsnorm", supports_dynamic_shapes=True | ||
) | ||
|
||
|
||
class TestAutomaticPlugin(DispatchTestCase): | ||
@parameterized.expand( | ||
[ | ||
((64, 64), (64,), torch.float16), | ||
((256, 256), (256,), torch.float16), | ||
] | ||
) | ||
def test_rmsnorm_float(self, input_shape, weight_shape, data_type): | ||
class rmsnorm(nn.Module): | ||
def forward(self, input, weight): | ||
return torch.ops.flashinfer.rmsnorm.default(input, weight) | ||
|
||
inputs = [ | ||
torch.randn(input_shape, device="cuda", dtype=data_type), | ||
torch.randn(weight_shape, device="cuda", dtype=data_type), | ||
] | ||
|
||
self.run_test(rmsnorm(), inputs, precision=dtype.f16) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_tests() |
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.
should we install here ?
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.
move it to tests/py/requirements.txt