1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_TOCO_TOCO_PORT_H_
16 #define TENSORFLOW_LITE_TOCO_TOCO_PORT_H_
17
18 // Portability layer for toco tool. Mainly, abstract filesystem access so we
19 // can build and use on google internal environments and on OSX.
20
21 #include <string>
22 #include "google/protobuf/text_format.h"
23 #include "tensorflow/lite/toco/format_port.h"
24 #include "tensorflow/core/lib/core/status.h"
25 #include "tensorflow/core/platform/logging.h"
26 #include "tensorflow/core/platform/platform.h"
27 #if defined(PLATFORM_GOOGLE)
28 #include "absl/strings/cord.h"
29 #endif // PLATFORM_GOOGLE
30
31 #ifdef PLATFORM_GOOGLE
32 #define TFLITE_PROTO_NS proto2
33 #else
34 #define TFLITE_PROTO_NS google::protobuf
35 #endif
36
37 #ifdef __ANDROID__
38 #include <sstream>
39 namespace std {
40
41 template <typename T>
to_string(T value)42 std::string to_string(T value)
43 {
44 std::ostringstream os ;
45 os << value ;
46 return os.str() ;
47 }
48
49 #ifdef __ARM_ARCH_7A__
50 double round(double x);
51 #endif
52 }
53 #endif
54
55 namespace toco {
56 namespace port {
57
58 // Things like tests use other initialization routines that need control
59 // of flags. However, for testing we still want to use toco_port.h facilities.
60 // This function sets initialized flag trivially.
61 void InitGoogleWasDoneElsewhere();
62 void InitGoogle(const char* usage, int* argc, char*** argv, bool remove_flags);
63 void CheckInitGoogleIsDone(const char* message);
64
65 namespace file {
66 class Options {};
Defaults()67 inline Options Defaults() {
68 Options o;
69 return o;
70 }
71 tensorflow::Status GetContents(const std::string& filename,
72 std::string* contents, const Options& options);
73 tensorflow::Status SetContents(const std::string& filename,
74 const std::string& contents,
75 const Options& options);
76 std::string JoinPath(const std::string& base, const std::string& filename);
77 tensorflow::Status Writable(const std::string& filename);
78 tensorflow::Status Readable(const std::string& filename,
79 const Options& options);
80 tensorflow::Status Exists(const std::string& filename, const Options& options);
81 } // namespace file
82
83 // Copy `src` string to `dest`. User must ensure `dest` has enough space.
84 #if defined(PLATFORM_GOOGLE)
85 void CopyToBuffer(const ::absl::Cord& src, char* dest);
86 #endif // PLATFORM_GOOGLE
87 void CopyToBuffer(const std::string& src, char* dest);
88
ReverseBits32(uint32 n)89 inline uint32 ReverseBits32(uint32 n) {
90 n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1);
91 n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2);
92 n = ((n >> 4) & 0x0F0F0F0F) | ((n & 0x0F0F0F0F) << 4);
93 return (((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) |
94 ((n & 0xFF000000) >> 24));
95 }
96 } // namespace port
97
ParseFromStringOverload(const std::string & in,TFLITE_PROTO_NS::Message * proto)98 inline bool ParseFromStringOverload(const std::string& in,
99 TFLITE_PROTO_NS::Message* proto) {
100 return TFLITE_PROTO_NS::TextFormat::ParseFromString(in, proto);
101 }
102
103 template <typename Proto>
ParseFromStringEitherTextOrBinary(const std::string & input_file_contents,Proto * proto)104 bool ParseFromStringEitherTextOrBinary(const std::string& input_file_contents,
105 Proto* proto) {
106 if (proto->ParseFromString(input_file_contents)) {
107 return true;
108 }
109
110 if (ParseFromStringOverload(input_file_contents, proto)) {
111 return true;
112 }
113
114 return false;
115 }
116
117 } // namespace toco
118
119 #endif // TENSORFLOW_LITE_TOCO_TOCO_PORT_H_
120