Skip to content
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 prod, max, min, and mean via reduce layer #2355

Merged
merged 9 commits into from
Oct 6, 2023

Conversation

zewenli98
Copy link
Collaborator

Description

Support prod, max, min, and mean via reduce layer

Fixes #2205

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

@github-actions github-actions bot added component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests labels Sep 30, 2023
@github-actions github-actions bot requested a review from apbose September 30, 2023 02:32
@zewenli98 zewenli98 added the WIP Work is in progress, pull request should not be merged yet label Sep 30, 2023
@zewenli98 zewenli98 self-assigned this Oct 2, 2023
@zewenli98 zewenli98 removed the WIP Work is in progress, pull request should not be merged yet label Oct 4, 2023
@zewenli98 zewenli98 requested a review from gs-olive October 4, 2023 18:03
):
input_val = cast_trt_tensor(ctx, input_val, trt.float32, name)

if dim is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on torch behavior, it seems that torch.mean can also accept an empty list as an indicator to reduce over all dimensions (but min, max, and prod don't seem to accept this). Consider switching this to: if dim is None or (isinstance(dim, (tuple, list)) and len(dim) == 0):

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! And then I also checked docs for amax and sum. They do accept empty sequence as well, so I made changes accordingly.

@zewenli98 zewenli98 force-pushed the reduce_dynamo_converters branch 2 times, most recently from d4f1451 to 6f136de Compare October 5, 2023 01:15
Comment on lines 742 to 764
@dynamo_tensorrt_converter(torch.ops.aten.min.default) # type: ignore[misc]
def aten_ops_min(
ctx: ConversionContext,
target: Target,
args: Tuple[Argument, ...],
kwargs: Dict[str, Argument],
name: str,
) -> Union[TRTTensor, Sequence[TRTTensor]]:
return impl.reduce.min(
ctx,
target,
SourceIR.ATEN,
name,
args[0],
dim=None,
keepdim=False,
return_indices=False,
)


@dynamo_tensorrt_converter(torch.ops.aten.min.dim, capability_validator=one_user_validator) # type: ignore[misc]
def aten_ops_min_dim(
ctx: ConversionContext,
target: Target,
args: Tuple[Argument, ...],
kwargs: Dict[str, Argument],
name: str,
) -> Union[TRTTensor, Sequence[TRTTensor]]:
return impl.reduce.min(
ctx,
target,
SourceIR.ATEN,
name,
args[0],
args[1],
args_bounds_check(args, 2, replacement=False),
return_indices=True,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider coalescing these by using something like:

    return impl.reduce.min(
        ctx,
        target,
        SourceIR.ATEN,
        name,
        args[0],
        args_bounds_check(args, 1, replacement=None),
        args_bounds_check(args, 2, replacement=False),
        return_indices=(target==torch.ops.aten.min.dim),
    )

The same could apply for max

Comment on lines +123 to +124
if return_indices:
return layer.get_output(0), None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@narendasan, @apbose - there are certain converter cases where indices are returned from an operator but never used nor accessed in the graph (confirmed via validator). In these cases, we wouldn't want to use extra computation time to add layers for an unused tensor. Which of these seems best?

  • Return None for the unused tensor, as here - (data, None)
  • Return only the data, since the unused tensor should never be accessed, as in: (data,)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max.dim(Tensor self, int dim, bool keepdim=False) -> (Tensor values, Tensor indices)

@zewenli98 zewenli98 force-pushed the reduce_dynamo_converters branch from 6f136de to 1717018 Compare October 6, 2023 00:54
@github-actions github-actions bot requested a review from narendasan October 6, 2023 18:17
@zewenli98 zewenli98 requested a review from gs-olive October 6, 2023 20:07
Copy link
Collaborator

@gs-olive gs-olive left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, pending CI pass.

@gs-olive
Copy link
Collaborator

gs-olive commented Oct 6, 2023

Tests passing - verified locally

@gs-olive gs-olive merged commit 80bbd8b into pytorch:main Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests priority: high
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose IReduceLayer in dynamo.conversion.impl
3 participants