Skip to content

Conversation

@jeffdonahue
Copy link
Contributor

This PR adds methods ShareData and ShareDiff so that explicit O(N) copies by value can be replaced with O(1) copies by reference using the = operator on shared_ptr<SyncedMem>s which hold the blob data.

This makes FlattenLayers essentially free and SplitLayers substantially cheaper -- only needing to do an axpy in the backward pass (which is mathematically necessary).

On my machine, this speeds up FlattenLayerTest from ~20s to ~12s_, with the vast majority of the speedup due to TestGPUGradient since FlattenLayer no longer actually does any GPU computation. The SplitLayerTest timing does not change much because the copies in the unit test are so small to begin with, but when I do the test on a somewhat realistic input size (change test_split_layer line 27 to blob_bottom_(new Blob<Dtype>(1000, 3, 200, 200)) instead of (2, 3, 6, 5)) and run using ./build/test/test_split_layer.testbin --gtest_filter="-_Gradient*" (suppresses gradient checking tests because they become absurdly slow with this blob size), I see a speedup from ~176s to ~171s. Not huge, but basically a free improvement.

*These unit tests (which I wrote) are way too slow even with this improvement, but that's a problem to address in another PR...

Copy link
Contributor

Choose a reason for hiding this comment

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

Share{Data,Diff}? Adoption usually means that a child is transferred from one family to another one. But in this situation, the shared_ptr is contained in both Blobs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks.

@jeffdonahue jeffdonahue changed the title add Adopt{Data,Diff} methods to blobs to enable "virtual" copies add Share{Data,Diff} methods to blobs to enable "virtual" copies Apr 12, 2014
@shelhamer
Copy link
Member

This is a sweet change Jeff! Fewer ops, less memory, and simplified splitting logic are all good. This sets the stage for weight sharing too (which I hope to hack at ICLR).

shelhamer added a commit that referenced this pull request Apr 13, 2014
add Share{Data,Diff} methods to blobs to enable "virtual" copies

- reduce operations by not copying values
- spare memory by shared_ptrs
- simplify SplitLayer logic
@shelhamer shelhamer merged commit 588c8e5 into BVLC:dev Apr 13, 2014
@jeffdonahue jeffdonahue deleted the blob-copy-by-reference branch April 15, 2014 17:17
@shelhamer shelhamer mentioned this pull request May 20, 2014
mitmul pushed a commit to mitmul/caffe that referenced this pull request Sep 30, 2014
add Share{Data,Diff} methods to blobs to enable "virtual" copies

- reduce operations by not copying values
- spare memory by shared_ptrs
- simplify SplitLayer logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants