My Project
Rules.h
Go to the documentation of this file.
1 //===- Rules.h - Helpers for declaring facts and rules ----------*- 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 helper classes and functions for managing state (facts),
10 // merging and tracking modification for various data types important for
11 // quantization.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_QUANTIZER_SUPPORT_RULES_H
16 #define MLIR_QUANTIZER_SUPPORT_RULES_H
17 
18 #include "llvm/ADT/Optional.h"
19 
20 #include <algorithm>
21 #include <limits>
22 #include <utility>
23 
24 namespace mlir {
25 namespace quantizer {
26 
31 
33  if (value == Modified || other.value == Modified) {
35  } else {
37  }
38  }
39 
41  value =
42  (value == Modified || other.value == Modified) ? Modified : Retained;
43  return *this;
44  }
45 };
46 
47 inline ModificationResult modify(bool isModified = true) {
50 }
51 
52 inline bool modified(ModificationResult m) {
54 }
55 
66 template <typename BinaryReducer>
68 public:
69  using ValueTy = typename BinaryReducer::ValueTy;
72  : value(BinaryReducer::initialValue()),
73  salience(std::numeric_limits<int>::min()) {}
74 
75  int getSalience() const { return salience; }
76  bool hasValue() const { return salience != std::numeric_limits<int>::min(); }
77  ValueTy getValue() const { return value; }
78  ModificationResult assertValue(int assertSalience, ValueTy assertValue) {
79  if (assertSalience > salience) {
80  // New salience band.
81  value = assertValue;
82  salience = assertSalience;
83  return modify(true);
84  } else if (assertSalience < salience) {
85  // Lower salience - ignore.
86  return modify(false);
87  }
88  // Merge within same salience band.
89  ValueTy updatedValue = BinaryReducer::reduce(value, assertValue);
90  auto mod = modify(value != updatedValue);
91  value = updatedValue;
92  return mod;
93  }
95  if (other.hasValue()) {
96  return assertValue(other.getSalience(), other.getValue());
97  }
98  return modify(false);
99  }
100 
101 private:
102  ValueTy value;
103  int salience;
104 };
105 
110  using ValueTy = std::pair<double, double>;
112  return std::make_pair(std::numeric_limits<double>::infinity(),
113  -std::numeric_limits<double>::infinity());
114  }
115  static ValueTy reduce(ValueTy lhs, ValueTy rhs) {
116  return std::make_pair(std::min(lhs.first, rhs.first),
117  std::max(lhs.second, rhs.second));
118  }
119 };
121 
123 template <typename T>
125  using ValueTy = T;
127  if (std::numeric_limits<T>::has_infinity()) {
128  return std::numeric_limits<T>::infinity();
129  } else {
130  return std::numeric_limits<T>::max();
131  }
132  }
133  static ValueTy reduce(ValueTy lhs, ValueTy rhs) { return std::min(lhs, rhs); }
134 };
135 using MinimizingDoubleFact =
138 
140 template <typename T>
142  using ValueTy = T;
144  if (std::numeric_limits<T>::has_infinity()) {
145  return -std::numeric_limits<T>::infinity();
146  } else {
147  return std::numeric_limits<T>::min();
148  }
149  }
150  static ValueTy reduce(ValueTy lhs, ValueTy rhs) { return std::max(lhs, rhs); }
151 };
152 using MaximizingDoubleFact =
155 
159 template <typename T>
161  struct ValueTy {
162  ValueTy() : conflict(false) {}
163  ValueTy(T value) : value(value), conflict(false) {}
164  ValueTy(T value, bool conflict) : value(value), conflict(conflict) {}
166  bool conflict;
167  bool operator==(const ValueTy &other) const {
168  if (conflict != other.conflict)
169  return false;
170  if (value && other.value) {
171  return *value == *other.value;
172  } else {
173  return !value && !other.value;
174  }
175  }
176  bool operator!=(const ValueTy &other) const { return !(*this == other); }
177  };
178  static ValueTy initialValue() { return ValueTy(); }
179  static ValueTy reduce(ValueTy lhs, ValueTy rhs) {
180  if (!lhs.value && !rhs.value)
181  return lhs;
182  else if (!lhs.value)
183  return rhs;
184  else if (!rhs.value)
185  return lhs;
186  else
187  return ValueTy(*lhs.value, *lhs.value != *rhs.value);
188  }
189 };
190 
191 template <typename T>
193 
196 
197 } // end namespace quantizer
198 } // end namespace mlir
199 
200 #endif // MLIR_QUANTIZER_SUPPORT_RULES_H
Definition: InferTypeOpInterface.cpp:20
bool modified(ModificationResult m)
Definition: Rules.h:52
BasePropagatedFact()
Definition: Rules.h:71
bool conflict
Definition: Rules.h:166
ModificationResult(ModificationEnum v)
Definition: Rules.h:30
typename ExpandingMinMaxReducer ::ValueTy ValueTy
Definition: Rules.h:69
llvm::Optional< T > value
Definition: Rules.h:165
static ValueTy reduce(ValueTy lhs, ValueTy rhs)
Definition: Rules.h:115
ModificationResult operator|=(ModificationResult other)
Definition: Rules.h:40
ModificationResult operator|(ModificationResult other)
Definition: Rules.h:32
Definition: LLVM.h:40
Typed indicator of whether a mutator produces a modification.
Definition: Rules.h:28
std::pair< double, double > ValueTy
Definition: Rules.h:110
ModificationResult assertValue(int assertSalience, ValueTy assertValue)
Definition: Rules.h:78
ModificationEnum
Definition: Rules.h:29
static ValueTy initialValue()
Definition: Rules.h:143
Definition: Rules.h:160
static ValueTy reduce(ValueTy lhs, ValueTy rhs)
Definition: Rules.h:133
int getSalience() const
Definition: Rules.h:75
bool operator!=(const ValueTy &other) const
Definition: Rules.h:176
int64_t mod(int64_t lhs, int64_t rhs)
Definition: MathExtras.h:41
A binary reducer that maximizes a numeric type.
Definition: Rules.h:141
ValueTy getValue() const
Definition: Rules.h:77
static ValueTy initialValue()
Definition: Rules.h:178
static ValueTy reduce(ValueTy lhs, ValueTy rhs)
Definition: Rules.h:150
A binary reducer that minimizing a numeric type.
Definition: Rules.h:124
enum mlir::quantizer::ModificationResult::ModificationEnum value
bool operator==(const ValueTy &other) const
Definition: Rules.h:167
static ValueTy initialValue()
Definition: Rules.h:126
bool hasValue() const
Definition: Rules.h:76
static ValueTy reduce(ValueTy lhs, ValueTy rhs)
Definition: Rules.h:179
ModificationResult modify(bool isModified=true)
Definition: Rules.h:47
ValueTy(T value)
Definition: Rules.h:163
ModificationResult mergeFrom(const ThisTy &other)
Definition: Rules.h:94
ValueTy(T value, bool conflict)
Definition: Rules.h:164
static ValueTy initialValue()
Definition: Rules.h:111