{"action":"created","comment":{"url":"https://api.github.com/repos/pytorch/pytorch/pulls/comments/168626176","pull_request_review_id":97026034,"id":168626176,"diff_hunk":"@@ -700,13 +734,79 @@ def test_all_gather_multigpu(self):\n         self._test_all_gather_multigpu_helper(group, group_id, rank,\n                                               rankToGPUMapping)\n \n+    # END TO END TEST FOR DISTRIBUTEDDATAPARALLEL\n+    def _test_DDP_helper(self, model, input_var, target, loss, optimizer):\n+        model.train()\n+        output = model(input_var)\n+        l = loss(output, target)\n+        l.backward()\n+        optimizer.step()\n+        return model\n+\n+    @unittest.skipIf(BACKEND != 'nccl' and BACKEND != 'gloo',\n+                     \"Only Nccl & Gloo backend support DistributedDataParallel\")\n+    @skip_if_no_cuda_distributed\n+    @skip_if_no_multigpu\n+    def test_DistributedDataParallel(self):\n+        # Run a simple end to end DDP model, use result of single node model\n+        # as baseline\n+        group, group_id, rank = self._init_global_test()\n+        rankToGPUMapping = self._init_multigpu_helper()\n+\n+        class Net(nn.Module):\n+            def __init__(self):\n+                super(Net, self).__init__()\n+                self.features = nn.Linear(2, 4, bias=False)\n+\n+            def forward(self, x):\n+                x = self.features(x)\n+                return x\n+\n+        # cpu training setup\n+        model = Net()\n+        model_cpu = copy.deepcopy(model)\n+        optimizer_cpu = torch.optim.SGD(model_cpu.parameters(), 0.1)\n+\n+        # DDP training setup\n+        model_DDP = copy.deepcopy(model)\n+        gpu_subset = list(rankToGPUMapping[rank])\n+        model_DDP.cuda(gpu_subset[0])\n+        model_DDP = nn.parallel.DistributedDataParallel(model_DDP,\n+                                                        device_ids=gpu_subset)\n+        optimizer_DDP = torch.optim.SGD(model_DDP.parameters(), 0.1)\n+\n+        # batch_size for DDP should be divisible by #GPU per node.\n+        batch_size = len(gpu_subset) * int(WORLD_SIZE)\n+        input_cpu = torch.randn(batch_size, 1, 2)\n+        target = torch.randn(batch_size, 1, 4)\n+        loss = nn.MSELoss()\n+\n+        model_cpu = self._test_DDP_helper(\n+            model_cpu,\n+            Variable(input_cpu, requires_grad=True),\n+            Variable(target),\n+            loss,\n+            optimizer_cpu)\n+\n+        # DDP scatters subsets of input_cpu to GPUs\n+        model_DDP = self._test_DDP_helper(\n+            model_DDP,\n+            Variable(input_cpu, requires_grad=True),\n+            Variable(target.cuda(gpu_subset[0], non_blocking=True)),\n+            loss,\n+            optimizer_DDP)\n+\n+        self.assertEqual(model_cpu.features.weight,\n+                         model_DDP.module.features.weight)","path":"test/test_distributed.py","position":248,"original_position":248,"commit_id":"ea79f9ec8a5740ffd85f0b7ea95916135ca9b1f6","original_commit_id":"ea79f9ec8a5740ffd85f0b7ea95916135ca9b1f6","user":{"login":"apaszke","id":4583066,"avatar_url":"https://avatars3.githubusercontent.com/u/4583066?v=4","gravatar_id":"","url":"https://api.github.com/users/apaszke","html_url":"https://github.com/apaszke","followers_url":"https://api.github.com/users/apaszke/followers","following_url":"https://api.github.com/users/apaszke/following{/other_user}","gists_url":"https://api.github.com/users/apaszke/gists{/gist_id}","starred_url":"https://api.github.com/users/apaszke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apaszke/subscriptions","organizations_url":"https://api.github.com/users/apaszke/orgs","repos_url":"https://api.github.com/users/apaszke/repos","events_url":"https://api.github.com/users/apaszke/events{/privacy}","received_events_url":"https://api.github.com/users/apaszke/received_events","type":"User","site_admin":false},"body":"To be honest I'm very surprised that they match. Every process will sample a random batch, and in the `model_cpu` case it will get some random gradient, while in the distributed case it will get the local gradient (same as for `model_cpu`) averaged with random gradients from other processes. Is it just me, or is the code wrong?","created_at":"2018-02-15T22:19:31Z","updated_at":"2018-02-15T22:19:40Z","html_url":"https://github.com/pytorch/pytorch/pull/5182#discussion_r168626176","pull_request_url":"https://api.github.com/repos/pytorch/pytorch/pulls/5182","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/comments/168626176"},"html":{"href":"https://github.com/pytorch/pytorch/pull/5182#discussion_r168626176"},"pull_request":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/5182"}}},"pull_request":{"url":"https://api.github.com/repos/pytorch/pytorch/pulls/5182","id":168518269,"html_url":"https://github.com/pytorch/pytorch/pull/5182","diff_url":"https://github.com/pytorch/pytorch/pull/5182.diff","patch_url":"https://github.com/pytorch/pytorch/pull/5182.patch","issue_url":"https://api.github.com/repos/pytorch/pytorch/issues/5182","number":5182,"state":"open","locked":false,"title":"add end to end test for DistributedDataParallel","user":{"login":"ailzhang","id":5248122,"avatar_url":"https://avatars2.githubusercontent.com/u/5248122?v=4","gravatar_id":"","url":"https://api.github.com/users/ailzhang","html_url":"https://github.com/ailzhang","followers_url":"https://api.github.com/users/ailzhang/followers","following_url":"https://api.github.com/users/ailzhang/following{/other_user}","gists_url":"https://api.github.com/users/ailzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/ailzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ailzhang/subscriptions","organizations_url":"https://api.github.com/users/ailzhang/orgs","repos_url":"https://api.github.com/users/ailzhang/repos","events_url":"https://api.github.com/users/ailzhang/events{/privacy}","received_events_url":"https://api.github.com/users/ailzhang/received_events","type":"User","site_admin":false},"body":"Add an end to end test for DistributedDataParallel for gloo & nccl backend. \r\n","created_at":"2018-02-12T08:09:15Z","updated_at":"2018-02-15T22:19:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f3d1acf23d86400b1eda22b58aa720138c1f74a7","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/pytorch/pytorch/pulls/5182/commits","review_comments_url":"https://api.github.com/repos/pytorch/pytorch/pulls/5182/comments","review_comment_url":"https://api.github.com/repos/pytorch/pytorch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pytorch/pytorch/issues/5182/comments","statuses_url":"https://api.github.com/repos/pytorch/pytorch/statuses/ea79f9ec8a5740ffd85f0b7ea95916135ca9b1f6","head":{"label":"ailzhang:test_DDP","ref":"test_DDP","sha":"ea79f9ec8a5740ffd85f0b7ea95916135ca9b1f6","user":{"login":"ailzhang","id":5248122,"avatar_url":"https://avatars2.githubusercontent.com/u/5248122?v=4","gravatar_id":"","url":"https://api.github.com/users/ailzhang","html_url":"https://github.com/ailzhang","followers_url":"https://api.github.com/users/ailzhang/followers","following_url":"https://api.github.com/users/ailzhang/following{/other_user}","gists_url":"https://api.github.com/users/ailzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/ailzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ailzhang/subscriptions","organizations_url":"https://api.github.com/users/ailzhang/orgs","repos_url":"https://api.github.com/users/ailzhang/repos","events_url":"https://api.github.com/users/ailzhang/events{/privacy}","received_events_url":"https://api.github.com/users/ailzhang/received_events","type":"User","site_admin":false},"repo":{"id":100415363,"name":"pytorch","full_name":"ailzhang/pytorch","owner":{"login":"ailzhang","id":5248122,"avatar_url":"https://avatars2.githubusercontent.com/u/5248122?v=4","gravatar_id":"","url":"https://api.github.com/users/ailzhang","html_url":"https://github.com/ailzhang","followers_url":"https://api.github.com/users/ailzhang/followers","following_url":"https://api.github.com/users/ailzhang/following{/other_user}","gists_url":"https://api.github.com/users/ailzhang/gists{/gist_id}","starred_url":"https://api.github.com/users/ailzhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ailzhang/subscriptions","organizations_url":"https://api.github.com/users/ailzhang/orgs","repos_url":"https://api.github.com/users/ailzhang/repos","events_url":"https://api.github.com/users/ailzhang/events{/privacy}","received_events_url":"https://api.github.com/users/ailzhang/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ailzhang/pytorch","description":"Tensors and Dynamic neural networks in Python  with strong GPU acceleration","fork":true,"url":"https://api.github.com/repos/ailzhang/pytorch","forks_url":"https://api.github.com/repos/ailzhang/pytorch/forks","keys_url":"https://api.github.com/repos/ailzhang/pytorch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ailzhang/pytorch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ailzhang/pytorch/teams","hooks_url":"https://api.github.com/repos/ailzhang/pytorch/hooks","issue_events_url":"https://api.github.com/repos/ailzhang/pytorch/issues/events{/number}","events_url":"https://api.github.com/repos/ailzhang/pytorch/events","assignees_url":"https://api.github.com/repos/ailzhang/pytorch/assignees{/user}","branches_url":"https://api.github.com/repos/ailzhang/pytorch/branches{/branch}","tags_url":"https://api.github.com/repos/ailzhang/pytorch/tags","blobs_url":"https://api.github.com/repos/ailzhang/pytorch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ailzhang/pytorch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ailzhang/pytorch/git/refs{/sha}","trees_url":"https://api.github.com/repos/ailzhang/pytorch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ailzhang/pytorch/statuses/{sha}","languages_url":"https://api.github.com/repos/ailzhang/pytorch/languages","stargazers_url":"https://api.github.com/repos/ailzhang/pytorch/stargazers","contributors_url":"https://api.github.com/repos/ailzhang/pytorch/contributors","subscribers_url":"https://api.github.com/repos/ailzhang/pytorch/subscribers","subscription_url":"https://api.github.com/repos/ailzhang/pytorch/subscription","commits_url":"https://api.github.com/repos/ailzhang/pytorch/commits{/sha}","git_commits_url":"https://api.github.com/repos/ailzhang/pytorch/git/commits{/sha}","comments_url":"https://api.github.com/repos/ailzhang/pytorch/comments{/number}","issue_comment_url":"https://api.github.com/repos/ailzhang/pytorch/issues/comments{/number}","contents_url":"https://api.github.com/repos/ailzhang/pytorch/contents/{+path}","compare_url":"https://api.github.com/repos/ailzhang/pytorch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ailzhang/pytorch/merges","archive_url":"https://api.github.com/repos/ailzhang/pytorch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ailzhang/pytorch/downloads","issues_url":"https://api.github.com/repos/ailzhang/pytorch/issues{/number}","pulls_url":"https://api.github.com/repos/ailzhang/pytorch/pulls{/number}","milestones_url":"https://api.github.com/repos/ailzhang/pytorch/milestones{/number}","notifications_url":"https://api.github.com/repos/ailzhang/pytorch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ailzhang/pytorch/labels{/name}","releases_url":"https://api.github.com/repos/ailzhang/pytorch/releases{/id}","deployments_url":"https://api.github.com/repos/ailzhang/pytorch/deployments","created_at":"2017-08-15T20:09:33Z","updated_at":"2017-08-15T20:09:38Z","pushed_at":"2018-02-14T01:39:53Z","git_url":"git://github.com/ailzhang/pytorch.git","ssh_url":"git@github.com:ailzhang/pytorch.git","clone_url":"https://github.com/ailzhang/pytorch.git","svn_url":"https://github.com/ailzhang/pytorch","homepage":"http://pytorch.org","size":22531,"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":"other","name":"Other","spdx_id":null,"url":null},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pytorch:master","ref":"master","sha":"a4d0a74ceefa9e18810f8caf0fdf66fea473008b","user":{"login":"pytorch","id":21003710,"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,"name":"pytorch","full_name":"pytorch/pytorch","owner":{"login":"pytorch","id":21003710,"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},"private":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":"2018-02-15T21:51:58Z","pushed_at":"2018-02-15T22:02:27Z","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":"http://pytorch.org","size":22800,"stargazers_count":12267,"watchers_count":12267,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2560,"mirror_url":null,"archived":false,"open_issues_count":720,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":2560,"open_issues":720,"watchers":12267,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/5182"},"html":{"href":"https://github.com/pytorch/pytorch/pull/5182"},"issue":{"href":"https://api.github.com/repos/pytorch/pytorch/issues/5182"},"comments":{"href":"https://api.github.com/repos/pytorch/pytorch/issues/5182/comments"},"review_comments":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/5182/comments"},"review_comment":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pytorch/pytorch/pulls/5182/commits"},"statuses":{"href":"https://api.github.com/repos/pytorch/pytorch/statuses/ea79f9ec8a5740ffd85f0b7ea95916135ca9b1f6"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"apaszke"}}