RulesTest.cpp
3.15 KB
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
//===- RulesTest.cpp - Rules unit tests -----------------------------------===//
//
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#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