xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/experimental/remat/metadata_util.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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 #include "tensorflow/lite/experimental/remat/metadata_util.h"
16 
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 namespace {
22 
23 // We serialize unsigneds as protobuf varints, i.e., in chunks of 7 bits each.
24 constexpr int kMod = (1 << 7);
25 
Serialize(std::string * out,uint32_t value)26 void Serialize(std::string* out, uint32_t value) {
27   for (; value >= kMod; value /= kMod) {
28     out->push_back(value % kMod + kMod);
29   }
30   out->push_back(value);
31 }
32 
Parse(const char ** data,size_t * size,uint32_t * out)33 bool Parse(const char** data, size_t* size, uint32_t* out) {
34   *out = 0;
35   uint32_t mul = 1;
36   for (bool done = false; !done;
37        mul *= kMod, done = !(**data & kMod), ++*data, --*size) {
38     if (*size == 0) {
39       return false;
40     }
41     *out += static_cast<unsigned char>(**data) % kMod * mul;
42   }
43   return true;
44 }
45 
46 // Signed ints are zigzag-encoded as unsigned varints, [..., -2, -1, 0, 1, 2,
47 // ...] ->
48 // [..., 3, 1, 0, 2, 4, ...].
Serialize(std::string * out,int32_t value)49 void Serialize(std::string* out, int32_t value) {
50   Serialize(out, static_cast<uint32_t>(
51                      value < 0 ? static_cast<uint32_t>(-(value + 1)) * 2 + 1
52                                : static_cast<uint32_t>(value) * 2));
53 }
54 
Parse(const char ** data,size_t * size,int32_t * out)55 bool Parse(const char** data, size_t* size, int32_t* out) {
56   uint32_t value = 0;
57   if (!Parse(data, size, &value)) {
58     return false;
59   }
60   const int32_t magnitude = value / 2;
61   *out = (value % 2) ? (-magnitude - 1) : magnitude;
62   return true;
63 }
64 
65 // Pairs are serialized as the concatenation of their elements' serialization.
66 template <class First, class Second>
Serialize(std::string * out,const std::pair<First,Second> & in)67 void Serialize(std::string* out, const std::pair<First, Second>& in) {
68   Serialize(out, in.first);
69   Serialize(out, in.second);
70 }
71 
72 template <class First, class Second>
Parse(const char ** data,size_t * size,std::pair<First,Second> * out)73 bool Parse(const char** data, size_t* size, std::pair<First, Second>* out) {
74   return Parse(data, size, &(out->first)) && Parse(data, size, &(out->second));
75 }
76 
77 // Vectors are serialized as the concetation of the serialization of their size
78 // and the the serializations of their elements.
79 template <class Value>
Serialize(std::string * out,const std::vector<Value> & in)80 void Serialize(std::string* out, const std::vector<Value>& in) {
81   Serialize(out, static_cast<uint32_t>(in.size()));
82   for (const auto& val : in) {
83     Serialize(out, val);
84   }
85 }
86 
87 template <class T>
Parse(const char ** data,size_t * size,std::vector<T> * out)88 bool Parse(const char** data, size_t* size, std::vector<T>* out) {
89   uint32_t num_elems = 0;
90   if (!Parse(data, size, &num_elems)) {
91     return false;
92   }
93   out->assign(num_elems, T{});
94   for (auto& elem : *out) {
95     if (!Parse(data, size, &elem)) {
96       return false;
97     }
98   }
99   return true;
100 }
101 
102 }  // namespace
103 
104 namespace tflite {
SerializeModelControlDependencies(const ModelControlDependencies & in)105 std::string SerializeModelControlDependencies(
106     const ModelControlDependencies& in) {
107   std::string out;
108   Serialize(&out, kModelControlDependenciesMetadataVersion);
109   Serialize(&out, in);
110   return out;
111 }
112 
ParseModelControlDependencies(const char * data,size_t size,ModelControlDependencies * out)113 bool ParseModelControlDependencies(const char* data, size_t size,
114                                    ModelControlDependencies* out) {
115   out->clear();
116   uint32_t version = 0;
117   return Parse(&data, &size, &version) &&
118          (version == kModelControlDependenciesMetadataVersion) &&
119          Parse(&data, &size, out) && (size == 0);
120 }
121 
122 }  // namespace tflite
123