1 //===- Metadata.h - Top level types and metadata ----------------*- 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 contains top level types needed to construct constraint graphs,
10 // including context/allocator support and concrete metadata structs for
11 // different quantization schemes (which must be attached to anchor nodes).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_QUANTIZER_SUPPORT_METADATA_H
16 #define MLIR_QUANTIZER_SUPPORT_METADATA_H
17 
18 #include <limits>
19 
20 #include "mlir/Dialect/QuantOps/QuantTypes.h"
21 #include "mlir/IR/MLIRContext.h"
22 #include "mlir/Quantizer/Support/Rules.h"
23 #include "llvm/ADT/SmallBitVector.h"
24 
25 namespace mlir {
26 namespace quantizer {
27 
28 class SolverContext {
29 public:
SolverContext(MLIRContext & mlirContext)30   SolverContext(MLIRContext &mlirContext) : mlirContext(mlirContext) {}
31 
getMlirContext()32   MLIRContext &getMlirContext() { return mlirContext; }
33 
getAllocator()34   llvm::BumpPtrAllocator &getAllocator() { return allocator; }
35 
36   // Optional path to write a debug DOT file for the CAG.
getDebugCAGDotPath()37   StringRef getDebugCAGDotPath() const { return debugCAGDotPath; }
setDebugCAGDotPath(StringRef p)38   void setDebugCAGDotPath(StringRef p) { debugCAGDotPath = p; }
39 
40 private:
41   MLIRContext &mlirContext;
42   llvm::BumpPtrAllocator allocator;
43   std::string debugCAGDotPath;
44 };
45 
46 /// Candidate for a quantized type conversion.
47 struct CandidateQuantizedType {
48   // Note that scheme encodes more than just the target type: it also encodes
49   // additional constraints.
50   enum class Scheme {
51     // Uses aggregate range information for all nodes in the cluster to
52     // solve for uniform scale and zero point.
53     UniformPerLayer,
54     // Uses aggregate per-axis range information for all nodes in the cluster
55     // to solve for per-axis uniform scale and zero point.
56     UniformPerAxisFixedPoint,
57     // Uses the |explicitScaleZeroPoint| to set the scale (and zero point = 0)
58     // for the uniform type. This typically overrides all other constraints
59     // and is used for wide accumulator types (i.e. i32 bias vectors).
60     UniformExplicitFixedPointScale,
61   };
62   unsigned ordinal;
63   quant::AnyQuantizedType quantizedType;
64   Scheme scheme;
65 };
66 
67 struct CAGUniformMetadata {
68   /// Default salience for facts that are derived from data either statically
69   /// discovered in the computation or observed from an outside source.
70   static constexpr int SalienceDefault = 0;
71 
72   /// Highest salience level for facts derived from overrides provided
73   /// explicitly.
74   static constexpr int SalienceForced = 100;
75 
76   /// Salience for facts derived from constraints in how the math is
77   /// expressed which must be satisfied.
78   static constexpr int SalienceRequired = 200;
79 
80   /// The range that the scheme must represent in order to accommodate the
81   /// underlying data.
82   ExpandingMinMaxFact requiredRange;
83 
84   /// Bool vector of scheme ordinals that are disabled.
85   llvm::SmallBitVector disabledCandidateTypes;
86 
87   /// If set, then a solution has converged for the given per-layer scheme.
88   quant::QuantizedType selectedType;
89 
90   /// Optional scale and zero point to be used by types which solve via the
91   /// UniformExplicitFixedPointScale scheme.
92   DiscreteScaleZeroPointFact explicitScaleZeroPoint;
93 
94   /// Prints a summary of the metadata suitable for display in a graph label.
95   void printSummary(raw_ostream &os) const;
96 };
97 
98 } // end namespace quantizer
99 } // end namespace mlir
100 
101 #endif // MLIR_QUANTIZER_SUPPORT_METADATA_H
102