add TString deallocation step - #128
Closed
dskkato wants to merge 1 commit into
Closed
Conversation
ljn917
commented
Sep 28, 2021
Contributor
I believe this should be merged, though not tested. Thanks. In addition, we also need to add test cases for the string typed tensor. |
serizbaforce-pushed
the
master
branch
2 times, most recently
from
February 28, 2022 10:20
3326662 to
c98a475Comparens-wxin
reviewed
May 24, 2022
There was a problem hiding this comment.
My simple test doesn't complain memory leak any more. However, when running against TF_TString {1} model, I got "heap-use-after-free". I can share with you my model if you would like to try it yourself.
The following code doesn't complain memory leak anymore.
int main(int argc, char **argv) {
// string modelPath = argv[1];
string sent1 {"I enjoy taking long walks\n along the beach with my dog."};
string sent2 {"I taking long walks\n along the beach with my dog."};
// auto sentence = cppflow::tensor(text);
auto sentence = cppflow::tensor(sent1);
auto sentence2 = cppflow::tensor(sent2);
std::cout << "tensor: " << sentence << std::endl;
// vector<TF_TString> data = sentence.get_data<TF_TString>();
// cppflow::model model(modelPath);
return 0;
}
However, the following code caused the "heap-use-after-free".
"tensor.h":
template<>
inline tensor::tensor(const std::string& value) {
TF_TString tstr[1];
TF_TString_Init(&tstr[0]);
TF_TString_Copy(&tstr[0], value.c_str(), value.size());
// *this = tensor(static_cast<enum TF_DataType>(TF_STRING), (void *) tstr, sizeof(tstr), {});
*this = tensor(static_cast<enum TF_DataType>(TF_STRING), (void *) tstr, sizeof(tstr), {1});
TF_TString_Dealloc(&tstr[0]);
}
my test code:
vector<float> generateEmbedding(cppflow::model model, string text) {
// vector<string> textVec(1);
// textVec.push_back(text);
cppflow::tensor sentence = cppflow::tensor(text);
// cppflow::tensor sentence = cppflow::tensor(textVec);
std::vector<cppflow::tensor> output = model({{"serving_default_sentences:0", sentence}},{"StatefulPartitionedCall_2:0"});
// vector<TF_TString> rawData = sentence.get_data<TF_TString>();
return convertModelResult(output);
}
serizba
commented
Sep 23, 2022
Owner
This solution does not work, as it deallocates before using the tensor. |
This file contains hidden or 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
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When I compare TString usage in cppflow and tensorflow's unittest, it seems that deallocation step may be required. But I did not check actual implementation of TString, so if I am wrong, please let it go.
https://github.com/tensorflow/tensorflow/blob/2305f9d0a58921987788fbd1d8fa43eda888233f/tensorflow/core/platform/ctstring_test.cc#L34-L68