{"action":"created","comment":{"url":"https://api.github.com/repos/pytorch/pytorch/pulls/comments/359369583","pull_request_review_id":334003927,"id":359369583,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDM1OTM2OTU4Mw==","diff_hunk":"@@ -159,13 +159,14 @@ Tensor _pdist_backward(const Tensor& grad, const Tensor& self, const double p, c\n }\n \n Tensor cosine_similarity(const Tensor& x1, const Tensor& x2, int64_t dim, double eps) {\n-  // Follow scipy impl to improve numerical precision\n-  // Use x / sqrt(x * x) instead of x / (sqrt(x) * sqrt(x))\n-  Tensor w12 = at::sum(x1 * x2, dim);\n-  Tensor w1 = at::sum(x1 * x1, dim);\n-  Tensor w2 = at::sum(x2 * x2, dim);\n-  Tensor n12 = (w1 * w2).clamp_min_(eps * eps).sqrt_();\n-  return w12.div_(n12);\n+  auto x1_norm = at::norm(x1, 2, dim, true) + eps;","path":"aten/src/ATen/native/Distance.cpp","position":43,"original_position":11,"commit_id":"0c2cd7f74e2921eca75d110fbf9db6fd4a19e2a4","original_commit_id":"edb2276a06fb2dcaaa18c1c7cebf4f82870a2ca1","user":{"login":"nikitaved","id":1255244,"node_id":"MDQ6VXNlcjEyNTUyNDQ=","avatar_url":"https://avatars2.githubusercontent.com/u/1255244?v=4","gravatar_id":"","url":"https://api.github.com/users/nikitaved","html_url":"https://github.com/nikitaved","followers_url":"https://api.github.com/users/nikitaved/followers","following_url":"https://api.github.com/users/nikitaved/following{/other_user}","gists_url":"https://api.github.com/users/nikitaved/gists{/gist_id}","starred_url":"https://api.github.com/users/nikitaved/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nikitaved/subscriptions","organizations_url":"https://api.github.com/users/nikitaved/orgs","repos_url":"https://api.github.com/users/nikitaved/repos","events_url":"https://api.github.com/users/nikitaved/events{/privacy}","received_events_url":"https://api.github.com/users/nikitaved/received_events","type":"User","site_admin":false},"body":"Ok, done!","created_at":"2019-12-18T14:23:08Z","updated_at":"2019-12-18T14:23:08Z","html_url":"https://github.com/pytorch/pytorch/pull/31378#discussion_r359369583","pull_request_url":"https://api.github.com/repos/pytorch/pytorch/pulls/31378","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/comments/359369583"},"html":{"href":"https://github.com/pytorch/pytorch/pull/31378#discussion_r359369583"},"pull_request":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/31378"}},"in_reply_to_id":359167787},"pull_request":{"url":"https://api.github.com/repos/pytorch/pytorch/pulls/31378","id":354265345,"node_id":"MDExOlB1bGxSZXF1ZXN0MzU0MjY1MzQ1","html_url":"https://github.com/pytorch/pytorch/pull/31378","diff_url":"https://github.com/pytorch/pytorch/pull/31378.diff","patch_url":"https://github.com/pytorch/pytorch/pull/31378.patch","issue_url":"https://api.github.com/repos/pytorch/pytorch/issues/31378","number":31378,"state":"open","locked":false,"title":"more numerically stable cosine_similarity","user":{"login":"nikitaved","id":1255244,"node_id":"MDQ6VXNlcjEyNTUyNDQ=","avatar_url":"https://avatars2.githubusercontent.com/u/1255244?v=4","gravatar_id":"","url":"https://api.github.com/users/nikitaved","html_url":"https://github.com/nikitaved","followers_url":"https://api.github.com/users/nikitaved/followers","following_url":"https://api.github.com/users/nikitaved/following{/other_user}","gists_url":"https://api.github.com/users/nikitaved/gists{/gist_id}","starred_url":"https://api.github.com/users/nikitaved/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nikitaved/subscriptions","organizations_url":"https://api.github.com/users/nikitaved/orgs","repos_url":"https://api.github.com/users/nikitaved/repos","events_url":"https://api.github.com/users/nikitaved/events{/privacy}","received_events_url":"https://api.github.com/users/nikitaved/received_events","type":"User","site_admin":false},"body":"**Previous behavior**: compute inner product, then normalize.\r\n**This patch**: first normalize, then compute inner product. This should be more numerically stable because it avoids loosing precision in inner product for inputs with large norms.\r\nBy design ensures that cosine similarity is within `(-1.0, +1.0)`, so it should fix [#29442](https://github.com/pytorch/pytorch/issues/29442).\r\n\r\nP.S. I had to change tests because this implementation handles division by 0 differently.","created_at":"2019-12-17T19:12:07Z","updated_at":"2019-12-18T14:23:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6afcceb544fa6c1f8f5e727f7e07dea98e241b15","assignee":null,"assignees":[],"requested_reviewers":[{"login":"albanD","id":6359743,"node_id":"MDQ6VXNlcjYzNTk3NDM=","avatar_url":"https://avatars3.githubusercontent.com/u/6359743?v=4","gravatar_id":"","url":"https://api.github.com/users/albanD","html_url":"https://github.com/albanD","followers_url":"https://api.github.com/users/albanD/followers","following_url":"https://api.github.com/users/albanD/following{/other_user}","gists_url":"https://api.github.com/users/albanD/gists{/gist_id}","starred_url":"https://api.github.com/users/albanD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/albanD/subscriptions","organizations_url":"https://api.github.com/users/albanD/orgs","repos_url":"https://api.github.com/users/albanD/repos","events_url":"https://api.github.com/users/albanD/events{/privacy}","received_events_url":"https://api.github.com/users/albanD/received_events","type":"User","site_admin":false}],"requested_teams":[],"labels":[{"id":1392590051,"node_id":"MDU6TGFiZWwxMzkyNTkwMDUx","url":"https://api.github.com/repos/pytorch/pytorch/labels/open%20source","name":"open source","color":"ededed","default":false,"description":null}],"milestone":null,"commits_url":"https://api.github.com/repos/pytorch/pytorch/pulls/31378/commits","review_comments_url":"https://api.github.com/repos/pytorch/pytorch/pulls/31378/comments","review_comment_url":"https://api.github.com/repos/pytorch/pytorch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pytorch/pytorch/issues/31378/comments","statuses_url":"https://api.github.com/repos/pytorch/pytorch/statuses/0c2cd7f74e2921eca75d110fbf9db6fd4a19e2a4","head":{"label":"Quansight:better-cosine-similarity","ref":"better-cosine-similarity","sha":"0c2cd7f74e2921eca75d110fbf9db6fd4a19e2a4","user":{"login":"Quansight","id":34879953,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM0ODc5OTUz","avatar_url":"https://avatars0.githubusercontent.com/u/34879953?v=4","gravatar_id":"","url":"https://api.github.com/users/Quansight","html_url":"https://github.com/Quansight","followers_url":"https://api.github.com/users/Quansight/followers","following_url":"https://api.github.com/users/Quansight/following{/other_user}","gists_url":"https://api.github.com/users/Quansight/gists{/gist_id}","starred_url":"https://api.github.com/users/Quansight/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Quansight/subscriptions","organizations_url":"https://api.github.com/users/Quansight/orgs","repos_url":"https://api.github.com/users/Quansight/repos","events_url":"https://api.github.com/users/Quansight/events{/privacy}","received_events_url":"https://api.github.com/users/Quansight/received_events","type":"Organization","site_admin":false},"repo":{"id":169491304,"node_id":"MDEwOlJlcG9zaXRvcnkxNjk0OTEzMDQ=","name":"pytorch","full_name":"Quansight/pytorch","private":false,"owner":{"login":"Quansight","id":34879953,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM0ODc5OTUz","avatar_url":"https://avatars0.githubusercontent.com/u/34879953?v=4","gravatar_id":"","url":"https://api.github.com/users/Quansight","html_url":"https://github.com/Quansight","followers_url":"https://api.github.com/users/Quansight/followers","following_url":"https://api.github.com/users/Quansight/following{/other_user}","gists_url":"https://api.github.com/users/Quansight/gists{/gist_id}","starred_url":"https://api.github.com/users/Quansight/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Quansight/subscriptions","organizations_url":"https://api.github.com/users/Quansight/orgs","repos_url":"https://api.github.com/users/Quansight/repos","events_url":"https://api.github.com/users/Quansight/events{/privacy}","received_events_url":"https://api.github.com/users/Quansight/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/Quansight/pytorch","description":"Tensors and Dynamic neural networks in Python  with strong GPU acceleration","fork":true,"url":"https://api.github.com/repos/Quansight/pytorch","forks_url":"https://api.github.com/repos/Quansight/pytorch/forks","keys_url":"https://api.github.com/repos/Quansight/pytorch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Quansight/pytorch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Quansight/pytorch/teams","hooks_url":"https://api.github.com/repos/Quansight/pytorch/hooks","issue_events_url":"https://api.github.com/repos/Quansight/pytorch/issues/events{/number}","events_url":"https://api.github.com/repos/Quansight/pytorch/events","assignees_url":"https://api.github.com/repos/Quansight/pytorch/assignees{/user}","branches_url":"https://api.github.com/repos/Quansight/pytorch/branches{/branch}","tags_url":"https://api.github.com/repos/Quansight/pytorch/tags","blobs_url":"https://api.github.com/repos/Quansight/pytorch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Quansight/pytorch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Quansight/pytorch/git/refs{/sha}","trees_url":"https://api.github.com/repos/Quansight/pytorch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Quansight/pytorch/statuses/{sha}","languages_url":"https://api.github.com/repos/Quansight/pytorch/languages","stargazers_url":"https://api.github.com/repos/Quansight/pytorch/stargazers","contributors_url":"https://api.github.com/repos/Quansight/pytorch/contributors","subscribers_url":"https://api.github.com/repos/Quansight/pytorch/subscribers","subscription_url":"https://api.github.com/repos/Quansight/pytorch/subscription","commits_url":"https://api.github.com/repos/Quansight/pytorch/commits{/sha}","git_commits_url":"https://api.github.com/repos/Quansight/pytorch/git/commits{/sha}","comments_url":"https://api.github.com/repos/Quansight/pytorch/comments{/number}","issue_comment_url":"https://api.github.com/repos/Quansight/pytorch/issues/comments{/number}","contents_url":"https://api.github.com/repos/Quansight/pytorch/contents/{+path}","compare_url":"https://api.github.com/repos/Quansight/pytorch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Quansight/pytorch/merges","archive_url":"https://api.github.com/repos/Quansight/pytorch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Quansight/pytorch/downloads","issues_url":"https://api.github.com/repos/Quansight/pytorch/issues{/number}","pulls_url":"https://api.github.com/repos/Quansight/pytorch/pulls{/number}","milestones_url":"https://api.github.com/repos/Quansight/pytorch/milestones{/number}","notifications_url":"https://api.github.com/repos/Quansight/pytorch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Quansight/pytorch/labels{/name}","releases_url":"https://api.github.com/repos/Quansight/pytorch/releases{/id}","deployments_url":"https://api.github.com/repos/Quansight/pytorch/deployments","created_at":"2019-02-06T23:14:25Z","updated_at":"2019-12-17T13:31:17Z","pushed_at":"2019-12-18T14:22:39Z","git_url":"git://github.com/Quansight/pytorch.git","ssh_url":"git@github.com:Quansight/pytorch.git","clone_url":"https://github.com/Quansight/pytorch.git","svn_url":"https://github.com/Quansight/pytorch","homepage":"https://pytorch.org","size":162742,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pytorch:master","ref":"master","sha":"d2067569e7783e20e73601295f04e7ce8e7acf54","user":{"login":"pytorch","id":21003710,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIxMDAzNzEw","avatar_url":"https://avatars3.githubusercontent.com/u/21003710?v=4","gravatar_id":"","url":"https://api.github.com/users/pytorch","html_url":"https://github.com/pytorch","followers_url":"https://api.github.com/users/pytorch/followers","following_url":"https://api.github.com/users/pytorch/following{/other_user}","gists_url":"https://api.github.com/users/pytorch/gists{/gist_id}","starred_url":"https://api.github.com/users/pytorch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pytorch/subscriptions","organizations_url":"https://api.github.com/users/pytorch/orgs","repos_url":"https://api.github.com/users/pytorch/repos","events_url":"https://api.github.com/users/pytorch/events{/privacy}","received_events_url":"https://api.github.com/users/pytorch/received_events","type":"Organization","site_admin":false},"repo":{"id":65600975,"node_id":"MDEwOlJlcG9zaXRvcnk2NTYwMDk3NQ==","name":"pytorch","full_name":"pytorch/pytorch","private":false,"owner":{"login":"pytorch","id":21003710,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIxMDAzNzEw","avatar_url":"https://avatars3.githubusercontent.com/u/21003710?v=4","gravatar_id":"","url":"https://api.github.com/users/pytorch","html_url":"https://github.com/pytorch","followers_url":"https://api.github.com/users/pytorch/followers","following_url":"https://api.github.com/users/pytorch/following{/other_user}","gists_url":"https://api.github.com/users/pytorch/gists{/gist_id}","starred_url":"https://api.github.com/users/pytorch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pytorch/subscriptions","organizations_url":"https://api.github.com/users/pytorch/orgs","repos_url":"https://api.github.com/users/pytorch/repos","events_url":"https://api.github.com/users/pytorch/events{/privacy}","received_events_url":"https://api.github.com/users/pytorch/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/pytorch/pytorch","description":"Tensors and Dynamic neural networks in Python with strong GPU acceleration","fork":false,"url":"https://api.github.com/repos/pytorch/pytorch","forks_url":"https://api.github.com/repos/pytorch/pytorch/forks","keys_url":"https://api.github.com/repos/pytorch/pytorch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pytorch/pytorch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pytorch/pytorch/teams","hooks_url":"https://api.github.com/repos/pytorch/pytorch/hooks","issue_events_url":"https://api.github.com/repos/pytorch/pytorch/issues/events{/number}","events_url":"https://api.github.com/repos/pytorch/pytorch/events","assignees_url":"https://api.github.com/repos/pytorch/pytorch/assignees{/user}","branches_url":"https://api.github.com/repos/pytorch/pytorch/branches{/branch}","tags_url":"https://api.github.com/repos/pytorch/pytorch/tags","blobs_url":"https://api.github.com/repos/pytorch/pytorch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pytorch/pytorch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pytorch/pytorch/git/refs{/sha}","trees_url":"https://api.github.com/repos/pytorch/pytorch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pytorch/pytorch/statuses/{sha}","languages_url":"https://api.github.com/repos/pytorch/pytorch/languages","stargazers_url":"https://api.github.com/repos/pytorch/pytorch/stargazers","contributors_url":"https://api.github.com/repos/pytorch/pytorch/contributors","subscribers_url":"https://api.github.com/repos/pytorch/pytorch/subscribers","subscription_url":"https://api.github.com/repos/pytorch/pytorch/subscription","commits_url":"https://api.github.com/repos/pytorch/pytorch/commits{/sha}","git_commits_url":"https://api.github.com/repos/pytorch/pytorch/git/commits{/sha}","comments_url":"https://api.github.com/repos/pytorch/pytorch/comments{/number}","issue_comment_url":"https://api.github.com/repos/pytorch/pytorch/issues/comments{/number}","contents_url":"https://api.github.com/repos/pytorch/pytorch/contents/{+path}","compare_url":"https://api.github.com/repos/pytorch/pytorch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pytorch/pytorch/merges","archive_url":"https://api.github.com/repos/pytorch/pytorch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pytorch/pytorch/downloads","issues_url":"https://api.github.com/repos/pytorch/pytorch/issues{/number}","pulls_url":"https://api.github.com/repos/pytorch/pytorch/pulls{/number}","milestones_url":"https://api.github.com/repos/pytorch/pytorch/milestones{/number}","notifications_url":"https://api.github.com/repos/pytorch/pytorch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pytorch/pytorch/labels{/name}","releases_url":"https://api.github.com/repos/pytorch/pytorch/releases{/id}","deployments_url":"https://api.github.com/repos/pytorch/pytorch/deployments","created_at":"2016-08-13T05:26:41Z","updated_at":"2019-12-18T14:22:42Z","pushed_at":"2019-12-18T14:22:44Z","git_url":"git://github.com/pytorch/pytorch.git","ssh_url":"git@github.com:pytorch/pytorch.git","clone_url":"https://github.com/pytorch/pytorch.git","svn_url":"https://github.com/pytorch/pytorch","homepage":"https://pytorch.org","size":257417,"stargazers_count":34707,"watchers_count":34707,"language":"C++","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":8659,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":4765,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":8659,"open_issues":4765,"watchers":34707,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/31378"},"html":{"href":"https://github.com/pytorch/pytorch/pull/31378"},"issue":{"href":"https://api.github.com/repos/pytorch/pytorch/issues/31378"},"comments":{"href":"https://api.github.com/repos/pytorch/pytorch/issues/31378/comments"},"review_comments":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/31378/comments"},"review_comment":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/31378/commits"},"statuses":{"href":"https://api.github.com/repos/pytorch/pytorch/statuses/0c2cd7f74e2921eca75d110fbf9db6fd4a19e2a4"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"nikitaved"}}