summaryrefslogtreecommitdiffstats
path: root/mlir/unittests/Quantizer/Support/RulesTest.cpp
blob: 7ddfb7157514646b64669a120cefda21339c312d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===- RulesTest.cpp - Rules unit tests -----------------------------------===//
//
// Copyright 2019 The MLIR Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================

#include "mlir/Quantizer/Support/Rules.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"

using namespace mlir;
using namespace mlir::quantizer;

namespace {

using TestDiscreteFact = DiscreteFact<int>;

TEST(ExpandingMinMaxReducer, Basic) {
  ExpandingMinMaxFact f;
  EXPECT_FALSE(f.hasValue());

  // First assertion always modifies.
  EXPECT_TRUE(modified(f.assertValue(0, {-1.0, 1.0})));
  EXPECT_TRUE(f.hasValue());
  EXPECT_EQ(std::make_pair(-1.0, 1.0), f.getValue());
  EXPECT_EQ(0, f.getSalience());

  // Assertion in the same band expands.
  EXPECT_TRUE(modified(f.assertValue(0, {-0.5, 2.0})));
  EXPECT_TRUE(f.hasValue());
  EXPECT_EQ(std::make_pair(-1.0, 2.0), f.getValue());
  EXPECT_EQ(0, f.getSalience());

  EXPECT_TRUE(modified(f.assertValue(0, {-2.0, 0.5})));
  EXPECT_TRUE(f.hasValue());
  EXPECT_EQ(std::make_pair(-2.0, 2.0), f.getValue());
  EXPECT_EQ(0, f.getSalience());

  // Same band smaller bound does not modify.
  EXPECT_FALSE(modified(f.assertValue(0, {-0.5, 0.5})));
  EXPECT_TRUE(f.hasValue());
  EXPECT_EQ(std::make_pair(-2.0, 2.0), f.getValue());
  EXPECT_EQ(0, f.getSalience());

  // Higher salience overrides.
  EXPECT_TRUE(modified(f.assertValue(10, {-0.2, 0.2})));
  EXPECT_TRUE(f.hasValue());
  EXPECT_EQ(std::make_pair(-0.2, 0.2), f.getValue());
  EXPECT_EQ(10, f.getSalience());

  // Lower salience no-ops.
  EXPECT_FALSE(modified(f.assertValue(5, {-2.0, 2.0})));
  EXPECT_TRUE(f.hasValue());
  EXPECT_EQ(std::make_pair(-0.2, 0.2), f.getValue());
  EXPECT_EQ(10, f.getSalience());

  // Merge from a fact without a value no-ops.
  ExpandingMinMaxFact f1;
  EXPECT_FALSE(modified(f.mergeFrom(f1)));
  EXPECT_TRUE(f.hasValue());
  EXPECT_EQ(std::make_pair(-0.2, 0.2), f.getValue());
  EXPECT_EQ(10, f.getSalience());

  // Merge from a fact with a value merges.
  EXPECT_TRUE(modified(f1.mergeFrom(f)));
  EXPECT_TRUE(f1.hasValue());
  EXPECT_EQ(std::make_pair(-0.2, 0.2), f1.getValue());
  EXPECT_EQ(10, f1.getSalience());
}

TEST(TestDiscreteFact, Basic) {
  TestDiscreteFact f;
  EXPECT_FALSE(f.hasValue());

  // Initial value.
  EXPECT_TRUE(modified(f.assertValue(0, {2})));
  EXPECT_FALSE(modified(f.assertValue(0, {2})));
  EXPECT_EQ(2, f.getValue().value);
  EXPECT_FALSE(f.getValue().conflict);

  // Conflicting update.
  EXPECT_TRUE(modified(f.assertValue(0, {4})));
  EXPECT_EQ(2, f.getValue().value); // Arbitrary but known to be first wins.
  EXPECT_TRUE(f.getValue().conflict);

  // Further update still conflicts.
  EXPECT_FALSE(modified(f.assertValue(0, {4})));
  EXPECT_EQ(2, f.getValue().value); // Arbitrary but known to be first wins.
  EXPECT_TRUE(f.getValue().conflict);

  // Different salience update does not conflict.
  EXPECT_TRUE(modified(f.assertValue(1, {6})));
  EXPECT_EQ(6, f.getValue().value);
  EXPECT_FALSE(f.getValue().conflict);
}

} // end anonymous namespace
OpenPOWER on IntegriCloud