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 aten._local_scalar_dense converter #2752

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chohk88
Copy link
Collaborator

@chohk88 chohk88 commented Apr 16, 2024

Description

A converter for the torch.ops.aten._local_scalar_dense operation, a low-level operation in PyTorch used internally to extract a scalar value from a tensor containing a single element.

To reviewer, I did not incorporate type-specific considerations in the implementation of the converter and test code. However, all test cases have successfully passed. I would greatly appreciate any feedback or suggestions regarding this aspect—especially if there are potential implications or improvements I might have overlooked.

image

Fixes # (issue)

Type of change

Please delete options that are not relevant and/or add your own.

  • 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

@chohk88 chohk88 self-assigned this Apr 16, 2024
@github-actions github-actions bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Apr 16, 2024
@github-actions github-actions bot requested a review from peri044 April 16, 2024 08:56
@chohk88 chohk88 linked an issue Apr 16, 2024 that may be closed by this pull request
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 08:56:26.557741+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 08:58:19.570898+00:00
@@ -6,28 +6,16 @@


class TestLocalScalarDenseConverter(DispatchTestCase):
    @parameterized.expand(
        [
-            (
-                torch.randn((5, 10, 5), dtype=torch.float32),
-            ),
-            (
-                torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),
-            ),
-            (
-                torch.randn((1), dtype=torch.float32),
-            ),
-            (
-                (torch.tensor([-2.4])),
-            ),
-            (
-                (torch.tensor([5.5, 3.5, 3.6])),
-            ),
-            (
-                (torch.tensor([True])),
-            ),
+            (torch.randn((5, 10, 5), dtype=torch.float32),),
+            (torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),),
+            (torch.randn((1), dtype=torch.float32),),
+            ((torch.tensor([-2.4])),),
+            ((torch.tensor([5.5, 3.5, 3.6])),),
+            ((torch.tensor([True])),),
            (
                torch.tensor(
                    [
                        float("nan"),
                        1.23,
@@ -42,13 +30,11 @@
                        1.23,
                        float("nan"),
                    ]
                ),
            ),
-            (
-                (torch.tensor([float("inf")])),
-            ),
+            ((torch.tensor([float("inf")])),),
        ]
    )
    def test_local_scalar_dense(self, data):
        class local_scalar_dense(nn.Module):
            def forward(self, input):

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 08:56:26.539822+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 08:58:21.048206+00:00
@@ -6,28 +6,16 @@


class TestLocalScalarDenseConverter(DispatchTestCase):
    @parameterized.expand(
        [
-            (
-                torch.randn((5, 10, 5), dtype=torch.float32),
-            ),
-            (
-                torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),
-            ),
-            (
-                torch.randn((1), dtype=torch.float32),
-            ),
-            (
-                (torch.tensor([-2.4])),
-            ),
-            (
-                (torch.tensor([5.5, 3.5, 3.6])),
-            ),
-            (
-                (torch.tensor([True])),
-            ),
+            (torch.randn((5, 10, 5), dtype=torch.float32),),
+            (torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),),
+            (torch.randn((1), dtype=torch.float32),),
+            ((torch.tensor([-2.4])),),
+            ((torch.tensor([5.5, 3.5, 3.6])),),
+            ((torch.tensor([True])),),
            (
                torch.tensor(
                    [
                        float("nan"),
                        1.23,
@@ -42,13 +30,11 @@
                        1.23,
                        float("nan"),
                    ]
                ),
            ),
-            (
-                (torch.tensor([float("inf")])),
-            ),
+            ((torch.tensor([float("inf")])),),
        ]
    )
    def test_local_scalar_dense(self, data):
        class local_scalar_dense(nn.Module):
            def forward(self, input):

@chohk88
Copy link
Collaborator Author

chohk88 commented Apr 16, 2024

I would appreciate your feedback on the type-related issues mentioned in the description.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 08:56:28.395579+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 08:59:57.272490+00:00
@@ -6,28 +6,16 @@


class TestLocalScalarDenseConverter(DispatchTestCase):
    @parameterized.expand(
        [
-            (
-                torch.randn((5, 10, 5), dtype=torch.float32),
-            ),
-            (
-                torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),
-            ),
-            (
-                torch.randn((1), dtype=torch.float32),
-            ),
-            (
-                (torch.tensor([-2.4])),
-            ),
-            (
-                (torch.tensor([5.5, 3.5, 3.6])),
-            ),
-            (
-                (torch.tensor([True])),
-            ),
+            (torch.randn((5, 10, 5), dtype=torch.float32),),
+            (torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),),
+            (torch.randn((1), dtype=torch.float32),),
+            ((torch.tensor([-2.4])),),
+            ((torch.tensor([5.5, 3.5, 3.6])),),
+            ((torch.tensor([True])),),
            (
                torch.tensor(
                    [
                        float("nan"),
                        1.23,
@@ -42,13 +30,11 @@
                        1.23,
                        float("nan"),
                    ]
                ),
            ),
-            (
-                (torch.tensor([float("inf")])),
-            ),
+            ((torch.tensor([float("inf")])),),
        ]
    )
    def test_local_scalar_dense(self, data):
        class local_scalar_dense(nn.Module):
            def forward(self, input):

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 08:56:26.670833+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_local_scalar_dense_aten.py	2024-04-16 09:00:15.174874+00:00
@@ -6,28 +6,16 @@


class TestLocalScalarDenseConverter(DispatchTestCase):
    @parameterized.expand(
        [
-            (
-                torch.randn((5, 10, 5), dtype=torch.float32),
-            ),
-            (
-                torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),
-            ),
-            (
-                torch.randn((1), dtype=torch.float32),
-            ),
-            (
-                (torch.tensor([-2.4])),
-            ),
-            (
-                (torch.tensor([5.5, 3.5, 3.6])),
-            ),
-            (
-                (torch.tensor([True])),
-            ),
+            (torch.randn((5, 10, 5), dtype=torch.float32),),
+            (torch.randint(-10, 10, (5, 1, 15), dtype=torch.int32),),
+            (torch.randn((1), dtype=torch.float32),),
+            ((torch.tensor([-2.4])),),
+            ((torch.tensor([5.5, 3.5, 3.6])),),
+            ((torch.tensor([True])),),
            (
                torch.tensor(
                    [
                        float("nan"),
                        1.23,
@@ -42,13 +30,11 @@
                        1.23,
                        float("nan"),
                    ]
                ),
            ),
-            (
-                (torch.tensor([float("inf")])),
-            ),
+            ((torch.tensor([float("inf")])),),
        ]
    )
    def test_local_scalar_dense(self, data):
        class local_scalar_dense(nn.Module):
            def forward(self, input):

@zewenli98
Copy link
Collaborator

@chohk88 @narendasan I checked the aten schema where there are only two funcs return Scalar type, i.e.,

- func: item(Tensor self) -> Scalar
- func: _local_scalar_dense(Tensor self) -> Scalar

I found some people have similar issues like: pytorch/xla#909
Their cases are moving data from XLA to CPU, so I'm thinking if this op is only called when moving data to CPUs.

For the functionality, my understanding to this function is to extract the first element in the input, whatever it is a single tensor or list of tensors, and then move it to CPUs. If true, I don't think it's meaningful to implement this converter. Instead, we can have a lowering pass to remove this op to avoid potential graph breaks.

Do you know why this converter is needed? or is this op used in any models?

@narendasan narendasan added the Blocked Issue cannot be resolved until a change in a dependency label May 3, 2024
@narendasan
Copy link
Collaborator

Needs #2810 to be implemented to fully work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked Issue cannot be resolved until a change in a dependency 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 needs-release-cherrypick
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aten._local_scalar_dense
4 participants