Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_special_specs.cc - github.com/google/ruy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41e6e51fd4fa70b4e0c57655b916415ec95d3780 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Copyright 2019 Google LLC. All Rights Reserved.

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.
==============================================================================*/

// This test covers non-basic specs.

#include "test.h"

namespace ruy {

template <typename AccumScalar, typename DstScalar,
          LoopStructure tLoopStructure>
struct LoopStructureSpec : BasicSpec<AccumScalar, DstScalar> {
  static constexpr LoopStructure kLoopStructure = tLoopStructure;
};

template <typename AccumScalar, typename DstScalar,
          ZeroPointSupport tZeroPointSupport>
struct ZeroPointSupportSpec : BasicSpec<AccumScalar, DstScalar> {
  static constexpr ZeroPointSupport kZeroPointSupport = tZeroPointSupport;
};

template <typename AccumScalar, typename DstScalar>
struct RCCSpec : BasicSpec<AccumScalar, DstScalar> {
  static constexpr LayoutSupport kLayoutSupport = LayoutSupport::kRCC;
};

template <typename AccumScalar, typename DstScalar, typename LhsKernelLayout,
          typename RhsKernelLayout>
struct StandardCppKernelLayoutSpec : BasicSpec<AccumScalar, DstScalar> {
  using StandardCppKernelLhsLayout = LhsKernelLayout;
  using StandardCppKernelRhsLayout = RhsKernelLayout;
  static int local_data_cache_size() { return 1; }
  static int shared_data_cache_size() { return 1; }
};

using LhsScalar = RUY_TEST_LHSSCALAR;
using RhsScalar = RUY_TEST_RHSSCALAR;
using AccumScalar = RUY_TEST_ACCUMSCALAR;
using DstScalar = RUY_TEST_DSTSCALAR;

template <LoopStructure tLoopStructure>
void TestLoopStructure() {
  using SpecType = LoopStructureSpec<AccumScalar, DstScalar, tLoopStructure>;
  using TestSetType = TestSet<LhsScalar, RhsScalar, SpecType>;
  for (int size = 1; size < 10; size++) {
    TestLinearAllOrders<TestSetType>(size, size, size);
  }
  TestLinearAllOrders<TestSetType>(3, 5, 78);
  TestLinearAllOrders<TestSetType>(19, 91, 7);
  TestLinearAllOrders<TestSetType>(71, 26, 44);
  TestLinearAllOrders<TestSetType>(81, 93, 72);
}

TEST(TestSpecialSpecs, LoopStructure) {
  static_assert(BasicSpec<std::uint8_t, std::int32_t>::kLoopStructure ==
                    LoopStructure::kAuto,
                "");
  static_assert(BasicSpec<float, float>::kLoopStructure == LoopStructure::kAuto,
                "");
  TestLoopStructure<LoopStructure::kSimple>();
  TestLoopStructure<LoopStructure::kGeneral>();
}

template <ZeroPointSupport tZeroPointSupport>
void TestZeroPointSupport(LhsScalar lhs_zero_point, RhsScalar rhs_zero_point,
                          DstScalar dst_zero_point,
                          ExpectedOutcome expected_outcome) {
  using SpecType =
      ZeroPointSupportSpec<AccumScalar, DstScalar, tZeroPointSupport>;
  using TestSetType = TestSet<LhsScalar, RhsScalar, SpecType>;
  TestSetType test_set;
  test_set.rows = 11;
  test_set.depth = 12;
  test_set.cols = 13;
  test_set.lhs_order = Order::kRowMajor;
  test_set.rhs_order = Order::kColMajor;
  test_set.dst_order = Order::kColMajor;
  test_set.layout_style = LayoutStyle::kPackedLinear;
  test_set.expected_outcome = expected_outcome;
  test_set.lhs_zero_point = lhs_zero_point;
  test_set.rhs_zero_point = rhs_zero_point;
  test_set.dst_zero_point = dst_zero_point;
  test_set.use_specified_zero_points = true;
  test_set.Run();
}

TEST(TestSpecialSpecs, ZeroPointSupport) {
  // Sanity check
  RUY_CHECK_EQ(SymmetricZeroPoint<std::uint8_t>(), 128);
  RUY_CHECK_EQ(SymmetricZeroPoint<std::int8_t>(), 0);

  if (std::is_floating_point<LhsScalar>::value) {
    return;
  }

  TestZeroPointSupport<ZeroPointSupport::kGeneral>(
      SymmetricZeroPoint<LhsScalar>(), SymmetricZeroPoint<RhsScalar>(),
      SymmetricZeroPoint<DstScalar>(), ExpectedOutcome::kSuccess);
  TestZeroPointSupport<ZeroPointSupport::kGeneral>(
      SymmetricZeroPoint<LhsScalar>() - 1, SymmetricZeroPoint<RhsScalar>(),
      SymmetricZeroPoint<DstScalar>(), ExpectedOutcome::kSuccess);
  TestZeroPointSupport<ZeroPointSupport::kSymmetric>(
      SymmetricZeroPoint<LhsScalar>(), SymmetricZeroPoint<RhsScalar>(),
      SymmetricZeroPoint<DstScalar>(), ExpectedOutcome::kSuccess);
  TestZeroPointSupport<ZeroPointSupport::kSymmetric>(
      SymmetricZeroPoint<LhsScalar>() + 1, SymmetricZeroPoint<RhsScalar>(),
      SymmetricZeroPoint<DstScalar>(), ExpectedOutcome::kDeath);
  TestZeroPointSupport<ZeroPointSupport::kSymmetric>(
      SymmetricZeroPoint<LhsScalar>(), SymmetricZeroPoint<RhsScalar>() + 1,
      SymmetricZeroPoint<DstScalar>(), ExpectedOutcome::kDeath);
  TestZeroPointSupport<ZeroPointSupport::kSymmetric>(
      SymmetricZeroPoint<LhsScalar>(), SymmetricZeroPoint<RhsScalar>(),
      SymmetricZeroPoint<DstScalar>() - 1, ExpectedOutcome::kDeath);
}

TEST(TestSpecialSpecs, RCC) {
  using RCCSpec = RCCSpec<AccumScalar, DstScalar>;
  using RCCTestSet = TestSet<LhsScalar, RhsScalar, RCCSpec>;
  TestRCC<RCCTestSet>(81, 93, 72);
  TestNonRCC<RCCTestSet>(81, 93, 72, ExpectedOutcome::kDeath);
}

template <typename LhsKernelLayout, typename RhsKernelLayout>
void TestStandardCppKernelLayout() {
  using SpecType =
      StandardCppKernelLayoutSpec<AccumScalar, DstScalar, LhsKernelLayout,
                                  RhsKernelLayout>;
  using TestSetType = TestSet<LhsScalar, RhsScalar, SpecType>;
  for (int size = 1; size < 10; size++) {
    TestLinearAllOrders<TestSetType>(size, size, size);
  }
  TestLinearAllOrders<TestSetType>(87, 34, 56);
  TestLinearAllOrders<TestSetType>(123, 234, 78);
}

TEST(TestSpecialSpecs, StandardCppKernelLayoutTrivial1x1) {
  TestStandardCppKernelLayout<FixedKernelLayout<Order::kColMajor, 1, 1>,
                              FixedKernelLayout<Order::kColMajor, 1, 1>>();
}

TEST(TestSpecialSpecs, StandardCppKernelLayoutSquare4x4) {
  TestStandardCppKernelLayout<FixedKernelLayout<Order::kRowMajor, 4, 4>,
                              FixedKernelLayout<Order::kRowMajor, 4, 4>>();
}

TEST(TestSpecialSpecs, StandardCppKernelLayoutRectangular4x8) {
  TestStandardCppKernelLayout<FixedKernelLayout<Order::kColMajor, 1, 4>,
                              FixedKernelLayout<Order::kColMajor, 1, 8>>();
}

}  // namespace ruy