-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-grown sparse table #9897
Merged
Yancey1989
merged 9 commits into
PaddlePaddle:develop
from
Yancey1989:auto_grwon_sparse_table
Apr 19, 2018
Merged
Auto-grown sparse table #9897
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ac7cb94
auto-grown sparse table
Yancey1989 1915254
fix ci
Yancey1989 ed6241c
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into auto_gr…
Yancey1989 ca32750
update
Yancey1989 2917a75
add some comments for sparse table
Yancey1989 b920b51
rename parameter
Yancey1989 70bf732
refine get interface
Yancey1989 13e7194
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into auto_gr…
Yancey1989 f12b3f3
use memcpy
Yancey1989 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,52 @@ limitations under the License. */ | |
namespace paddle { | ||
namespace framework { | ||
|
||
struct ReAllocateVisitor { | ||
ReAllocateVisitor(framework::Tensor* tensor, const framework::DDim& dims) | ||
: tensor_(tensor), dims_(dims) {} | ||
|
||
template <typename T> | ||
void operator()() const { | ||
framework::Tensor cpu_tensor; | ||
platform::CPUPlace cpu; | ||
T* ptr = cpu_tensor.mutable_data<T>(dims_, cpu); | ||
const T* old_ptr = | ||
tensor_->memory_size() == 0 ? nullptr : tensor_->data<T>(); | ||
if (old_ptr != nullptr) { | ||
std::copy(old_ptr, old_ptr + tensor_->numel(), ptr); | ||
} | ||
tensor_->ShareDataWith(cpu_tensor); | ||
} | ||
|
||
framework::Tensor* tensor_; | ||
framework::DDim dims_; | ||
}; | ||
|
||
struct TensorCopyVisitor { | ||
TensorCopyVisitor(framework::Tensor* dst, int64_t dst_offset, | ||
const framework::Tensor src, int64_t src_offset, | ||
int64_t size) | ||
: dst_(dst), | ||
dst_offset_(dst_offset), | ||
src_(src), | ||
src_offset_(src_offset), | ||
size_(size) {} | ||
|
||
template <typename T> | ||
void operator()() const { | ||
// TODO(Yancey1989): support other place | ||
platform::CPUPlace cpu; | ||
memory::Copy(cpu, dst_->mutable_data<T>(cpu) + dst_offset_, cpu, | ||
src_.data<T>() + src_offset_, size_ * sizeof(T)); | ||
} | ||
|
||
framework::Tensor* dst_; | ||
int64_t dst_offset_; | ||
framework::Tensor src_; | ||
int64_t src_offset_; | ||
int64_t size_; | ||
}; | ||
|
||
void SerializeToStream(std::ostream& os, const SelectedRows& selected_rows, | ||
const platform::DeviceContext& dev_ctx) { | ||
{ // the 1st field, uint32_t version | ||
|
@@ -69,5 +115,66 @@ void DeserializeFromStream(std::istream& is, SelectedRows* selected_rows, | |
TensorFromStream(is, selected_rows->mutable_value(), dev_ctx); | ||
} | ||
|
||
bool SelectedRows::HasKey(int64_t key) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add some comments here to explain why we need these APIs for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
return std::find(rows_.begin(), rows_.end(), key) == rows_.end() ? false | ||
: true; | ||
} | ||
|
||
std::vector<int64_t> SelectedRows::Get(std::vector<int64_t> keys, | ||
framework::Tensor* value) const { | ||
PADDLE_ENFORCE(value->IsInitialized(), | ||
"The value tensor should be initialized."); | ||
std::vector<int64_t> non_keys; | ||
int64_t value_width = value_->numel() / value_->dims()[0]; | ||
PADDLE_ENFORCE_EQ(value_width, value->numel() / value->dims()[0], | ||
"output tensor should have the same shape with table " | ||
"execpt the dims[0]."); | ||
|
||
for (size_t i = 0; i < keys.size(); ++i) { | ||
int64_t index = Index(keys[i]); | ||
if (index == -1) { | ||
non_keys.push_back(keys[i]); | ||
} else { | ||
framework::VisitDataType( | ||
framework::ToDataType(value_->type()), | ||
TensorCopyVisitor(value, i * value_width, *value_.get(), | ||
index * value_width, value_width)); | ||
} | ||
} | ||
return non_keys; | ||
} | ||
|
||
bool SelectedRows::Set(int64_t key, const framework::Tensor& value) { | ||
PADDLE_ENFORCE(value.IsInitialized(), "The value should be initialized."); | ||
if (value_->IsInitialized()) { | ||
PADDLE_ENFORCE_EQ( | ||
value.type(), value_->type(), | ||
"The type of the value should be same with the original value"); | ||
} | ||
PADDLE_ENFORCE_EQ(value.dims()[0], static_cast<size_t>(1), | ||
"The first dim of value should be 1."); | ||
auto index = Index(key); | ||
bool is_new_key = false; | ||
if (index == -1) { | ||
rows_.push_back(key); | ||
index = rows_.size() - 1; | ||
is_new_key = true; | ||
// whether need to resize the table | ||
if (static_cast<int64_t>(rows_.size()) > value_->dims()[0]) { | ||
auto dims = value_->dims(); | ||
dims[0] = (dims[0] + 1) << 1; | ||
framework::VisitDataType(framework::ToDataType(value.type()), | ||
ReAllocateVisitor(value_.get(), dims)); | ||
} | ||
} | ||
|
||
framework::VisitDataType( | ||
framework::ToDataType(value.type()), | ||
TensorCopyVisitor(value_.get(), | ||
index * value_->numel() / value_->dims()[0], value, | ||
static_cast<int64_t>(0), value.numel())); | ||
return is_new_key; | ||
} | ||
|
||
} // namespace framework | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to use
memory::copy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.