80a18c11a8dd494c802e6fe9cdf4194457cc9ec5
[platform/core/ml/nnfw.git] / runtime / libs / misc / src / tensor / Comparator.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "misc/tensor/Comparator.h"
18 #include "misc/tensor/Zipper.h"
19
20 #include "misc/fp32.h"
21
22 namespace nnfw
23 {
24 namespace misc
25 {
26 namespace tensor
27 {
28
29 std::vector<Diff<float>> Comparator::compare(const Shape &shape, const Reader<float> &expected,
30                                              const Reader<float> &obtained,
31                                              Observer *observer) const
32 {
33   std::vector<Diff<float>> res;
34
35   zip(shape, expected, obtained) <<
36       [&](const Index &index, float expected_value, float obtained_value) {
37         if (!_compare_fn(expected_value, obtained_value))
38         {
39           res.emplace_back(index, expected_value, obtained_value);
40         }
41
42         // Update max_diff_index, if necessary
43         if (observer != nullptr)
44         {
45           observer->notify(index, expected_value, obtained_value);
46         }
47       };
48
49   return res;
50 }
51
52 } // namespace tensor
53 } // namespace misc
54 } // namespace nnfw