0007-remove-support-for-creating-a-model-handler-from-a-f.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. From 9488411c0779c22ba93fb03994e90cda25b65bd0 Mon Sep 17 00:00:00 2001
  2. From: Robert Ogden <robertogden@chromium.org>
  3. Date: Wed, 25 May 2022 11:03:30 -0700
  4. Subject: [PATCH 7/9] remove support for creating a model handler from a file
  5. ---
  6. third_party/tflite_support/README.chromium | 1 +
  7. .../cc/task/core/external_file_handler.cc | 137 +-----------------
  8. .../cc/task/core/external_file_handler.h | 20 ---
  9. .../cc/task/core/tflite_engine.cc | 2 -
  10. .../cc/task/core/tflite_engine.h | 2 -
  11. 5 files changed, 8 insertions(+), 154 deletions(-)
  12. diff --git a/third_party/tflite_support/README.chromium b/third_party/tflite_support/README.chromium
  13. index 3d5f5dbf2edf9..c1da2b4e73efe 100644
  14. --- a/third_party/tflite_support/README.chromium
  15. +++ b/third_party/tflite_support/README.chromium
  16. @@ -37,6 +37,7 @@ is a no-op in chromium builds and upsets clang.
  17. Update Process (internal: http://shortn/_nwz8liqimy):
  18. 1) Run these commands:
  19. ```
  20. +
  21. pushd third_party/tflite_support/
  22. rm -rf src/
  23. git clone https://github.com/tensorflow/tflite-support/
  24. 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
  25. index 5e17e14dc5f7a..9c4cc2009baea 100644
  26. --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
  27. +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.cc
  28. @@ -15,12 +15,6 @@ limitations under the License.
  29. #include "tensorflow_lite_support/cc/task/core/external_file_handler.h"
  30. -#include <errno.h>
  31. -#include <fcntl.h>
  32. -#include <stddef.h>
  33. -#include <sys/mman.h>
  34. -#include <unistd.h>
  35. -
  36. #include <memory>
  37. #include <string>
  38. @@ -40,18 +34,6 @@ using ::tflite::support::CreateStatusWithPayload;
  39. using ::tflite::support::StatusOr;
  40. using ::tflite::support::TfLiteSupportStatus;
  41. -// Gets the offset aligned to page size for mapping given files into memory by
  42. -// file descriptor correctly, as according to mmap(2), the offset used in mmap
  43. -// must be a multiple of sysconf(_SC_PAGE_SIZE).
  44. -int64 GetPageSizeAlignedOffset(int64 offset) {
  45. - int64 aligned_offset = offset;
  46. - int64 page_size = sysconf(_SC_PAGE_SIZE);
  47. - if (offset % page_size != 0) {
  48. - aligned_offset = offset / page_size * page_size;
  49. - }
  50. - return aligned_offset;
  51. -}
  52. -
  53. } // namespace
  54. /* static */
  55. @@ -71,123 +53,18 @@ absl::Status ExternalFileHandler::MapExternalFile() {
  56. if (!external_file_.file_content().empty()) {
  57. return absl::OkStatus();
  58. }
  59. - if (external_file_.file_name().empty() &&
  60. - !external_file_.has_file_descriptor_meta()) {
  61. - return CreateStatusWithPayload(
  62. - StatusCode::kInvalidArgument,
  63. - "ExternalFile must specify at least one of 'file_content', 'file_name' "
  64. - "or 'file_descriptor_meta'.",
  65. - TfLiteSupportStatus::kInvalidArgumentError);
  66. - }
  67. - // Obtain file descriptor, offset and size.
  68. - int fd = -1;
  69. - if (!external_file_.file_name().empty()) {
  70. - owned_fd_ = open(external_file_.file_name().c_str(), O_RDONLY);
  71. - if (owned_fd_ < 0) {
  72. - const std::string error_message = absl::StrFormat(
  73. - "Unable to open file at %s", external_file_.file_name());
  74. - switch (errno) {
  75. - case ENOENT:
  76. - return CreateStatusWithPayload(
  77. - StatusCode::kNotFound, error_message,
  78. - TfLiteSupportStatus::kFileNotFoundError);
  79. - case EACCES:
  80. - case EPERM:
  81. - return CreateStatusWithPayload(
  82. - StatusCode::kPermissionDenied, error_message,
  83. - TfLiteSupportStatus::kFilePermissionDeniedError);
  84. - case EINTR:
  85. - return CreateStatusWithPayload(StatusCode::kUnavailable,
  86. - error_message,
  87. - TfLiteSupportStatus::kFileReadError);
  88. - case EBADF:
  89. - return CreateStatusWithPayload(StatusCode::kFailedPrecondition,
  90. - error_message,
  91. - TfLiteSupportStatus::kFileReadError);
  92. - default:
  93. - return CreateStatusWithPayload(
  94. - StatusCode::kUnknown,
  95. - absl::StrFormat("%s, errno=%d", error_message, errno),
  96. - TfLiteSupportStatus::kFileReadError);
  97. - }
  98. - }
  99. - fd = owned_fd_;
  100. - } else {
  101. - fd = external_file_.file_descriptor_meta().fd();
  102. - if (fd < 0) {
  103. - return CreateStatusWithPayload(
  104. - StatusCode::kInvalidArgument,
  105. - absl::StrFormat("Provided file descriptor is invalid: %d < 0", fd),
  106. - TfLiteSupportStatus::kInvalidArgumentError);
  107. - }
  108. - buffer_offset_ = external_file_.file_descriptor_meta().offset();
  109. - buffer_size_ = external_file_.file_descriptor_meta().length();
  110. - }
  111. - // Get actual file size. Always use 0 as offset to lseek(2) to get the actual
  112. - // file size, as SEEK_END returns the size of the file *plus* offset.
  113. - size_t file_size = lseek(fd, /*offset=*/0, SEEK_END);
  114. - if (file_size <= 0) {
  115. - return CreateStatusWithPayload(
  116. - StatusCode::kUnknown,
  117. - absl::StrFormat("Unable to get file size, errno=%d", errno),
  118. - TfLiteSupportStatus::kFileReadError);
  119. - }
  120. - // Deduce buffer size if not explicitly provided through file descriptor.
  121. - if (buffer_size_ <= 0) {
  122. - buffer_size_ = file_size - buffer_offset_;
  123. - }
  124. - // Check for out of range issues.
  125. - if (file_size <= buffer_offset_) {
  126. - return CreateStatusWithPayload(
  127. - StatusCode::kInvalidArgument,
  128. - absl::StrFormat("Provided file offset (%d) exceeds or matches actual "
  129. - "file length (%d)",
  130. - buffer_offset_, file_size),
  131. - TfLiteSupportStatus::kInvalidArgumentError);
  132. - }
  133. - if (file_size < buffer_size_ + buffer_offset_) {
  134. - return CreateStatusWithPayload(
  135. - StatusCode::kInvalidArgument,
  136. - absl::StrFormat("Provided file length + offset (%d) exceeds actual "
  137. - "file length (%d)",
  138. - buffer_size_ + buffer_offset_, file_size),
  139. - TfLiteSupportStatus::kInvalidArgumentError);
  140. - }
  141. - // If buffer_offset_ is not multiple of sysconf(_SC_PAGE_SIZE), align with
  142. - // extra leading bytes and adjust buffer_size_ to account for the extra
  143. - // leading bytes.
  144. - buffer_aligned_offset_ = GetPageSizeAlignedOffset(buffer_offset_);
  145. - buffer_aligned_size_ = buffer_size_ + buffer_offset_ - buffer_aligned_offset_;
  146. - // Map into memory.
  147. - buffer_ = mmap(/*addr=*/nullptr, buffer_aligned_size_, PROT_READ, MAP_SHARED,
  148. - fd, buffer_aligned_offset_);
  149. - if (buffer_ == MAP_FAILED) {
  150. - return CreateStatusWithPayload(
  151. - StatusCode::kUnknown,
  152. - absl::StrFormat("Unable to map file to memory buffer, errno=%d", errno),
  153. - TfLiteSupportStatus::kFileMmapError);
  154. - }
  155. - return absl::OkStatus();
  156. + return CreateStatusWithPayload(
  157. + StatusCode::kInvalidArgument,
  158. + "ExternalFile must specify 'file_content' in Chromium.",
  159. + TfLiteSupportStatus::kInvalidArgumentError);
  160. +
  161. }
  162. absl::string_view ExternalFileHandler::GetFileContent() {
  163. - if (!external_file_.file_content().empty()) {
  164. - return external_file_.file_content();
  165. - } else {
  166. - return absl::string_view(static_cast<const char*>(buffer_) +
  167. - buffer_offset_ - buffer_aligned_offset_,
  168. - buffer_size_);
  169. - }
  170. + return external_file_.file_content();
  171. }
  172. -ExternalFileHandler::~ExternalFileHandler() {
  173. - if (buffer_ != MAP_FAILED) {
  174. - munmap(buffer_, buffer_aligned_size_);
  175. - }
  176. - if (owned_fd_ >= 0) {
  177. - close(owned_fd_);
  178. - }
  179. -}
  180. +ExternalFileHandler::~ExternalFileHandler() = default;
  181. } // namespace core
  182. } // namespace task
  183. 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
  184. index e8b6831c6ad69..a7daa175f77f5 100644
  185. --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
  186. +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/external_file_handler.h
  187. @@ -65,26 +65,6 @@ class ExternalFileHandler {
  188. // Reference to the input ExternalFile.
  189. const ExternalFile& external_file_;
  190. - // The file descriptor of the ExternalFile if provided by path, as it is
  191. - // opened and owned by this class. Set to -1 otherwise.
  192. - int owned_fd_{-1};
  193. -
  194. - // Points to the memory buffer mapped from the file descriptor of the
  195. - // ExternalFile, if provided by path or file descriptor.
  196. - void* buffer_{};
  197. -
  198. - // The mapped memory buffer offset, if any.
  199. - int64 buffer_offset_{};
  200. - // The size in bytes of the mapped memory buffer, if any.
  201. - int64 buffer_size_{};
  202. -
  203. - // As mmap(2) requires the offset to be a multiple of sysconf(_SC_PAGE_SIZE):
  204. -
  205. - // The aligned mapped memory buffer offset, if any.
  206. - int64 buffer_aligned_offset_{};
  207. - // The aligned mapped memory buffer size in bytes taking into account the
  208. - // offset shift introduced by buffer_aligned_memory_offset_, if any.
  209. - int64 buffer_aligned_size_{};
  210. };
  211. } // namespace core
  212. 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
  213. index e0f69cd1c80ac..5999090cab973 100644
  214. --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
  215. +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.cc
  216. @@ -15,8 +15,6 @@ limitations under the License.
  217. #include "tensorflow_lite_support/cc/task/core/tflite_engine.h"
  218. -#include <unistd.h>
  219. -
  220. #include <memory>
  221. #include "absl/strings/match.h" // from @com_google_absl
  222. 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
  223. index 9b44c6e5c022a..53dabdc4841d7 100644
  224. --- a/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
  225. +++ b/third_party/tflite_support/src/tensorflow_lite_support/cc/task/core/tflite_engine.h
  226. @@ -16,8 +16,6 @@ limitations under the License.
  227. #ifndef TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TFLITE_ENGINE_H_
  228. #define TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_TFLITE_ENGINE_H_
  229. -#include <sys/mman.h>
  230. -
  231. #include <memory>
  232. #include "absl/memory/memory.h" // from @com_google_absl
  233. --
  234. 2.36.1.124.g0e6072fb45-goog