Skip to content

Conversation

@mcarilli
Copy link
Collaborator

@mcarilli mcarilli commented Jul 6, 2018

This PR improves perfomance of (formerly) latency-bound non-contig-dim reduction kernels by up to 20X, while maintaining determinism.

Currently, reducing across a non-contiguous dimension uses the parallelism exposed across the number of output elements. This means that performance suffers if the number of output elements is small. Example:

a = torch.cuda.FloatTensor(32768, 32)
a.sum(dim=0)

Before this PR, a.sum's kernel (kernelReduceNoncontigDim_shared) took 138 microseconds on my machine. The speed-of-light estimate (based on a bandwidth of 700 GB/s) should be around 6 microseconds. After this PR's changes, a.sum(dim=0)'s kernel takes 6.9 microseconds on my machine.

Christian implemented some nice logic to squeeze out better performance for cases like a.sum using intra-block and instruction-level parallelism across the dimension being reduced, but his kernel still only launched one block for every 32 output elements. This was insufficient to saturate the device in many cases, like a.sum here (where only one block is launched).

My PR adds block cooperation across the dimension being reduced. Many blocks, instead of one block, help to reduce into each 32 output elements. Internally, each block leverages all of Christian's nice logic to compute a partial reduction into a per-block staging buffer, then the last block to finish combines the results to compute the final output.

Block cooperation does require THCudaMalloc-ing staging and semaphore buffers, so it's not always worthwhile. I included a set of rough heuristics to decide when the kernel should choose to use block cooperation. These heuristics are based on Python-side timings of calling sum() many times in a loop, and comparing to the old implementation.

I tested a wide range of sizes (to determine heuristics) and as long as the number of output elements is greater than 16ish, I don't think there are any remaining pathological sizes where users will encounter unexpectedly poor performance.

@mcarilli
Copy link
Collaborator Author

mcarilli commented Jul 6, 2018

There is one failure, which appears unrelated, especially since everything else passed.

...
19:42:55 lib/python2.7/dist-packages/caffe2/python/parallel_workers_test.py::ParallelWorkersTest::testParallelWorkersShutdownFun PASSED [ 19%]
19:42:55 lib/python2.7/dist-packages/caffe2/python/parallelize_bmuf_distributed_test.py::DistributedTest::test_bmuf_distributed Build timed out (after 45 minutes). Marking the build as failed.
20:24:28 Build was aborted
...

@mcarilli
Copy link
Collaborator Author

Has anyone had a chance to look at this yet? Only one file is affected, and there's a pretty significant benefit. The kernel does underlie a lot of core Pytorch ops, so I don't mind if the review process needs to be slow and careful, I just want to make sure people are aware.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colesbury has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@colesbury
Copy link
Member

Nice speed-up!

@mcarilli
Copy link
Collaborator Author

@colesbury thanks for the quick movement! I wasn't trying to rush you, just making sure it didn't get lost in the noise.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Jul 13, 2018
… reductions (#9214)

Summary:
This PR improves perfomance of (formerly) latency-bound non-contig-dim reduction kernels by up to 20X, while maintaining determinism.

Currently, reducing across a non-contiguous dimension uses the parallelism exposed across the number of output elements.  This means that performance suffers if the number of output elements is small.  Example:
```
a = torch.cuda.FloatTensor(32768, 32)
a.sum(dim=0)
```
Before this PR, `a.sum`'s kernel (kernelReduceNoncontigDim_shared) took 138 microseconds on my machine.  The speed-of-light estimate (based on a bandwidth of 700 GB/s) should be around 6 microseconds.  After this PR's changes, `a.sum(dim=0)`'s kernel takes 6.9 microseconds on my machine.

Christian implemented some nice logic to squeeze out better performance for cases like `a.sum` using intra-block and instruction-level parallelism across the dimension being reduced, but his kernel still only launched one block for every 32 output elements.  This was insufficient to saturate the device in many cases, like `a.sum` here (where only one block is launched).

My PR adds block cooperation across the dimension being reduced.  Many blocks, instead of one block, help to reduce into each 32 output elements.  Internally, each block leverages all of Christian's nice logic to compute a partial reduction into a per-block staging buffer, then the last block to finish combines the results to compute the final output.

Block cooperation does require THCudaMalloc-ing staging and semaphore buffers, so it's not always worthwhile.  I included a set of rough heuristics to decide when the kernel should choose to use block cooperation.  These heuristics are based on Python-side timings of calling sum() many times in a loop, and comparing to the old implementation.

 I tested a wide range of sizes (to determine heuristics) and as long as the number of output elements is greater than 16ish, I don't think there are any remaining pathological sizes where users will encounter unexpectedly poor performance.
Pull Request resolved: pytorch/pytorch#9214

Reviewed By: gchanan

Differential Revision: D8808127

Pulled By: colesbury

fbshipit-source-id: 139f310fc6ea6d187a7c983128f8eb8e1c9b4be3
zdevito pushed a commit to zdevito/ATen that referenced this pull request Jul 13, 2018
… reductions (#9214)

Summary:
This PR improves perfomance of (formerly) latency-bound non-contig-dim reduction kernels by up to 20X, while maintaining determinism.

Currently, reducing across a non-contiguous dimension uses the parallelism exposed across the number of output elements.  This means that performance suffers if the number of output elements is small.  Example:
```
a = torch.cuda.FloatTensor(32768, 32)
a.sum(dim=0)
```
Before this PR, `a.sum`'s kernel (kernelReduceNoncontigDim_shared) took 138 microseconds on my machine.  The speed-of-light estimate (based on a bandwidth of 700 GB/s) should be around 6 microseconds.  After this PR's changes, `a.sum(dim=0)`'s kernel takes 6.9 microseconds on my machine.

Christian implemented some nice logic to squeeze out better performance for cases like `a.sum` using intra-block and instruction-level parallelism across the dimension being reduced, but his kernel still only launched one block for every 32 output elements.  This was insufficient to saturate the device in many cases, like `a.sum` here (where only one block is launched).

My PR adds block cooperation across the dimension being reduced.  Many blocks, instead of one block, help to reduce into each 32 output elements.  Internally, each block leverages all of Christian's nice logic to compute a partial reduction into a per-block staging buffer, then the last block to finish combines the results to compute the final output.

Block cooperation does require THCudaMalloc-ing staging and semaphore buffers, so it's not always worthwhile.  I included a set of rough heuristics to decide when the kernel should choose to use block cooperation.  These heuristics are based on Python-side timings of calling sum() many times in a loop, and comparing to the old implementation.

 I tested a wide range of sizes (to determine heuristics) and as long as the number of output elements is greater than 16ish, I don't think there are any remaining pathological sizes where users will encounter unexpectedly poor performance.
Pull Request resolved: pytorch/pytorch#9214

Reviewed By: gchanan

Differential Revision: D8808127

Pulled By: colesbury

fbshipit-source-id: 139f310fc6ea6d187a7c983128f8eb8e1c9b4be3
goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
… reductions (pytorch#9214)

Summary:
This PR improves perfomance of (formerly) latency-bound non-contig-dim reduction kernels by up to 20X, while maintaining determinism.

Currently, reducing across a non-contiguous dimension uses the parallelism exposed across the number of output elements.  This means that performance suffers if the number of output elements is small.  Example:
```
a = torch.cuda.FloatTensor(32768, 32)
a.sum(dim=0)
```
Before this PR, `a.sum`'s kernel (kernelReduceNoncontigDim_shared) took 138 microseconds on my machine.  The speed-of-light estimate (based on a bandwidth of 700 GB/s) should be around 6 microseconds.  After this PR's changes, `a.sum(dim=0)`'s kernel takes 6.9 microseconds on my machine.

Christian implemented some nice logic to squeeze out better performance for cases like `a.sum` using intra-block and instruction-level parallelism across the dimension being reduced, but his kernel still only launched one block for every 32 output elements.  This was insufficient to saturate the device in many cases, like `a.sum` here (where only one block is launched).

My PR adds block cooperation across the dimension being reduced.  Many blocks, instead of one block, help to reduce into each 32 output elements.  Internally, each block leverages all of Christian's nice logic to compute a partial reduction into a per-block staging buffer, then the last block to finish combines the results to compute the final output.

Block cooperation does require THCudaMalloc-ing staging and semaphore buffers, so it's not always worthwhile.  I included a set of rough heuristics to decide when the kernel should choose to use block cooperation.  These heuristics are based on Python-side timings of calling sum() many times in a loop, and comparing to the old implementation.

 I tested a wide range of sizes (to determine heuristics) and as long as the number of output elements is greater than 16ish, I don't think there are any remaining pathological sizes where users will encounter unexpectedly poor performance.
Pull Request resolved: pytorch#9214

Reviewed By: gchanan

Differential Revision: D8808127

Pulled By: colesbury

fbshipit-source-id: 139f310fc6ea6d187a7c983128f8eb8e1c9b4be3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants