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
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/op_boilerplate.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#if NOT_EXCLUDED(OP_tile_to_shape)
|
|
|
|
|
|
|
|
#include <ops/declarable/headers/shape.h>
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
2020-02-13 18:59:35 +01:00
|
|
|
CUSTOM_OP_IMPL(tile_to_shape, 1, 1, false, 0, -1) {
|
2019-12-20 20:35:39 +01:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
2019-12-20 20:35:39 +01:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
std::vector<Nd4jLong> outShape(block.getIArguments()->begin(), block.getIArguments()->end());
|
|
|
|
|
|
|
|
if (block.isInplace()) {
|
2019-12-20 20:35:39 +01:00
|
|
|
input->tileToShape(outShape, *input);
|
2019-06-06 14:21:15 +02:00
|
|
|
} else {
|
2019-12-20 20:35:39 +01:00
|
|
|
input->tileToShape(outShape, *output);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_SHAPE_FN(tile_to_shape) {
|
|
|
|
auto in = inputShape->at(0);
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
// output shape always equals to arguments
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
auto conv = ArrayUtils::toLongVector(*block.getIArguments());
|
|
|
|
|
2020-01-30 08:07:24 +01:00
|
|
|
auto newShape = ConstantShapeHelper::getInstance()->createShapeInfo(ArrayOptions::dataType(in), shape::order(in), conv);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
return SHAPELIST(newShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_TYPES(tile_to_shape) {
|
|
|
|
getOpDescriptor()
|
2020-03-02 10:49:41 +01:00
|
|
|
->setAllowedInputTypes(sd::DataType::ANY)
|
2019-06-06 14:21:15 +02:00
|
|
|
->setSameMode(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_TYPES(tile_to_shape_bp) {
|
|
|
|
getOpDescriptor()
|
2020-03-02 10:49:41 +01:00
|
|
|
->setAllowedInputTypes(sd::DataType::ANY)
|
2019-06-06 14:21:15 +02:00
|
|
|
->setAllowedOutputTypes({ALL_FLOATS});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CUSTOM_OP_IMPL(tile_to_shape_bp, 2, 1, true, 0, -1) {
|
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto epsNext = INPUT_VARIABLE(1);
|
|
|
|
|
|
|
|
auto gradX = OUTPUT_VARIABLE(0);
|
|
|
|
|
|
|
|
auto axisX = ShapeUtils::evalBroadcastBackwardAxis(input->shapeInfo(), epsNext->shapeInfo());
|
2019-12-20 20:35:39 +01:00
|
|
|
// FIX ME: reduceAlongDimension should have a signature with result pass to avoid assigning twice
|
2019-06-06 14:21:15 +02:00
|
|
|
if (!axisX.empty()) {
|
2019-12-20 20:35:39 +01:00
|
|
|
auto tempRes = epsNext->reduceAlongDimension(reduce::Sum, axisX);
|
2019-06-06 14:21:15 +02:00
|
|
|
gradX->assign(tempRes);
|
|
|
|
} else
|
|
|
|
gradX->assign(epsNext);
|
|
|
|
|
|
|
|
STORE_RESULT(gradX);
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_SHAPE_FN(tile_to_shape_bp) {
|
|
|
|
auto in = inputShape->at(0);
|
|
|
|
|
|
|
|
Nd4jLong *newShape;
|
|
|
|
COPY_SHAPE(in, newShape);
|
|
|
|
|
|
|
|
return SHAPELIST(CONSTANT(newShape));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|