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

scatter_add_decomposition #2740

Merged
merged 2 commits into from
Jul 24, 2024
Merged

scatter_add_decomposition #2740

merged 2 commits into from
Jul 24, 2024

Conversation

apbose
Copy link
Collaborator

@apbose apbose commented Apr 9, 2024

The issue in this PR is for cases where there is index collision. Example of such cases-
scatter_add(input_tensor = torch.zeros(3,5), dim=1, index_tensor = torch.tensor([[0, 1, 2, 0]]).cuda(), src_tensor = torch.tensor([[1, 2, 3, 1]], dtype=torch.int32).cuda())
Looks like straighforward decomposition by stacking the src_tensor would not work, since it would again lead to collision for some cases.
The better way to implement is to do converter implementation and allot the values using advanced indexing.
Working on this now.

@apbose apbose self-assigned this Apr 9, 2024
@apbose apbose marked this pull request as draft April 9, 2024 21:12
@github-actions github-actions bot added component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Apr 9, 2024
@github-actions github-actions bot requested a review from peri044 April 9, 2024 21:12
@apbose apbose mentioned this pull request Apr 9, 2024
@apbose apbose removed the request for review from peri044 April 9, 2024 21:13
@apbose apbose force-pushed the scatter_add_decomposition branch from a49c420 to e8c7b50 Compare May 14, 2024 20:58
@apbose apbose marked this pull request as ready for review May 14, 2024 21:04
@apbose apbose requested a review from gs-olive May 14, 2024 21:04
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/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-05-14 21:04:24.249027+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-05-14 21:06:14.833958+00:00
@@ -181,13 +181,16 @@
    input_tensor: torch.Tensor,
    src_tensor: torch.Tensor,
    dim: int,
    index: torch.Tensor,
) -> torch.Tensor:
-    input_tensor_to_add = torch.scatter(torch.empty_like(input_tensor), dim, index, src_tensor)
+    input_tensor_to_add = torch.scatter(
+        torch.empty_like(input_tensor), dim, index, src_tensor
+    )
    scatter_add_tensor = torch.add(input_tensor, input_tensor_to_add.cuda())
    return scatter_add_tensor
+

def get_decompositions(
    enable_experimental_decompositions: bool = False,
) -> Dict[OpOverload, Callable[[Any], Any]]:
    if enable_experimental_decompositions:

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/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-05-14 21:04:33.408462+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-05-14 21:06:24.734368+00:00
@@ -181,13 +181,16 @@
    input_tensor: torch.Tensor,
    src_tensor: torch.Tensor,
    dim: int,
    index: torch.Tensor,
) -> torch.Tensor:
-    input_tensor_to_add = torch.scatter(torch.empty_like(input_tensor), dim, index, src_tensor)
+    input_tensor_to_add = torch.scatter(
+        torch.empty_like(input_tensor), dim, index, src_tensor
+    )
    scatter_add_tensor = torch.add(input_tensor, input_tensor_to_add.cuda())
    return scatter_add_tensor
+

def get_decompositions(
    enable_experimental_decompositions: bool = False,
) -> Dict[OpOverload, Callable[[Any], Any]]:
    if enable_experimental_decompositions:

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/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-05-14 21:01:28.751182+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-05-14 21:09:51.626381+00:00
@@ -181,13 +181,16 @@
    input_tensor: torch.Tensor,
    src_tensor: torch.Tensor,
    dim: int,
    index: torch.Tensor,
) -> torch.Tensor:
-    input_tensor_to_add = torch.scatter(torch.empty_like(input_tensor), dim, index, src_tensor)
+    input_tensor_to_add = torch.scatter(
+        torch.empty_like(input_tensor), dim, index, src_tensor
+    )
    scatter_add_tensor = torch.add(input_tensor, input_tensor_to_add.cuda())
    return scatter_add_tensor
+

def get_decompositions(
    enable_experimental_decompositions: bool = False,
) -> Dict[OpOverload, Callable[[Any], Any]]:
    if enable_experimental_decompositions:

dim: int,
index: torch.Tensor,
) -> torch.Tensor:
input_tensor_to_add = torch.scatter(torch.empty_like(input_tensor), dim, index, src_tensor)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could torch.empty_like(input_tensor) instead be just input_tensor, or is it required to be empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes it would be required. The logic is like we would apply scatter with the indices to an empty tensor and then add it to the input tensor.

index: torch.Tensor,
) -> torch.Tensor:
input_tensor_to_add = torch.scatter(torch.empty_like(input_tensor), dim, index, src_tensor)
scatter_add_tensor = torch.add(input_tensor, input_tensor_to_add.cuda())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Without .cuda() does the decomposition fail?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes in this case it complains that the two tensors are on different devices.

@apbose
Copy link
Collaborator Author

apbose commented May 16, 2024

@gs-olive though the test cases pass, I don't think that the decomposition is taking place.
It fails in the backend at aot_export_joint_simple. Shows me something like

CRITICAL:torch_tensorrt.dynamo.backend.backends:Halting compilation on build failure since pass_through_build_failures was specified as True. To
return the default Torch implementation and avoid halting compilation on engine build failures, specify pass_through_build_failures=False.
W0516 15:51:10.818862 139669023303488 torch/_dynamo/exc.py:201] [0/0] Backend compiler failed with a fake tensor exception at
W0516 15:51:10.818862 139669023303488 torch/_dynamo/exc.py:201] [0/0]   File "<eval_with_key>.5 from /home/abose/Documents/work/torchTRT_TRT10_scatter_5_2/TensorRT/tests/py/dynamo/lowering/test_decompositions.py:523 in forward", line 9, in forward
W0516 15:51:10.818862 139669023303488 torch/_dynamo/exc.py:201] [0/0]     return scatter_add_default
W0516 15:51:10.818862 139669023303488 torch/_dynamo/exc.py:201] [0/0] Adding a graph break.

Is this due to torch.unique?

@apbose apbose marked this pull request as draft June 6, 2024 23:00
@apbose apbose force-pushed the scatter_add_decomposition branch 2 times, most recently from bcc09f9 to 10a384e Compare July 11, 2024 20:45
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/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-07-11 20:45:53.887948+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/lowering/_decompositions.py	2024-07-11 20:47:45.424382+00:00
@@ -259,23 +259,36 @@
    select_src_dim = src_copy.shape[dim]
    to_stack_dummy_src = tuple(torch.empty(src_shape) for _ in range(select_src_dim))
    for index_src_dim in range(0, select_src_dim, 1):
        select_tensor_dim = torch.select(src_copy, dim, index_src_dim)
        to_stack_src = to_stack_dummy_src
-        if(index_src_dim == 0):
-            to_stack_src = (select_tensor_dim.cpu(),) + to_stack_dummy_src[index_src_dim+1:] 
-        elif(index_src_dim == select_src_dim - 1 ):
-            to_stack_src = to_stack_dummy_src[:index_src_dim] + (select_tensor_dim.cpu(),)
+        if index_src_dim == 0:
+            to_stack_src = (select_tensor_dim.cpu(),) + to_stack_dummy_src[
+                index_src_dim + 1 :
+            ]
+        elif index_src_dim == select_src_dim - 1:
+            to_stack_src = to_stack_dummy_src[:index_src_dim] + (
+                select_tensor_dim.cpu(),
+            )
        else:
-            to_stack_src = to_stack_dummy_src[:index_src_dim] + (select_tensor_dim.cpu(),) + to_stack_dummy_src[index_src_dim+1:]
+            to_stack_src = (
+                to_stack_dummy_src[:index_src_dim]
+                + (select_tensor_dim.cpu(),)
+                + to_stack_dummy_src[index_src_dim + 1 :]
+            )

        stacked_src = torch.stack(to_stack_src, dim)
-        input_tensor_to_add = torch.scatter(torch.empty_like(input_tensor, dtype= torch.float32), dim, index, stacked_src.cuda())
+        input_tensor_to_add = torch.scatter(
+            torch.empty_like(input_tensor, dtype=torch.float32),
+            dim,
+            index,
+            stacked_src.cuda(),
+        )
        scatter_add_tensor = torch.add(scatter_add_tensor, input_tensor_to_add)
    return scatter_add_tensor

-    
+
def get_decompositions(
    enable_experimental_decompositions: bool = False,
) -> Dict[OpOverload, Callable[[Any], Any]]:
    if enable_experimental_decompositions:
        CORE_ATEN_DECOMPOSITIONS_FILTERED: Dict[OpOverload, Callable[[Any], Any]] = {

Fixing scatter_add test cases. To do: fix the index collision cases

Index collision cases

Index collision cases- removing the torch.unique checl
@apbose apbose marked this pull request as ready for review July 11, 2024 22:57
@apbose apbose requested review from peri044 and zewenli98 July 11, 2024 22:58
Comment on lines 249 to 254
def scatter_add_decomposition(
input_tensor: torch.Tensor,
src_tensor: torch.Tensor,
dim: int,
index: torch.Tensor,
) -> torch.Tensor:
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems the order of args doesn't match with tests, which made CI failed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for pointing this out! Corrected.

@apbose apbose force-pushed the scatter_add_decomposition branch from 8027b9d to 2f7221e Compare July 16, 2024 18:00
Copy link
Collaborator

@zewenli98 zewenli98 left a comment

Choose a reason for hiding this comment

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

LGTM

@lanluo-nvidia lanluo-nvidia merged commit 225fe3b into main Jul 24, 2024
42 of 61 checks passed
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: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants