{"action":"created","comment":{"url":"https://api.github.com/repos/pytorch/glow/pulls/comments/189711176","pull_request_review_id":121926140,"id":189711176,"diff_hunk":"@@ -544,6 +544,183 @@ static void mergeMatMul(Function *F) {\n   }\n }\n \n+/// \\returns True if the two slices \\p A and \\p B access consecutive spacial\n+/// regions on the \\p dim dimension. For example Slice(0..10) Slice(10..20) are\n+/// consecutive but Slice(0..10) Slice(20..30) are not.\n+static bool areSlicesConsecutive(SliceNode *A, SliceNode *B, unsigned dim) {\n+  // The input node that we slice must be identical.\n+  if (A->getInput().getNode() != B->getInput().getNode()) {\n+    return false;\n+  }\n+\n+  // The result type (and the size of the shape) must be identical.\n+  if (A->getResult().getType() != B->getResult().getType()) {\n+    return false;\n+  }\n+\n+  auto aStart = A->getStart();\n+  auto bStart = B->getStart();\n+\n+  assert(aStart.size() > dim && \"Invalid dimension\");\n+\n+  for (int i = 0, e = aStart.size(); i < e; i++) {\n+    if (i == dim) {\n+      auto resSize = A->getResult().dims();\n+      // This is the stride (the delta between the two slices on the requested\n+      // dimension).\n+      auto delta = bStart[i] - aStart[i];\n+      // The distance between the two slices must be identical to the size of\n+      // the result.\n+      if (resSize[dim] != delta) {\n+        return false;\n+      }\n+\n+      continue;\n+    }\n+\n+    // The non-consecutive dimensions must be identical.\n+    if (aStart[i] != bStart[i]) {\n+      return false;\n+    }\n+  }\n+\n+  return true;\n+}\n+\n+/// Find a sequence of slices in \\p input that construct a whole tensor.\n+/// \\returns True if an order was found. The order is recorded in \\p order.\n+static bool orderConsecutiveSlices(llvm::ArrayRef<SliceNode *> input,\n+                                   unsigned dimension,\n+                                   std::vector<SliceNode *> &order) {\n+  // This is the 'last' slice to be found in the sequence of slices.\n+  SliceNode *lastSlice = nullptr;\n+\n+  // Find the 'first' slice in the sequence.\n+  for (SliceNode *SN : input) {\n+    auto start = SN->getStart();\n+\n+    // Invalid dimension.\n+    if (start.size() <= dimension) {\n+      return false;\n+    }\n+\n+    // Check if this slice extract the first element.\n+    if (start[dimension] == 0) {\n+      // We found the first element.\n+      lastSlice = SN;\n+      order.push_back(lastSlice);\n+      break;\n+    }\n+  }\n+\n+  // We could not find a 'first' slice.\n+  if (!lastSlice) {\n+    return false;\n+  }\n+\n+  // Now that we've found the first slice in the sequence, try to order the rest\n+  // of the slices after the first one.\n+  bool addedSlice = true;\n+  while (addedSlice) {\n+    addedSlice = false;\n+\n+    // For each slice:\n+    for (SliceNode *SN : input) {\n+      // Ignore slices of invalid types.\n+      if (lastSlice->getResult().getType() != SN->getResult().getType()) {\n+        continue;\n+      }\n+\n+      // Check if SN comes after the last slice in the sequence.\n+      if (areSlicesConsecutive(lastSlice, SN, dimension)) {\n+        // Add the consecutive slice and schedule another iteration.\n+        lastSlice = SN;\n+        order.push_back(lastSlice);\n+        addedSlice = true;\n+        continue;\n+      }\n+    }\n+  } // While adding new slices.\n+\n+  // Check that the last slice completes the tensor.\n+  auto startCoor = lastSlice->getStart();\n+  auto resDim = lastSlice->getResult()->getType()->dims();\n+  auto inDim = lastSlice->getInput()->getType()->dims();\n+\n+  // Check if for all dimensions, the size of the result tensor plus the start\n+  // coordinate matches the size of the tensor.\n+  for (int i = 0, e = startCoor.size(); i < e; i++) {\n+    if (startCoor[i] + resDim[i] != inDim[i])\n+      return false;\n+  }\n+\n+  // Report success if we found at least two slices that extract from the input.\n+  return order.size() > 1;\n+}\n+\n+/// Merge multiple batched add nodes into a large batched-add node.\n+static void mergeBatchedAdd(Function *F) {\n+  auto &nodes = F->getNodes();\n+\n+  // We index the batched add nodes by the slice operand.\n+  llvm::DenseMap<Node *, std::vector<BatchedAddNode *>> rightBAUsers;\n+\n+  // Collect all of the batched add nodes and index them by the 'slice' operand.\n+  for (auto const &node : nodes) {\n+    if (auto *BA = dyn_cast<BatchedAddNode>(node)) {\n+      rightBAUsers[BA->getSlice().getNode()].push_back(BA);\n+    }\n+  }\n+\n+  // For each 'slice' that batched add nodes access:\n+  for (auto &it : rightBAUsers) {\n+    auto &BAs = it.second;\n+\n+    // Collects the left-hand-side operands that the batched-adds add into. We\n+    // only collect 'slice' nodes.\n+    std::vector<SliceNode *> slices;\n+\n+    for (auto *BA : BAs) {\n+      if (auto *S = dyn_cast<SliceNode>(BA->getBatch().getNode())) {\n+        slices.push_back(S);\n+      }\n+    }\n+\n+    // Check if the slice nodes that we've collected cover a whole tensor.\n+    std::vector<SliceNode *> order;\n+    bool found = orderConsecutiveSlices(slices, 0, order);\n+\n+    if (!found) {\n+      continue;\n+    }\n+\n+    // We found a sequence of batched-add-slice that cover the input tensor. We\n+    // can transform the graph and create one big batched-add.\n+    std::vector<Node *> newSlices;\n+    SliceNode *S = llvm::cast<SliceNode>(order[0]);\n+    auto *BA = F->createBatchedAdd(\"mergedBA\", S->getInput(), it.first);\n+\n+    // Create the new slices.\n+    for (int i = 0, e = order.size(); i < e; i++) {\n+      auto *orig = order[i];\n+      newSlices.push_back(F->createSlice(orig->getName(), BA, orig->getStart(),\n+                                         orig->getType()));\n+    }\n+\n+    // Update all of our batched add nodes to use a slice from the vectorized\n+    // batched add.","path":"lib/Optimizer/GraphOptimizer.cpp","position":168,"original_position":168,"commit_id":"d4ae8e104d5591eac2dfb3ea5cf191f7ed843d88","original_commit_id":"d4ae8e104d5591eac2dfb3ea5cf191f7ed843d88","user":{"login":"jfix71","id":1198212,"avatar_url":"https://avatars3.githubusercontent.com/u/1198212?v=4","gravatar_id":"","url":"https://api.github.com/users/jfix71","html_url":"https://github.com/jfix71","followers_url":"https://api.github.com/users/jfix71/followers","following_url":"https://api.github.com/users/jfix71/following{/other_user}","gists_url":"https://api.github.com/users/jfix71/gists{/gist_id}","starred_url":"https://api.github.com/users/jfix71/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfix71/subscriptions","organizations_url":"https://api.github.com/users/jfix71/orgs","repos_url":"https://api.github.com/users/jfix71/repos","events_url":"https://api.github.com/users/jfix71/events{/privacy}","received_events_url":"https://api.github.com/users/jfix71/received_events","type":"User","site_admin":false},"body":"nit: This comment is a bit confusing -- If I'm understanding correctly, might be better to say something like: \"Replace the original individual batched adds with corresponding slices from the new merged batch add.\"","created_at":"2018-05-21T20:55:15Z","updated_at":"2018-05-21T20:55:41Z","html_url":"https://github.com/pytorch/glow/pull/1051#discussion_r189711176","pull_request_url":"https://api.github.com/repos/pytorch/glow/pulls/1051","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/pytorch/glow/pulls/comments/189711176"},"html":{"href":"https://github.com/pytorch/glow/pull/1051#discussion_r189711176"},"pull_request":{"href":"https://api.github.com/repos/pytorch/glow/pulls/1051"}}},"pull_request":{"url":"https://api.github.com/repos/pytorch/glow/pulls/1051","id":189201998,"html_url":"https://github.com/pytorch/glow/pull/1051","diff_url":"https://github.com/pytorch/glow/pull/1051.diff","patch_url":"https://github.com/pytorch/glow/pull/1051.patch","issue_url":"https://api.github.com/repos/pytorch/glow/issues/1051","number":1051,"state":"open","locked":false,"title":"[optimizer] Merge multiple batched-add nodes into a single large node. ","user":{"login":"nadavrot","id":8635342,"avatar_url":"https://avatars3.githubusercontent.com/u/8635342?v=4","gravatar_id":"","url":"https://api.github.com/users/nadavrot","html_url":"https://github.com/nadavrot","followers_url":"https://api.github.com/users/nadavrot/followers","following_url":"https://api.github.com/users/nadavrot/following{/other_user}","gists_url":"https://api.github.com/users/nadavrot/gists{/gist_id}","starred_url":"https://api.github.com/users/nadavrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nadavrot/subscriptions","organizations_url":"https://api.github.com/users/nadavrot/orgs","repos_url":"https://api.github.com/users/nadavrot/repos","events_url":"https://api.github.com/users/nadavrot/events{/privacy}","received_events_url":"https://api.github.com/users/nadavrot/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-05-19T19:00:49Z","updated_at":"2018-05-21T20:55:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"54907fa343ae102d5769beecc2751d0778e5d388","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":706497924,"url":"https://api.github.com/repos/pytorch/glow/labels/CLA%20Signed","name":"CLA Signed","color":"009900","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/pytorch/glow/pulls/1051/commits","review_comments_url":"https://api.github.com/repos/pytorch/glow/pulls/1051/comments","review_comment_url":"https://api.github.com/repos/pytorch/glow/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pytorch/glow/issues/1051/comments","statuses_url":"https://api.github.com/repos/pytorch/glow/statuses/d4ae8e104d5591eac2dfb3ea5cf191f7ed843d88","head":{"label":"nadavrot:merge_batchadd","ref":"merge_batchadd","sha":"d4ae8e104d5591eac2dfb3ea5cf191f7ed843d88","user":{"login":"nadavrot","id":8635342,"avatar_url":"https://avatars3.githubusercontent.com/u/8635342?v=4","gravatar_id":"","url":"https://api.github.com/users/nadavrot","html_url":"https://github.com/nadavrot","followers_url":"https://api.github.com/users/nadavrot/followers","following_url":"https://api.github.com/users/nadavrot/following{/other_user}","gists_url":"https://api.github.com/users/nadavrot/gists{/gist_id}","starred_url":"https://api.github.com/users/nadavrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nadavrot/subscriptions","organizations_url":"https://api.github.com/users/nadavrot/orgs","repos_url":"https://api.github.com/users/nadavrot/repos","events_url":"https://api.github.com/users/nadavrot/events{/privacy}","received_events_url":"https://api.github.com/users/nadavrot/received_events","type":"User","site_admin":false},"repo":{"id":131927089,"name":"glow","full_name":"nadavrot/glow","owner":{"login":"nadavrot","id":8635342,"avatar_url":"https://avatars3.githubusercontent.com/u/8635342?v=4","gravatar_id":"","url":"https://api.github.com/users/nadavrot","html_url":"https://github.com/nadavrot","followers_url":"https://api.github.com/users/nadavrot/followers","following_url":"https://api.github.com/users/nadavrot/following{/other_user}","gists_url":"https://api.github.com/users/nadavrot/gists{/gist_id}","starred_url":"https://api.github.com/users/nadavrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nadavrot/subscriptions","organizations_url":"https://api.github.com/users/nadavrot/orgs","repos_url":"https://api.github.com/users/nadavrot/repos","events_url":"https://api.github.com/users/nadavrot/events{/privacy}","received_events_url":"https://api.github.com/users/nadavrot/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nadavrot/glow","description":"Compiler for Neural Network hardware accelerators","fork":true,"url":"https://api.github.com/repos/nadavrot/glow","forks_url":"https://api.github.com/repos/nadavrot/glow/forks","keys_url":"https://api.github.com/repos/nadavrot/glow/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nadavrot/glow/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nadavrot/glow/teams","hooks_url":"https://api.github.com/repos/nadavrot/glow/hooks","issue_events_url":"https://api.github.com/repos/nadavrot/glow/issues/events{/number}","events_url":"https://api.github.com/repos/nadavrot/glow/events","assignees_url":"https://api.github.com/repos/nadavrot/glow/assignees{/user}","branches_url":"https://api.github.com/repos/nadavrot/glow/branches{/branch}","tags_url":"https://api.github.com/repos/nadavrot/glow/tags","blobs_url":"https://api.github.com/repos/nadavrot/glow/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nadavrot/glow/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nadavrot/glow/git/refs{/sha}","trees_url":"https://api.github.com/repos/nadavrot/glow/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nadavrot/glow/statuses/{sha}","languages_url":"https://api.github.com/repos/nadavrot/glow/languages","stargazers_url":"https://api.github.com/repos/nadavrot/glow/stargazers","contributors_url":"https://api.github.com/repos/nadavrot/glow/contributors","subscribers_url":"https://api.github.com/repos/nadavrot/glow/subscribers","subscription_url":"https://api.github.com/repos/nadavrot/glow/subscription","commits_url":"https://api.github.com/repos/nadavrot/glow/commits{/sha}","git_commits_url":"https://api.github.com/repos/nadavrot/glow/git/commits{/sha}","comments_url":"https://api.github.com/repos/nadavrot/glow/comments{/number}","issue_comment_url":"https://api.github.com/repos/nadavrot/glow/issues/comments{/number}","contents_url":"https://api.github.com/repos/nadavrot/glow/contents/{+path}","compare_url":"https://api.github.com/repos/nadavrot/glow/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nadavrot/glow/merges","archive_url":"https://api.github.com/repos/nadavrot/glow/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nadavrot/glow/downloads","issues_url":"https://api.github.com/repos/nadavrot/glow/issues{/number}","pulls_url":"https://api.github.com/repos/nadavrot/glow/pulls{/number}","milestones_url":"https://api.github.com/repos/nadavrot/glow/milestones{/number}","notifications_url":"https://api.github.com/repos/nadavrot/glow/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nadavrot/glow/labels{/name}","releases_url":"https://api.github.com/repos/nadavrot/glow/releases{/id}","deployments_url":"https://api.github.com/repos/nadavrot/glow/deployments","created_at":"2018-05-03T01:51:06Z","updated_at":"2018-05-21T18:16:19Z","pushed_at":"2018-05-21T20:01:20Z","git_url":"git://github.com/nadavrot/glow.git","ssh_url":"git@github.com:nadavrot/glow.git","clone_url":"https://github.com/nadavrot/glow.git","svn_url":"https://github.com/nadavrot/glow","homepage":null,"size":6093,"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":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pytorch:master","ref":"master","sha":"ef0dd41090506ce5fe2a5af8dd4171927796c519","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":105281531,"name":"glow","full_name":"pytorch/glow","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/glow","description":"Compiler for Neural Network hardware accelerators","fork":false,"url":"https://api.github.com/repos/pytorch/glow","forks_url":"https://api.github.com/repos/pytorch/glow/forks","keys_url":"https://api.github.com/repos/pytorch/glow/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pytorch/glow/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pytorch/glow/teams","hooks_url":"https://api.github.com/repos/pytorch/glow/hooks","issue_events_url":"https://api.github.com/repos/pytorch/glow/issues/events{/number}","events_url":"https://api.github.com/repos/pytorch/glow/events","assignees_url":"https://api.github.com/repos/pytorch/glow/assignees{/user}","branches_url":"https://api.github.com/repos/pytorch/glow/branches{/branch}","tags_url":"https://api.github.com/repos/pytorch/glow/tags","blobs_url":"https://api.github.com/repos/pytorch/glow/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pytorch/glow/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pytorch/glow/git/refs{/sha}","trees_url":"https://api.github.com/repos/pytorch/glow/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pytorch/glow/statuses/{sha}","languages_url":"https://api.github.com/repos/pytorch/glow/languages","stargazers_url":"https://api.github.com/repos/pytorch/glow/stargazers","contributors_url":"https://api.github.com/repos/pytorch/glow/contributors","subscribers_url":"https://api.github.com/repos/pytorch/glow/subscribers","subscription_url":"https://api.github.com/repos/pytorch/glow/subscription","commits_url":"https://api.github.com/repos/pytorch/glow/commits{/sha}","git_commits_url":"https://api.github.com/repos/pytorch/glow/git/commits{/sha}","comments_url":"https://api.github.com/repos/pytorch/glow/comments{/number}","issue_comment_url":"https://api.github.com/repos/pytorch/glow/issues/comments{/number}","contents_url":"https://api.github.com/repos/pytorch/glow/contents/{+path}","compare_url":"https://api.github.com/repos/pytorch/glow/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pytorch/glow/merges","archive_url":"https://api.github.com/repos/pytorch/glow/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pytorch/glow/downloads","issues_url":"https://api.github.com/repos/pytorch/glow/issues{/number}","pulls_url":"https://api.github.com/repos/pytorch/glow/pulls{/number}","milestones_url":"https://api.github.com/repos/pytorch/glow/milestones{/number}","notifications_url":"https://api.github.com/repos/pytorch/glow/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pytorch/glow/labels{/name}","releases_url":"https://api.github.com/repos/pytorch/glow/releases{/id}","deployments_url":"https://api.github.com/repos/pytorch/glow/deployments","created_at":"2017-09-29T14:28:18Z","updated_at":"2018-05-21T19:54:33Z","pushed_at":"2018-05-21T20:01:21Z","git_url":"git://github.com/pytorch/glow.git","ssh_url":"git@github.com:pytorch/glow.git","clone_url":"https://github.com/pytorch/glow.git","svn_url":"https://github.com/pytorch/glow","homepage":null,"size":6067,"stargazers_count":497,"watchers_count":497,"language":"C++","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":44,"mirror_url":null,"archived":false,"open_issues_count":9,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":44,"open_issues":9,"watchers":497,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pytorch/glow/pulls/1051"},"html":{"href":"https://github.com/pytorch/glow/pull/1051"},"issue":{"href":"https://api.github.com/repos/pytorch/glow/issues/1051"},"comments":{"href":"https://api.github.com/repos/pytorch/glow/issues/1051/comments"},"review_comments":{"href":"https://api.github.com/repos/pytorch/glow/pulls/1051/comments"},"review_comment":{"href":"https://api.github.com/repos/pytorch/glow/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pytorch/glow/pulls/1051/commits"},"statuses":{"href":"https://api.github.com/repos/pytorch/glow/statuses/d4ae8e104d5591eac2dfb3ea5cf191f7ed843d88"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"jfix71"}}