TensorNetwork icon indicating copy to clipboard operation
TensorNetwork copied to clipboard

Add power to each backend and as a Tensor method in linalg

Open alewis opened this issue 5 years ago • 28 comments

We should support the numpy function np.power.

Edit: it would also be good to support elementwise exponentiation of Tensor using the ** operator

alewis avatar Sep 28 '20 12:09 alewis

@wkostuch how's this sound? I could help you through it if you want a good way to get started!

alewis avatar Sep 28 '20 12:09 alewis

@alewis, this looks good, thanks! I'd love to have a go at it.

I'm excited to (hopefully) start contributing, and if something comes up I'll be sure to contact you.

wkostuch avatar Sep 28 '20 21:09 wkostuch

Great! The first step would be to add the function to the abstract backend.

To do this, first fork TensorNetwork, track the google repo as a remote, and start a new branch for your modification (lemme know if you need help with that part).

Within this new branch, go to tensornetwork/backends/abstract_backend.py, where various "empty" backend functions are implemented. These implementations just raise NotImplementedError when called. So the first step would be to add an equivalent for power.

Next you check backend_tests.py, which contains a bunch of tests that make sure NotImplementedError is indeed called, and add your own for the relevant function. Make sure your tests work: cd tensornetwork, pytest ./backends/backend_tests.py.

That would be enough for your first PR! Commit, push, and navigate back to this repo, where there should be an option to merge your recent push.

alewis avatar Sep 28 '20 21:09 alewis

So a pull request with just the "empty" function (and corresponding test) that raises an error and then go from there after that?

I wrote the code for that, but then when trying to run the tests found out that one of the dependencies doesn't play nice with Windows, so I'm in the midst of setting up a Linux environment to get around that. Once that's done I'll run the tests and keep on working. Thanks again for all the guidance, I really appreciate it!

wkostuch avatar Sep 30 '20 03:09 wkostuch

Yes that's right, just the empty function at first. In general we try to keep pull requests to 200 lines or shorter, excluding test code, although sometimes this is not possible. That's because all the pull requests are to be peer reviewed, and it's hard for the reviewer to catch things when they are longer than this.

Regarding Windows, if you are using Windows 10 there is a so called "Windows Subsystem for Linux" that essentially allows you to run Ubuntu inside Windows as an app. I used to develop this way - it works pretty well and while some setup is required, it's a lot easier than setting up a partition and so on, especially on a laptop with a GPU. There's instructions to get it working here: https://docs.microsoft.com/en-us/windows/wsl/install-win10

alewis avatar Sep 30 '20 14:09 alewis

Makes sense! I got WSL running, so the empty function and test are now waiting for a review.

wkostuch avatar Oct 01 '20 02:10 wkostuch

The next step would be to add the same function, and test(s) of it, to the numpy backend, using the existing functions in numpy_backend.py and the test file as an example. This next needs to be repeated for TensorFlow, PyTorch, and eventually Symmetric, though Symmetric might require some specialized physics knowledge and perhaps would be best left for later.

alewis avatar Oct 03 '20 21:10 alewis

Add the same empty function and tests as before or go ahead and put each backend's version of power in?

wkostuch avatar Oct 04 '20 18:10 wkostuch

Yeah, so this time you'd want to actually add the function. I'd recommend you make each backend a separate PR.

alewis avatar Oct 05 '20 14:10 alewis

Hello there @alewis! I would like to help with this, this is my first time contributing.

LuiGiovanni avatar Dec 01 '20 20:12 LuiGiovanni

Alright, maybe you could do the Jax version?

@wkostuch

alewis avatar Dec 01 '20 23:12 alewis

Sure thing! @alewis I'm sorry but this is the first time of me working on this project just to confirm I will work on the files in the Jax folder found inside the back end folder right?

TensorNetwork/tensornetwork/backends/jax

LuiGiovanni avatar Dec 04 '20 00:12 LuiGiovanni

That's correct

mganahl avatar Dec 04 '20 07:12 mganahl

Perfect, I'll get on it right away thank you! btw is there a place where we can communicate? Like matrix, slack, discord, or something? @mganahl @alewis

LuiGiovanni avatar Dec 04 '20 18:12 LuiGiovanni

Thanks. I think github should be sufficient.

mganahl avatar Dec 04 '20 18:12 mganahl

Perfect, I just want to confirm what I'm going to do, which is basically to implement the power function that wkostuch added to the abstract files, but implement them in the Jax files right? each file would be its own PR? @mganahl @alewis

LuiGiovanni avatar Dec 04 '20 18:12 LuiGiovanni

that's right. There is only jax_backend.py that needs to be updated.

mganahl avatar Dec 04 '20 18:12 mganahl

Perfect! Thanks so much for the help!

LuiGiovanni avatar Dec 04 '20 19:12 LuiGiovanni

@mganahl @alewis I also need to update the jax_backend_test.py right?

EDIT: I am having some issues with the jax_backend_test.py file it keeps reporting for the missing jax module but I have already installed it with pip3 but the issue persist.

LuiGiovanni avatar Dec 04 '20 23:12 LuiGiovanni

Hi there! I believe that the power function has been implemented, is there anywhere else where this function needs to be implemented? @mganahl @alewis

LuiGiovanni avatar Dec 05 '20 17:12 LuiGiovanni

Hello @alewis @mganahl After viewing all files in the backend, I notice the power method is missing in both the Pytorch and Symmetric backends, should I implement them there as well?

LuiGiovanni avatar Dec 08 '20 19:12 LuiGiovanni

Sure, give it a shot!

mganahl avatar Dec 08 '20 19:12 mganahl

Hello @mganahl @alewis I was running pytest for pytorch_backend_test.py and I am receiving this error message at line 16, this is the output I get:

============================================================= test session starts ============================================================== platform darwin -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 rootdir: /Users/nsl-glopez/Documents/GithubOpenSource/TensorNetwork collected 173 items

pytorch_backend_test.py Fatal Python error: Aborted

Current thread 0x000000010c1455c0 (most recent call first): File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/torch/functional.py", line 936 in tensordot File "/Users/nsl-glopez/Documents/GithubOpenSource/TensorNetwork/tensornetwork/backends/pytorch/pytorch_backend.py", line 46 in tensordot File "/Users/nsl-glopez/Documents/GithubOpenSource/TensorNetwork/tensornetwork/backends/pytorch/pytorch_backend_test.py", line 16 in test_tensordot File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/python.py", line 184 in pytest_pyfunc_call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 84 in File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 93 in _hookexec File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/hooks.py", line 286 in call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/python.py", line 1627 in runtest File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/runner.py", line 163 in pytest_runtest_call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 84 in File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 93 in _hookexec File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/hooks.py", line 286 in call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/runner.py", line 256 in File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/runner.py", line 310 in from_call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/runner.py", line 255 in call_runtest_hook File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/runner.py", line 216 in call_and_report File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/runner.py", line 127 in runtestprotocol File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/runner.py", line 110 in pytest_runtest_protocol File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 84 in File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 93 in _hookexec File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/hooks.py", line 286 in call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/main.py", line 338 in pytest_runtestloop File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 84 in File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 93 in _hookexec File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/hooks.py", line 286 in call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/main.py", line 313 in _main File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/main.py", line 257 in wrap_session File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/main.py", line 306 in pytest_cmdline_main File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 84 in File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/manager.py", line 93 in _hookexec File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/pluggy/hooks.py", line 286 in call File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/config/init.py", line 164 in main File "/Users/nsl-glopez/miniconda3/lib/python3.8/site-packages/_pytest/config/init.py", line 187 in console_main File "/Users/nsl-glopez/miniconda3/bin/pytest", line 8 in [1] 9285 abort pytest pytorch_backend_test.py

LuiGiovanni avatar Dec 15 '20 23:12 LuiGiovanni

Hard to say from this error message except that the call to torch.tensordot is for some reason failing. Did you change anything about either pytorch_backend.tensordot, the tensordot test, or the test file?

alewis avatar Dec 16 '20 17:12 alewis

Hi there @alewis. The only changes I had made to the PyTorch backend was the power function and the test for it, I just reverted those changes, and the error persists, something else is the culprit since I also compared the master file from GitHub to my own from my branch and they are the same.

LuiGiovanni avatar Dec 17 '20 20:12 LuiGiovanni

can you open a pull request with the changes? I'll can have look tommorw

mganahl avatar Dec 17 '20 22:12 mganahl

Sure thing, thanks!

LuiGiovanni avatar Dec 17 '20 22:12 LuiGiovanni

Nice one !

KLN888 avatar Aug 26 '21 06:08 KLN888