2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "MinMaxObserver.h"
19 #include <luci/IR/CircleOpcode.h>
21 using DataType = luci_interpreter::DataType;
23 namespace record_minmax
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)
30 // CircleOutput does not produce a tensor
31 assert(node->opcode() != luci::CircleOpcode::CIRCLEOUTPUT);
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);
41 if (node->opcode() == luci::CircleOpcode::CONST)
43 // node is not activation. Do nothing.
47 if (node->opcode() == luci::CircleOpcode::ARG_MAX)
49 // Output of arg_max is the index of the largest value across axes of a tensor
50 // this should not be quantized
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");
58 const auto data = tensor->data<float>();
59 const auto num_elements = tensor->shape().num_elements();
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;
66 _minmax_data.recordMinMax(node, min, max);
69 } // namespace record_minmax