Skip to content

Conversation

@xuhdev
Copy link
Collaborator

@xuhdev xuhdev commented Aug 26, 2020

Before this PR,

import torch
import numpy as np

a = torch.tensor([1, 2], dtype=torch.bool)
c = np.array([1, 2], dtype=np.bool)
print(a[0] == c[0])

a = torch.tensor([1, 2], dtype=torch.complex64)
c = np.array([1, 2], dtype=np.complex64)
print(a[0] == c[0])

 # This case is still broken
a = torch.tensor([1 + 1j, 2 + 2j], dtype=torch.complex64)
c = np.array([1 + 1j, 2 + 2j], dtype=np.complex64)
print(a[0] == c[0])

outputs

False
False
False

After this PR, it outputs:

tensor(True)
/home/user/src/pytorch/torch/tensor.py:25: ComplexWarning: Casting complex values to real discards the imaginary part return f(*args, **kwargs)
tensor(True)
tensor(False)

Related issue: #43579

cc @anjali411 @mruberry

@xuhdev
Copy link
Collaborator Author

xuhdev commented Aug 26, 2020

I couldn't find any Python exposure of is_numpy_scalar. Does anyone know where this is tested?

@mruberry
Copy link
Collaborator

I couldn't find any Python exposure of is_numpy_scalar. Does anyone know where this is tested?

You'd have to test it in C++ if you wanted to test it directly. Instead, however, you can probably test the desired effect (testing is_numpy_scalar indirectly), by adding or extending a test of our NumPy interop. Those tests are in test_torch (although I've been meaning to pull them out into their own test suite to make sifting through them easier). If you wanted to be very thorough you could even write a new test that creates torch tensors and their corresponding NumPy arrays and verifies they compare as equal for all dtypes.

@mruberry mruberry added module: complex Related to complex number support in PyTorch triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Aug 26, 2020
@mruberry mruberry requested review from anjali411 and mruberry August 26, 2020 19:20
@mruberry mruberry added the module: numpy Related to numpy support, and also numpy compatibility of our operators label Aug 26, 2020
@xuhdev
Copy link
Collaborator Author

xuhdev commented Aug 27, 2020

@mruberry I've added test now

@codecov
Copy link

codecov bot commented Aug 27, 2020

Codecov Report

Merging #43644 into master will increase coverage by 0.00%.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #43644   +/-   ##
=======================================
  Coverage   69.28%   69.29%           
=======================================
  Files         379      379           
  Lines       47035    47035           
=======================================
+ Hits        32590    32591    +1     
+ Misses      14445    14444    -1     
Impacted Files Coverage Δ
torch/testing/_internal/expecttest.py 78.57% <0.00%> (+1.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7035cd0...fc4728d. Read the comment docs.

@xuhdev xuhdev requested a review from nairbv August 27, 2020 23:09
Copy link
Collaborator

Choose a reason for hiding this comment

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

Currently the complex tensors won't actually have any complex values and there's only a single 1D tensor. What about extending the test slightly to support complex values and multiple tensor sizes? It could do something like this:

is_complex = dtype in (torch.cfloat, torch.cdouble)
if is_complex:
  tensors = (...)
else:
  tensors = (
    torch.tensor(5, dtype=dtype, device=device),
    torch.tensor([0, 1, 2], dtype=dtype, device=device),
    torch.tensor([[1, 2], [3, 4]], dtype=dtype, device=device),
  )

for tensor in tensors:
  np_array = tensor.cpu().numpy()
  for t, a in product((tensor[0], tensor[0].item()), (np_array[0], np_array[0].item()):
    self.assertEqual(t, a)
    if not is_complex:
      self.assertTrue(t == a)

  if is_complex:
    self.assertTrue(t[0] == np_array[0].item())
    self.assertTrue(t[0].item() == np_array[0])
    self.assertFalse(t[0] == np_array[0])  

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good

Copy link
Collaborator

Choose a reason for hiding this comment

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

Per the following discussion, if you follow this pattern the later conditionals will probably need to check dtype is torch.cfloat instead of the is_complex boolean.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What do you mean?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Won't self.assertFalse(t[0] == np_array[0]) be true only for cfloat, not for cdouble?

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, thanks. Anyway, I have structured the test a bit differently.

@mruberry
Copy link
Collaborator

Hey @xuhdev! This looks really cool. Just have one question about testing for you to review. Looking forward to hearing your thoughts!

@xuhdev xuhdev requested a review from mruberry August 28, 2020 23:08
@xuhdev xuhdev force-pushed the is-numpy-scalar branch 2 times, most recently from 944b26f to 5faeaa0 Compare August 29, 2020 01:07
Copy link
Contributor

Choose a reason for hiding this comment

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

@mruberry do we have any function similar to _make_tensors in TestTorchDeviceType?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Recap of offline discussion: #43451 is adding a make_tensor method to common_utils.py.

Copy link
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.

Copy link
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.

@anjali411
Copy link
Contributor

anjali411 commented Sep 1, 2020

@xuhdev can you rebase this PR again? phabricator is complaining since another PR landed touching files edited in this PR

Before this PR,

```python
import torch
import numpy as np

a = torch.tensor([1, 2], dtype=torch.bool)
c = np.array([1, 2], dtype=np.bool)
print(a[0] == c[0])

a = torch.tensor([1, 2], dtype=torch.complex64)
c = np.array([1, 2], dtype=np.complex64)
print(a[0] == c[0])

 # This case is still broken
a = torch.tensor([1 + 1j, 2 + 2j], dtype=torch.complex64)
c = np.array([1 + 1j, 2 + 2j], dtype=np.complex64)
print(a[0] == c[0])
```

outputs

```
False
False
False
```

After this PR, it outputs:

```
tensor(True)
/home/hong/xusrc/pytorch/torch/tensor.py:25: ComplexWarning: Casting complex values to real discards the imaginary part return f(*args, **kwargs)
tensor(True)
tensor(False)
```

Related issue: pytorch#43579
@xuhdev
Copy link
Collaborator Author

xuhdev commented Sep 1, 2020

@anjali411 Done!

Copy link
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.

@facebook-github-bot
Copy link
Contributor

@anjali411 merged this pull request in 4bb5d33.

@xuhdev xuhdev deleted the is-numpy-scalar branch September 3, 2020 09:04
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 module: numpy Related to numpy support, and also numpy compatibility of our operators 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.

5 participants