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
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Created by raver119 on 08.10.2017.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "../scalar.h"
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/op_boilerplate.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <types/types.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <helpers/LoopKind.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include "../legacy_ops.h"
|
|
|
|
|
|
|
|
using namespace simdOps;
|
|
|
|
|
|
|
|
namespace functions {
|
|
|
|
namespace scalar {
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
template<typename OpType>
|
2020-05-09 07:06:14 +02:00
|
|
|
void ScalarTransform<X, Y, Z>::transform(const void *vx, const Nd4jLong *xShapeInfo,
|
|
|
|
void *vextraParams,
|
|
|
|
void *vz, const Nd4jLong *zShapeInfo,
|
|
|
|
const void *vscalars,
|
|
|
|
int *dimension, int dimensionLength,
|
|
|
|
const Nd4jLong *xTadShapeInfo, const Nd4jLong *xTadOffsets,
|
|
|
|
const Nd4jLong *zTadShapeInfo, const Nd4jLong *zTadOffsets,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
|
|
|
|
|
|
|
auto x = reinterpret_cast<const X *>(vx);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto z = reinterpret_cast<Z *>(vz);
|
2020-05-09 07:06:14 +02:00
|
|
|
auto scalars = reinterpret_cast<const Y *>(vscalars);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto extraParams = reinterpret_cast<Z *>(vextraParams);
|
|
|
|
|
|
|
|
if (zTadShapeInfo == nullptr) {
|
|
|
|
zTadShapeInfo = xTadShapeInfo;
|
|
|
|
zTadOffsets = xTadOffsets;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int xTadEws = shape::elementWiseStride(xTadShapeInfo);
|
|
|
|
const int zTadEws = shape::elementWiseStride(zTadShapeInfo);
|
|
|
|
const int tadLength = shape::tadLength(xShapeInfo, dimension, dimensionLength);
|
|
|
|
const int numTads = shape::length(xShapeInfo) / tadLength;
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::LoopKind::Kind kindOfLoop = sd::LoopKind::deduceKindOfLoopXZ(xTadShapeInfo, zTadShapeInfo);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
if (kindOfLoop != sd::LoopKind::EWS1 && kindOfLoop != sd::LoopKind::EWSNONZERO) {
|
2019-06-06 14:21:15 +02:00
|
|
|
printf("ScalarTransform<X, Z>::transform: super-bad loop visited. Shouldn't ever happen\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:26:55 +02:00
|
|
|
int num_threads = sd::math::nd4j_min<int>(numTads, sd::Environment::getInstance().maxThreads());
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
if (kindOfLoop == sd::LoopKind::EWS1) {
|
2019-11-13 15:15:18 +01:00
|
|
|
for (auto r = start; r < stop; r++) {
|
2019-06-06 14:21:15 +02:00
|
|
|
auto oZ = z + zTadOffsets[r];
|
|
|
|
auto oX = x + xTadOffsets[r];
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (int f = 0; f < tadLength; f++)
|
2019-06-06 14:21:15 +02:00
|
|
|
oZ[f] = OpType::op(oX[f], scalars[r], extraParams);
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-11-13 15:15:18 +01:00
|
|
|
for (auto r = start; r < stop; r++) {
|
2019-06-06 14:21:15 +02:00
|
|
|
auto oZ = z + zTadOffsets[r];
|
|
|
|
auto oX = x + xTadOffsets[r];
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (int f = 0; f < tadLength; f++)
|
2019-06-06 14:21:15 +02:00
|
|
|
oZ[f * zTadEws] = OpType::op(oX[f * xTadEws], scalars[r], extraParams);
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
void ScalarTransform<X,Y,Z>::transform(int opNum,
|
2020-05-09 07:06:14 +02:00
|
|
|
const void *x, const Nd4jLong *xShapeInfo,
|
|
|
|
void *extraParams,
|
|
|
|
void *z, const Nd4jLong *zShapeInfo,
|
|
|
|
const void *scalars,
|
|
|
|
int *dimension, int dimensionLength,
|
|
|
|
const Nd4jLong *xTadShapeInfo, const Nd4jLong *xTadOffsets,
|
|
|
|
const Nd4jLong *zTadShapeInfo, const Nd4jLong *zTadOffsets,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
DISPATCH_BY_OPNUM_TTT(transform, PARAMS(x, xShapeInfo, extraParams, z, zShapeInfo, scalars, dimension, dimensionLength, xTadShapeInfo, xTadOffsets, zTadShapeInfo, zTadOffsets, start, stop), SCALAR_OPS);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
void ScalarTransform<X, Y, Z>::transform(const int opNum,
|
2020-05-09 07:06:14 +02:00
|
|
|
const void *x, Nd4jLong xStride,
|
|
|
|
void *z, Nd4jLong zStride,
|
|
|
|
const void *scalar,
|
|
|
|
void *extraParams,
|
|
|
|
const uint64_t n,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
DISPATCH_BY_OPNUM_TTT(transform, PARAMS(x, xStride, z, zStride, scalar, extraParams, n, start, stop), SCALAR_OPS);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
void ScalarTransform<X, Y, Z>::transform(const int opNum,
|
2020-05-09 07:06:14 +02:00
|
|
|
const void *x, const Nd4jLong *xShapeInfo,
|
|
|
|
void *z, const Nd4jLong *zShapeInfo,
|
|
|
|
const void *scalar,
|
|
|
|
void *extraParams,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
DISPATCH_BY_OPNUM_TTT(transform, PARAMS(x, xShapeInfo, z, zShapeInfo, scalar, extraParams, start, stop), SCALAR_OPS);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
template<typename OpType>
|
2020-05-09 07:06:14 +02:00
|
|
|
void ScalarTransform<X, Y, Z>::transform(const void *vx, const Nd4jLong *xShapeInfo,
|
|
|
|
void *vz, const Nd4jLong *zShapeInfo,
|
|
|
|
const void *vscalar,
|
|
|
|
void *vextraParams,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-05-09 07:06:14 +02:00
|
|
|
auto x = reinterpret_cast<const X *>(vx);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto z = reinterpret_cast<Z *>(vz);
|
2020-05-09 07:06:14 +02:00
|
|
|
auto scalar = reinterpret_cast<const Y *>(vscalar)[0];
|
2019-06-06 14:21:15 +02:00
|
|
|
auto extraParams = reinterpret_cast<Z *>(vextraParams);
|
|
|
|
|
|
|
|
const auto len = shape::length(xShapeInfo);
|
|
|
|
const auto xEws = shape::elementWiseStride(xShapeInfo);
|
|
|
|
const auto zEws = shape::elementWiseStride(zShapeInfo);
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::LoopKind::Kind kindOfLoop = sd::LoopKind::deduceKindOfLoopXZ(xShapeInfo, zShapeInfo);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
if (kindOfLoop == sd::LoopKind::EWS1 || kindOfLoop == sd::LoopKind::EWSNONZERO) {
|
2019-11-13 15:15:18 +01:00
|
|
|
transform<OpType>(x, xEws, z, zEws, vscalar, extraParams, len, start, stop);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
const bool canCastX = sd::DataTypeUtils::castShapeInfo<uint>(xShapeInfo, xShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if(shape::haveSameShapeAndStrides(xShapeInfo, zShapeInfo)) {
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
z[offset] = OpType::op(x[offset], scalar, extraParams);
|
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
const bool canCastZ = sd::DataTypeUtils::castShapeInfo<uint>(zShapeInfo, zShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
auto xOffset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto zOffset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
|
|
|
z[zOffset] = OpType::op(x[xOffset], scalar, extraParams);
|
|
|
|
};
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
template<typename OpType>
|
2020-05-09 07:06:14 +02:00
|
|
|
void ScalarTransform<X, Y, Z>::transform(const void *vx, Nd4jLong xEws,
|
|
|
|
void *vz, Nd4jLong zEws,
|
|
|
|
const void *vscalar,
|
|
|
|
void *vextraParams,
|
|
|
|
const uint64_t len, const uint64_t start, const uint64_t stop) {
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2020-05-09 07:06:14 +02:00
|
|
|
auto x = reinterpret_cast<const X *>(vx);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto z = reinterpret_cast<Z *>(vz);
|
2020-05-09 07:06:14 +02:00
|
|
|
auto scalar = reinterpret_cast<const Y *>(vscalar)[0];
|
2019-06-06 14:21:15 +02:00
|
|
|
auto extraParams = reinterpret_cast<Z *>(vextraParams);
|
|
|
|
|
|
|
|
if (xEws == 1 && zEws == 1) {
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++)
|
|
|
|
z[i] = OpType::op(x[i], scalar, extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++)
|
|
|
|
z[i * zEws] = OpType::op(x[i * xEws], scalar, extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|