summaryrefslogtreecommitdiff
path: root/compiler/i5diff/src/entry.cpp
blob: 456467f542fa84da6327176ce75385412928a34f (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * 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 <nncc/core/ADT/tensor/Shape.h>
#include <nncc/core/ADT/tensor/LexicalLayout.h>
#include <nncc/core/ADT/tensor/IndexEnumerator.h>

#include <H5Cpp.h>

#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>

namespace
{

enum class ErrorCode
{
  CountMismatch,
  TypeMismatch,
  ShapeMismatch,
  ValueMismatch,
};

template <ErrorCode EC> class ErrorDetail;

// TODO Record the details
template <> class ErrorDetail<ErrorCode::CountMismatch>
{
public:
  ErrorDetail() = default;
};

// TODO Record the details
template <> class ErrorDetail<ErrorCode::TypeMismatch>
{
public:
  ErrorDetail() = default;
};

// TODO Record the details
template <> class ErrorDetail<ErrorCode::ShapeMismatch>
{
public:
  ErrorDetail() = default;
};

// TODO Record the details
template <> class ErrorDetail<ErrorCode::ValueMismatch>
{
public:
  ErrorDetail() = default;
};

struct Observer
{
  virtual ~Observer() = default;

  virtual void notify(const ErrorDetail<ErrorCode::CountMismatch> &) = 0;
  virtual void notify(const ErrorDetail<ErrorCode::TypeMismatch> &) = 0;
  virtual void notify(const ErrorDetail<ErrorCode::ShapeMismatch> &) = 0;
  virtual void notify(const ErrorDetail<ErrorCode::ValueMismatch> &) = 0;
};

class Mux final : public Observer
{
public:
  Mux() = default;

public:
  void attach(Observer *o) { _observers.insert(o); }

private:
  template <ErrorCode EC> void notify_all(const ErrorDetail<EC> &e)
  {
    for (auto o : _observers)
    {
      o->notify(e);
    }
  }

public:
  void notify(const ErrorDetail<ErrorCode::CountMismatch> &e) final { notify_all(e); }
  void notify(const ErrorDetail<ErrorCode::TypeMismatch> &e) final { notify_all(e); }
  void notify(const ErrorDetail<ErrorCode::ShapeMismatch> &e) final { notify_all(e); }
  void notify(const ErrorDetail<ErrorCode::ValueMismatch> &e) final { notify_all(e); }

public:
  std::set<Observer *> _observers;
};

class ExitcodeTracker final : public Observer
{
public:
  const int &exitcode(void) const { return _exitcode; }

public:
  void notify(const ErrorDetail<ErrorCode::CountMismatch> &) { _exitcode = 1; }
  void notify(const ErrorDetail<ErrorCode::TypeMismatch> &) { _exitcode = 1; }
  void notify(const ErrorDetail<ErrorCode::ShapeMismatch> &) { _exitcode = 1; }
  void notify(const ErrorDetail<ErrorCode::ValueMismatch> &) { _exitcode = 1; }

public:
  int _exitcode = 0;
};

} // namespace

//
// HDF5 helpers
//
namespace
{

enum class DataType
{
  UNKNOWN,
  FLOAT32,
  /* TO BE ADDED */
};

DataType to_internal_dtype(const H5::DataType &dtype)
{
  if (dtype == H5::PredType::IEEE_F32BE)
  {
    return DataType::FLOAT32;
  }
  return DataType::UNKNOWN;
}

using TensorShape = nncc::core::ADT::tensor::Shape;

TensorShape to_internal_shape(const H5::DataSpace &dataspace)
{
  int rank = dataspace.getSimpleExtentNdims();

  std::vector<hsize_t> dims;

  dims.resize(rank, 0);

  dataspace.getSimpleExtentDims(dims.data());

  TensorShape res;

  res.resize(rank);
  for (int axis = 0; axis < rank; ++axis)
  {
    res.dim(axis) = dims[axis];
  }

  return res;
}

uint32_t element_count(const H5::DataSpace &dataspace)
{
  return nncc::core::ADT::tensor::num_elements(to_internal_shape(dataspace));
}

std::vector<float> as_float_vector(const H5::DataSet &dataset)
{
  std::vector<float> buffer;

  buffer.resize(element_count(dataset.getSpace()));
  dataset.read(buffer.data(), H5::PredType::NATIVE_FLOAT);

  return buffer;
}

using LexicalLayout = nncc::core::ADT::tensor::LexicalLayout;
using TensorIndexEnumerator = nncc::core::ADT::tensor::IndexEnumerator;

} // namespace

// TODO Report the details
int entry(int argc, char **argv)
{
  // The current implementation works only for command-line of the following form:
  //
  //   i5diff -d 0.001 /path/to/left.h5 /path/to/right.h5
  //
  // TODO Support more options
  assert(argc == 5);
  assert(std::string(argv[1]) == "-d");
  assert(std::string(argv[2]) == "0.001");

  H5::H5File lhs{argv[3], H5F_ACC_RDONLY};
  H5::H5File rhs{argv[4], H5F_ACC_RDONLY};

  ExitcodeTracker exitcode_tracker;

  Mux mux;
  mux.attach(&exitcode_tracker);

  // Compare values
  do
  {
    // NOTE The name of value group SHOULD BE aligned with nnkit HDF5 actions
    const std::string value_grpname{"value"};

    H5::Group lhs_value_grp = lhs.openGroup(value_grpname);
    H5::Group rhs_value_grp = rhs.openGroup(value_grpname);

    // Compare value count
    int64_t value_count = -1;
    {
      uint32_t lhs_value_count = static_cast<uint32_t>(lhs_value_grp.getNumObjs());
      uint32_t rhs_value_count = static_cast<uint32_t>(rhs_value_grp.getNumObjs());

      if (lhs_value_count != rhs_value_count)
      {
        ErrorDetail<ErrorCode::CountMismatch> error{};
        mux.notify(error);
        break;
      }

      value_count = std::max<int64_t>(lhs_value_count, rhs_value_count);
    }
    assert(value_count >= 0);

    // Compare each dataset
    for (int64_t n = 0; n < value_count; ++n)
    {
      // NOTE The name of dataset SHOULD BE aligned with nnkit HDF5 actions
      const std::string dataset_name = std::to_string(n);

      auto lhs_dataset = lhs_value_grp.openDataSet(dataset_name);
      auto rhs_dataset = rhs_value_grp.openDataSet(dataset_name);

      auto lhs_dtype = to_internal_dtype(lhs_dataset.getDataType());
      auto rhs_dtype = to_internal_dtype(rhs_dataset.getDataType());

      // TODO Support other data types
      assert(rhs_dtype == DataType::FLOAT32);
      assert(lhs_dtype == DataType::FLOAT32);

      if (lhs_dtype != rhs_dtype)
      {
        ErrorDetail<ErrorCode::TypeMismatch> error{};
        mux.notify(error);
        continue;
      }

      auto lhs_shape = to_internal_shape(lhs_dataset.getSpace());
      auto rhs_shape = to_internal_shape(rhs_dataset.getSpace());

      if (!(lhs_shape == rhs_shape))
      {
        ErrorDetail<ErrorCode::ShapeMismatch> error{};
        mux.notify(error);
        continue;
      }

      assert(lhs_shape == rhs_shape);
      assert(lhs_dtype == rhs_dtype);
      const auto &shape = lhs_shape;
      const auto &dtype = lhs_dtype;

      switch (dtype)
      {
        case DataType::FLOAT32:
        {
          auto lhs_vector = as_float_vector(lhs_dataset);
          auto rhs_vector = as_float_vector(rhs_dataset);

          assert(lhs_vector.size() == rhs_vector.size());

          LexicalLayout layout;

          for (TensorIndexEnumerator e{shape}; e.valid(); e.advance())
          {
            const auto &ind = e.current();
            auto lhs_value = lhs_vector.at(layout.offset(shape, ind));
            auto rhs_value = rhs_vector.at(layout.offset(shape, ind));

            // TODO Abstract equality criterion
            if (std::abs(lhs_value - rhs_value) >= 0.001f)
            {
              ErrorDetail<ErrorCode::ValueMismatch> error{};
              mux.notify(error);
              continue;
            }
          }

          break;
        }
        default:
          throw std::runtime_error{"Not supported, yet"};
      };
    }
  } while (false);

  // TODO Compare names (if requested)

  return exitcode_tracker.exitcode();
}