ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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
25namespace onert
26{
27namespace backend
28{
29namespace train
30{
31namespace ops
32{
33
35 : cpu::ops::MeanLayer(), _back_prop_input{nullptr}, _back_prop_output{nullptr}
36{
37 // DO NOTHING
38}
39
41 const IPortableTensor *back_prop_output)
42{
43 _back_prop_input = back_prop_input;
44 _back_prop_output = back_prop_output;
45}
46
48
50{
51 nnfw::cker::Shape keep_dim_shape;
52 // If _keep_dims is false, the input rank and the output rank can be different.
53 // MeanGrad does not support other ranking cases. This code corrects the shape
54 // by creating a temporary shape having the same rank as the input.
55 if (_keep_dims == false)
56 {
57 keep_dim_shape.ReplaceWith(getShape(_input));
58 auto axes_vec = cpu::ops::getReducerAxes(_axes);
59 for (const auto &axis : axes_vec)
60 {
61 keep_dim_shape.SetDim(axis, 1);
62 }
63 }
64 else
65 {
66 keep_dim_shape.ReplaceWith(getShape(_back_prop_output));
67 }
68
69 switch (_back_prop_output->data_type())
70 {
71 case OperandType::FLOAT32:
72 {
73 nnfw::cker::train::MeanGrad(keep_dim_shape, getBuffer<float>(_back_prop_output),
74 getShape(_back_prop_input), getBuffer<float>(_back_prop_input));
75 break;
76 }
77 default:
78 throw std::runtime_error("train MeanLayer: unsupported data type");
79 }
80}
81
82} // namespace ops
83} // namespace train
84} // namespace backend
85} // namespace onert
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:49
const IPortableTensor * _axes
Definition MeanLayer.h:50
void configureBackward(IPortableTensor *back_prop_input, const IPortableTensor *back_prop_output)
Definition MeanLayer.cc:40
void forward(bool training) override
Definition MeanLayer.cc:47
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.