{"action":"created","comment":{"url":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/comments/211764039","pull_request_review_id":148255505,"id":211764039,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxMTc2NDAzOQ==","diff_hunk":"@@ -0,0 +1,36 @@\n+=====================\n+Sharing Intermediates\n+=====================\n+\n+If you want to compute multiple similar contractions with common terms, you can embed them in a :func:`~opt_einsum.shared_intermediates` context. Computations of subexpressions in this context will be memoized, and will be garbage collected when the contexts exits.\n+\n+For example, suppose we want to compute marginals at each point in a factor chain:\n+\n+.. code:: python\n+\n+    >>> inputs = 'ab,bc,cd,de,ef'\n+    >>> factors = [np.random.rand(4, 4) for _ in range(5)]\n+    >>> marginals = {output: contract('{}->{}'.format(inputs, output), *factors)\n+    >>>              for output in 'abcdef'}\n+\n+To share this computation, we can perform all contractions in a shared context\n+\n+.. code:: python\n+\n+    >>> with shared_intermediates():\n+    >>>     marginals = {output: contract('{}->{}'.format(inputs, output), *factors)\n+    >>>                  for output in 'abcdef'}","path":"docs/source/sharing_intermediates.rst","position":22,"original_position":22,"commit_id":"0139c75939945821b61712638429358df03d145f","original_commit_id":"0139c75939945821b61712638429358df03d145f","user":{"login":"dgasmith","id":1769841,"node_id":"MDQ6VXNlcjE3Njk4NDE=","avatar_url":"https://avatars2.githubusercontent.com/u/1769841?v=4","gravatar_id":"","url":"https://api.github.com/users/dgasmith","html_url":"https://github.com/dgasmith","followers_url":"https://api.github.com/users/dgasmith/followers","following_url":"https://api.github.com/users/dgasmith/following{/other_user}","gists_url":"https://api.github.com/users/dgasmith/gists{/gist_id}","starred_url":"https://api.github.com/users/dgasmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgasmith/subscriptions","organizations_url":"https://api.github.com/users/dgasmith/orgs","repos_url":"https://api.github.com/users/dgasmith/repos","events_url":"https://api.github.com/users/dgasmith/events{/privacy}","received_events_url":"https://api.github.com/users/dgasmith/received_events","type":"User","site_admin":false},"body":"Might be worth showing a quick timing comparison on bigger tensors with an explicit demonstration of the contractions done.","created_at":"2018-08-21T21:20:25Z","updated_at":"2018-08-21T21:24:11Z","html_url":"https://github.com/dgasmith/opt_einsum/pull/43#discussion_r211764039","pull_request_url":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/comments/211764039"},"html":{"href":"https://github.com/dgasmith/opt_einsum/pull/43#discussion_r211764039"},"pull_request":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43"}}},"pull_request":{"url":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43","id":209417354,"node_id":"MDExOlB1bGxSZXF1ZXN0MjA5NDE3MzU0","html_url":"https://github.com/dgasmith/opt_einsum/pull/43","diff_url":"https://github.com/dgasmith/opt_einsum/pull/43.diff","patch_url":"https://github.com/dgasmith/opt_einsum/pull/43.patch","issue_url":"https://api.github.com/repos/dgasmith/opt_einsum/issues/43","number":43,"state":"open","locked":false,"title":"Implement shared_intermediates context manager","user":{"login":"fritzo","id":648532,"node_id":"MDQ6VXNlcjY0ODUzMg==","avatar_url":"https://avatars0.githubusercontent.com/u/648532?v=4","gravatar_id":"","url":"https://api.github.com/users/fritzo","html_url":"https://github.com/fritzo","followers_url":"https://api.github.com/users/fritzo/followers","following_url":"https://api.github.com/users/fritzo/following{/other_user}","gists_url":"https://api.github.com/users/fritzo/gists{/gist_id}","starred_url":"https://api.github.com/users/fritzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fritzo/subscriptions","organizations_url":"https://api.github.com/users/fritzo/orgs","repos_url":"https://api.github.com/users/fritzo/repos","events_url":"https://api.github.com/users/fritzo/events{/privacy}","received_events_url":"https://api.github.com/users/fritzo/received_events","type":"User","site_admin":false},"body":"Resolves #9 \r\n\r\n## Description\r\n\r\nThis implements a `shared_intermediates` context manager, within which multiple calls to `contract` will share intermediate computations. This is very useful e.g. in message passing algorithms. The first draft of this PR is copied directly [from Pyro](https://github.com/uber/pyro/blob/1b1910eadff80178197879bf847beb4443fd9014/pyro/ops/einsum/shared.py), where we are using sharing for probabilistic inference in graphical models.\r\n\r\nThe implementation uses a special internal backend `opt_einsum.backends.shared`. When a sharing context is opened\r\n```py\r\nwith shared_intermediates():\r\n    contract(..., backend='foo')\r\n    contract(..., backend='foo')\r\n```\r\na special `handle_sharing()` context is activated inside the `contract` function, temporarily switching to the `shared` backend, and setting the original backend `foo` in a global `shared._CURRENT_BACKEND` variable. The sharing backend then performs all the original `foo` operations and also memoizes them.\r\n\r\nOne design choice is to memoize by `id()` rather than by value. This makes sense from a computational perspective (equality comparison is expensive), but requires a bit more care by users.\r\n\r\nA second design choice is to expose the memoization `cache` to users. This makes it easy to share intermediates by passing the cache around, as is necessary when it is difficult to perform all computation in a context:\r\n```py\r\nwith shared_intermediates() as cache:\r\n    contract(...)\r\n...pass control elsewhere...\r\nwith shared_intermediates(cache):  # <-- reuse previous cache\r\n    contract(...)\r\ndel cache  # <-- intermediates will be garbage collected\r\n```\r\n\r\n## Todos\r\n- [x] Cache type conversions e.g. `numpy`->`torch`->`numpy` so args can be compared by id.\r\n- [x] fix failing tests\r\n- [x] add tests that pass around the cache\r\n- [x] add tests where we tests the normal ordering:\r\n    ```py\r\n    with shared_intermeidates() as cache:\r\n        a = np.random.rand(4, 4)\r\n        b = np.random.rand(4, 4)\r\n        contract(\"ab,bc->ac\", a, b)\r\n\r\n    assert get_cache_key(\"zc,az->ac\", b, a) in cache\r\n    ...\r\n    ```\r\n- [x] test against the Pyro [rm-einsum-shared](https://github.com/uber/pyro/pull/1327) branch\r\n- [x] test nested sharing\r\n- [x] write docs\r\n- [x] move `_alpha_canonicalize()` to `parser.py`\r\n- [x] test caching with constant expressions\r\n\r\n## Questions\r\n- [x] Could a maintainer suggest where the `handle_sharing()` context should be moved? I've attempted to insert it into `ContractExpression.__call__()`, but I'm a bit lost.\r\n\r\n## Status\r\n- [x] Ready to go","created_at":"2018-08-20T05:51:52Z","updated_at":"2018-08-21T21:24:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"98db8b22569df35b26c5ff01a253671d7a934e99","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43/commits","review_comments_url":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43/comments","review_comment_url":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dgasmith/opt_einsum/issues/43/comments","statuses_url":"https://api.github.com/repos/dgasmith/opt_einsum/statuses/0139c75939945821b61712638429358df03d145f","head":{"label":"fritzo:sharing","ref":"sharing","sha":"0139c75939945821b61712638429358df03d145f","user":{"login":"fritzo","id":648532,"node_id":"MDQ6VXNlcjY0ODUzMg==","avatar_url":"https://avatars0.githubusercontent.com/u/648532?v=4","gravatar_id":"","url":"https://api.github.com/users/fritzo","html_url":"https://github.com/fritzo","followers_url":"https://api.github.com/users/fritzo/followers","following_url":"https://api.github.com/users/fritzo/following{/other_user}","gists_url":"https://api.github.com/users/fritzo/gists{/gist_id}","starred_url":"https://api.github.com/users/fritzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fritzo/subscriptions","organizations_url":"https://api.github.com/users/fritzo/orgs","repos_url":"https://api.github.com/users/fritzo/repos","events_url":"https://api.github.com/users/fritzo/events{/privacy}","received_events_url":"https://api.github.com/users/fritzo/received_events","type":"User","site_admin":false},"repo":{"id":145172512,"node_id":"MDEwOlJlcG9zaXRvcnkxNDUxNzI1MTI=","name":"opt_einsum","full_name":"fritzo/opt_einsum","owner":{"login":"fritzo","id":648532,"node_id":"MDQ6VXNlcjY0ODUzMg==","avatar_url":"https://avatars0.githubusercontent.com/u/648532?v=4","gravatar_id":"","url":"https://api.github.com/users/fritzo","html_url":"https://github.com/fritzo","followers_url":"https://api.github.com/users/fritzo/followers","following_url":"https://api.github.com/users/fritzo/following{/other_user}","gists_url":"https://api.github.com/users/fritzo/gists{/gist_id}","starred_url":"https://api.github.com/users/fritzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fritzo/subscriptions","organizations_url":"https://api.github.com/users/fritzo/orgs","repos_url":"https://api.github.com/users/fritzo/repos","events_url":"https://api.github.com/users/fritzo/events{/privacy}","received_events_url":"https://api.github.com/users/fritzo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/fritzo/opt_einsum","description":"⚡️Optimizing numpy's einsum function with order optimization and GPU support.","fork":true,"url":"https://api.github.com/repos/fritzo/opt_einsum","forks_url":"https://api.github.com/repos/fritzo/opt_einsum/forks","keys_url":"https://api.github.com/repos/fritzo/opt_einsum/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fritzo/opt_einsum/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fritzo/opt_einsum/teams","hooks_url":"https://api.github.com/repos/fritzo/opt_einsum/hooks","issue_events_url":"https://api.github.com/repos/fritzo/opt_einsum/issues/events{/number}","events_url":"https://api.github.com/repos/fritzo/opt_einsum/events","assignees_url":"https://api.github.com/repos/fritzo/opt_einsum/assignees{/user}","branches_url":"https://api.github.com/repos/fritzo/opt_einsum/branches{/branch}","tags_url":"https://api.github.com/repos/fritzo/opt_einsum/tags","blobs_url":"https://api.github.com/repos/fritzo/opt_einsum/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fritzo/opt_einsum/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fritzo/opt_einsum/git/refs{/sha}","trees_url":"https://api.github.com/repos/fritzo/opt_einsum/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fritzo/opt_einsum/statuses/{sha}","languages_url":"https://api.github.com/repos/fritzo/opt_einsum/languages","stargazers_url":"https://api.github.com/repos/fritzo/opt_einsum/stargazers","contributors_url":"https://api.github.com/repos/fritzo/opt_einsum/contributors","subscribers_url":"https://api.github.com/repos/fritzo/opt_einsum/subscribers","subscription_url":"https://api.github.com/repos/fritzo/opt_einsum/subscription","commits_url":"https://api.github.com/repos/fritzo/opt_einsum/commits{/sha}","git_commits_url":"https://api.github.com/repos/fritzo/opt_einsum/git/commits{/sha}","comments_url":"https://api.github.com/repos/fritzo/opt_einsum/comments{/number}","issue_comment_url":"https://api.github.com/repos/fritzo/opt_einsum/issues/comments{/number}","contents_url":"https://api.github.com/repos/fritzo/opt_einsum/contents/{+path}","compare_url":"https://api.github.com/repos/fritzo/opt_einsum/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fritzo/opt_einsum/merges","archive_url":"https://api.github.com/repos/fritzo/opt_einsum/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fritzo/opt_einsum/downloads","issues_url":"https://api.github.com/repos/fritzo/opt_einsum/issues{/number}","pulls_url":"https://api.github.com/repos/fritzo/opt_einsum/pulls{/number}","milestones_url":"https://api.github.com/repos/fritzo/opt_einsum/milestones{/number}","notifications_url":"https://api.github.com/repos/fritzo/opt_einsum/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fritzo/opt_einsum/labels{/name}","releases_url":"https://api.github.com/repos/fritzo/opt_einsum/releases{/id}","deployments_url":"https://api.github.com/repos/fritzo/opt_einsum/deployments","created_at":"2018-08-17T22:43:52Z","updated_at":"2018-08-17T22:43:54Z","pushed_at":"2018-08-21T17:51:05Z","git_url":"git://github.com/fritzo/opt_einsum.git","ssh_url":"git@github.com:fritzo/opt_einsum.git","clone_url":"https://github.com/fritzo/opt_einsum.git","svn_url":"https://github.com/fritzo/opt_einsum","homepage":"http://optimized-einsum.readthedocs.io","size":508,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dgasmith:master","ref":"master","sha":"49e2a91801ce1a071f9ac5ffc044631d1bf65b45","user":{"login":"dgasmith","id":1769841,"node_id":"MDQ6VXNlcjE3Njk4NDE=","avatar_url":"https://avatars2.githubusercontent.com/u/1769841?v=4","gravatar_id":"","url":"https://api.github.com/users/dgasmith","html_url":"https://github.com/dgasmith","followers_url":"https://api.github.com/users/dgasmith/followers","following_url":"https://api.github.com/users/dgasmith/following{/other_user}","gists_url":"https://api.github.com/users/dgasmith/gists{/gist_id}","starred_url":"https://api.github.com/users/dgasmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgasmith/subscriptions","organizations_url":"https://api.github.com/users/dgasmith/orgs","repos_url":"https://api.github.com/users/dgasmith/repos","events_url":"https://api.github.com/users/dgasmith/events{/privacy}","received_events_url":"https://api.github.com/users/dgasmith/received_events","type":"User","site_admin":false},"repo":{"id":27930623,"node_id":"MDEwOlJlcG9zaXRvcnkyNzkzMDYyMw==","name":"opt_einsum","full_name":"dgasmith/opt_einsum","owner":{"login":"dgasmith","id":1769841,"node_id":"MDQ6VXNlcjE3Njk4NDE=","avatar_url":"https://avatars2.githubusercontent.com/u/1769841?v=4","gravatar_id":"","url":"https://api.github.com/users/dgasmith","html_url":"https://github.com/dgasmith","followers_url":"https://api.github.com/users/dgasmith/followers","following_url":"https://api.github.com/users/dgasmith/following{/other_user}","gists_url":"https://api.github.com/users/dgasmith/gists{/gist_id}","starred_url":"https://api.github.com/users/dgasmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dgasmith/subscriptions","organizations_url":"https://api.github.com/users/dgasmith/orgs","repos_url":"https://api.github.com/users/dgasmith/repos","events_url":"https://api.github.com/users/dgasmith/events{/privacy}","received_events_url":"https://api.github.com/users/dgasmith/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dgasmith/opt_einsum","description":"⚡️Optimizing NumPy's einsum function with order optimization and GPU support.","fork":false,"url":"https://api.github.com/repos/dgasmith/opt_einsum","forks_url":"https://api.github.com/repos/dgasmith/opt_einsum/forks","keys_url":"https://api.github.com/repos/dgasmith/opt_einsum/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dgasmith/opt_einsum/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dgasmith/opt_einsum/teams","hooks_url":"https://api.github.com/repos/dgasmith/opt_einsum/hooks","issue_events_url":"https://api.github.com/repos/dgasmith/opt_einsum/issues/events{/number}","events_url":"https://api.github.com/repos/dgasmith/opt_einsum/events","assignees_url":"https://api.github.com/repos/dgasmith/opt_einsum/assignees{/user}","branches_url":"https://api.github.com/repos/dgasmith/opt_einsum/branches{/branch}","tags_url":"https://api.github.com/repos/dgasmith/opt_einsum/tags","blobs_url":"https://api.github.com/repos/dgasmith/opt_einsum/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dgasmith/opt_einsum/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dgasmith/opt_einsum/git/refs{/sha}","trees_url":"https://api.github.com/repos/dgasmith/opt_einsum/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dgasmith/opt_einsum/statuses/{sha}","languages_url":"https://api.github.com/repos/dgasmith/opt_einsum/languages","stargazers_url":"https://api.github.com/repos/dgasmith/opt_einsum/stargazers","contributors_url":"https://api.github.com/repos/dgasmith/opt_einsum/contributors","subscribers_url":"https://api.github.com/repos/dgasmith/opt_einsum/subscribers","subscription_url":"https://api.github.com/repos/dgasmith/opt_einsum/subscription","commits_url":"https://api.github.com/repos/dgasmith/opt_einsum/commits{/sha}","git_commits_url":"https://api.github.com/repos/dgasmith/opt_einsum/git/commits{/sha}","comments_url":"https://api.github.com/repos/dgasmith/opt_einsum/comments{/number}","issue_comment_url":"https://api.github.com/repos/dgasmith/opt_einsum/issues/comments{/number}","contents_url":"https://api.github.com/repos/dgasmith/opt_einsum/contents/{+path}","compare_url":"https://api.github.com/repos/dgasmith/opt_einsum/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dgasmith/opt_einsum/merges","archive_url":"https://api.github.com/repos/dgasmith/opt_einsum/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dgasmith/opt_einsum/downloads","issues_url":"https://api.github.com/repos/dgasmith/opt_einsum/issues{/number}","pulls_url":"https://api.github.com/repos/dgasmith/opt_einsum/pulls{/number}","milestones_url":"https://api.github.com/repos/dgasmith/opt_einsum/milestones{/number}","notifications_url":"https://api.github.com/repos/dgasmith/opt_einsum/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dgasmith/opt_einsum/labels{/name}","releases_url":"https://api.github.com/repos/dgasmith/opt_einsum/releases{/id}","deployments_url":"https://api.github.com/repos/dgasmith/opt_einsum/deployments","created_at":"2014-12-12T17:31:24Z","updated_at":"2018-08-21T19:01:00Z","pushed_at":"2018-08-21T17:51:07Z","git_url":"git://github.com/dgasmith/opt_einsum.git","ssh_url":"git@github.com:dgasmith/opt_einsum.git","clone_url":"https://github.com/dgasmith/opt_einsum.git","svn_url":"https://github.com/dgasmith/opt_einsum","homepage":"http://optimized-einsum.readthedocs.io","size":524,"stargazers_count":101,"watchers_count":101,"language":"Python","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":15,"mirror_url":null,"archived":false,"open_issues_count":4,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":15,"open_issues":4,"watchers":101,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43"},"html":{"href":"https://github.com/dgasmith/opt_einsum/pull/43"},"issue":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/issues/43"},"comments":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/issues/43/comments"},"review_comments":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43/comments"},"review_comment":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/pulls/43/commits"},"statuses":{"href":"https://api.github.com/repos/dgasmith/opt_einsum/statuses/0139c75939945821b61712638429358df03d145f"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"dgasmith"}}