Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / record-minmax / src / MinMaxObserver.cpp
1 /*
2  * Copyright (c) 2020 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 "MinMaxObserver.h"
18
19 #include <luci/IR/CircleOpcode.h>
20
21 using DataType = luci_interpreter::DataType;
22
23 namespace record_minmax
24 {
25
26 // postTensorWrite is only called for a node producing a tensor
27 void MinMaxObserver::postTensorWrite(const luci::CircleNode *node,
28                                      const luci_interpreter::Tensor *tensor)
29 {
30   // CircleOutput does not produce a tensor
31   assert(node->opcode() != luci::CircleOpcode::CIRCLEOUTPUT);
32
33   // Operators with multiple outputs
34   assert(node->opcode() != luci::CircleOpcode::IF);
35   assert(node->opcode() != luci::CircleOpcode::SPLIT);
36   assert(node->opcode() != luci::CircleOpcode::SPLIT_V);
37   assert(node->opcode() != luci::CircleOpcode::TOPK_V2);
38   assert(node->opcode() != luci::CircleOpcode::UNPACK);
39   assert(node->opcode() != luci::CircleOpcode::WHILE);
40
41   if (node->opcode() == luci::CircleOpcode::CONST)
42   {
43     // node is not activation. Do nothing.
44     return;
45   }
46
47   if (node->opcode() == luci::CircleOpcode::ARG_MAX)
48   {
49     // Output of arg_max is the index of the largest value across axes of a tensor
50     // this should not be quantized
51     return;
52   }
53
54   // Only support recording of float32 values
55   if (tensor->element_type() != DataType::FLOAT32)
56     throw std::runtime_error("Tensor's data type is not float");
57
58   const auto data = tensor->data<float>();
59   const auto num_elements = tensor->shape().num_elements();
60
61   std::vector<float> buf(data, data + num_elements);
62   auto minmax = std::minmax_element(buf.begin(), buf.end());
63   float min = *minmax.first;
64   float max = *minmax.second;
65
66   _minmax_data.recordMinMax(node, min, max);
67 }
68
69 } // namespace record_minmax