-
Notifications
You must be signed in to change notification settings - Fork 359
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
Conversation
a49c420
to
e8c7b50
Compare
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.
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:
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.
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:
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.
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) |
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.
Could torch.empty_like(input_tensor)
instead be just input_tensor
, or is it required to be empty?
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.
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()) |
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.
Without .cuda()
does the decomposition fail?
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.
Yes in this case it complains that the two tensors are on different devices.
@gs-olive though the test cases pass, I don't think that the decomposition is taking place.
Is this due to torch.unique? |
bcc09f9
to
10a384e
Compare
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.
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
def scatter_add_decomposition( | ||
input_tensor: torch.Tensor, | ||
src_tensor: torch.Tensor, | ||
dim: int, | ||
index: torch.Tensor, | ||
) -> torch.Tensor: |
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.
It seems the order of args doesn't match with tests, which made CI failed.
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.
Thanks for pointing this out! Corrected.
8027b9d
to
2f7221e
Compare
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.
LGTM
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.