1 //===- QuantizeUtils.h - Support utilities for quantization -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef MLIR_DIALECT_QUANT_QUANTIZEUTILS_H_
10 #define MLIR_DIALECT_QUANT_QUANTIZEUTILS_H_
11 
12 namespace mlir {
13 class Attribute;
14 class Type;
15 
16 namespace quant {
17 class QuantizedType;
18 class UniformQuantizedType;
19 class UniformQuantizedValueConverter;
20 
21 /// Converts an attribute from a type based on
22 /// quantizedElementType.getExpressedType() to one based on
23 /// quantizedElementType.getStorageType(), where quantizedElementType is as from
24 /// QuantizedType::getQuantizedElementType().
25 /// Returns nullptr if the conversion is not supported. On success, stores the
26 /// converted type in outConvertedType.
27 ///
28 /// Examples:
29 /// 1. realValue is a primitive value attribute:
30 /// (realValue: FloatAttr, quantizedElementType: UniformQuantizedType[i8:f32])
31 ///   -> (IntegerAttr, outConvertedType: i8)
32 /// 2. realValue is an elements attribute:
33 /// (realValue: DenseElementsAttr[tensor<2x2xf32>],
34 ///  quantizedElementType: UniformQuantizedType[i8:f32])
35 ///   -> (DenseElementsAttr[tensor<2x2xi8>], outConvertedType: tensor<2x2xi8>)
36 Attribute quantizeAttr(Attribute realValue, QuantizedType quantizedElementType,
37                        Type &outConvertedType);
38 
39 /// Converts an attribute from a type based on
40 /// quantizedElementType.getExpressedType() to one based on
41 /// quantizedElementType.getStorageType(), where quantizedElementType is as from
42 /// QuantizedType::getQuantizedElementType() and casted to an
43 /// UniformQuantizedType. Returns nullptr if the conversion is not supported. On
44 /// success, stores the converted type in outConvertedType.
45 ///
46 /// Examples:
47 /// 1. realValue is a primitive value attribute:
48 /// (realValue: FloatAttr, quantizedElementType: UniformQuantizedType[i8:f32])
49 ///   -> (IntegerAttr, outConvertedType: i8)
50 /// 2. realValue is an elements attribute:
51 /// (realValue: DenseElementsAttr[tensor<2x2xf32>],
52 ///  quantizedElementType: UniformQuantizedType[i8:f32])
53 ///   -> (DenseElementsAttr[tensor<2x2xi8>], outConvertedType: tensor<2x2xi8>)
54 Attribute quantizeAttrUniform(Attribute realValue,
55                               UniformQuantizedType quantizedElementType,
56                               const UniformQuantizedValueConverter &converter,
57                               Type &outConvertedType);
58 } // namespace quant
59 } // namespace mlir
60 
61 #endif // MLIR_DIALECT_QUANT_QUANTIZEUTILS_H_
62