ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MeanLayer.cc
Go to the documentation of this file.
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 "MeanLayer.h"
18
19#include "OperationUtils.h"
20
22
24{
25
26MeanLayer::MeanLayer() : _input(nullptr), _axes(nullptr), _output(nullptr), _keep_dims(false)
27{
28 // DO NOTHING
29}
30
32{
33 const auto inputShape = getShape(_input);
34 const auto axisVec = getReducerAxes(_axes);
35 bool axis_is_1_and_2 =
36 _keep_dims && inputShape.DimensionsCount() == 4 && axisVec.size() == 2 &&
37 ((axisVec[0] == 1 && axisVec[1] == 2) || (axisVec[0] == 2 && axisVec[1] == 1));
38
39 if (axis_is_1_and_2)
40 {
41 nnfw::cker::MeanAxis1And2(inputShape, getBuffer<float>(_input), getShape(_output),
42 getBuffer<float>(_output));
43 }
44 else
45 {
46 nnfw::cker::Mean(inputShape, getBuffer<float>(_input), getShape(_output),
47 getBuffer<float>(_output), axisVec);
48 }
49}
50
57
59 IPortableTensor *output, bool keep_dims)
60{
61 _input = input;
62 _axes = axes;
63 _output = output;
64 _keep_dims = keep_dims;
65
66 if (_input->data_type() != OperandType::FLOAT32 &&
67 _input->data_type() != OperandType::QUANT_UINT8_ASYMM)
68 throw std::runtime_error{"Mean: unsupported data type"};
69}
70
72{
73 if (_input->data_type() == OperandType::FLOAT32)
74 {
76 }
77 else if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
78 {
79 MeanQuant8();
80 }
81 else
82 {
83 throw std::runtime_error{"Mean: unsupported data type"};
84 }
85}
86
87} // namespace onert::backend::cpu::ops
A tensor class that is portable for other backends.
float data_scale() const override final
int32_t data_zero_point() const override final
ir::DataType data_type() const override final
void configure(const IPortableTensor *input, const IPortableTensor *axes, IPortableTensor *output, bool keep_dims)
Definition MeanLayer.cc:58
const IPortableTensor * _input
Definition MeanLayer.h:43
const IPortableTensor * _axes
Definition MeanLayer.h:44
void MeanQ8Asymm(const Shape &input_shape, const In *input_data, float input_scale, int32_t input_offset, const Shape &output_shape, Out *output_data, float output_scale, int32_t output_offset, const std::vector< int > &axes)
Definition ReduceMean.h:221
void MeanAxis1And2(const Shape &input_shape, const In *input_data, const Shape &output_shape, Out *output_data)
Definition ReduceMean.h:233
void Mean(const Shape &input_shape, const In *input_data, const Shape &output_shape, Out *output_data, const std::vector< int > &axes)
Definition ReduceMean.h:211
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
std::vector< int32_t > getReducerAxes(const IPortableTensor *axes)