123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- From 9488411c0779c22ba93fb03994e90cda25b65bd0 Mon Sep 17 00:00:00 2001
- From: Robert Ogden <robertogden@chromium.org>
- Date: Wed, 25 May 2022 11:03:30 -0700
- Subject: [PATCH 7/9] remove support for creating a model handler from a file
- ---
- third_party/tflite_support/README.chromium | 1 +
- .../cc/task/core/external_file_handler.cc | 137 +-----------------
- .../cc/task/core/external_file_handler.h | 20 ---
- .../cc/task/core/tflite_engine.cc | 2 -
- .../cc/task/core/tflite_engine.h | 2 -
- 5 files changed, 8 insertions(+), 154 deletions(-)
- diff --git a/third_party/tflite_support/README.chromium b/third_party/tflite_support/README.chromium
- index 3d5f5dbf2edf9..c1da2b4e73efe 100644
- --- a/third_party/tflite_support/README.chromium
- +++ b/third_party/tflite_support/README.chromium
- @@ -37,6 +37,7 @@ is a no-op in chromium builds and upsets clang.
- Update Process (internal: http://shortn/_nwz8liqimy):
- 1) Run these commands:
- ```
- +
- pushd third_party/tflite_support/
- rm -rf src/
- git clone https://github.com/tensorflow/tflite-support/
- diff --git a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
- index 5e17e14dc5f7a..9c4cc2009baea 100644
- --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
- +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
- @@ -15,12 +15,6 @@ limitations under the License.
-
- #include "tensorflow_lite_support/cc/task/core/external_file_handler.h"
-
- -#include <errno.h>
- -#include <fcntl.h>
- -#include <stddef.h>
- -#include <sys/mman.h>
- -#include <unistd.h>
- -
- #include <memory>
- #include <string>
-
- @@ -40,18 +34,6 @@ using ::tflite::support::CreateStatusWithPayload;
- using ::tflite::support::StatusOr;
- using ::tflite::support::TfLiteSupportStatus;
-
- -// Gets the offset aligned to page size for mapping given files into memory by
- -// file descriptor correctly, as according to mmap(2), the offset used in mmap
- -// must be a multiple of sysconf(_SC_PAGE_SIZE).
- -int64 GetPageSizeAlignedOffset(int64 offset) {
- - int64 aligned_offset = offset;
- - int64 page_size = sysconf(_SC_PAGE_SIZE);
- - if (offset % page_size != 0) {
- - aligned_offset = offset / page_size * page_size;
- - }
- - return aligned_offset;
- -}
- -
- } // namespace
-
- /* static */
- @@ -71,123 +53,18 @@ absl::Status ExternalFileHandler::MapExternalFile() {
- if (!external_file_.file_content().empty()) {
- return absl::OkStatus();
- }
- - if (external_file_.file_name().empty() &&
- - !external_file_.has_file_descriptor_meta()) {
- - return CreateStatusWithPayload(
- - StatusCode::kInvalidArgument,
- - "ExternalFile must specify at least one of 'file_content', 'file_name' "
- - "or 'file_descriptor_meta'.",
- - TfLiteSupportStatus::kInvalidArgumentError);
- - }
- - // Obtain file descriptor, offset and size.
- - int fd = -1;
- - if (!external_file_.file_name().empty()) {
- - owned_fd_ = open(external_file_.file_name().c_str(), O_RDONLY);
- - if (owned_fd_ < 0) {
- - const std::string error_message = absl::StrFormat(
- - "Unable to open file at %s", external_file_.file_name());
- - switch (errno) {
- - case ENOENT:
- - return CreateStatusWithPayload(
- - StatusCode::kNotFound, error_message,
- - TfLiteSupportStatus::kFileNotFoundError);
- - case EACCES:
- - case EPERM:
- - return CreateStatusWithPayload(
- - StatusCode::kPermissionDenied, error_message,
- - TfLiteSupportStatus::kFilePermissionDeniedError);
- - case EINTR:
- - return CreateStatusWithPayload(StatusCode::kUnavailable,
- - error_message,
- - TfLiteSupportStatus::kFileReadError);
- - case EBADF:
- - return CreateStatusWithPayload(StatusCode::kFailedPrecondition,
- - error_message,
- - TfLiteSupportStatus::kFileReadError);
- - default:
- - return CreateStatusWithPayload(
- - StatusCode::kUnknown,
- - absl::StrFormat("%s, errno=%d", error_message, errno),
- - TfLiteSupportStatus::kFileReadError);
- - }
- - }
- - fd = owned_fd_;
- - } else {
- - fd = external_file_.file_descriptor_meta().fd();
- - if (fd < 0) {
- - return CreateStatusWithPayload(
- - StatusCode::kInvalidArgument,
- - absl::StrFormat("Provided file descriptor is invalid: %d < 0", fd),
- - TfLiteSupportStatus::kInvalidArgumentError);
- - }
- - buffer_offset_ = external_file_.file_descriptor_meta().offset();
- - buffer_size_ = external_file_.file_descriptor_meta().length();
- - }
- - // Get actual file size. Always use 0 as offset to lseek(2) to get the actual
- - // file size, as SEEK_END returns the size of the file *plus* offset.
- - size_t file_size = lseek(fd, /*offset=*/0, SEEK_END);
- - if (file_size <= 0) {
- - return CreateStatusWithPayload(
- - StatusCode::kUnknown,
- - absl::StrFormat("Unable to get file size, errno=%d", errno),
- - TfLiteSupportStatus::kFileReadError);
- - }
- - // Deduce buffer size if not explicitly provided through file descriptor.
- - if (buffer_size_ <= 0) {
- - buffer_size_ = file_size - buffer_offset_;
- - }
- - // Check for out of range issues.
- - if (file_size <= buffer_offset_) {
- - return CreateStatusWithPayload(
- - StatusCode::kInvalidArgument,
- - absl::StrFormat("Provided file offset (%d) exceeds or matches actual "
- - "file length (%d)",
- - buffer_offset_, file_size),
- - TfLiteSupportStatus::kInvalidArgumentError);
- - }
- - if (file_size < buffer_size_ + buffer_offset_) {
- - return CreateStatusWithPayload(
- - StatusCode::kInvalidArgument,
- - absl::StrFormat("Provided file length + offset (%d) exceeds actual "
- - "file length (%d)",
- - buffer_size_ + buffer_offset_, file_size),
- - TfLiteSupportStatus::kInvalidArgumentError);
- - }
- - // If buffer_offset_ is not multiple of sysconf(_SC_PAGE_SIZE), align with
- - // extra leading bytes and adjust buffer_size_ to account for the extra
- - // leading bytes.
- - buffer_aligned_offset_ = GetPageSizeAlignedOffset(buffer_offset_);
- - buffer_aligned_size_ = buffer_size_ + buffer_offset_ - buffer_aligned_offset_;
- - // Map into memory.
- - buffer_ = mmap(/*addr=*/nullptr, buffer_aligned_size_, PROT_READ, MAP_SHARED,
- - fd, buffer_aligned_offset_);
- - if (buffer_ == MAP_FAILED) {
- - return CreateStatusWithPayload(
- - StatusCode::kUnknown,
- - absl::StrFormat("Unable to map file to memory buffer, errno=%d", errno),
- - TfLiteSupportStatus::kFileMmapError);
- - }
- - return absl::OkStatus();
- + return CreateStatusWithPayload(
- + StatusCode::kInvalidArgument,
- + "ExternalFile must specify 'file_content' in Chromium.",
- + TfLiteSupportStatus::kInvalidArgumentError);
- +
- }
-
- absl::string_view ExternalFileHandler::GetFileContent() {
- - if (!external_file_.file_content().empty()) {
- - return external_file_.file_content();
- - } else {
- - return absl::string_view(static_cast<const char*>(buffer_) +
- - buffer_offset_ - buffer_aligned_offset_,
- - buffer_size_);
- - }
- + return external_file_.file_content();
- }
-
- -ExternalFileHandler::~ExternalFileHandler() {
- - if (buffer_ != MAP_FAILED) {
- - munmap(buffer_, buffer_aligned_size_);
- - }
- - if (owned_fd_ >= 0) {
- - close(owned_fd_);
- - }
- -}
- +ExternalFileHandler::~ExternalFileHandler() = default;
-
- } // namespace core
- } // namespace task
- diff --git a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
- index e8b6831c6ad69..a7daa175f77f5 100644
- --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
- +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
- @@ -65,26 +65,6 @@ class ExternalFileHandler {
- // Reference to the input ExternalFile.
- const ExternalFile& external_file_;
-
- - // The file descriptor of the ExternalFile if provided by path, as it is
- - // opened and owned by this class. Set to -1 otherwise.
- - int owned_fd_{-1};
- -
- - // Points to the memory buffer mapped from the file descriptor of the
- - // ExternalFile, if provided by path or file descriptor.
- - void* buffer_{};
- -
- - // The mapped memory buffer offset, if any.
- - int64 buffer_offset_{};
- - // The size in bytes of the mapped memory buffer, if any.
- - int64 buffer_size_{};
- -
- - // As mmap(2) requires the offset to be a multiple of sysconf(_SC_PAGE_SIZE):
- -
- - // The aligned mapped memory buffer offset, if any.
- - int64 buffer_aligned_offset_{};
- - // The aligned mapped memory buffer size in bytes taking into account the
- - // offset shift introduced by buffer_aligned_memory_offset_, if any.
- - int64 buffer_aligned_size_{};
- };
-
- } // namespace core
- diff --git a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
- index e0f69cd1c80ac..5999090cab973 100644
- --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
- +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
- @@ -15,8 +15,6 @@ limitations under the License.
-
- #include "tensorflow_lite_support/cc/task/core/tflite_engine.h"
-
- -#include <unistd.h>
- -
- #include <memory>
-
- #include "absl/strings/match.h" // from @com_google_absl
- diff --git a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
- index 9b44c6e5c022a..53dabdc4841d7 100644
- --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
- +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
- @@ -16,8 +16,6 @@ limitations under the License.
- #ifndef TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TFLITE_ENGINE_H_
- #define TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TFLITE_ENGINE_H_
-
- -#include <sys/mman.h>
- -
- #include <memory>
-
- #include "absl/memory/memory.h" // from @com_google_absl
- --
- 2.36.1.124.g0e6072fb45-goog
|