/
segment_sum.cpp
84 lines (63 loc) · 3.13 KB
/
segment_sum.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
/*******************************************************************************
* 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
******************************************************************************/
//
// Created by george@skymind.io on 2/21/2018.
//
#include <ops/declarable/CustomOperations.h>
#include <ops/declarable/helpers/segment.h>
namespace nd4j {
namespace ops {
CUSTOM_OP_IMPL(segment_sum, 2, 1, false, 0, 0) {
NDArray<T>* input = INPUT_VARIABLE(0);
NDArray<T>* idxSegments = INPUT_VARIABLE(1);
NDArray<T>* segmentedOutput = OUTPUT_VARIABLE(0);
REQUIRE_TRUE(idxSegments->isVector(), 0, "segment_sum: segment indexes array should be a vector, but it rank is %i.", idxSegments->rankOf());
REQUIRE_TRUE(idxSegments->lengthOf() == input->sizeAt(0), 0, "segment_sum: segment indexes array length should be equal to the input first dimension, but %i != %i.", idxSegments->lengthOf(), input->sizeAt(0));
Nd4jLong expected, wrong;
REQUIRE_TRUE(helpers::segmentIndicesValidate(idxSegments, expected, wrong), 0, "segment_sum: segment indices should be arranged, but %i > %i",
wrong, expected);
helpers::segmentSumFunctor(input, idxSegments, segmentedOutput);
return ND4J_STATUS_OK;
}
DECLARE_SHAPE_FN(segment_sum) {
NDArray<T>* idxVector = INPUT_VARIABLE(1);
auto in = inputShape->at(0);
int outRank = shape::rank(in);
Nd4jLong* outputShape = nullptr;
T val = (*idxVector)(idxVector->lengthOf() - 1);
int numOfClasses = static_cast<int>(val) + 1;
ALLOCATE(outputShape, block.getWorkspace(), shape::shapeInfoLength(outRank), Nd4jLong);
outputShape[0] = outRank;
outputShape[1] = numOfClasses;
for(int i = 1; i < outRank; ++i)
outputShape[i + 1] = shape::sizeAt(in, i);
shape::updateStrides(outputShape, shape::order(in));
return SHAPELIST(outputShape);
}
CUSTOM_OP_IMPL(segment_sum_bp, 3, 2, false, 0, 0) {
return helpers::segmentSumFunctorBP(INPUT_VARIABLE(0), INPUT_VARIABLE(1), INPUT_VARIABLE(2), OUTPUT_VARIABLE(0));
}
DECLARE_SHAPE_FN(segment_sum_bp){
Nd4jLong* in = inputShape->at(0);
Nd4jLong* inIdx = inputShape->at(1);
Nd4jLong* outShape;
Nd4jLong* outIndex;
COPY_SHAPE(in, outShape);
COPY_SHAPE(inIdx, outIndex);
return SHAPELIST(outShape, outIndex);
}
}
}