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 Yurii Shyrma, created on 21.09.2018
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include <helpers/TAD.h>
|
|
|
|
#include<ops/declarable/helpers/ismax.h>
|
|
|
|
#include <helpers/ConstantTadHelper.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 {
|
|
|
|
|
2019-08-21 09:12:11 +02:00
|
|
|
template <typename X, typename Z>
|
2019-06-06 14:21:15 +02:00
|
|
|
static void ismax_(const NDArray* input, NDArray* output, const std::vector<int>& dimensions) {
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
if (input->isVector()) {
|
|
|
|
int dimensionsLength = dimensions.size();
|
|
|
|
int length = input->lengthOf();
|
2019-08-21 09:12:11 +02:00
|
|
|
if (!dimensions.empty() && (input->shapeOf())[dimensions[0]] == 1) {
|
2019-06-06 14:21:15 +02:00
|
|
|
for (int i = 0; i < length; i++)
|
2019-08-21 09:12:11 +02:00
|
|
|
output->p<Z>(i, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int eleStride = shape::elementWiseStride(input->getShapeInfo());
|
|
|
|
if (eleStride == 1) {
|
|
|
|
int maxIdx = 0;
|
2019-08-21 09:12:11 +02:00
|
|
|
auto currMax = input->e<X>(0);
|
2019-06-06 14:21:15 +02:00
|
|
|
if (length < ELEMENT_THRESHOLD) {
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
2019-08-21 09:12:11 +02:00
|
|
|
if (currMax < input->e<X>(i)) {
|
|
|
|
currMax = input->e<X>(i);
|
2019-06-06 14:21:15 +02:00
|
|
|
maxIdx = i;
|
|
|
|
}
|
2019-08-21 09:12:11 +02:00
|
|
|
output->p<Z>(i, 0);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
{
|
|
|
|
int maxIdxLocal = maxIdx;
|
2019-08-21 09:12:11 +02:00
|
|
|
auto currMaxLocal = currMax;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
2019-08-21 09:12:11 +02:00
|
|
|
if (currMaxLocal < input->e<X>(i)) {
|
|
|
|
currMaxLocal = input->e<X>(i);
|
2019-06-06 14:21:15 +02:00
|
|
|
maxIdxLocal = i;
|
|
|
|
}
|
2019-08-21 09:12:11 +02:00
|
|
|
output->p<Z>(i, 0);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PRAGMA_OMP_CRITICAL
|
|
|
|
{
|
|
|
|
if (currMax < currMaxLocal) {
|
|
|
|
currMax = currMaxLocal;
|
|
|
|
maxIdx = maxIdxLocal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 09:12:11 +02:00
|
|
|
output->p<Z>(maxIdx, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int maxIdx = 0;
|
2019-08-21 09:12:11 +02:00
|
|
|
auto currMax = input->e<X>(0);
|
2019-06-06 14:21:15 +02:00
|
|
|
if (length < ELEMENT_THRESHOLD) {
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
2019-08-23 08:00:10 +02:00
|
|
|
if (currMax < input->e<X>(i)) {
|
|
|
|
currMax = input->e<X>(i);
|
2019-06-06 14:21:15 +02:00
|
|
|
maxIdx = i;
|
|
|
|
}
|
2019-08-21 09:12:11 +02:00
|
|
|
output->p<Z>(i, 0.f);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
{
|
|
|
|
int maxIdxLocal = maxIdx;
|
2019-08-21 09:12:11 +02:00
|
|
|
auto currMaxLocal = currMax;
|
2019-06-06 14:21:15 +02:00
|
|
|
for (int i = 0; i < length; i++) {
|
2019-08-23 08:00:10 +02:00
|
|
|
if (currMaxLocal < input->e<X>(i)) {
|
|
|
|
currMaxLocal = input->e<X>(i);
|
2019-06-06 14:21:15 +02:00
|
|
|
maxIdxLocal = i;
|
|
|
|
}
|
2019-08-21 09:12:11 +02:00
|
|
|
output->p<Z>(i, 0.f);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PRAGMA_OMP_CRITICAL
|
|
|
|
{
|
|
|
|
if (currMax < currMaxLocal) {
|
|
|
|
currMax = currMaxLocal;
|
|
|
|
maxIdx = maxIdxLocal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 09:12:11 +02:00
|
|
|
output->p<Z>(maxIdx, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int dimensionsLength = dimensions.size();
|
|
|
|
//int tads = tad.numTads;
|
|
|
|
//decompose in to several sub tads after
|
|
|
|
//moving all dimensions (in sorted order)
|
|
|
|
//to the back.
|
|
|
|
//permuted version of the input shape info for setting up the tad problem
|
2020-03-02 10:49:41 +01:00
|
|
|
auto tadPack = sd::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), const_cast<int*>(dimensions.data()), dimensionsLength);
|
|
|
|
auto tadPackZ = sd::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(), const_cast<int*>(dimensions.data()), dimensionsLength);
|
2019-08-21 14:05:47 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
auto tadShapeShapeInfo = tadPack.primaryShapeInfo();
|
|
|
|
auto tadOffsets = tadPack.primaryOffsets();
|
2019-08-21 14:05:47 +02:00
|
|
|
auto zOfsets = tadPackZ.platformOffsets();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
int tadLength = shape::length(tadShapeShapeInfo);
|
|
|
|
int tads = tadPack.numberOfTads();
|
|
|
|
|
|
|
|
int tadsPerThread = tads / TAD_THRESHOLD;
|
2020-03-02 10:49:41 +01:00
|
|
|
int num_threads = sd::math::nd4j_max<int>(1, tadsPerThread);
|
|
|
|
num_threads = sd::math::nd4j_min<int>(num_threads, omp_get_max_threads());
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
auto tadEWS = shape::elementWiseStride(tadShapeShapeInfo);
|
2019-08-21 14:05:47 +02:00
|
|
|
auto zEWS = shape::elementWiseStride(tadPackZ.primaryShapeInfo());
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
int span = (tads / num_threads) + 8;
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto r = start; r < stop; r++) {
|
2019-08-21 09:12:11 +02:00
|
|
|
auto rX = const_cast<NDArray*>(input)->bufferAsT<X>() + tadOffsets[r];
|
2019-08-21 14:05:47 +02:00
|
|
|
auto rZ = output->bufferAsT<Z>() + zOfsets[r];
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-21 09:12:11 +02:00
|
|
|
auto maxValue = rX[0];
|
2019-06-06 14:21:15 +02:00
|
|
|
int maxIdx = 0;
|
|
|
|
if (tadEWS == 1 && zEWS == 1) {
|
|
|
|
for (int i = 0; i < tadLength; i++) {
|
|
|
|
if (rX[i] > maxValue) {
|
|
|
|
maxIdx = i;
|
|
|
|
maxValue = rX[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (int i = 0; i < tadLength; i++) {
|
2019-08-21 09:12:11 +02:00
|
|
|
rZ[i] = maxIdx == i ? (Z) 1 : (Z) 0;
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-08-21 14:05:47 +02:00
|
|
|
else if (tadEWS > 1 && zEWS > 1) {
|
2019-06-06 14:21:15 +02:00
|
|
|
for (int i = 0; i < tadLength; i++) {
|
|
|
|
if (rX[i * tadEWS] > maxValue) {
|
|
|
|
maxIdx = i;
|
|
|
|
maxValue = rX[i * tadEWS];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (int i = 0; i < tadLength; i++) {
|
2019-08-21 09:12:11 +02:00
|
|
|
rZ[i * zEWS] = maxIdx == i ? (Z) 1 : (Z) 0;
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2019-08-21 14:05:47 +02:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < tadLength; i++) {
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xOffset = shape::getIndexOffset(i, tadShapeShapeInfo);
|
2019-08-21 14:05:47 +02:00
|
|
|
if (rX[xOffset] > maxValue) {
|
|
|
|
maxIdx = i;
|
|
|
|
maxValue = rX[xOffset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (int i = 0; i < tadLength; i++) {
|
2019-09-11 19:12:09 +02:00
|
|
|
auto zOffset = shape::getIndexOffset(i, tadPackZ.primaryShapeInfo());
|
2019-08-21 14:05:47 +02:00
|
|
|
rZ[zOffset] = maxIdx == i ? (Z) 1 : (Z) 0;
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
2020-03-09 06:21:44 +01:00
|
|
|
sd::Threads::parallel_tad(func, 0, tads);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void ismax(sd::LaunchContext * context, const NDArray *input, NDArray *output, const std::vector<int>& dimensions) {
|
2019-08-21 09:12:11 +02:00
|
|
|
BUILD_DOUBLE_SELECTOR(input->dataType(), output->dataType(), ismax_, (input, output, dimensions), LIBND4J_TYPES, LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|