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

Fix decode float #203

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gmuraru
Copy link
Contributor

@gmuraru gmuraru commented Dec 12, 2020

Types of changes

Use true_div when decoding a value - this would give "bad" values when decoding float encoded numbers.
Ex: fp.encode(4.6) --> 301465 --> fp.decode(301465) --> 4

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Docs change / refactoring / dependency upgrade

Motivation and Context / Related issue

Correct decoding of float encoded values

How Has This Been Tested (if it applies)

  • Changed the tests to take into consideration float values

Checklist

  • The documentation is up-to-date with the changes I made.
  • I have read the CONTRIBUTING document and completed the CLA (see CONTRIBUTING).
  • All tests passed, and additional code has been covered with new tests.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 12, 2020
tensor = tensor.float()
if reference.dtype == torch.long:
reference = reference.float()
test_passed = torch.allclose(tensor, reference, rtol=1/2 ** 15)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Q: Does this look strange? (to use rtol with 1/2 ** 15)
I think this might also be changed to 1e-4<-- this is the biggest power of 10 that we could use to "catch" the precision given by 2**16 (< 10^5)

@gmuraru gmuraru changed the title Fix decode Fix decode float Dec 12, 2020
@@ -64,7 +64,7 @@ def decode(self, tensor):
assert is_int_tensor(tensor), "input must be a LongTensor"
if self._scale > 1:
correction = (tensor < 0).long()
dividend = tensor / self._scale - correction
dividend = tensor // self._scale - correction
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for noticing this. The definitions of truediv has been changed recently in PyTorch for the torch.long dtype. The previous behavior was identical to floordiv.

knottb pushed a commit to gmuraru/CrypTen that referenced this pull request Dec 18, 2020
Summary:
Pull Request resolved: fairinternal/CrypTen#203

- Modified crypten.load to allow the input `f` to be None.
- Added assert to ensure exactly  one of `f` or `preloaded` is None
- Updated unit tests to ensure preloaded loads correctly.

Reviewed By: knottb

Differential Revision: D21212000

fbshipit-source-id: 82042e208a3a95328ec5472dba9ce964d8033075
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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants