1 /* Copyright 2018 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_TOOLS_OPTIMIZE_QUANTIZE_WEIGHTS_H_ 16 #define TENSORFLOW_LITE_TOOLS_OPTIMIZE_QUANTIZE_WEIGHTS_H_ 17 18 #include <cstdint> 19 #include <memory> 20 #include <string> 21 22 #include "flatbuffers/flexbuffers.h" 23 #include "absl/container/flat_hash_set.h" 24 #include "tensorflow/lite/context.h" 25 #include "tensorflow/lite/model.h" 26 #include "tensorflow/lite/schema/schema_generated.h" 27 28 namespace tflite { 29 namespace optimize { 30 using absl::flat_hash_set; 31 32 // Supported resulting types from quantization process. 33 enum class BufferType { QUANTIZED_INT8, QUANTIZED_FLOAT16 }; 34 35 // This macro is for internal use for conversions requiring previous behavior. 36 #ifdef TFLITE_USE_PREVIOUS_HYBRID_SCHEME 37 constexpr bool kUseUpdatedHybridSchemeDefault = false; 38 #else 39 constexpr bool kUseUpdatedHybridSchemeDefault = true; 40 #endif 41 42 // Quantizes input_model and populates the provided builder with the new model. 43 // By default only weights tensors weight more than 1024 elements will be 44 // quantized. 45 // 46 // A tflite::Model can be obtained from the builder with: 47 // const uint8_t* buffer = builder->GetBufferPointer(); 48 // tflite::Model* model = GetModel(buffer); 49 TfLiteStatus QuantizeWeights( 50 flatbuffers::FlatBufferBuilder* builder, const Model* input_model, 51 BufferType quant_type = BufferType::QUANTIZED_INT8); 52 53 // Same as above, but only weights with greater than or equal 54 // weights_min_num_elements elements will be quantized. 55 TfLiteStatus QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, 56 const Model* input_model, 57 uint64_t weights_min_num_elements); 58 59 // Stores information about how to quantize a user-specified custom operation. 60 typedef struct { 61 std::vector<std::int32_t> quantizable_input_indices; 62 bool is_hybrid; 63 } CustomOpInfo; 64 65 // Map from custom op code to custom op quantization information. 66 typedef std::unordered_map<string, CustomOpInfo> CustomOpMap; 67 68 // Same as above, but with entry point of quantizing custom ops. 69 TfLiteStatus QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, 70 const Model* input_model, 71 uint64_t weights_min_num_elements, 72 const CustomOpMap& custom_op_map); 73 74 // Same as above, but if use updated_hybrid_scheme is false, 75 // use previous quantization scheme. Optional op_denylist argument 76 // disables hybrid evaluation for provided BuiltinOperators. 77 TfLiteStatus QuantizeWeights( 78 flatbuffers::FlatBufferBuilder* builder, const Model* input_model, 79 uint64_t weights_min_num_elements, const CustomOpMap& custom_op_map, 80 bool use_updated_hybrid_scheme, 81 const flat_hash_set<BuiltinOperator>& op_denylist = {}); 82 83 namespace internal { 84 // If use_hybrid_evaluation is false, will disable using hybrid eval for 85 // operations that support it. 86 // 87 // We use this internal QuantizeWeights call to test models with hybrid 88 // evaluation disabled. 89 TfLiteStatus QuantizeWeights(flatbuffers::FlatBufferBuilder* builder, 90 const Model* input_model, 91 uint64_t weights_min_num_elements, 92 bool use_hybrid_evaluation); 93 } // namespace internal 94 95 } // namespace optimize 96 } // namespace tflite 97 98 #endif // TENSORFLOW_LITE_TOOLS_OPTIMIZE_QUANTIZE_WEIGHTS_H_ 99