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) 2023 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
21#include <cker/Shape.h>
24
26{
27
29 : cpu::ops::MeanLayer(), _back_prop_input{nullptr}, _back_prop_output{nullptr}
30{
31 // DO NOTHING
32}
33
35 const IPortableTensor *back_prop_output)
36{
37 _back_prop_input = back_prop_input;
38 _back_prop_output = back_prop_output;
39}
40
42
44{
45 nnfw::cker::Shape keep_dim_shape;
46 // If _keep_dims is false, the input rank and the output rank can be different.
47 // MeanGrad does not support other ranking cases. This code corrects the shape
48 // by creating a temporary shape having the same rank as the input.
49 if (_keep_dims == false)
50 {
51 keep_dim_shape.ReplaceWith(getShape(_input));
52 auto axes_vec = cpu::ops::getReducerAxes(_axes);
53 for (const auto &axis : axes_vec)
54 {
55 keep_dim_shape.SetDim(axis, 1);
56 }
57 }
58 else
59 {
60 keep_dim_shape.ReplaceWith(getShape(_back_prop_output));
61 }
62
63 switch (_back_prop_output->data_type())
64 {
65 case OperandType::FLOAT32:
66 {
67 nnfw::cker::train::MeanGrad(keep_dim_shape, getBuffer<float>(_back_prop_output),
68 getShape(_back_prop_input), getBuffer<float>(_back_prop_input));
69 break;
70 }
71 default:
72 throw std::runtime_error("train MeanLayer: unsupported data type");
73 }
74}
75
76} // namespace onert::backend::train::ops
void ReplaceWith(int dimensions_count, const int32_t *dims_data)
Definition Shape.h:130
void SetDim(int i, int32_t val)
Definition Shape.h:98
A tensor class that is portable for other backends.
ir::DataType data_type() const override final
const IPortableTensor * _input
Definition MeanLayer.h:43
const IPortableTensor * _axes
Definition MeanLayer.h:44
void configureBackward(IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output)
Definition MeanLayer.cc:34
void forward(bool training) override
Definition MeanLayer.cc:41
void MeanGrad(const Shape &incoming_shape, const T *incoming_data, const Shape &grad_shape, T *grad_data)
Definition ReduceMean.h:32
std::vector< int32_t > getReducerAxes(const IPortableTensor *axes)
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
Get shape of tensor.