cavis/libnd4j/include/ops/declarable/generic/parity_ops/segment_mean.cpp

103 lines
4.2 KiB
C++
Raw Normal View History

2019-06-06 14:21:15 +02:00
/*******************************************************************************
* 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 sd {
2019-06-06 14:21:15 +02:00
namespace ops {
CUSTOM_OP_IMPL(segment_mean, 2, 1, false, 0, 0) {
auto input = INPUT_VARIABLE(0);
auto idxSegments = INPUT_VARIABLE(1);
auto segmentedOutput = OUTPUT_VARIABLE(0);
REQUIRE_TRUE(idxSegments->isVector(), 0, "segment_mean: segment indexes array should be a vector, but it rank is %i.", idxSegments->rankOf());
REQUIRE_TRUE(idxSegments->lengthOf() == input->sizeAt(0), 0, "segment_mean: segment indexes array length should be equal to the input first dimension, but %i != %i.", idxSegments->lengthOf(), input->sizeAt(0));
auto expected = NDArrayFactory::create(input->dataType(), 0.f, block.launchContext());
auto wrong = NDArrayFactory::create(input->dataType(), 0.f, block.launchContext());
REQUIRE_TRUE(helpers::segmentIndicesValidate(block.launchContext(), idxSegments, expected, wrong), 0, "segment_mean: segment indices should be arranged, but %2.1f > %2.1f", expected.e<float>(0), wrong.e<float>(0));
segmentedOutput->nullify();
helpers::segmentMeanFunctor(block.launchContext(), input, idxSegments, segmentedOutput);
return Status::OK();
}
DECLARE_SHAPE_FN(segment_mean) {
auto idxVector = INPUT_VARIABLE(1);
auto in = inputShape->at(0);
int outRank = shape::rank(in);
Nd4jLong* outputShape = nullptr;
int val = (*idxVector).e<int>(idxVector->lengthOf() - 1);
int numOfClasses = 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);
ShapeUtils::updateStridesAndType(outputShape, in, shape::order(in));
return SHAPELIST(CONSTANT(outputShape));
}
DECLARE_TYPES(segment_mean) {
getOpDescriptor()
->setAllowedInputTypes({ALL_INTS, ALL_FLOATS})
->setAllowedInputTypes(1, {ALL_INTS})
->setAllowedOutputTypes({ALL_FLOATS})
->setSameMode(false);
}
CUSTOM_OP_IMPL(segment_mean_bp, 3, 2, false, 0, 0) {
auto input = INPUT_VARIABLE(0);
auto indices = INPUT_VARIABLE(1);
auto gradOut = INPUT_VARIABLE(2);
Nullify (#304) * initial commit Signed-off-by: raver119 <raver119@gmail.com> * bunch of tweaks Signed-off-by: raver119 <raver119@gmail.com> * hamming distance nullification Signed-off-by: raver119 <raver119@gmail.com> * Add output array value assignment for testing/debugging Signed-off-by: Alex Black <blacka101@gmail.com> * don't assign empty arrays Signed-off-by: raver119 <raver119@gmail.com> * conv2d/conv3d/depthwise2d nullified Signed-off-by: raver119 <raver119@gmail.com> * conv2d/conv3d/depthwise2d nullified Signed-off-by: raver119 <raver119@gmail.com> * conv2d/conv3d/depthwise2d nullified Signed-off-by: raver119 <raver119@gmail.com> * few more fixes Signed-off-by: raver119 <raver119@gmail.com> * im2col Signed-off-by: raver119 <raver119@gmail.com> * pooling? Signed-off-by: raver119 <raver119@gmail.com> * more nullified Signed-off-by: raver119 <raver119@gmail.com> * ismax nullified Signed-off-by: raver119 <raver119@gmail.com> * rollback ismax nullification Signed-off-by: raver119 <raver119@gmail.com> * synchronized cublas handle use on per-device basis Signed-off-by: raver119 <raver119@gmail.com> * hiding method from jcpp Signed-off-by: raver119 <raver119@gmail.com> * get rid of test assigns in DeclarableOp Signed-off-by: raver119 <raver119@gmail.com> * get rid of assigns Signed-off-by: raver119 <raver119@gmail.com> * proper deviceId is back Signed-off-by: raver119 <raver119@gmail.com> * include fixed Signed-off-by: raver119 <raver119@gmail.com> Co-authored-by: Alex Black <blacka101@gmail.com>
2020-03-20 06:49:28 +01:00
auto output = OUTPUT_NULLIFIED(0);
auto outIndices = OUTPUT_NULLIFIED(1);
2019-06-06 14:21:15 +02:00
outIndices->assign(indices);
return helpers::segmentMeanFunctorBP(block.launchContext(), input, indices, gradOut, output);
}
DECLARE_SHAPE_FN(segment_mean_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(CONSTANT(outShape), CONSTANT(outIndex));
}
DECLARE_TYPES(segment_mean_bp) {
getOpDescriptor()
->setAllowedInputTypes(sd::DataType::ANY)
2019-06-06 14:21:15 +02:00
->setAllowedOutputTypes(0, {ALL_FLOATS})
->setAllowedOutputTypes(1, {ALL_INTS})
->setSameMode(false);
}
}
}