/
reduceMean.cpp
161 lines (120 loc) · 6.29 KB
/
reduceMean.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// @author Yurii Shyrma (iuriish@yahoo.com), created on 01.06.2018
//
#include <ops/declarable/CustomOperations.h>
#include <ops/declarable/helpers/axis.h>
namespace nd4j {
namespace ops {
//////////////////////////////////////////////////////////////////////////
CUSTOM_OP_IMPL(reduce_mean, 1, 1, false, 0, 0) {
auto input = INPUT_VARIABLE(0);
auto output = OUTPUT_VARIABLE(0);
auto dimensions = *block.getIArguments();
if (block.width() > 1) {
auto axesVector = INPUT_VARIABLE(1);
helpers::adjustAxis(input, axesVector, dimensions);
}
// else if (block.getIArguments()->size())
bool keepDims = false;
if (block.getBArguments()->size())
keepDims = B_ARG(0);
else if (block.getTArguments()->size())
keepDims = (bool)T_ARG(0);
REQUIRE_TRUE(dimensions.size() <= input->rankOf(), 0, "REDUCE_MEAN OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());
for(const auto& item : dimensions)
REQUIRE_TRUE(item > -input->rankOf() || item < input->rankOf(), 0, "REDUCE_MEAN OP: the input dimension to reduce along must be in range (-%i, %i), but got %i instead !" , input->rankOf(), input->rankOf(), item);
input->reduceAlongDimension(reduce::Mean, output, dimensions, keepDims);
return Status::OK();
}
DECLARE_TYPES(reduce_mean) {
getOpDescriptor()
->setAllowedInputTypes(nd4j::DataType::ANY)
->setAllowedOutputTypes({ALL_FLOATS});
}
DECLARE_SHAPE_FN(reduce_mean) {
auto dimensions = *block.getIArguments();
if (block.width() > 1) {
auto axesVector = INPUT_VARIABLE(1);
helpers::adjustAxis(INPUT_VARIABLE(0), axesVector, dimensions);
}
// else if (block.getIArguments()->size())
bool keepDims = false;
if (block.getBArguments()->size())
keepDims = B_ARG(0);
else if (block.getTArguments()->size())
keepDims = (bool)T_ARG(0);
REQUIRE_TRUE(dimensions.size() <= inputShape->at(0)[0], 0, "REDUCE_MEAN OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());
for(const auto& item : dimensions)
REQUIRE_TRUE(item > -inputShape->at(0)[0] || item < inputShape->at(0)[0], 0, "REDUCE_MEAN OP: the input dimension to reduce along must be in range (-%i, %i), but got %i instead !" , inputShape->at(0)[0], inputShape->at(0)[0], item);
auto outShapeInfo = ShapeUtils::evalReduceShapeInfo(shape::order(inputShape->at(0)), dimensions, inputShape->at(0), keepDims, false, block.getWorkspace());
return SHAPELIST(outShapeInfo);
}
DECLARE_TYPES(reduce_mean_bp) {
getOpDescriptor()
->setAllowedInputTypes(nd4j::DataType::ANY)
->setAllowedOutputTypes({ALL_FLOATS});
}
//////////////////////////////////////////////////////////////////////////
CUSTOM_OP_IMPL(reduce_mean_bp, 2, 1, false, 0, 0) {
auto input = INPUT_VARIABLE(0);
auto gradO = INPUT_VARIABLE(1);
auto gradI = OUTPUT_VARIABLE(0);
auto dimensions = *block.getIArguments();
if (block.width() > 2) {
auto axesVector = INPUT_VARIABLE(2);
helpers::adjustAxis(input, axesVector, dimensions);
}
// else if (block.getIArguments()->size())
bool keepDims = false;
if (block.getBArguments()->size())
keepDims = B_ARG(0);
else if (block.getTArguments()->size())
keepDims = (bool)T_ARG(0);
REQUIRE_TRUE(dimensions.size() <= input->rankOf(), 0, "REDUCE_MEAN_BP OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());
for(const auto& item : dimensions)
REQUIRE_TRUE(item > -input->rankOf() || item < input->rankOf(), 0, "REDUCE_MEAN_BP OP: the input dimension to reduce along must be in range (-%i, %i), but got %i instead !" , input->rankOf(), input->rankOf(), item);
if(gradO->isScalar()) {
gradI->assign((*gradO) / input->lengthOf());
}
else {
(*gradI).assign((gradO->lengthOf() + 0.) / input->lengthOf());
Nd4jLong* gradOShapeKeepDims = ShapeUtils::evalReduceShapeInfo(input->ordering(), dimensions, *input, true, false, block.getWorkspace());
const bool isGradOShapeBroadcast = shape::equalsSoft(gradOShapeKeepDims, gradO->getShapeInfo());
if(!isGradOShapeBroadcast)
gradO = gradO->reshape(gradO->ordering(), ShapeUtils::pullShapeFromShapeInfo(gradOShapeKeepDims)); // for example could be something like [a,b] -> [1,a,1,b]
*gradI *= *gradO;
if(!isGradOShapeBroadcast)
delete gradO;
}
return Status::OK();
}
DECLARE_SHAPE_FN(reduce_mean_bp) {
auto dimensions = *block.getIArguments();
if (block.width() > 2) {
auto axesVector = INPUT_VARIABLE(2);
helpers::adjustAxis(INPUT_VARIABLE(0), axesVector, dimensions);
}
REQUIRE_TRUE(dimensions.size() <= inputShape->at(0)[0], 0, "REDUCE_MEAN_BP OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());
for(const auto& item : dimensions)
REQUIRE_TRUE(item > -inputShape->at(0)[0] || item < inputShape->at(0)[0], 0, "REDUCE_MEAN_BP OP: the input dimension to reduce along must be in range (-%i, %i), but got %i instead !" , inputShape->at(0)[0], inputShape->at(0)[0], item);
Nd4jLong* gradIshapeInfo(nullptr);
COPY_SHAPE(inputShape->at(0), gradIshapeInfo);
return SHAPELIST(gradIshapeInfo);
}
}
}