My Project
UniformSolvers.h
Go to the documentation of this file.
1 //===- UniformSolvers.h - Uniform type solver algorithms --------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 // This file defines algorithms for solving uniform type parameters for various
10 // conditions (i.e. fixed-point, affine, scale matching, etc).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_QUANTIZER_SUPPORT_UNIFORMSOLVERS_H
15 #define MLIR_QUANTIZER_SUPPORT_UNIFORMSOLVERS_H
16 
17 #include <cstdint>
18 #include <limits>
19 
20 namespace llvm {
21 class raw_ostream;
22 } // end namespace llvm
23 
24 namespace mlir {
25 namespace quantizer {
26 
28  static UniformStorageParams getQuint8() { return {255, 0}; }
29  static UniformStorageParams getQuint8SymmetricRight() { return {254, 1}; }
30  static UniformStorageParams getQuint16() { return {32767, 0}; }
31 
32  uint64_t numLevels;
33  int64_t minValue;
34 };
35 
39 public:
41  double boundingMin, double boundingMax)
42  : storageParams(storageParams), boundingMin(boundingMin),
43  boundingMax(boundingMax) {}
44 
46  bool compute();
47 
48  // Params.
49  double getBoundingMin() const { return boundingMin; }
50  double getBoundingMax() const { return boundingMax; }
51  bool isSatisfied() const { return satisfied; }
52  double getAdjMin() const { return adjMin; }
53  double getAdjMax() const { return adjMax; }
54  double getScale() const { return delta; }
55  int64_t getZp() const { return zp; }
56  int getStepCount() const { return stepCount; }
57 
58  // Quantize and dequantize.
59  int64_t quantize(double x) const;
60  double dequantize(int64_t xq) const;
61 
62 private:
63  const UniformStorageParams storageParams;
64  const double boundingMin;
65  const double boundingMax;
66 
67  // Results
68  int stepCount = 0;
69  double adjMin = std::numeric_limits<double>::quiet_NaN();
70  double adjMax = std::numeric_limits<double>::quiet_NaN();
71  double delta = std::numeric_limits<double>::quiet_NaN();
72  int64_t zp = 0;
73 
74  bool satisfied = false;
75 };
76 
77 llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
78  const UniformStorageParams &p);
79 
80 llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
82 
83 } // end namespace quantizer
84 } // end namespace mlir
85 
86 #endif // MLIR_QUANTIZER_SUPPORT_UNIFORMSOLVERS_H
Definition: InferTypeOpInterface.cpp:20
UniformParamsFromMinMaxSolver(const UniformStorageParams &storageParams, double boundingMin, double boundingMax)
Definition: UniformSolvers.h:40
uint64_t numLevels
Definition: UniformSolvers.h:32
Definition: PassRegistry.cpp:413
bool isSatisfied() const
Definition: UniformSolvers.h:51
static UniformStorageParams getQuint16()
Definition: UniformSolvers.h:30
double getBoundingMax() const
Definition: UniformSolvers.h:50
int64_t getZp() const
Definition: UniformSolvers.h:55
double getAdjMax() const
Definition: UniformSolvers.h:53
static UniformStorageParams getQuint8()
Definition: UniformSolvers.h:28
int64_t minValue
Definition: UniformSolvers.h:33
static UniformStorageParams getQuint8SymmetricRight()
Definition: UniformSolvers.h:29
int getStepCount() const
Definition: UniformSolvers.h:56
double getScale() const
Definition: UniformSolvers.h:54
std::ostream & operator<<(std::ostream &out, const llvm::Twine &twine)
Definition: DebugStringHelper.h:36
Definition: UniformSolvers.h:27
Definition: UniformSolvers.h:38
double getAdjMin() const
Definition: UniformSolvers.h:52
double getBoundingMin() const
Definition: UniformSolvers.h:49