Skip to content

Add type annotation logic for complex numbers#50884

Closed
anjali411 wants to merge 8 commits intogh/anjali411/88/basefrom
gh/anjali411/88/head
Closed

Add type annotation logic for complex numbers#50884
anjali411 wants to merge 8 commits intogh/anjali411/88/basefrom
gh/anjali411/88/head

Conversation

@anjali411
Copy link
Copy Markdown
Contributor

@anjali411 anjali411 commented Jan 21, 2021

Stack from ghstack:

Differential Revision:

Differential Revision: D26086963

@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 33e9e2a (more details on the Dr. CI page):


  • 1/1 failures introduced in this PR

🕵️ 1 new failure recognized by patterns

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

See CircleCI build pytorch_linux_xenial_py3_6_gcc5_4_test (1/1)

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

Jan 26 22:48:53 AssertionError: mypy failed: torch/fx/experimental/accelerated_graph_module.py:4: error: Cannot find implementation or library stub for module named 'glow.fb.fx_glow_binding.fx_glow' [import]
Jan 26 22:47:34 Test results will be stored in test-reports/python-unittest
Jan 26 22:47:42   test_doc_examples (__main__.TestTypeHints) ... ok (7.820s)
Jan 26 22:48:53   test_run_mypy (__main__.TestTypeHints) ... FAIL (71.291s)
Jan 26 22:48:53 
Jan 26 22:48:53 ======================================================================
Jan 26 22:48:53 FAIL [71.291s]: test_run_mypy (__main__.TestTypeHints) [mypy.ini]
Jan 26 22:48:53 ----------------------------------------------------------------------
Jan 26 22:48:53 Traceback (most recent call last):
Jan 26 22:48:53   File "test_type_hints.py", line 171, in test_run_mypy
Jan 26 22:48:53     self.fail(f"mypy failed: {stdout} {stderr}")
Jan 26 22:48:53 AssertionError: mypy failed: torch/fx/experimental/accelerated_graph_module.py:4: error: Cannot find implementation or library stub for module named 'glow.fb.fx_glow_binding.fx_glow'  [import]
Jan 26 22:48:53 torch/fx/experimental/accelerated_graph_module.py:4: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Jan 26 22:48:53 torch/fx/experimental/accelerated_graph_module.py:4: error: Cannot find implementation or library stub for module named 'glow'  [import]
Jan 26 22:48:53 torch/fx/experimental/accelerated_graph_module.py:4: error: Cannot find implementation or library stub for module named 'glow.fb'  [import]
Jan 26 22:48:53 torch/fx/experimental/accelerated_graph_module.py:4: error: Cannot find implementation or library stub for module named 'glow.fb.fx_glow_binding'  [import]
Jan 26 22:48:53 Found 4 errors in 1 file (checked 1212 source files)
Jan 26 22:48:53  
Jan 26 22:48:53 
Jan 26 22:48:53 ----------------------------------------------------------------------
Jan 26 22:48:53 Ran 2 tests in 79.111s
Jan 26 22:48:53 

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 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! Do you plan to support constant literals in TorchScript code in this diff? If not, I think it is ready to land. If so, I think some more changes are required.

Comment thread test/test_complex.py Outdated
Comment thread test/test_complex.py Outdated
return py::cast<int64_t>(obj);
} else if (py::isinstance<py::float_>(obj)) {
return py::cast<double>(obj);
} else if (PyComplex_CheckExact(obj.ptr())) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dumb question but why does this line not say something like py::isinstance<py::complex_>(...) like all the others?

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.

Complex type is not available in pybind (see doc here: https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html#accessing-python-libraries-from-c) so I just pass the underlying PyObject* to PyComplex_CheckExact to check whether it's complex

Comment thread torch/jit/annotations.py Outdated
Comment on lines +308 to +309
if inspect.isclass(ann) and issubclass(ann, builtins.complex):
return 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.

Another dumb question, why is this not ann is complex?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, is there a usecase for creating subclasses of complex?

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.

complex is a python class hence the isclass call. umm no there's no use specific case of creating subclasses of complex

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

But aren't int and float classes too?

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.

ok wait you are right ann is complex works!

Comment on lines 251 to 252
case TypeKind::ComplexDoubleType:
return parseSingleConstant(arg_type->kind());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

parseSingleConstant needs to be updated to handle complex numbers. I would either get rid of it and do it in a follow-up PR or add a test for it (and update parseSingleConstant and other code as needed).

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.

I'll just remove this change for the current PR and add it separately

@anjali411
Copy link
Copy Markdown
Contributor Author

Nice! Do you plan to support constant literals in TorchScript code in this diff? If not, I think it is ready to land. If so, I think some more changes are required.

no I'll just make a separate PR for that! also, addressed all the comments and updated the PR

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.

🚀

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in b6eaca9.

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

Test Plan: Imported from OSS

Reviewed By: heitorschueroff

Differential Revision: D26086963

fbshipit-source-id: f103f7f529d63d701c4f17862e30eafbab7d0c68
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants