-
Notifications
You must be signed in to change notification settings - Fork 136
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
[Feat]Copy-free save and load for cuckoo hashtable #243
Open
Lifann
wants to merge
7
commits into
tensorflow:master
Choose a base branch
from
Lifann:indep-table-save
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
78c4e5d
[fix] CI fail for protobuf update.
rhdong 35cf2ef
[fix] Fixed segment fault due to lambda may capture the thread_contex…
MoFHeka d262b69
[refactor] clean some warnings
rhdong ac8ec80
fix(comment): Fix error in comment.
Lifann 097bd21
Bugfix: rehashing in GPU hashtable is not enough when meeting large i…
Lifann c9acc63
Add ops of ExportToFile and ImportFromFile without full volume copying
Lifann b374514
[fix] stopping the Bazel in dev docker to update automatically .
rhdong 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,6 +304,18 @@ class CuckooHashTableOfTensors final : public LookupInterface { | |
return table_->export_values(ctx, value_dim); | ||
} | ||
|
||
Status SaveToFile(OpKernelContext* ctx, const string filepath, | ||
const size_t buffer_size) { | ||
int64 value_dim = value_shape_.dim_size(0); | ||
return table_->save_to_file(ctx, value_dim, filepath, buffer_size); | ||
} | ||
|
||
Status LoadFromFile(OpKernelContext* ctx, const string filepath, | ||
const size_t buffer_size) { | ||
int64 value_dim = value_shape_.dim_size(0); | ||
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. int64_t |
||
return table_->load_from_file(ctx, value_dim, filepath, buffer_size); | ||
} | ||
|
||
DataType key_dtype() const override { return DataTypeToEnum<K>::v(); } | ||
|
||
DataType value_dtype() const override { return DataTypeToEnum<V>::v(); } | ||
|
@@ -607,6 +619,36 @@ class HashTableExportOp : public HashTableOpKernel { | |
} | ||
}; | ||
|
||
// Op that export all keys and values to file. | ||
template <class K, class V> | ||
class HashTableExportToFileOp : public HashTableOpKernel { | ||
public: | ||
explicit HashTableExportToFileOp(OpKernelConstruction* ctx) | ||
: HashTableOpKernel(ctx) { | ||
int64 signed_buffer_size = 0; | ||
ctx->GetAttr("buffer_size", &signed_buffer_size); | ||
buffer_size_ = static_cast<size_t>(signed_buffer_size); | ||
} | ||
|
||
void Compute(OpKernelContext* ctx) override { | ||
LookupInterface* table; | ||
OP_REQUIRES_OK(ctx, GetTable(ctx, &table)); | ||
core::ScopedUnref unref_me(table); | ||
|
||
const Tensor& ftensor = ctx->input(1); | ||
OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(ftensor.shape()), | ||
errors::InvalidArgument("filepath must be scalar.")); | ||
string filepath = string(ftensor.scalar<tstring>()().data()); | ||
|
||
lookup::CuckooHashTableOfTensors<K, V>* table_cuckoo = | ||
(lookup::CuckooHashTableOfTensors<K, V>*)table; | ||
OP_REQUIRES_OK(ctx, table_cuckoo->SaveToFile(ctx, filepath, buffer_size_)); | ||
} | ||
|
||
private: | ||
size_t buffer_size_; | ||
}; | ||
|
||
// Clear the table and insert data. | ||
class HashTableImportOp : public HashTableOpKernel { | ||
public: | ||
|
@@ -637,6 +679,37 @@ class HashTableImportOp : public HashTableOpKernel { | |
} | ||
}; | ||
|
||
// Op that export all keys and values to file. | ||
template <class K, class V> | ||
class HashTableImportFromFileOp : public HashTableOpKernel { | ||
public: | ||
explicit HashTableImportFromFileOp(OpKernelConstruction* ctx) | ||
: HashTableOpKernel(ctx) { | ||
int64 signed_buffer_size = 0; | ||
ctx->GetAttr("buffer_size", &signed_buffer_size); | ||
buffer_size_ = static_cast<size_t>(signed_buffer_size); | ||
} | ||
|
||
void Compute(OpKernelContext* ctx) override { | ||
LookupInterface* table; | ||
OP_REQUIRES_OK(ctx, GetTable(ctx, &table)); | ||
core::ScopedUnref unref_me(table); | ||
|
||
const Tensor& ftensor = ctx->input(1); | ||
OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(ftensor.shape()), | ||
errors::InvalidArgument("filepath must be scalar.")); | ||
string filepath = string(ftensor.scalar<tstring>()().data()); | ||
|
||
lookup::CuckooHashTableOfTensors<K, V>* table_cuckoo = | ||
(lookup::CuckooHashTableOfTensors<K, V>*)table; | ||
OP_REQUIRES_OK(ctx, | ||
table_cuckoo->LoadFromFile(ctx, filepath, buffer_size_)); | ||
} | ||
|
||
private: | ||
size_t buffer_size_; | ||
}; | ||
|
||
REGISTER_KERNEL_BUILDER( | ||
Name(PREFIX_OP_NAME(CuckooHashTableFind)).Device(DEVICE_CPU), | ||
HashTableFindOp); | ||
|
@@ -679,7 +752,17 @@ REGISTER_KERNEL_BUILDER( | |
.Device(DEVICE_CPU) \ | ||
.TypeConstraint<key_dtype>("Tin") \ | ||
.TypeConstraint<value_dtype>("Tout"), \ | ||
HashTableFindWithExistsOp<key_dtype, value_dtype>); | ||
HashTableFindWithExistsOp<key_dtype, value_dtype>); \ | ||
REGISTER_KERNEL_BUILDER(Name(PREFIX_OP_NAME(CuckooHashTableExportToFile)) \ | ||
.Device(DEVICE_CPU) \ | ||
.TypeConstraint<key_dtype>("key_dtype") \ | ||
.TypeConstraint<value_dtype>("value_dtype"), \ | ||
HashTableExportToFileOp<key_dtype, value_dtype>); \ | ||
REGISTER_KERNEL_BUILDER(Name(PREFIX_OP_NAME(CuckooHashTableImportFromFile)) \ | ||
.Device(DEVICE_CPU) \ | ||
.TypeConstraint<key_dtype>("key_dtype") \ | ||
.TypeConstraint<value_dtype>("value_dtype"), \ | ||
HashTableImportFromFileOp<key_dtype, value_dtype>); | ||
|
||
REGISTER_KERNEL(int32, double); | ||
REGISTER_KERNEL(int32, float); | ||
|
Oops, something went wrong.
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.
int64_t
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.
tensorflow::int64 is returned from
dim_size
and also used in table_->save()