add Share{Data,Diff} methods to blobs to enable "virtual" copies #318
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds methods
ShareDataandShareDiffso that explicit O(N) copies by value can be replaced with O(1) copies by reference using the=operator onshared_ptr<SyncedMem>s which hold the blob data.This makes
FlattenLayers essentially free andSplitLayers substantially cheaper -- only needing to do anaxpyin the backward pass (which is mathematically necessary).On my machine, this speeds up
FlattenLayerTestfrom ~20s to ~12s_, with the vast majority of the speedup due to TestGPUGradient since FlattenLayer no longer actually does any GPU computation. TheSplitLayerTesttiming 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 (changetest_split_layerline 27 toblob_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...