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

Simplify backend switch #370

Merged
merged 11 commits into from
Apr 7, 2021
Merged

Simplify backend switch #370

merged 11 commits into from
Apr 7, 2021

Conversation

stavros11
Copy link
Member

Simplifies the backend switching mechanism by creating a seperate backend class for each of the einsum backends. For example currently we have a single NumpyBackend class that handles both numpy_defaulteinsum and numpy_matmuleinsum. In this PR I seperate the backends by create different classes NumpyDefaultEinsumBackend and NumpyMatmulEinsumBackend. I believe this allows a cleaner switching function and may be easier to maintain if we add more backends either for simulation or hardware.

There is no change from the user perspective, that is the user can still switch the backends using qibo.set_backend("custom"), qibo.set_backend("defaulteinsum"), qibo.set_backend("numpy_defaulteinsum"), etc..

@stavros11 stavros11 requested a review from scarrazza April 1, 2021 07:25
@codecov
Copy link

codecov bot commented Apr 1, 2021

Codecov Report

Merging #370 (b004548) into master (6642e00) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #370   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           78        78           
  Lines        12733     12741    +8     
=========================================
+ Hits         12733     12741    +8     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/qibo/backends/abstract.py 100.00% <ø> (ø)
src/qibo/tests/test_variational.py 100.00% <ø> (ø)
src/qibo/backends/__init__.py 100.00% <100.00%> (ø)
src/qibo/backends/numpy.py 100.00% <100.00%> (ø)
src/qibo/backends/tensorflow.py 100.00% <100.00%> (ø)
src/qibo/core/states.py 100.00% <100.00%> (ø)
src/qibo/tests/conftest.py 100.00% <100.00%> (ø)
src/qibo/tests_new/test_backends_agreement.py 100.00% <100.00%> (ø)
src/qibo/tests_new/test_backends_init.py 100.00% <100.00%> (ø)
...ibo/tests_new/test_core_circuit_backpropagation.py 100.00% <100.00%> (ø)
... and 2 more

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 6642e00...b004548. Read the comment docs.

Copy link
Member

@scarrazza scarrazza left a comment

Choose a reason for hiding this comment

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

Thanks looks good!

"numpy": NumpyDefaultEinsumBackend,
"numpy_defaulteinsum": NumpyDefaultEinsumBackend,
"numpy_matmuleinsum": NumpyMatmulEinsumBackend
}
Copy link
Member

@scarrazza scarrazza Apr 5, 2021

Choose a reason for hiding this comment

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

I am checking this PR, and I think we could be a little bit more specific for this dict.
What do you think if we include some docs so when you raise an error we can show some message like:

ValueError: Unknown backend *. Please select one of the available backends: 
- custom: uses precompiled primitives, fastest simulation engine...
- tensorflow: same/alias as custom...
- defaulteinsum: uses tf primitives, and einsum, faster on GPU...
- matmuleinsum: ...

Copy link
Member

Choose a reason for hiding this comment

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

Another motivation to do that is the follow: suppose I set qibo.set_backend("numpy"), then when calling qibo.get_backend() it prints numpy_defaulteinsum, so this may confuse the user, and in the worst case break some programmatic check.

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated the error when an invalid backend is given so that a list of the available backends is printed. You can check the error message by passing a wrong value on purpose (for example do qibo.set_backend("test")).

Regarding the second point, I agree that it may be a bit confusing that numpy and numpy_defaulteinsum are essentially the same backend (same for custom and tensorflow). The reason I did it this way is because I wanted to enable the user to do qibo.set_backend("numpy") as a shortcut for qibo.set_backend("numpy_defaulteinsum"). If you think it is confusing we can remove numpy and keep only the numpy_defaulteinsum option.

Copy link
Member

@scarrazza scarrazza left a comment

Choose a reason for hiding this comment

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

@stavros11 just few comments concerning the layout, otherwise looks good to merge.

calc_backend, gate_backend = gate_backend
if gate_backend == "custom":
calc_backend = "tensorflow"
bk = _construct_backend(calc_backend)
K.assign(bk)
Copy link
Member

Choose a reason for hiding this comment

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

If we replace this line with K = bk the bug in #374 seems to be fixed.

This sounds like a problem with the init global allocation and the _CONSTRUCTED_BACKENDS.
One possibility to avoid this issue consists in always reuse the backends from _CONSTRUCTED_BACKENDS.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just pushed some updates in the backend switching mechanism that fixes the device issue and avoids the .assign function. I think the idea is close to one of the solutions we were discussing earlier today. I still need to polish several things, fix the tests and your above comments but let me know if you have any comments on this implementation.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, looks pretty good, and indeed it is quite close to my draft in #374. I think we can keep your approach (without the new method), given that you already expose all required features!

@stavros11
Copy link
Member Author

Thanks for checking. I made some additional updates regarding your previous comments above. If CI passes I think this PR is okay now.

@scarrazza scarrazza merged commit 84d8e41 into master Apr 7, 2021
@delete-merged-branch delete-merged-branch bot deleted the switcher branch April 7, 2021 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants