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 on 05.04.18.
|
|
|
|
//
|
|
|
|
#include <ops/declarable/helpers/dynamic.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void _dynamicPartitionFunctor(NDArray const* input, NDArray const* indices, std::vector<NDArray*>& outputList) {
|
|
|
|
std::vector<std::pair<NDArray *, int>> outputs(outputList.size());
|
|
|
|
int sourceDimsLen = input->rankOf() - indices->rankOf();
|
|
|
|
if (sourceDimsLen) {
|
|
|
|
std::vector<int> sourceDims(sourceDimsLen);
|
|
|
|
|
|
|
|
for (int i = sourceDimsLen; i > 0; i--)
|
|
|
|
sourceDims[sourceDimsLen - i] = input->rankOf() - i;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
ResultSet listOfTensors = input->allTensorsAlongDimension(sourceDims);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
unsigned int outSize = outputList.size();
|
|
|
|
|
2020-06-06 14:26:55 +02:00
|
|
|
//PRAGMA_OMP_PARALLEL_FOR_IF(outSize > Environment::getInstance().tadThreshold())
|
2019-06-06 14:21:15 +02:00
|
|
|
for (unsigned int i = 0; i < outSize; i++) {
|
|
|
|
outputs[i].first = outputList[i];
|
|
|
|
std::vector<int> outDims(outputs[i].first->rankOf() - 1);
|
|
|
|
|
|
|
|
int r = outputs[i].first->rankOf();
|
|
|
|
|
|
|
|
for (int k = 1; k < r; k++)
|
|
|
|
outDims[k - 1] = k;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
ResultSet listOutForCurrent = outputs[i].first->allTensorsAlongDimension(outDims);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
outputs[i].second = 0;
|
|
|
|
|
2020-06-06 14:26:55 +02:00
|
|
|
//PRAGMA_OMP_PARALLEL_FOR_IF(indices->lengthOf() > Environment::getInstance().elementwiseThreshold())
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong e = 0; e < indices->lengthOf(); ++e)
|
2019-06-06 14:21:15 +02:00
|
|
|
if ((*indices).e<Nd4jLong>(e) == i)
|
2019-12-20 20:35:39 +01:00
|
|
|
listOutForCurrent.at(outputs[i].second++)->assign(listOfTensors.at(e));
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
unsigned int outSize = outputList.size();
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
outputs[i].first = outputList[i];
|
|
|
|
outputs[i].second = 0;
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong e = 0; e < indices->lengthOf(); ++e)
|
2019-11-13 15:15:18 +01:00
|
|
|
if (indices->e<Nd4jLong>(e) == i)
|
|
|
|
outputs[i].first->p(outputs[i].second++, input->e<T>(e));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_tad(func, 0, outSize);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
static int _dynamicStitchFunctor(std::vector<NDArray*> const& inputs, std::vector<NDArray*> const& indices, NDArray* output){
|
|
|
|
|
|
|
|
int numOfData = inputs.size();
|
|
|
|
|
|
|
|
if (output->isVector()) {
|
|
|
|
for (int e = 0; e < numOfData; e++) {
|
|
|
|
auto data = inputs[e];
|
|
|
|
auto index = indices[e];
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong i = 0; i < index->lengthOf(); i++) {
|
2019-06-06 14:21:15 +02:00
|
|
|
Nd4jLong pos = index->e<Nd4jLong>(i);
|
|
|
|
if (pos < 0) {
|
|
|
|
nd4j_printf("dynamic_stitch: Index value should be non-negative. But %i was given", pos);
|
|
|
|
return ND4J_STATUS_VALIDATION;
|
|
|
|
}
|
|
|
|
if (pos >= output->lengthOf()) {
|
|
|
|
nd4j_printf("dynamic_stitch: Index should be less than %i. But %i was given",
|
|
|
|
output->lengthOf(), pos);
|
|
|
|
return ND4J_STATUS_VALIDATION;
|
|
|
|
}
|
|
|
|
output->p<T>(pos, data->e<T>(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::vector<int> restDims(output->rankOf() - 1);
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = restDims.size(); i > 0; i--)
|
2019-06-06 14:21:15 +02:00
|
|
|
restDims[restDims.size() - i] = output->rankOf() - i;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
ResultSet listOfOutTensors = output->allTensorsAlongDimension(restDims);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
for (int e = 0; e < numOfData; e++) {
|
|
|
|
auto data = inputs[e];
|
|
|
|
auto index = indices[e];
|
|
|
|
std::vector<int> sourceDims(data->rankOf() - index->rankOf());
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = sourceDims.size(); i > 0; i--)
|
2019-06-06 14:21:15 +02:00
|
|
|
sourceDims[sourceDims.size() - i] = data->rankOf() - i;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
ResultSet listOfTensors = data->allTensorsAlongDimension(sourceDims) ;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong i = 0; i < index->lengthOf(); i++) {
|
2019-06-06 14:21:15 +02:00
|
|
|
auto pos = index->e<Nd4jLong>(i);
|
|
|
|
if (pos < 0) {
|
|
|
|
nd4j_printf("dynamic_stitch: Index value should be non-negative. But %i was given", pos);
|
|
|
|
return ND4J_STATUS_VALIDATION;
|
|
|
|
}
|
|
|
|
if (pos >= output->lengthOf()) {
|
|
|
|
nd4j_printf("dynamic_stitch: Index should be less than %i. But %i was given",
|
|
|
|
output->lengthOf(), pos);
|
|
|
|
return ND4J_STATUS_VALIDATION;
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
listOfOutTensors.at(pos)->assign(listOfTensors.at(i));
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ND4J_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void _dynamicPartitionFunctorBP(NDArray const* input, NDArray const* indices, std::vector<NDArray*> const& inputGradientList, std::vector<NDArray*>& outputList) {
|
|
|
|
std::vector<std::pair<NDArray *, int>> outputs(inputGradientList.size());
|
|
|
|
|
|
|
|
int sourceDimsLen = input->rankOf() - indices->rankOf();
|
|
|
|
if (sourceDimsLen) { // multidimensional case
|
|
|
|
std::vector<int> sourceDims(sourceDimsLen);
|
|
|
|
|
|
|
|
for (int i = sourceDimsLen; i > 0; i--)
|
|
|
|
sourceDims[sourceDimsLen - i] = input->rankOf() - i;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
ResultSet listOfTensors = outputList[0]->allTensorsAlongDimension(sourceDims);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = 0; i < inputGradientList.size(); i++) {
|
2019-06-06 14:21:15 +02:00
|
|
|
outputs[i].first = inputGradientList[i];
|
|
|
|
if (outputs[i].first->rankOf() < 1) continue; // skip empty gradient outs
|
|
|
|
std::vector<int> outDims(outputs[i].first->rankOf() - 1);
|
|
|
|
|
|
|
|
for (int k = 1; k < outputs[i].first->rankOf(); k++)
|
|
|
|
outDims[k - 1] = k;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
ResultSet listOutForCurrent = outputs[i].first->allTensorsAlongDimension(outDims);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
outputs[i].second = 0;
|
|
|
|
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong e = 0; e < indices->lengthOf(); ++e)
|
2019-06-06 14:21:15 +02:00
|
|
|
if (indices->e<Nd4jLong>(e) == i)
|
2019-12-20 20:35:39 +01:00
|
|
|
listOfTensors.at(e)->assign(listOutForCurrent.at(outputs[i].second++));
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // one-dimensional case
|
|
|
|
auto output = outputList[0];
|
|
|
|
unsigned int gradsSize = inputGradientList.size();
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
outputs[i].first = inputGradientList[i];
|
|
|
|
outputs[i].second = 0;
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong e = 0; e < indices->lengthOf(); ++e)
|
2019-11-13 15:15:18 +01:00
|
|
|
if (indices->e<Nd4jLong>(e) == i)
|
|
|
|
output->p<T>(e, outputs[i].first->e<T>(outputs[i].second++));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_tad(func, 0, gradsSize);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
outputList[1]->assign(indices);
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void dynamicPartitionFunctor(sd::LaunchContext * context, NDArray const* input, NDArray const* indices, std::vector<NDArray*>& outputList) {
|
2019-06-06 14:21:15 +02:00
|
|
|
auto xType = input->dataType();
|
|
|
|
|
|
|
|
BUILD_SINGLE_SELECTOR(xType, _dynamicPartitionFunctor, (input, indices, outputList), LIBND4J_TYPES);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static int _dynamicStitchFunctorBP(std::vector<NDArray*> const& inputs, std::vector<NDArray*> const& indices, NDArray const* gradInput, std::vector<NDArray*>& outputList){
|
|
|
|
throw std::runtime_error("Not umplemented yet");
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
int dynamicStitchFunctor(sd::LaunchContext * context, std::vector<NDArray*> const& inputs, std::vector<NDArray*> const& indices, NDArray* output){
|
2019-06-06 14:21:15 +02:00
|
|
|
auto xType = inputs.at(0)->dataType();
|
|
|
|
|
|
|
|
BUILD_SINGLE_SELECTOR(xType, return _dynamicStitchFunctor, (inputs, indices, output), LIBND4J_TYPES);
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
int dynamicStitchFunctorBP(sd::LaunchContext * context, std::vector<NDArray*> const& inputs, std::vector<NDArray*> const& indices, NDArray const* gradInput, std::vector<NDArray*>& outputList) {
|
2019-06-06 14:21:15 +02:00
|
|
|
auto xType = inputs.at(0)->dataType();
|
|
|
|
|
|
|
|
BUILD_SINGLE_SELECTOR(xType, return _dynamicStitchFunctorBP, (inputs, indices, gradInput, outputList), LIBND4J_TYPES);
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void dynamicPartitionFunctorBP(sd::LaunchContext * context, NDArray const* input, NDArray const* indices, std::vector<NDArray*> const& inputGradientList, std::vector<NDArray*>& outputList) {
|
2019-06-06 14:21:15 +02:00
|
|
|
auto xType = input->dataType();
|
|
|
|
|
|
|
|
BUILD_SINGLE_SELECTOR(xType, _dynamicPartitionFunctorBP, (input, indices, inputGradientList, outputList), LIBND4J_TYPES);
|
|
|
|
}
|
|
|
|
|
|
|
|
BUILD_SINGLE_TEMPLATE(template void _dynamicPartitionFunctorBP, (NDArray const* input, NDArray const* indices, std::vector<NDArray*> const& inputGradientList, std::vector<NDArray*>& outputList);, LIBND4J_TYPES);
|
|
|
|
BUILD_SINGLE_TEMPLATE(template int _dynamicStitchFunctorBP, (std::vector<NDArray*> const& inputs, std::vector<NDArray*> const& indices, NDArray const* gradInput, std::vector<NDArray*>& outputList);, LIBND4J_TYPES);
|
|
|
|
|
|
|
|
BUILD_SINGLE_TEMPLATE(template void _dynamicPartitionFunctor, (NDArray const* input, NDArray const* indices, std::vector<NDArray*>& outputList);, LIBND4J_TYPES);
|
|
|
|
BUILD_SINGLE_TEMPLATE(template int _dynamicStitchFunctor, (std::vector<NDArray*> const& inputs, std::vector<NDArray*> const& indices, NDArray* output);, LIBND4J_TYPES);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|