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 16 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTIZEUTILS_H_ 17 #define TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTIZEUTILS_H_ 18 19 namespace mlir { 20 class Attribute; 21 class Type; 22 23 namespace quant { 24 class QuantizedType; 25 class UniformQuantizedType; 26 } // namespace quant 27 namespace quantfork { 28 class UniformQuantizedValueConverter; 29 30 /// Converts an attribute from a type based on 31 /// quantizedElementType.getExpressedType() to one based on 32 /// quantizedElementType.getStorageType(), where quantizedElementType is as from 33 /// QuantizedType::getQuantizedElementType(). 34 /// Returns nullptr if the conversion is not supported. On success, stores the 35 /// converted type in outConvertedType. 36 /// 37 /// Examples: 38 /// 1. realValue is a primitive value attribute: 39 /// (realValue: FloatAttr, quantizedElementType: UniformQuantizedType[i8:f32]) 40 /// -> (IntegerAttr, outConvertedType: i8) 41 /// 2. realValue is an elements attribute: 42 /// (realValue: DenseElementsAttr[tensor<2x2xf32>], 43 /// quantizedElementType: UniformQuantizedType[i8:f32]) 44 /// -> (DenseElementsAttr[tensor<2x2xi8>], outConvertedType: tensor<2x2xi8>) 45 Attribute quantizeAttr(Attribute realValue, 46 quant::QuantizedType quantizedElementType, 47 Type &outConvertedType); 48 49 /// Converts an attribute from a type based on 50 /// quantizedElementType.getExpressedType() to one based on 51 /// quantizedElementType.getStorageType(), where quantizedElementType is as from 52 /// QuantizedType::getQuantizedElementType() and casted to an 53 /// UniformQuantizedType. Returns nullptr if the conversion is not supported. On 54 /// success, stores the converted type in outConvertedType. 55 /// 56 /// Examples: 57 /// 1. realValue is a primitive value attribute: 58 /// (realValue: FloatAttr, quantizedElementType: UniformQuantizedType[i8:f32]) 59 /// -> (IntegerAttr, outConvertedType: i8) 60 /// 2. realValue is an elements attribute: 61 /// (realValue: DenseElementsAttr[tensor<2x2xf32>], 62 /// quantizedElementType: UniformQuantizedType[i8:f32]) 63 /// -> (DenseElementsAttr[tensor<2x2xi8>], outConvertedType: tensor<2x2xi8>) 64 Attribute quantizeAttrUniform(Attribute realValue, 65 quant::UniformQuantizedType quantizedElementType, 66 const UniformQuantizedValueConverter &converter, 67 Type &outConvertedType); 68 } // namespace quantfork 69 } // namespace mlir 70 71 #endif // TENSORFLOW_COMPILER_MLIR_LITE_QUANTIZATION_IR_QUANTIZEUTILS_H_ 72