Skip to content

Remove unnecessary whitespace in complex tensors#36331

Closed
choidongyeon wants to merge 9 commits intopytorch:masterfrom
choidongyeon:complex_whitespace
Closed

Remove unnecessary whitespace in complex tensors#36331
choidongyeon wants to merge 9 commits intopytorch:masterfrom
choidongyeon:complex_whitespace

Conversation

@choidongyeon
Copy link
Copy Markdown

@choidongyeon choidongyeon commented Apr 9, 2020

This PR addresses Issue #36279.
Previously, printing of complex tensors would sometimes yield extra spaces before the elements as shown below:

print(torch.tensor([[1 + 1.340j, 3 + 4j], [1.2 + 1.340j, 6.5 + 7j]], dtype=torch.complex64))

would yield

tensor([[(1.0000 + 1.3400j),
         (3.0000 + 4.0000j)],
        [(1.2000 + 1.3400j),
         (6.5000 + 7.0000j)]], dtype=torch.complex64)

This occurs primarily because when the max width for the element is being assigned, the formatter's max_width is calculated prior to truncating the float values. As a result, self.max_width would end up being much longer than the final length of the element string to be printed.

I address this by adding a boolean variable that checks if a complex tensor contains only ints and change the control flow for calculating self.max_width accordingly.

Here are some sample outputs of both float and complex tensors:

tensor([[0., 0.],
        [0., 0.]], dtype=torch.float64) 

tensor([[(0.+0.j), (0.+0.j)],
        [(0.+0.j), (0.+0.j)]], dtype=torch.complex64) 

tensor([1.2000, 1.3400], dtype=torch.float64) 

tensor([(1.2000+1.3400j)], dtype=torch.complex64) 

tensor([[(1.0000+1.3400j), (3.0000+4.0000j)],
        [(1.2000+1.3400j), (6.5000+7.0000j)]], dtype=torch.complex64) 

tensor([1.0000, 2.0000, 3.0000, 4.5000]) 

tensor([(1.+2.j)], dtype=torch.complex64) 

cc @ezyang @anjali411 @dylanbespalko

@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Apr 9, 2020

💊 CircleCI build failures summary and remediations

As of commit 972eb13 (more details on the Dr. CI page):


  • 1/2 failures introduced in this PR

  • 1/2 broken upstream at merge base e311e53 since Apr 09

    Please rebase on the viable/strict branch (expand for instructions)

    If your commit is newer than viable/strict, you can try basing on an older, stable commit:

    git fetch https://github.com/pytorch/pytorch viable/strict
    git rebase --onto FETCH_HEAD $(git merge-base origin/master HEAD)
    

    If your commit is older than viable/strict:

    git fetch https://github.com/pytorch/pytorch viable/strict
    git rebase FETCH_HEAD
    

    Check out the recency history of this "viable master" tracking branch.


XLA failure

Job pytorch_xla_linux_xenial_py3_6_clang7_test is failing. Please create an issue with title prefixed by [PT_BREAK] in pytorch/xla and link to to this PR. If you have questions, please reach out to @ailzhang / @dlibenzi / @JackCaoG.


🚧 1 upstream failure:

These were probably caused by upstream breakages:


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker.

See how this bot performed.

This comment has been revised 21 times.

@anjali411 anjali411 self-requested a review April 9, 2020 20:19
@anjali411 anjali411 added the module: complex Related to complex number support in PyTorch label Apr 9, 2020
@anjali411
Copy link
Copy Markdown
Contributor

I just saw that this PR leads to this:

>>> torch.tensor([1+2j],dtype=torch.complex64)
tensor([(1 + 2.j)], dtype=torch.complex64)

It should instead output tensor([(1. + 2.j)], dtype=torch.complex64) So I think we should also fix this in this PR

@anjali411
Copy link
Copy Markdown
Contributor

also we should move this check inside format function within complex branch and not pass has_non_zero_decimal_val in format(...) since has_non_zero_decimal_val is only used by complex

@choidongyeon
Copy link
Copy Markdown
Author

@anjali411 Thanks for pointing these out. Will work on them later today.

@choidongyeon
Copy link
Copy Markdown
Author

I just saw that this PR leads to this:

>>> torch.tensor([1+2j],dtype=torch.complex64)
tensor([(1 + 2.j)], dtype=torch.complex64)

It should instead output tensor([(1. + 2.j)], dtype=torch.complex64) So I think we should also fix this in this PR

Addressed this in most recent commit. Will fix the second one in a bit.

@zhangguanheng66 zhangguanheng66 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Apr 9, 2020
@choidongyeon
Copy link
Copy Markdown
Author

also we should move this check inside format function within complex branch and not pass has_non_zero_decimal_val in format(...) since has_non_zero_decimal_val is only used by complex

This was a great suggestion. The only problem is that format works with single element of a tensor rather than all elements of a tensor. Since we need to check the whole tensor, it wouldn't be possible to put the check inside of format. However, I was able to work with just the additions I had already made in the Formatter constructor for this PR. Makes the code much more DRY. Not sure how you feel about the attribute name complex_with_decimal though?

Comment thread torch/_tensor_str.py Outdated
for value in tensor_view])
for value in tensor_view:
value_str = '{}'.format(value)
if self.complex_dtype and self.complex_with_decimal:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if self.complex_dtype 
   if self.complex_with_decimal:
        value_str = ('{{:.{}f}}').format(PRINT_OPTS.precision).format(value)
   else:
        value_str = "{:.0f}".format(value.item())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

attribute name is okay though?

Copy link
Copy Markdown
Contributor

@anjali411 anjali411 Apr 9, 2020

Choose a reason for hiding this comment

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

I think we should change it to has_non_zero_decimal_val as mentioned here :D

Comment thread torch/_tensor_str.py Outdated
tensor_view = tensor.reshape(-1)

if not self.floating_dtype:
self.complex_with_decimal = False
Copy link
Copy Markdown
Contributor

@anjali411 anjali411 Apr 9, 2020

Choose a reason for hiding this comment

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

I think we should change it to has_non_zero_decimal_val and perhaps add a comment that it's only used for complex

@anjali411
Copy link
Copy Markdown
Contributor

anjali411 commented Apr 9, 2020

@choidongyeon looks good overall.
nit-

>>> import torch
>>> torch.tensor([1+2j])
tensor([(1. + 2.j)], dtype=torch.complex64)
>>> import numpy as np
>>> np.array([1+2j])
array([1.+2.j])

we should follow numpy and remove the extra space between real and imag values

@choidongyeon
Copy link
Copy Markdown
Author

@choidongyeon looks good overall.
nit-

>>> import torch
>>> torch.tensor([1+2j])
tensor([(1. + 2.j)], dtype=torch.complex64)
>>> import numpy as np
>>> np.array([1+2j])
array([1.+2.j])

we should follow numpy and remove the extra space between real and imag values

Easy peasy. Updated the PR, also updated the PR summary with some sample current outputs.

Copy link
Copy Markdown
Contributor

@anjali411 anjali411 left a comment

Choose a reason for hiding this comment

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

great job! thanks for working on this :D

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@anjali411 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@choidongyeon
Copy link
Copy Markdown
Author

@anjali411 Thanks for being so responsive!
Also, was there originally a comment about an unnecessary comma or am I making this up? I see an email but I don't see it in this thread..

@anjali411
Copy link
Copy Markdown
Contributor

@anjali411 Thanks for being so responsive!
Also, was there originally a comment about an unnecessary comma or am I making this up? I see an email but I don't see it in this thread..

of course! let me know if you'd like to work on other issues related to complex numbers.

yeah I realized it was something else and so I removed it

@choidongyeon choidongyeon deleted the complex_whitespace branch April 10, 2020 02:54
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@anjali411 merged this pull request in 2f5b523.

ashishfarmer pushed a commit to ashishfarmer/pytorch that referenced this pull request Apr 13, 2020
Summary:
This PR addresses Issue pytorch#36279.
Previously, printing of complex tensors would sometimes yield extra spaces before the elements as shown below:
```
print(torch.tensor([[1 + 1.340j, 3 + 4j], [1.2 + 1.340j, 6.5 + 7j]], dtype=torch.complex64))
```
would yield
```
tensor([[(1.0000 + 1.3400j),
         (3.0000 + 4.0000j)],
        [(1.2000 + 1.3400j),
         (6.5000 + 7.0000j)]], dtype=torch.complex64)
```
This occurs primarily because when the max width for the element is being assigned, the formatter's max_width is calculated prior to truncating the float values. As a result, ```self.max_width``` would end up being much longer than the final length of the element string to be printed.

I address this by adding a boolean variable that checks if a complex tensor contains only ints and change the control flow for calculating ```self.max_width``` accordingly.

Here are some sample outputs of both float and complex tensors:

```
tensor([[0., 0.],
        [0., 0.]], dtype=torch.float64)

tensor([[(0.+0.j), (0.+0.j)],
        [(0.+0.j), (0.+0.j)]], dtype=torch.complex64)

tensor([1.2000, 1.3400], dtype=torch.float64)

tensor([(1.2000+1.3400j)], dtype=torch.complex64)

tensor([[(1.0000+1.3400j), (3.0000+4.0000j)],
        [(1.2000+1.3400j), (6.5000+7.0000j)]], dtype=torch.complex64)

tensor([1.0000, 2.0000, 3.0000, 4.5000])

tensor([(1.+2.j)], dtype=torch.complex64)
```

cc ezyang anjali411 dylanbespalko
Pull Request resolved: pytorch#36331

Differential Revision: D20955663

Pulled By: anjali411

fbshipit-source-id: c26a651eb5c9db6fcc315ad8d5c1bd9f4b4708f7
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
This PR addresses Issue pytorch#36279.
Previously, printing of complex tensors would sometimes yield extra spaces before the elements as shown below:
```
print(torch.tensor([[1 + 1.340j, 3 + 4j], [1.2 + 1.340j, 6.5 + 7j]], dtype=torch.complex64))
```
would yield
```
tensor([[(1.0000 + 1.3400j),
         (3.0000 + 4.0000j)],
        [(1.2000 + 1.3400j),
         (6.5000 + 7.0000j)]], dtype=torch.complex64)
```
This occurs primarily because when the max width for the element is being assigned, the formatter's max_width is calculated prior to truncating the float values. As a result, ```self.max_width``` would end up being much longer than the final length of the element string to be printed.

I address this by adding a boolean variable that checks if a complex tensor contains only ints and change the control flow for calculating ```self.max_width``` accordingly.

Here are some sample outputs of both float and complex tensors:

```
tensor([[0., 0.],
        [0., 0.]], dtype=torch.float64)

tensor([[(0.+0.j), (0.+0.j)],
        [(0.+0.j), (0.+0.j)]], dtype=torch.complex64)

tensor([1.2000, 1.3400], dtype=torch.float64)

tensor([(1.2000+1.3400j)], dtype=torch.complex64)

tensor([[(1.0000+1.3400j), (3.0000+4.0000j)],
        [(1.2000+1.3400j), (6.5000+7.0000j)]], dtype=torch.complex64)

tensor([1.0000, 2.0000, 3.0000, 4.5000])

tensor([(1.+2.j)], dtype=torch.complex64)
```

cc ezyang anjali411 dylanbespalko
Pull Request resolved: pytorch#36331

Differential Revision: D20955663

Pulled By: anjali411

fbshipit-source-id: c26a651eb5c9db6fcc315ad8d5c1bd9f4b4708f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: complex Related to complex number support in PyTorch open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants