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

Some pointwise unary ops return 2-D results from 1-D inputs #12671

Open
Tracked by #13521 ...
jdh8 opened this issue Sep 13, 2024 · 8 comments
Open
Tracked by #13521 ...

Some pointwise unary ops return 2-D results from 1-D inputs #12671

jdh8 opened this issue Sep 13, 2024 · 8 comments
Assignees
Labels
bug Something isn't working P1 pytorch-compiler

Comments

@jdh8
Copy link
Contributor

jdh8 commented Sep 13, 2024

As we found out in tenstorrent/pytorch2.0_ttnn#198, several ops produce (1, N) tensors when (N,) tensors are expected.

Affected ops:

  • ceil
  • floor
  • gelu
  • rsqrt
  • sqrt

Spared ops:

  • cos
  • erf
  • exp

The lists above are non-exhaustive (yet). I fired this ticket before keeping trying on other ops because this issue is probably widespread.

@ayerofieiev-tt
Copy link
Member

ayerofieiev-tt commented Sep 13, 2024

@jdh8 is the input tilized?

@jdh8
Copy link
Contributor Author

jdh8 commented Sep 26, 2024

I’m trying to compare implementations of unary ops, for example, ttnn::cos and ttnn::sqrt. However, I searched the C++ codebase, but I only found call sites instead of definitions. Where do I find these implementations?

(python_env) jdh8@tt-metal-skymizer-n150-3:~/tt-metal/ttnn/cpp$ git grep -i 'cos *('
ttnn/operations/eltwise/complex_unary/device/complex_unary_op.cpp:    Tensor c = ttnn::cos(input_b,output_mem_config);
ttnn/operations/eltwise/unary_backward/device/unary_backward_op.cpp:// name: cos(Tensor self) -> Tensor
ttnn/operations/eltwise/unary_backward/device/unary_backward_op.cpp:// # - name: acos(Tensor self) -> Tensor
ttnn/operations/eltwise/unary_backward/device/unary_backward_op.cpp:// self: grad * self.cos()
ttnn/operations/eltwise/unary_backward/device/unary_backward_op.cpp:    Tensor grad_input = ttnn::multiply(grad, ttnn::cos(input_tensor, output_mem_config), std::nullopt, output_mem_config);

@jdh8
Copy link
Contributor Author

jdh8 commented Oct 2, 2024

After #13339, cos becomes affected. Now I believe that returning a 2-D tensor is intended. Squeezing 2-D tensors in the compiler looks like a true solution now.

@jdh8
Copy link
Contributor Author

jdh8 commented Oct 13, 2024

I made a workaround in the compiler at tenstorrent/pytorch2.0_ttnn#198 because I can't find the root cause. That PR also enables conversion for all rounding ops: ceil, floor, round, trunc.

@eyonland
Copy link
Contributor

eyonland commented Oct 21, 2024

At the moment we believe that all unary eltwise ops are impacted by this. It sounds like our to_layout is converting to [1[32], 1024] for tiled output instead of the expected shape of [1024, 1[32]] .
To align with torch we have to do ttnn.transpose(input_tensor, -2, -1). See example below.

import ttnn
import torch

w = 1024
torch_input_tensor = torch.rand((w,), dtype=torch.bfloat16)
device_id = 0
device = ttnn.open_device(device_id=device_id)
input_tensor = ttnn.from_torch(torch_input_tensor, layout=ttnn.TILE_LAYOUT, device=device)

# required to align with torch
input_tensor = ttnn.transpose(input_tensor, -2, -1)
print(input_tensor.shape)
scalar = 3
output_tensor = ttnn.erf(input_tensor)
print(output_tensor.shape)

output_tensor = ttnn.from_device(output_tensor)
output_tensor = ttnn.to_torch(output_tensor)
print(output_tensor.shape)

pytorch_output_tensor = torch.erf(torch_input_tensor)
print("what torch does")
print(pytorch_output_tensor.shape)

ttnn.close_device(device=device)

@jdh8 can you confirm?

@ayerofieiev-tt , if our understanding is correct, this sounds like it is not an eltwise issue.

@ayerofieiev-tt
Copy link
Member

@jdh8 , I will keep track of this one. We might have to update ops later, but at this moment this is pretty much blocked by Tensor Layout work

jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Nov 27, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Nov 28, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Nov 28, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Dec 10, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Dec 10, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Dec 12, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Dec 12, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Dec 19, 2024
jdh8 added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Dec 19, 2024
ayerofieiev-tt added a commit to tenstorrent/pytorch2.0_ttnn that referenced this issue Dec 19, 2024
* Test conversion to `ttnn.floor` with known input variations

* Make import global to reduce overhead

* Convert `aten.ceil` to `ttnn.ceil`

* Try (1066,) with some other univariate functions

The results are inconclusive:
- `cos` passes
- `sqrt` and `floor` fail

* Test unary ops with fast and approximate mode

* Try squeezing out the extraneous dimension for 1-D tensors

* Restore conversion for `aten.remainder.Scalar`

* Fix the workaround to squeeze back to 1-D tensors

* Implement conversion to `ttnn.round`

* Implement conversion to `ttnn.trunc`

* Update conversion for `ttnn.round`

* Lessen PCC for `ttnn.round` kernel (tenstorrent/tt-metal#13851)

* Convert `torch.round` with various decimal places

* Update parameters of `ttnn.round`

* Restore code wrongly removed when rebasing 1355625

* Update list of pointwise unary ops

* Restore general handler for `aten.hardtanh`

* Properly test 1-D cases after working around tenstorrent/tt-metal#12671

* Simplify the workaround for tenstorrent/tt-metal#12671

---------

Co-authored-by: Artem Yerofieiev <[email protected]>
@prajaramanTT
Copy link

@jdh8 @ayerofieiev-tt Is this still an open issue ? If not, can you please close this ? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P1 pytorch-compiler
Projects
Status: No status
Development

No branches or pull requests

4 participants