summaryrefslogtreecommitdiff
path: root/compiler/locomotiv/src/Node/MatMul.test.cpp
blob: f1f3a52d3357cd25cb3b2be6a2fdcdcd23f64b1b (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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.
 */

#include "NodeExecution.h"

#include "locomotiv/NodeData.h"
#include "NodeDataImpl.h"
#include "NodeDomain.h"

#include <nncc/core/ADT/tensor/Shape.h>
#include <nncc/core/ADT/tensor/Buffer.h>
#include <nncc/core/ADT/tensor/Overlay.h>
#include <nncc/core/ADT/tensor/LexicalLayout.h>
#include "nncc/core/ADT/tensor/IndexEnumerator.h"

#include <gtest/gtest.h>

namespace
{
using nncc::core::ADT::tensor::Shape;
using nncc::core::ADT::tensor::LexicalLayout;
using nncc::core::ADT::tensor::make_buffer;
using nncc::core::ADT::tensor::make_overlay;

template <typename T>
void run_test(const T *lhs, const T *rhs, const T *expected_output, const Shape &lhs_shape,
              const Shape &rhs_shape, const Shape &out_shape, loco::DataType expected_datatype)
{
  auto g = loco::make_graph();
  // Fill lhs MatrixEncode
  auto lhs_enc = g->nodes()->create<loco::MatrixEncode>();
  {
    auto lhs_enc_buf = make_buffer<T, LexicalLayout>(lhs_shape);
    auto lhs_overlay = make_overlay<T, LexicalLayout>(lhs_shape, const_cast<T *>(lhs));
    for (nncc::core::ADT::tensor::IndexEnumerator e{lhs_shape}; e.valid(); e.advance())
    {
      const auto &ind = e.current();
      lhs_enc_buf.at(ind) = lhs_overlay.at(ind);
    }

    auto enc_data = locomotiv::make_data(lhs_enc_buf);
    locomotiv::annot_data(lhs_enc, std::move(enc_data));
    locomotiv::annot_domain(lhs_enc, loco::Domain::Matrix);
  }
  // Fill rhs MatrixEncode
  auto rhs_enc = g->nodes()->create<loco::MatrixEncode>();
  {
    auto rhs_enc_buf = make_buffer<T, LexicalLayout>(rhs_shape);
    auto rhs_overlay = make_overlay<T, LexicalLayout>(rhs_shape, const_cast<T *>(rhs));
    for (nncc::core::ADT::tensor::IndexEnumerator e{rhs_shape}; e.valid(); e.advance())
    {
      const auto &ind = e.current();
      rhs_enc_buf.at(ind) = rhs_overlay.at(ind);
    }

    auto enc_data = locomotiv::make_data(rhs_enc_buf);
    locomotiv::annot_data(rhs_enc, std::move(enc_data));
    locomotiv::annot_domain(rhs_enc, loco::Domain::Matrix);
  }

  // build MatMul
  auto mat_mul = g->nodes()->create<loco::MatMul>();
  mat_mul->lhs(lhs_enc);
  mat_mul->rhs(rhs_enc);

  // run interpreter
  locomotiv::NodeExecution::get().run(mat_mul);

  // get result of calculation
  auto mat_mul_result = locomotiv::annot_data(mat_mul);

  // check the result
  ASSERT_NE(mat_mul_result, nullptr);
  ASSERT_TRUE(mat_mul_result->dtype() == expected_datatype);
  ASSERT_TRUE(*(mat_mul_result->shape()) == out_shape);

  auto out_overlay = make_overlay<T, LexicalLayout>(out_shape, const_cast<T *>(expected_output));
  for (nncc::core::ADT::tensor::IndexEnumerator e{out_shape}; e.valid(); e.advance())
  {
    const auto &ind = e.current();
    if (expected_datatype == loco::DataType::FLOAT32)
      ASSERT_FLOAT_EQ(out_overlay.at(ind), mat_mul_result->as_f32_bufptr()->at(ind));
    else if (expected_datatype == loco::DataType::S32)
      ASSERT_EQ(out_overlay.at(ind), mat_mul_result->as_s32_bufptr()->at(ind));
    else
      throw std::runtime_error("NYI for these DataTypes");
  }

  ASSERT_EQ(loco::Domain::Matrix, locomotiv::annot_domain(mat_mul));
}

} // namespace

// clang-format off
/* from the code below:

import numpy as np

a = [[-0.48850584,  1.4292705,  -1.3424522],
     [1.7021934,  -0.39246717,  0.6248314]]

b = [[-0.0830195,  0.21088193, -0.11781317],
     [0.07755677, 1.6337638,   1.0792778],
     [-1.6922939, -1.5437212,   0.96667504]]

print(np.array(a) @ np.array(b))
*/
TEST(NodeExecution_MatMul, f32_2x3_3x3)
{
  using nncc::core::ADT::tensor::Shape;

  const float lhs[] =
  {
    -0.48850584,  1.4292705,  -1.3424522,
     1.7021934,  -0.39246717,  0.6248314
  };

  const float rhs[] =
  {
    -0.0830195,  0.21088193, -0.11781317,
     0.07755677, 1.6337638,   1.0792778,
    -1.6922939, -1.5437212,   0.96667504
  };

  const float out[] =
  {
    2.42322878,  4.30444527,  0.30241731,
    -1.2291521,  -1.2468023,  -0.02011299
  };

  run_test<float>(lhs, rhs, out, Shape{2, 3}, Shape{3, 3}, Shape{2, 3}, loco::DataType::FLOAT32);
}

/* from the code below:

import numpy as np

a = np.random.randint(10000, size=(4, 2))

b = np.random.randint(10000, size=(2, 6))

print(a)
print(b)
print(np.array(a) @ np.array(b))
*/
TEST(NodeExecution_MatMul, s32_4x2_2x6)
{
  using nncc::core::ADT::tensor::Shape;

  const int32_t lhs[] =
  {
    6392, 4993,
      54, 9037,
    3947, 5820,
    5800, 4181
  };

  const int32_t rhs[] =
  {
    2694, 8376, 8090, 1285, 7492, 1652,
    5427, 8798, 7634, 2229, 5439, 6999
  };

  const int32_t out[] =
  {
    44317059, 97467806, 89827842, 19343117, 75045791, 45505591,
    49189275, 79959830, 69425318, 20212863, 49556811, 63339171,
    42218358, 84264432, 76361110, 18044675, 61225904, 47254624,
    38315487, 85365238, 78839754, 16772449, 66194059, 38844419
  };

  run_test<int32_t>(lhs, rhs, out, Shape{4, 2}, Shape{2, 6}, Shape{4, 6}, loco::DataType::S32);
}

// clang-format on