{"action":"closed","number":285,"pull_request":{"url":"https://api.github.com/repos/torch/cutorch/pulls/285","id":51717740,"html_url":"https://github.com/torch/cutorch/pull/285","diff_url":"https://github.com/torch/cutorch/pull/285.diff","patch_url":"https://github.com/torch/cutorch/pull/285.patch","issue_url":"https://api.github.com/repos/torch/cutorch/issues/285","number":285,"state":"closed","locked":false,"title":"Fix tensor index bugs and performance; better testing","user":{"login":"wickedfoo","id":1911637,"avatar_url":"https://avatars.githubusercontent.com/u/1911637?v=3","gravatar_id":"","url":"https://api.github.com/users/wickedfoo","html_url":"https://github.com/wickedfoo","followers_url":"https://api.github.com/users/wickedfoo/followers","following_url":"https://api.github.com/users/wickedfoo/following{/other_user}","gists_url":"https://api.github.com/users/wickedfoo/gists{/gist_id}","starred_url":"https://api.github.com/users/wickedfoo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wickedfoo/subscriptions","organizations_url":"https://api.github.com/users/wickedfoo/orgs","repos_url":"https://api.github.com/users/wickedfoo/repos","events_url":"https://api.github.com/users/wickedfoo/events{/privacy}","received_events_url":"https://api.github.com/users/wickedfoo/received_events","type":"User","site_admin":false},"body":"The old non-contiguous indexSelect kernel was slow, and the code as checked in had a bug (the grid and block were swapped).\r\n\r\nAlthough I cannot explain it by reading the code (it looked correct), we noticed memory corruption behavior from the old contiguous indexSelect kernel as well that only went away when we disabled the contiguous version internally.\r\n\r\nI deleted the old kernels and rewrote them with something that is to my mind much simpler, and faster for all except the single outermost contiguous case (though in this case, not slow by much). For a very common case, index() of a 1-d tensor producing a 1-d result, the new kernel is much faster. Once this is accepted, I will replace the other index* kernels using a similar pattern.\r\n\r\nThe basic idea is that you can handle all dimensions except for the indexed dimension via normal indexing math in a tensor with the indexed dimension being size 1. The indexed dimension's offset (offset += index * stride[index_dim]) is then added in. The indexing is also specialized in the same way that the other pointwise etc. kernels are done as well.\r\n\r\nI also added much more strenuous testing: multi-dimensions, large tensors, transposed/tensors with holes. I started to refactor the tensor construction code in test.lua, and will try and sprinkle this around in more tests in subsequent pull requests, since everywhere we should be testing with large + small, contig + non-contig + non-contig-with-holes tensors, especially when there are kernel specializations for various cases.\r\n\r\nperformance:\r\n\r\n![image](https://cloud.githubusercontent.com/assets/1911637/11380733/e6d4c8e6-92c5-11e5-9151-2079935fa8a3.png)\r\n\r\ncode for the above perf numbers:\r\n```\r\nlocal function doTime(f)\r\n   local timer = torch.Timer()\r\n   cutorch.synchronize()\r\n   timer:reset()\r\n\r\n   f()\r\n   cutorch.synchronize()\r\n   print(timer:time().real * 1000)\r\nend\r\n\r\nlocal src = torch.CudaTensor(1000, 10, 1000):zero()\r\nlocal idx = torch.LongTensor({1, 3, 5, 7, 9}):cuda()\r\ndoTime(function()\r\n      local out = src:index(2, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(10, 1000, 1000):zero()\r\nlocal idx = torch.LongTensor({1, 3, 5, 7, 9}):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(1000, 10, 1000):zero()\r\nlocal idx = torch.linspace(1, 250, 250):mul(2):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(2, 5000, 1000):zero()\r\nlocal idx = torch.LongTensor({2}):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\n-- transposed\r\n\r\nlocal src = torch.CudaTensor(1000, 1000, 10):zero():transpose(2, 3)\r\nlocal idx = torch.LongTensor({1, 3, 5, 7, 9}):cuda()\r\ndoTime(function()\r\n      local out = src:index(2, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(10, 1000, 1000):zero():transpose(2, 3)\r\nlocal idx = torch.LongTensor({1, 3, 5, 7, 9}):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(1000, 1000, 10):zero():transpose(2, 3)\r\nlocal idx = torch.linspace(1, 250, 250):mul(2):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(2, 5000, 1000):zero():transpose(2, 3)\r\nlocal idx = torch.LongTensor({2}):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\n\r\nlocal src = torch.CudaTensor(10, 1000, 1000):zero():transpose(1, 2)\r\nlocal idx = torch.LongTensor({1, 3, 5, 7, 9}):cuda()\r\ndoTime(function()\r\n      local out = src:index(2, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(1000, 10, 1000):zero():transpose(1, 2)\r\nlocal idx = torch.LongTensor({1, 3, 5, 7, 9}):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(10, 1000, 1000):zero():transpose(1, 2)\r\nlocal idx = torch.linspace(1, 250, 250):mul(2):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\nlocal src = torch.CudaTensor(5000, 2, 1000):zero():transpose(1, 2)\r\nlocal idx = torch.LongTensor({2}):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n\r\n-- sparse 1-d\r\n\r\nlocal src = torch.CudaTensor(10000000):zero()\r\nlocal idx = torch.linspace(1, src:size(1) / 2, src:size(1) / 2):mul(2):cuda()\r\ndoTime(function()\r\n      local out = src:index(1, idx)\r\nend)\r\n```","created_at":"2015-11-24T21:13:18Z","updated_at":"2015-12-02T20:29:34Z","closed_at":"2015-12-02T20:29:34Z","merged_at":"2015-12-02T20:29:34Z","merge_commit_sha":"da79512ed5e85e206e2fd38e33a5208c34d4b6d8","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/torch/cutorch/pulls/285/commits","review_comments_url":"https://api.github.com/repos/torch/cutorch/pulls/285/comments","review_comment_url":"https://api.github.com/repos/torch/cutorch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/torch/cutorch/issues/285/comments","statuses_url":"https://api.github.com/repos/torch/cutorch/statuses/6369863df8c691e275759232803be27b75e2c266","head":{"label":"wickedfoo:index-changes","ref":"index-changes","sha":"6369863df8c691e275759232803be27b75e2c266","user":{"login":"wickedfoo","id":1911637,"avatar_url":"https://avatars.githubusercontent.com/u/1911637?v=3","gravatar_id":"","url":"https://api.github.com/users/wickedfoo","html_url":"https://github.com/wickedfoo","followers_url":"https://api.github.com/users/wickedfoo/followers","following_url":"https://api.github.com/users/wickedfoo/following{/other_user}","gists_url":"https://api.github.com/users/wickedfoo/gists{/gist_id}","starred_url":"https://api.github.com/users/wickedfoo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wickedfoo/subscriptions","organizations_url":"https://api.github.com/users/wickedfoo/orgs","repos_url":"https://api.github.com/users/wickedfoo/repos","events_url":"https://api.github.com/users/wickedfoo/events{/privacy}","received_events_url":"https://api.github.com/users/wickedfoo/received_events","type":"User","site_admin":false},"repo":{"id":43698445,"name":"cutorch","full_name":"wickedfoo/cutorch","owner":{"login":"wickedfoo","id":1911637,"avatar_url":"https://avatars.githubusercontent.com/u/1911637?v=3","gravatar_id":"","url":"https://api.github.com/users/wickedfoo","html_url":"https://github.com/wickedfoo","followers_url":"https://api.github.com/users/wickedfoo/followers","following_url":"https://api.github.com/users/wickedfoo/following{/other_user}","gists_url":"https://api.github.com/users/wickedfoo/gists{/gist_id}","starred_url":"https://api.github.com/users/wickedfoo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wickedfoo/subscriptions","organizations_url":"https://api.github.com/users/wickedfoo/orgs","repos_url":"https://api.github.com/users/wickedfoo/repos","events_url":"https://api.github.com/users/wickedfoo/events{/privacy}","received_events_url":"https://api.github.com/users/wickedfoo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/wickedfoo/cutorch","description":"A CUDA backend for Torch7","fork":true,"url":"https://api.github.com/repos/wickedfoo/cutorch","forks_url":"https://api.github.com/repos/wickedfoo/cutorch/forks","keys_url":"https://api.github.com/repos/wickedfoo/cutorch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wickedfoo/cutorch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wickedfoo/cutorch/teams","hooks_url":"https://api.github.com/repos/wickedfoo/cutorch/hooks","issue_events_url":"https://api.github.com/repos/wickedfoo/cutorch/issues/events{/number}","events_url":"https://api.github.com/repos/wickedfoo/cutorch/events","assignees_url":"https://api.github.com/repos/wickedfoo/cutorch/assignees{/user}","branches_url":"https://api.github.com/repos/wickedfoo/cutorch/branches{/branch}","tags_url":"https://api.github.com/repos/wickedfoo/cutorch/tags","blobs_url":"https://api.github.com/repos/wickedfoo/cutorch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wickedfoo/cutorch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wickedfoo/cutorch/git/refs{/sha}","trees_url":"https://api.github.com/repos/wickedfoo/cutorch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wickedfoo/cutorch/statuses/{sha}","languages_url":"https://api.github.com/repos/wickedfoo/cutorch/languages","stargazers_url":"https://api.github.com/repos/wickedfoo/cutorch/stargazers","contributors_url":"https://api.github.com/repos/wickedfoo/cutorch/contributors","subscribers_url":"https://api.github.com/repos/wickedfoo/cutorch/subscribers","subscription_url":"https://api.github.com/repos/wickedfoo/cutorch/subscription","commits_url":"https://api.github.com/repos/wickedfoo/cutorch/commits{/sha}","git_commits_url":"https://api.github.com/repos/wickedfoo/cutorch/git/commits{/sha}","comments_url":"https://api.github.com/repos/wickedfoo/cutorch/comments{/number}","issue_comment_url":"https://api.github.com/repos/wickedfoo/cutorch/issues/comments{/number}","contents_url":"https://api.github.com/repos/wickedfoo/cutorch/contents/{+path}","compare_url":"https://api.github.com/repos/wickedfoo/cutorch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wickedfoo/cutorch/merges","archive_url":"https://api.github.com/repos/wickedfoo/cutorch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wickedfoo/cutorch/downloads","issues_url":"https://api.github.com/repos/wickedfoo/cutorch/issues{/number}","pulls_url":"https://api.github.com/repos/wickedfoo/cutorch/pulls{/number}","milestones_url":"https://api.github.com/repos/wickedfoo/cutorch/milestones{/number}","notifications_url":"https://api.github.com/repos/wickedfoo/cutorch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wickedfoo/cutorch/labels{/name}","releases_url":"https://api.github.com/repos/wickedfoo/cutorch/releases{/id}","created_at":"2015-10-05T16:33:36Z","updated_at":"2015-10-05T16:33:37Z","pushed_at":"2015-12-02T20:27:14Z","git_url":"git://github.com/wickedfoo/cutorch.git","ssh_url":"git@github.com:wickedfoo/cutorch.git","clone_url":"https://github.com/wickedfoo/cutorch.git","svn_url":"https://github.com/wickedfoo/cutorch","homepage":"","size":10265,"stargazers_count":0,"watchers_count":0,"language":"Cuda","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"torch:master","ref":"master","sha":"b8ea7ebb4b82d231c485640c0dec4ad8fc0fb51f","user":{"login":"torch","id":4978924,"avatar_url":"https://avatars.githubusercontent.com/u/4978924?v=3","gravatar_id":"","url":"https://api.github.com/users/torch","html_url":"https://github.com/torch","followers_url":"https://api.github.com/users/torch/followers","following_url":"https://api.github.com/users/torch/following{/other_user}","gists_url":"https://api.github.com/users/torch/gists{/gist_id}","starred_url":"https://api.github.com/users/torch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/torch/subscriptions","organizations_url":"https://api.github.com/users/torch/orgs","repos_url":"https://api.github.com/users/torch/repos","events_url":"https://api.github.com/users/torch/events{/privacy}","received_events_url":"https://api.github.com/users/torch/received_events","type":"Organization","site_admin":false},"repo":{"id":13677478,"name":"cutorch","full_name":"torch/cutorch","owner":{"login":"torch","id":4978924,"avatar_url":"https://avatars.githubusercontent.com/u/4978924?v=3","gravatar_id":"","url":"https://api.github.com/users/torch","html_url":"https://github.com/torch","followers_url":"https://api.github.com/users/torch/followers","following_url":"https://api.github.com/users/torch/following{/other_user}","gists_url":"https://api.github.com/users/torch/gists{/gist_id}","starred_url":"https://api.github.com/users/torch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/torch/subscriptions","organizations_url":"https://api.github.com/users/torch/orgs","repos_url":"https://api.github.com/users/torch/repos","events_url":"https://api.github.com/users/torch/events{/privacy}","received_events_url":"https://api.github.com/users/torch/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/torch/cutorch","description":"A CUDA backend for Torch7","fork":false,"url":"https://api.github.com/repos/torch/cutorch","forks_url":"https://api.github.com/repos/torch/cutorch/forks","keys_url":"https://api.github.com/repos/torch/cutorch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/torch/cutorch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/torch/cutorch/teams","hooks_url":"https://api.github.com/repos/torch/cutorch/hooks","issue_events_url":"https://api.github.com/repos/torch/cutorch/issues/events{/number}","events_url":"https://api.github.com/repos/torch/cutorch/events","assignees_url":"https://api.github.com/repos/torch/cutorch/assignees{/user}","branches_url":"https://api.github.com/repos/torch/cutorch/branches{/branch}","tags_url":"https://api.github.com/repos/torch/cutorch/tags","blobs_url":"https://api.github.com/repos/torch/cutorch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/torch/cutorch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/torch/cutorch/git/refs{/sha}","trees_url":"https://api.github.com/repos/torch/cutorch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/torch/cutorch/statuses/{sha}","languages_url":"https://api.github.com/repos/torch/cutorch/languages","stargazers_url":"https://api.github.com/repos/torch/cutorch/stargazers","contributors_url":"https://api.github.com/repos/torch/cutorch/contributors","subscribers_url":"https://api.github.com/repos/torch/cutorch/subscribers","subscription_url":"https://api.github.com/repos/torch/cutorch/subscription","commits_url":"https://api.github.com/repos/torch/cutorch/commits{/sha}","git_commits_url":"https://api.github.com/repos/torch/cutorch/git/commits{/sha}","comments_url":"https://api.github.com/repos/torch/cutorch/comments{/number}","issue_comment_url":"https://api.github.com/repos/torch/cutorch/issues/comments{/number}","contents_url":"https://api.github.com/repos/torch/cutorch/contents/{+path}","compare_url":"https://api.github.com/repos/torch/cutorch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/torch/cutorch/merges","archive_url":"https://api.github.com/repos/torch/cutorch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/torch/cutorch/downloads","issues_url":"https://api.github.com/repos/torch/cutorch/issues{/number}","pulls_url":"https://api.github.com/repos/torch/cutorch/pulls{/number}","milestones_url":"https://api.github.com/repos/torch/cutorch/milestones{/number}","notifications_url":"https://api.github.com/repos/torch/cutorch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/torch/cutorch/labels{/name}","releases_url":"https://api.github.com/repos/torch/cutorch/releases{/id}","created_at":"2013-10-18T12:29:23Z","updated_at":"2015-11-30T09:52:31Z","pushed_at":"2015-12-02T20:29:34Z","git_url":"git://github.com/torch/cutorch.git","ssh_url":"git@github.com:torch/cutorch.git","clone_url":"https://github.com/torch/cutorch.git","svn_url":"https://github.com/torch/cutorch","homepage":"","size":11941,"stargazers_count":85,"watchers_count":85,"language":"Cuda","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":69,"mirror_url":null,"open_issues_count":34,"forks":69,"open_issues":34,"watchers":85,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/torch/cutorch/pulls/285"},"html":{"href":"https://github.com/torch/cutorch/pull/285"},"issue":{"href":"https://api.github.com/repos/torch/cutorch/issues/285"},"comments":{"href":"https://api.github.com/repos/torch/cutorch/issues/285/comments"},"review_comments":{"href":"https://api.github.com/repos/torch/cutorch/pulls/285/comments"},"review_comment":{"href":"https://api.github.com/repos/torch/cutorch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/torch/cutorch/pulls/285/commits"},"statuses":{"href":"https://api.github.com/repos/torch/cutorch/statuses/6369863df8c691e275759232803be27b75e2c266"}},"merged":true,"mergeable":null,"mergeable_state":"unknown","merged_by":{"login":"soumith","id":1310570,"avatar_url":"https://avatars.githubusercontent.com/u/1310570?v=3","gravatar_id":"","url":"https://api.github.com/users/soumith","html_url":"https://github.com/soumith","followers_url":"https://api.github.com/users/soumith/followers","following_url":"https://api.github.com/users/soumith/following{/other_user}","gists_url":"https://api.github.com/users/soumith/gists{/gist_id}","starred_url":"https://api.github.com/users/soumith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soumith/subscriptions","organizations_url":"https://api.github.com/users/soumith/orgs","repos_url":"https://api.github.com/users/soumith/repos","events_url":"https://api.github.com/users/soumith/events{/privacy}","received_events_url":"https://api.github.com/users/soumith/received_events","type":"User","site_admin":false},"comments":3,"review_comments":9,"commits":1,"additions":437,"deletions":291,"changed_files":8}}