-
Notifications
You must be signed in to change notification settings - Fork 363
support argmax converter #2291
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
support argmax converter #2291
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from . import ( | ||
activation, | ||
attention, | ||
argmax, | ||
cast, | ||
cat, | ||
condition, | ||
|
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,74 @@ | ||
from typing import Optional | ||
|
||
import tensorrt as trt | ||
from torch.fx.node import Target | ||
from torch_tensorrt.dynamo._SourceIR import SourceIR | ||
from torch_tensorrt.dynamo.conversion import impl | ||
from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext | ||
from torch_tensorrt.dynamo.conversion.converter_utils import ( | ||
cast_trt_tensor, | ||
flatten_dims, | ||
get_axes_for_reduce_op, | ||
get_positive_dim, | ||
) | ||
from torch_tensorrt.fx.converters.converter_utils import set_layer_name | ||
from torch_tensorrt.fx.types import TRTTensor | ||
|
||
|
||
def argmax( | ||
ctx: ConversionContext, | ||
target: Target, | ||
source_ir: Optional[SourceIR], | ||
name: str, | ||
input: TRTTensor, | ||
dim: Optional[int], | ||
keep_dim: bool = False, | ||
) -> TRTTensor: | ||
if input.dtype == trt.int32: | ||
input = cast_trt_tensor(ctx, input, trt.float32, name, target, source_ir) | ||
|
||
# Three different cases here: | ||
# 1. dim == None, flatten input tensor first, keep_dim will be ignore and the output rank == input rank | ||
# 2. input rank == 1: TopK layer does not support 1 dimensional topk operation. Broadcast input to rank == 2 | ||
# 3. normal cases, no additional handlings | ||
out = input | ||
|
||
if dim is None: | ||
new_shape = (*flatten_dims(input, 0, -1), 1) | ||
out = impl.shuffle.reshape( | ||
ctx, target, source_ir, f"{name}_flatten", input, new_shape | ||
) | ||
elif len(input.shape) == 1: | ||
new_shape = (*input.shape, 1) | ||
out = impl.shuffle.reshape( | ||
ctx, target, source_ir, f"{name}_broadcast", input, new_shape | ||
) | ||
|
||
# Reduce over the flattened input if the dimension is None, otherwise the specified dimension | ||
reduce_mask = get_axes_for_reduce_op( | ||
get_positive_dim(dim if dim is not None else 0, len(out.shape)) | ||
) | ||
|
||
topk_layer = ctx.net.add_topk(out, trt.TopKOperation.MAX, 1, reduce_mask) | ||
set_layer_name(topk_layer, target, name, source_ir) | ||
|
||
out = topk_layer.get_output(1) | ||
|
||
if dim is None: | ||
new_shape = ((1,) * len(input.shape)) if keep_dim else () | ||
out = impl.shuffle.reshape( | ||
ctx, target, source_ir, f"{name}_unflatten", out, new_shape | ||
) | ||
elif len(input.shape) == 1: | ||
out = impl.squeeze.squeeze( | ||
ctx, | ||
target, | ||
source_ir, | ||
f"{name}_squeeze", | ||
out, | ||
1 if keep_dim else (0, 1), | ||
) | ||
elif not keep_dim: | ||
out = impl.squeeze.squeeze(ctx, target, source_ir, f"{name}_squeeze", out, dim) | ||
|
||
return out |
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,41 @@ | ||
import torch | ||
import torch.nn as nn | ||
from parameterized import parameterized | ||
from torch.testing._internal.common_utils import run_tests | ||
|
||
from .harness import DispatchTestCase | ||
|
||
|
||
class TestArgmaxConverter(DispatchTestCase): | ||
@parameterized.expand( | ||
[ | ||
# input dimension == 1 | ||
("dim_1_keep_dim_true", (3,), 0, True), | ||
("dim_1_keep_dim_true", (3,), 0, False), | ||
# dim == None | ||
("dim_none", (3,), None, True), | ||
("dim_none", (3, 3), None, True), | ||
("dim_none", (3, 3, 3), None, False), | ||
# # common cases | ||
("dim_1_keep_dim_true", (3, 3), 1, True), | ||
("dim_1_keep_dim_false", (3, 3), 1, False), | ||
("dim_0_keep_dim_true", (4, 4, 4), 0, True), | ||
("dim_0_keep_dim_false", (4, 4, 4), 0, False), | ||
("dim_negative_keep_dim_true", (1, 2, 3), -1, True), | ||
] | ||
) | ||
def test_argmax(self, _, input_shape, dim, keep_dim): | ||
class ArgMax(nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, input): | ||
return torch.ops.aten.argmax.default(input, dim, keep_dim) | ||
|
||
input = [torch.randn(*input_shape)] | ||
|
||
self.run_test(ArgMax(), input) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_tests() |
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.