Skip to content

Add serialization logic for complex numbers#50885

Closed
anjali411 wants to merge 10 commits intogh/anjali411/89/basefrom
gh/anjali411/89/head
Closed

Add serialization logic for complex numbers#50885
anjali411 wants to merge 10 commits intogh/anjali411/89/basefrom
gh/anjali411/89/head

Conversation

@anjali411
Copy link
Copy Markdown
Contributor

@anjali411 anjali411 commented Jan 21, 2021

Stack from ghstack:

Differential Revision: D26094906

@facebook-github-bot facebook-github-bot added cla signed oncall: jit Add this issue/PR to JIT oncall triage queue labels Jan 21, 2021
@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Jan 21, 2021

💊 CI failures summary and remediations

As of commit 3b5f8da (more details on the Dr. CI page):


  • 6/6 failures possibly* introduced in this PR
    • 1/6 non-CircleCI failure(s)

🕵️ 5 new failures recognized by patterns

The following CI failures do not appear to be due to upstream breakages:

See CircleCI build pytorch_linux_xenial_py3_clang5_asan_test1 (1/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

Jan 27 19:40:18 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule" [override]
Jan 27 19:38:33 Test results will be stored in test-reports/python-unittest
Jan 27 19:38:44   test_doc_examples (__main__.TestTypeHints) ... ok (11.505s)
Jan 27 19:40:18   test_run_mypy (__main__.TestTypeHints) ... FAIL (93.383s)
Jan 27 19:40:18 
Jan 27 19:40:18 ======================================================================
Jan 27 19:40:18 FAIL [93.383s]: test_run_mypy (__main__.TestTypeHints) [mypy.ini]
Jan 27 19:40:18 ----------------------------------------------------------------------
Jan 27 19:40:18 Traceback (most recent call last):
Jan 27 19:40:18   File "test_type_hints.py", line 171, in test_run_mypy
Jan 27 19:40:18     self.fail(f"mypy failed: {stdout} {stderr}")
Jan 27 19:40:18 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule"  [override]
Jan 27 19:40:18 Found 1 error in 1 file (checked 1211 source files)
Jan 27 19:40:18  
Jan 27 19:40:18 
Jan 27 19:40:18 ----------------------------------------------------------------------
Jan 27 19:40:18 Ran 2 tests in 104.888s
Jan 27 19:40:18 
Jan 27 19:40:18 FAILED (failures=1)
Jan 27 19:40:18 
Jan 27 19:40:18 Generating XML reports...
Jan 27 19:40:18 Generated XML report: test-reports/python-unittest/TEST-TestTypeHints-20210127193833.xml

See CircleCI build pytorch_linux_bionic_py3_8_gcc9_coverage_test1 (2/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

Jan 27 19:43:47 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule" [override]
Jan 27 19:42:23   test_run_mypy (__main__.TestTypeHints)
Jan 27 19:43:47 Runs mypy over all files specified in our mypy configs ... FAIL (83.491s)
Jan 27 19:43:47 
Jan 27 19:43:47 ======================================================================
Jan 27 19:43:47 FAIL [83.491s]: test_run_mypy (__main__.TestTypeHints) [mypy.ini]
Jan 27 19:43:47 Runs mypy over all files specified in our mypy configs
Jan 27 19:43:47 ----------------------------------------------------------------------
Jan 27 19:43:47 Traceback (most recent call last):
Jan 27 19:43:47   File "test_type_hints.py", line 171, in test_run_mypy
Jan 27 19:43:47     self.fail(f"mypy failed: {stdout} {stderr}")
Jan 27 19:43:47 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule"  [override]
Jan 27 19:43:47 Found 1 error in 1 file (checked 1211 source files)
Jan 27 19:43:47  
Jan 27 19:43:47 
Jan 27 19:43:47 ----------------------------------------------------------------------
Jan 27 19:43:47 Ran 2 tests in 92.598s
Jan 27 19:43:47 
Jan 27 19:43:47 FAILED (failures=1)
Jan 27 19:43:47 
Jan 27 19:43:47 Generating XML reports...
Jan 27 19:43:47 Generated XML report: test-reports/python-unittest/TEST-TestTypeHints-20210127194214.xml

See CircleCI build pytorch_linux_xenial_py3_6_gcc5_4_test (3/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

Jan 27 19:37:44 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule" [override]
Jan 27 19:36:15 Test results will be stored in test-reports/python-unittest
Jan 27 19:36:24   test_doc_examples (__main__.TestTypeHints) ... ok (9.248s)
Jan 27 19:37:44   test_run_mypy (__main__.TestTypeHints) ... FAIL (80.512s)
Jan 27 19:37:44 
Jan 27 19:37:44 ======================================================================
Jan 27 19:37:44 FAIL [80.512s]: test_run_mypy (__main__.TestTypeHints) [mypy.ini]
Jan 27 19:37:44 ----------------------------------------------------------------------
Jan 27 19:37:44 Traceback (most recent call last):
Jan 27 19:37:44   File "test_type_hints.py", line 171, in test_run_mypy
Jan 27 19:37:44     self.fail(f"mypy failed: {stdout} {stderr}")
Jan 27 19:37:44 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule"  [override]
Jan 27 19:37:44 Found 1 error in 1 file (checked 1211 source files)
Jan 27 19:37:44  
Jan 27 19:37:44 
Jan 27 19:37:44 ----------------------------------------------------------------------
Jan 27 19:37:44 Ran 2 tests in 89.761s
Jan 27 19:37:44 
Jan 27 19:37:44 FAILED (failures=1)
Jan 27 19:37:44 
Jan 27 19:37:44 Generating XML reports...
Jan 27 19:37:44 Generated XML report: test-reports/python-unittest/TEST-TestTypeHints-20210127193615.xml

See CircleCI build pytorch_linux_bionic_py3_6_clang9_test (4/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

Jan 27 19:33:33 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule" [override]
Jan 27 19:32:06 ----------------------------------------------------------------------
Jan 27 19:32:15   test_doc_examples (__main__.TestTypeHints) ... ok (9.232s)
Jan 27 19:33:33   test_run_mypy (__main__.TestTypeHints) ... FAIL (78.159s)
Jan 27 19:33:33 
Jan 27 19:33:33 ======================================================================
Jan 27 19:33:33 FAIL [78.159s]: test_run_mypy (__main__.TestTypeHints) [mypy.ini]
Jan 27 19:33:33 ----------------------------------------------------------------------
Jan 27 19:33:33 Traceback (most recent call last):
Jan 27 19:33:33   File "test_type_hints.py", line 171, in test_run_mypy
Jan 27 19:33:33     self.fail(f"mypy failed: {stdout} {stderr}")
Jan 27 19:33:33 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule"  [override]
Jan 27 19:33:33 Found 1 error in 1 file (checked 1211 source files)
Jan 27 19:33:33  
Jan 27 19:33:33 
Jan 27 19:33:33 ----------------------------------------------------------------------
Jan 27 19:33:33 Ran 2 tests in 87.392s
Jan 27 19:33:33 
Jan 27 19:33:33 FAILED (failures=1)
Jan 27 19:33:33 
Jan 27 19:33:33 Generating XML reports...
Jan 27 19:33:33 Generated XML report: test-reports/python-unittest/TEST-TestTypeHints-20210127193206.xml

See CircleCI build pytorch_linux_xenial_cuda10_2_cudnn7_py3_gcc7_test1 (5/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

Jan 27 19:56:38 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule" [override]
Jan 27 19:55:13 ----------------------------------------------------------------------
Jan 27 19:55:22   test_doc_examples (__main__.TestTypeHints) ... ok (8.845s)
Jan 27 19:56:38   test_run_mypy (__main__.TestTypeHints) ... FAIL (75.437s)
Jan 27 19:56:38 
Jan 27 19:56:38 ======================================================================
Jan 27 19:56:38 FAIL [75.437s]: test_run_mypy (__main__.TestTypeHints) [mypy.ini]
Jan 27 19:56:38 ----------------------------------------------------------------------
Jan 27 19:56:38 Traceback (most recent call last):
Jan 27 19:56:38   File "test_type_hints.py", line 171, in test_run_mypy
Jan 27 19:56:38     self.fail(f"mypy failed: {stdout} {stderr}")
Jan 27 19:56:38 AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule"  [override]
Jan 27 19:56:38 Found 1 error in 1 file (checked 1211 source files)
Jan 27 19:56:38  
Jan 27 19:56:38 
Jan 27 19:56:38 ----------------------------------------------------------------------
Jan 27 19:56:38 Ran 2 tests in 84.282s
Jan 27 19:56:38 
Jan 27 19:56:38 FAILED (failures=1)
Jan 27 19:56:38 
Jan 27 19:56:38 Generating XML reports...
Jan 27 19:56:38 Generated XML report: test-reports/python-unittest/TEST-TestTypeHints-20210127195513.xml

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 to the (internal) Dr. CI Users group.

anjali411 added a commit that referenced this pull request Jan 21, 2021
ghstack-source-id: e82d699
Pull Request resolved: #50885
anjali411 added a commit that referenced this pull request Jan 21, 2021
ghstack-source-id: 792bb4e
Pull Request resolved: #50885
anjali411 added a commit that referenced this pull request Jan 22, 2021
ghstack-source-id: e5d3338
Pull Request resolved: #50885
anjali411 added a commit that referenced this pull request Jan 22, 2021
ghstack-source-id: d3b7b1f
Pull Request resolved: #50885
anjali411 added a commit that referenced this pull request Jan 26, 2021
ghstack-source-id: 48f1434
Pull Request resolved: #50885
@anjali411 anjali411 added the module: complex Related to complex number support in PyTorch label Jan 26, 2021
Copy link
Copy Markdown

@SplitInfinity SplitInfinity left a comment

Choose a reason for hiding this comment

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

Nice! We should add a test for build_complexdoublelist or remove the deserialization logic for it. Other than that, I think this is ready to land.

Comment thread test/test_complex.py Outdated
Comment on lines +32 to +35
buffer = io.BytesIO()
torch.jit.save(scripted, buffer)
buffer.seek(0)
loaded = torch.jit.load(buffer)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think you can use self.getExportImportCopy here, which will save and load from a buffer for you.

Comment on lines +523 to +524
} else if (class_name == "build_complexdoublelist") {
elem_type = ComplexDoubleType::get();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we have tests for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good catch! this change shouldn't be here because complex lists are not supported yet

anjali411 added a commit that referenced this pull request Jan 26, 2021
ghstack-source-id: 62a589d
Pull Request resolved: #50885
@anjali411
Copy link
Copy Markdown
Contributor Author

@SplitInfinity updated the PR based on the comments!

Copy link
Copy Markdown

@SplitInfinity SplitInfinity left a comment

Choose a reason for hiding this comment

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

Nice!

Comment thread test/test_complex.py Outdated
Comment thread test/test_complex.py Outdated
def test_pickle(self):
class ComplexModule(torch.nn.Module):
def __init__(self):
super(ComplexModule, self).__init__()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
super(ComplexModule, self).__init__()
super().__init__()

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@anjali411 merged this pull request in 2de4ecd.

@mruberry
Copy link
Copy Markdown
Collaborator

Unlanding. This appears to have broken several builds. Example failure:

test_run_mypy [mypy.ini] - TestTypeHints
test_type_hints.py

Traceback (most recent call last):
  File "test_type_hints.py", line 171, in test_run_mypy
    self.fail(f"mypy failed: {stdout} {stderr}")
AssertionError: mypy failed: test/test_complex.py:21: error: Signature of "forward" incompatible with supertype "ScriptModule"  [override]
Found 1 error in 1 file (checked 1211 source files)

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been reverted by dfdb154.

case StorageType::Kind:
case NumberType::Kind:
case FloatType::Kind:
case ComplexDoubleType::Kind:
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.

I guess this already existed but this naming looks inconsistent -- aren't these referring to python types, so this should be ComplexType?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I was under the impression that there will eventually be a ComplexFloatType as well but if not, then I agree that it should be ComplexType.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm I didn't know that we follow the python naming here. yeah should be ComplexType!

// Unpickle a tensor
bool quantized = class_name == "_rebuild_qtensor";
rebuildTensor(quantized);
} else if (module_name == "builtins" && class_name == "complex") {
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.

above I see some stuff about floatlists -- we don't need to handle complexlists because we don't define them in e.g. native_functions yet?

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.

it's also a little unclear to me if this is handling endianness -- do you know?

Copy link
Copy Markdown

@SplitInfinity SplitInfinity Jan 28, 2021

Choose a reason for hiding this comment

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

The real and imaginary parts of the complex number are pickled using pushIValue which seems to take care of it endianness. See the relevant excerpts of the pickling/unpickling code below.

void Pickler::pushDouble(double value) {
  push<PickleOpCode>(PickleOpCode::BINFLOAT);
  // Python pickle format is big endian, swap.
  push<double>(swapDouble(value));
}
double Unpickler::readFloat() {
  AT_ASSERT(sizeof(double) == 8);
  double big_endian = read<double>();
  double little_endian;

  // Pickle floats are big endian, so reverse the bytes
  auto big_endian_ptr = reinterpret_cast<const char*>(&big_endian);
  std::reverse_copy(
      big_endian_ptr,
      big_endian_ptr + sizeof(big_endian),
      reinterpret_cast<char*>(&little_endian));

  return little_endian;
}

@facebook-github-bot facebook-github-bot deleted the gh/anjali411/89/head branch January 31, 2021 15:18
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary: Pull Request resolved: pytorch#50885

Test Plan: Imported from OSS

Reviewed By: SplitInfinity

Differential Revision: D26094906

Pulled By: anjali411

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

Labels

cla signed Merged module: complex Related to complex number support in PyTorch oncall: jit Add this issue/PR to JIT oncall triage queue Reverted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants