Skip to content
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

Improve load time on serialized entries. #67370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 23 additions & 14 deletions tensorflow/lite/delegates/serialization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include <errno.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>

#include <cstring>
Expand Down Expand Up @@ -193,22 +194,30 @@ TfLiteStatus SerializationEntry::GetData(TfLiteContext* context,
std::strerror(errno));
return kTfLiteDelegateDataReadError;
}
char buffer[512];
while (true) {
int bytes_read = read(fd, buffer, 512);
if (bytes_read == 0) {
// EOF
close(fd);
return kTfLiteOk;
} else if (bytes_read < 0) {
close(fd);
TF_LITE_KERNEL_LOG(context, "Error reading %s: %s", filepath.c_str(),
std::strerror(errno));
return kTfLiteDelegateDataReadError;
} else {
data->append(buffer, bytes_read);

struct stat file_stat;
if (fstat(fd, &file_stat) < 0) {
close(fd);
TF_LITE_KERNEL_LOG(context, "Could not fstat %s: %s", filepath.c_str(),
std::strerror(errno));
return kTfLiteDelegateDataReadError;
}
data->resize(file_stat.st_size);

size_t total_read = 0;
while (total_read < data->size()) {
ssize_t bytes_read = read(fd, data->data() + total_read, data->size() - total_read);
total_read += bytes_read;

if (bytes_read < 0) {
close(fd);
TF_LITE_KERNEL_LOG(context, "Error reading %s: %s", filepath.c_str(),
std::strerror(errno));
return kTfLiteDelegateDataReadError;
}
}

close(fd);
#endif // defined(_WIN32)

TFLITE_LOG_PROD(TFLITE_LOG_INFO,
Expand Down