1 /* Copyright 2021 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 16 #ifndef TENSORFLOW_LITE_TOOLS_STRIP_BUFFERS_STRIPPING_LIB_H_ 17 #define TENSORFLOW_LITE_TOOLS_STRIP_BUFFERS_STRIPPING_LIB_H_ 18 19 #include <string> 20 21 #include "absl/strings/string_view.h" 22 #include "tensorflow/lite/c/common.h" 23 #include "tensorflow/lite/schema/schema_generated.h" 24 25 namespace tflite { 26 27 // Strips eligible buffers from Flatbuffer, to generate a 'leaner' model. 28 // Buffers for tensors that satisfy the following constraints are stripped out: 29 // 1. Are either of: Float32, Int32, UInt8, Int8 30 // 2. If Int32, the tensor should have a min of 10 elements 31 // NOTE: This only supports a single Subgraph for now. 32 TfLiteStatus StripWeightsFromFlatbuffer( 33 const Model* input_model, 34 flatbuffers::FlatBufferBuilder* new_model_builder); 35 36 // The same function as above, but takes in the input model flatbuffer in a 37 // string and returns the stripped version in a string. 38 // Returns empty string on error. 39 // NOTE: This only supports a single Subgraph for now. 40 std::string StripWeightsFromFlatbuffer( 41 const absl::string_view input_flatbuffer); 42 43 // Generates buffers with random data, for tensors that were mutated using 44 // strip_buffers_from_fb. 45 // The modified flatbuffer is built into new_model_builder. 46 // NOTE: This only supports a single Subgraph for now. 47 TfLiteStatus ReconstituteConstantTensorsIntoFlatbuffer( 48 const Model* input_model, 49 flatbuffers::FlatBufferBuilder* new_model_builder); 50 51 // The same function as above but takes in the input model flatbuffer in a 52 // string and returns the reconstituded version in a string. 53 // Returns empty string on error. 54 // NOTE: This only supports a single Subgraph for now. 55 std::string ReconstituteConstantTensorsIntoFlatbuffer( 56 const absl::string_view input_flatbuffer); 57 58 // Return true if the input model has been stripped before. 59 bool FlatbufferHasStrippedWeights(const Model* input_model); 60 61 } // namespace tflite 62 63 #endif // TENSORFLOW_LITE_TOOLS_STRIP_BUFFERS_STRIPPING_LIB_H_ 64