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