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 string& filename, string* contents,
72 const Options& options);
73 tensorflow::Status SetContents(const string& filename, const string& contents,
74 const Options& options);
75 string JoinPath(const string& base, const string& filename);
76 tensorflow::Status Writable(const string& filename);
77 tensorflow::Status Readable(const string& filename, const Options& options);
78 tensorflow::Status Exists(const string& filename, const Options& options);
79 } // namespace file
80
81 // Copy `src` string to `dest`. User must ensure `dest` has enough space.
82 #if defined(PLATFORM_GOOGLE)
83 void CopyToBuffer(const ::Cord& src, char* dest);
84 #endif // PLATFORM_GOOGLE
85 void CopyToBuffer(const string& src, char* dest);
86
ReverseBits32(uint32 n)87 inline uint32 ReverseBits32(uint32 n) {
88 n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1);
89 n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2);
90 n = ((n >> 4) & 0x0F0F0F0F) | ((n & 0x0F0F0F0F) << 4);
91 return (((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) |
92 ((n & 0xFF000000) >> 24));
93 }
94 } // namespace port
95
ParseFromStringOverload(const std::string & in,TFLITE_PROTO_NS::Message * proto)96 inline bool ParseFromStringOverload(const std::string& in,
97 TFLITE_PROTO_NS::Message* proto) {
98 return TFLITE_PROTO_NS::TextFormat::ParseFromString(in, proto);
99 }
100
101 template <typename Proto>
ParseFromStringEitherTextOrBinary(const std::string & input_file_contents,Proto * proto)102 bool ParseFromStringEitherTextOrBinary(const std::string& input_file_contents,
103 Proto* proto) {
104 if (proto->ParseFromString(input_file_contents)) {
105 return true;
106 }
107
108 if (ParseFromStringOverload(input_file_contents, proto)) {
109 return true;
110 }
111
112 return false;
113 }
114
115 } // namespace toco
116
117 #endif // TENSORFLOW_LITE_TOCO_TOCO_PORT_H_
118