{"action":"created","issue":{"url":"https://api.github.com/repos/torch/cunn/issues/250","repository_url":"https://api.github.com/repos/torch/cunn","labels_url":"https://api.github.com/repos/torch/cunn/issues/250/labels{/name}","comments_url":"https://api.github.com/repos/torch/cunn/issues/250/comments","events_url":"https://api.github.com/repos/torch/cunn/issues/250/events","html_url":"https://github.com/torch/cunn/pull/250","id":148468525,"number":250,"title":"Add SpatialClassNLLCriterion","user":{"login":"apaszke","id":4583066,"avatar_url":"https://avatars.githubusercontent.com/u/4583066?v=3","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},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-04-14T20:07:54Z","updated_at":"2016-04-26T08:10:13Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/torch/cunn/pulls/250","html_url":"https://github.com/torch/cunn/pull/250","diff_url":"https://github.com/torch/cunn/pull/250.diff","patch_url":"https://github.com/torch/cunn/pull/250.patch"},"body":"Original `ClassNLLCriterion` is awfully slow when it comes to spatial data. It forces you to transpose it to a 2D Tensor (NCHW -> NHWC), which is pretty expensive by itself for big tensors, and then it uses a **hardcoded number of 32 threads** for processing (e.g. 1M elements)... This means that around 80% of my training loop is spent doing criterion stuff.\r\n\r\nHere is a nice comparison from visual profiler:\r\n<img width=\"581\" alt=\"screen shot 2016-04-14 at 21 52 22\" src=\"https://cloud.githubusercontent.com/assets/4583066/14541741/b6289700-028b-11e6-884e-bc5a2b648d57.png\">\r\nOn the left 100x fw+bw pass of SpatialClassNLLCriterion, on the right 1 fw+bw of ClassNLLCriterion (not including transpose).\r\n\r\nThese are the times for `4x20x512x1024` input and `4x512x1024` targets (100 runs):\r\n\r\n| Criterion                                      | Time for 100 runs (s) |\r\n|------------------------------------------------|-----------------------|\r\n| SpatialClassNLLCriterion                       | 0.15400      |\r\n| ClassNLLCriterion                              | 4.03642       |\r\n| Transpose + ClassNLLCriterion + Transpose back | 21.66718       |\r\n\r\nIf you agree to merge this module here I will also provide Lua code and write a CPU version for `nn`. This can speed up `cudnn.SpatialCrossEntropyCriterion` significantly."},"comment":{"url":"https://api.github.com/repos/torch/cunn/issues/comments/214662778","html_url":"https://github.com/torch/cunn/pull/250#issuecomment-214662778","issue_url":"https://api.github.com/repos/torch/cunn/issues/250","id":214662778,"user":{"login":"apaszke","id":4583066,"avatar_url":"https://avatars.githubusercontent.com/u/4583066?v=3","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},"created_at":"2016-04-26T08:10:13Z","updated_at":"2016-04-26T08:10:13Z","body":"@soumith Yeah, I have a test case that compares this implementation to ClassNLLCriterion, but it also uses a lua part of SpatialClassNLLCriterion (I only don't have a CPU part at the moment). Should I send a full PR to nn first or would you like me to check raw THCUNN functions?"}}