2021-02-01 13:31:45 +01:00
|
|
|
/* ******************************************************************************
|
|
|
|
*
|
2019-06-06 14:21:15 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2021-02-01 13:31:45 +01:00
|
|
|
* See the NOTICE file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2019-06-06 14:21:15 +02:00
|
|
|
* 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, created on 16.04.2018
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <ops/declarable/helpers/reverse.h>
|
|
|
|
#include <helpers/ShapeUtils.h>
|
|
|
|
#include <array/ResultSet.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>
|
|
|
|
inline void swap(T* arr, Nd4jLong from, Nd4jLong to) {
|
|
|
|
T tmp = arr[from];
|
|
|
|
arr[from] = arr[to];
|
|
|
|
arr[to] = tmp;
|
|
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// this legacy op is written by raver119@gmail.com
|
|
|
|
|
|
|
|
template<typename T>
|
2020-05-09 07:06:14 +02:00
|
|
|
static void reverseArray(sd::LaunchContext * context, void const* vinArr, Nd4jLong const*inShapeBuffer, void *voutArr, Nd4jLong const*outShapeBuffer, int numOfElemsToReverse = 0) {
|
|
|
|
auto inArr = reinterpret_cast<T const*>(vinArr);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto outArr = reinterpret_cast<T *>(voutArr);
|
|
|
|
|
|
|
|
Nd4jLong inLength = shape::length(inShapeBuffer);
|
|
|
|
Nd4jLong outLength = shape::length(outShapeBuffer);
|
|
|
|
if(numOfElemsToReverse == 0)
|
|
|
|
numOfElemsToReverse = inLength;
|
|
|
|
int inEWS = shape::elementWiseStride(inShapeBuffer);
|
|
|
|
char inOrder = shape::order(inShapeBuffer);
|
|
|
|
auto sLength = numOfElemsToReverse - 1;
|
|
|
|
|
|
|
|
// two step phase here
|
|
|
|
if (inArr == outArr) {
|
|
|
|
if (inEWS == 1) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto idx = sLength - e;
|
2020-05-09 07:06:14 +02:00
|
|
|
swap(const_cast<T*>(inArr), e, idx);
|
2019-11-13 15:15:18 +01:00
|
|
|
}
|
|
|
|
};
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, numOfElemsToReverse / 2);
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
else if (inEWS > 1) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto idx1 = (sLength - e) * inEWS;
|
|
|
|
Nd4jLong idx2 = e * inEWS;
|
2020-05-09 07:06:14 +02:00
|
|
|
swap(const_cast<T*>(inArr), idx1, idx2);
|
2019-11-13 15:15:18 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, numOfElemsToReverse / 2);
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
else {
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto inOffset = shape::getIndexOffset(e, inShapeBuffer);
|
|
|
|
auto outOffset = shape::getIndexOffset(sLength - e, inShapeBuffer);
|
|
|
|
swap(outArr, inOffset, outOffset);
|
|
|
|
}
|
|
|
|
};
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, numOfElemsToReverse / 2);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
else {
|
|
|
|
// single step phase here
|
|
|
|
auto outEWS = shape::elementWiseStride(outShapeBuffer);
|
|
|
|
char outOrder = shape::order(outShapeBuffer);
|
|
|
|
|
|
|
|
if (inEWS == 1 && outEWS == 1 && inOrder == outOrder) {
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (Nd4jLong e = start; e < stop; e++)
|
2019-11-13 15:15:18 +01:00
|
|
|
outArr[sLength - e] = inArr[e];
|
|
|
|
};
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, numOfElemsToReverse);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if(inLength != numOfElemsToReverse) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto f2 = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++)
|
2019-11-13 15:15:18 +01:00
|
|
|
outArr[e] = inArr[e];
|
|
|
|
};
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(f2, numOfElemsToReverse, inLength);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
else if (inEWS >= 1 && outEWS >= 1 && inOrder == outOrder) {
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++)
|
2019-11-13 15:15:18 +01:00
|
|
|
outArr[(sLength - e) * outEWS] = inArr[e * inEWS];
|
|
|
|
};
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, numOfElemsToReverse);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if(inLength != numOfElemsToReverse) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto f2 = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++)
|
2019-11-13 15:15:18 +01:00
|
|
|
outArr[e * outEWS] = inArr[e * inEWS];
|
|
|
|
};
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(f2, numOfElemsToReverse, inLength);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
else {
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto inOffset = shape::getIndexOffset(e, inShapeBuffer);
|
|
|
|
auto outOffset = shape::getIndexOffset(sLength - e, outShapeBuffer);
|
|
|
|
outArr[outOffset] = inArr[inOffset];
|
|
|
|
}
|
|
|
|
};
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, numOfElemsToReverse);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if(inLength != numOfElemsToReverse) {
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto f2 = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto inOffset = shape::getIndexOffset(e, inShapeBuffer);
|
|
|
|
auto outOffset = shape::getIndexOffset(e, outShapeBuffer);
|
|
|
|
outArr[outOffset] = inArr[inOffset];
|
|
|
|
}
|
|
|
|
};
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(f2, numOfElemsToReverse, inLength);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2020-03-02 10:49:41 +01:00
|
|
|
static void reverseSequence_(sd::LaunchContext * context, const NDArray* input, const NDArray* seqLengths, NDArray* output, int seqDim, const int batchDim){
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
int posOfNonUnityDim = -1;
|
2020-05-09 07:06:14 +02:00
|
|
|
if(input->isVector() || shape::isLikeVector(input->shapeInfo(), posOfNonUnityDim)) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if((seqDim == 0 && input->sizeAt(0) == 1) || (batchDim == posOfNonUnityDim))
|
|
|
|
output->assign(input);
|
|
|
|
else
|
2020-05-09 07:06:14 +02:00
|
|
|
helpers::reverseArray<T>(context, const_cast<NDArray*>(input)->buffer(), const_cast<NDArray*>(input)->shapeInfo(), output->buffer(), output->shapeInfo(), seqLengths->e<int>(0));
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
if(seqDim > batchDim)
|
|
|
|
--seqDim;
|
|
|
|
|
|
|
|
std::vector<int> dimensions = ShapeUtils::evalDimsToExclude(input->rankOf(), {batchDim});
|
|
|
|
|
|
|
|
auto inSubArrsSet = input->allTensorsAlongDimension(dimensions);
|
|
|
|
auto outSubArrsSet = output->allTensorsAlongDimension(dimensions);
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
for(int i = 0; i < inSubArrsSet.size(); ++i) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
Nd4jLong numOfElemsToReverse = seqLengths->e<Nd4jLong>(i);
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
if(numOfElemsToReverse == 0 || numOfElemsToReverse == 1) {
|
2019-12-20 20:35:39 +01:00
|
|
|
outSubArrsSet.at(i)->assign(inSubArrsSet.at(i));
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-12-20 20:35:39 +01:00
|
|
|
auto inInnerSet = inSubArrsSet.at(i)->allTensorsAlongDimension({seqDim});
|
|
|
|
auto outInnerSet = outSubArrsSet.at(i)->allTensorsAlongDimension({seqDim});
|
|
|
|
for(int j = 0; j < inInnerSet.size(); ++j)
|
2020-05-09 07:06:14 +02:00
|
|
|
helpers::reverseArray<T>(context, inInnerSet.at(j)->buffer(), inInnerSet.at(j)->shapeInfo(), outInnerSet.at(j)->buffer(), outInnerSet.at(j)->shapeInfo(), numOfElemsToReverse);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void reverseSequence(sd::LaunchContext * context, const NDArray* input, const NDArray* seqLengths, NDArray* output, int seqDim, const int batchDim) {
|
2019-11-13 15:15:18 +01:00
|
|
|
BUILD_SINGLE_SELECTOR(input->dataType(), reverseSequence_, (context, input, seqLengths, output, seqDim, batchDim), LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2020-05-12 06:47:09 +02:00
|
|
|
void reverse(sd::LaunchContext * context, const NDArray* input, NDArray* output, const std::vector<int>* intArgs) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-05-12 06:47:09 +02:00
|
|
|
auto listOut = output->allTensorsAlongDimension(*intArgs);
|
|
|
|
auto listIn = input->allTensorsAlongDimension(*intArgs);
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
NDArray *subArrIn, *subArrOut;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
for(int i = 0; i < listIn.size(); ++i) { // listIn.size() = listOut.size()
|
|
|
|
subArrIn = listIn.at(i);
|
|
|
|
subArrOut = listOut.at(i);
|
2020-05-09 07:06:14 +02:00
|
|
|
BUILD_SINGLE_SELECTOR(input->dataType(), helpers::reverseArray, (context, subArrIn->buffer(), subArrIn->shapeInfo(), subArrOut->buffer(), subArrOut->shapeInfo()), LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
BUILD_SINGLE_TEMPLATE(template void reverseSequence_, (sd::LaunchContext * context, const NDArray* input, const NDArray* seqLengths, NDArray* output, int seqDim, const int batchDim), LIBND4J_TYPES);
|
2020-05-09 07:06:14 +02:00
|
|
|
BUILD_SINGLE_TEMPLATE(template void reverseArray, (sd::LaunchContext * context, void const*inArr, Nd4jLong const*inShapeBuffer, void* outArr, Nd4jLong const* outShapeBuffer, int numOfElemsToReverse), LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|