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, created on 15.12.17.
|
|
|
|
// @author Yurii Shyrma (iuriish@yahoo.com)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <types/types.h>
|
|
|
|
#include <op_boilerplate.h>
|
|
|
|
#include <loops/random.h>
|
|
|
|
#include <OmpLaunchHelper.h>
|
|
|
|
|
|
|
|
using namespace randomOps;
|
|
|
|
|
|
|
|
namespace functions {
|
|
|
|
namespace random {
|
|
|
|
|
2020-01-31 06:57:31 +01:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
template<typename X>
|
|
|
|
template<typename OpClass>
|
|
|
|
void RandomFunction<X>::execTransform(Nd4jPointer state,
|
|
|
|
void *vx,
|
|
|
|
Nd4jLong *xShapeInfo,
|
|
|
|
void *vy,
|
|
|
|
Nd4jLong *yShapeInfo,
|
|
|
|
void *vz,
|
|
|
|
Nd4jLong *zShapeInfo,
|
|
|
|
void *vextraArguments) {
|
|
|
|
|
|
|
|
auto x = reinterpret_cast<X *>(vx);
|
|
|
|
auto y = reinterpret_cast<X *>(vy);
|
|
|
|
auto z = reinterpret_cast<X *>(vz);
|
|
|
|
auto extraArguments = reinterpret_cast<X *>(vextraArguments);
|
|
|
|
|
|
|
|
if (OpClass::requiresSpecial) {
|
|
|
|
OpClass::specialOp(state, x, xShapeInfo, y, yShapeInfo, z, zShapeInfo, extraArguments);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:12:09 +02:00
|
|
|
auto length = shape::length(zShapeInfo);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
nd4j::graph::RandomGenerator* rng = reinterpret_cast<nd4j::graph::RandomGenerator*>(state);
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
if(shape::haveSameShapeAndStrides(xShapeInfo, yShapeInfo) && shape::haveSameShapeAndStrides(xShapeInfo, zShapeInfo)) {
|
|
|
|
|
|
|
|
|
2020-01-31 06:57:31 +01:00
|
|
|
if(shape::elementWiseStride(zShapeInfo) == 1 && shape::elementWiseStride(xShapeInfo) == 1 && shape::elementWiseStride(yShapeInfo) == 1 &&
|
|
|
|
shape::order(xShapeInfo) == shape::order(zShapeInfo) && shape::order(zShapeInfo) == shape::order(yShapeInfo) ){
|
|
|
|
|
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
z[i] = OpClass::op(x[i], y[i], i, length, rng, extraArguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
const bool canCastX = nd4j::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
|
|
|
|
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2020-01-31 06:57:31 +01:00
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
z[offset] = OpClass::op(x[offset], y[offset], i, length, rng, extraArguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else if (shape::haveSameShapeAndStrides(xShapeInfo, yShapeInfo)) {
|
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
2019-09-11 19:12:09 +02:00
|
|
|
const bool canCastX = nd4j::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
const bool canCastZ = nd4j::DataTypeUtils::castShapeInfo(zShapeInfo, zShapeInfoCast);
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2019-06-06 14:21:15 +02:00
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto zOffset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
2019-06-06 14:21:15 +02:00
|
|
|
z[zOffset] = OpClass::op(x[offset], y[offset], i, length, rng, extraArguments);
|
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else if (shape::haveSameShapeAndStrides(xShapeInfo, zShapeInfo)) {
|
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint yShapeInfoCast[MAX_RANK];
|
2019-09-11 19:12:09 +02:00
|
|
|
const bool canCastX = nd4j::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
const bool canCastY = nd4j::DataTypeUtils::castShapeInfo(yShapeInfo, yShapeInfoCast);
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2019-06-06 14:21:15 +02:00
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto yOffset = shape::indexOffset(i, yShapeInfo, yShapeInfoCast, canCastY);
|
2019-06-06 14:21:15 +02:00
|
|
|
z[offset] = OpClass::op(x[offset], y[yOffset], i, length, rng, extraArguments);
|
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else if (shape::haveSameShapeAndStrides(yShapeInfo, zShapeInfo)) {
|
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint yShapeInfoCast[MAX_RANK];
|
2019-09-11 19:12:09 +02:00
|
|
|
const bool canCastX = nd4j::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
const bool canCastY = nd4j::DataTypeUtils::castShapeInfo(yShapeInfo, yShapeInfoCast);
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2019-06-06 14:21:15 +02:00
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto xOffset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto offset = shape::indexOffset(i, yShapeInfo, yShapeInfoCast, canCastY);
|
2019-06-06 14:21:15 +02:00
|
|
|
z[offset] = OpClass::op(x[xOffset], y[offset], i, length, rng, extraArguments);
|
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint yShapeInfoCast[MAX_RANK];
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
2019-09-11 19:12:09 +02:00
|
|
|
const bool canCastX = nd4j::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
const bool canCastY = nd4j::DataTypeUtils::castShapeInfo(yShapeInfo, yShapeInfoCast);
|
|
|
|
const bool canCastZ = nd4j::DataTypeUtils::castShapeInfo(zShapeInfo, zShapeInfoCast);
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2019-06-06 14:21:15 +02:00
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto xOffset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto yOffset = shape::indexOffset(i, yShapeInfo, yShapeInfoCast, canCastY);
|
|
|
|
auto zOffset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
2019-06-06 14:21:15 +02:00
|
|
|
z[zOffset] = OpClass::op(x[xOffset], y[yOffset], i, length, rng, extraArguments);
|
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
template<typename OpClass>
|
|
|
|
void RandomFunction<X>::execTransform(Nd4jPointer state,
|
|
|
|
void *vx,
|
|
|
|
Nd4jLong *xShapeInfo,
|
|
|
|
void *vz,
|
|
|
|
Nd4jLong *zShapeInfo,
|
|
|
|
void *vextraArguments) {
|
|
|
|
auto x = reinterpret_cast<X *>(vx);
|
|
|
|
auto z = reinterpret_cast<X *>(vz);
|
|
|
|
auto extraArguments = reinterpret_cast<X *>(vextraArguments);
|
|
|
|
|
|
|
|
auto length = shape::length(zShapeInfo);
|
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
const bool canCastX = nd4j::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
|
|
|
|
|
|
|
nd4j::graph::RandomGenerator* rng = reinterpret_cast<nd4j::graph::RandomGenerator*>(state);
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
if(shape::haveSameShapeAndStrides(xShapeInfo, zShapeInfo)) {
|
|
|
|
|
2020-01-31 06:57:31 +01:00
|
|
|
if(shape::elementWiseStride(zShapeInfo) == 1 && shape::elementWiseStride(xShapeInfo) == 1 && shape::order(xShapeInfo) == shape::order(zShapeInfo)){
|
|
|
|
|
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
z[i] = OpClass::op(x[i], i, length, rng, extraArguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2020-01-31 06:57:31 +01:00
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
z[offset] = OpClass::op(x[offset], i, length, rng, extraArguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
|
|
|
const bool canCastZ = nd4j::DataTypeUtils::castShapeInfo(zShapeInfo, zShapeInfoCast);
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2019-06-06 14:21:15 +02:00
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto xOffset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto zOffset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
2019-06-06 14:21:15 +02:00
|
|
|
z[zOffset] = OpClass::op(x[xOffset], i, length, rng, extraArguments);
|
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
template<typename OpClass>
|
|
|
|
void RandomFunction<X>::execTransform(Nd4jPointer state, void *vz, Nd4jLong *zShapeInfo, void *vextraArguments) {
|
|
|
|
|
|
|
|
auto z = reinterpret_cast<X *>(vz);
|
|
|
|
auto extraArguments = reinterpret_cast<X *>(vextraArguments);
|
|
|
|
|
|
|
|
auto length = shape::length(zShapeInfo);
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
nd4j::graph::RandomGenerator* rng = reinterpret_cast<nd4j::graph::RandomGenerator*>(state);
|
|
|
|
|
2020-01-31 06:57:31 +01:00
|
|
|
if(shape::elementWiseStride(zShapeInfo) == 1){
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-01-31 06:57:31 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
z[i] = OpClass::op( i, length, rng, extraArguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
nd4j::OmpLaunchHelper info(length);
|
|
|
|
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
|
|
|
const bool canCastZ = nd4j::DataTypeUtils::castShapeInfo(zShapeInfo, zShapeInfoCast);
|
2019-11-13 15:15:18 +01:00
|
|
|
|
2020-01-31 06:57:31 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2020-01-31 06:57:31 +01:00
|
|
|
auto offset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
|
|
|
z[offset] = OpClass::op(i, length, rng, extraArguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
samediff::Threads::parallel_for(func, 0, length, 1);
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
void RandomFunction<X>::execTransform(int opNum, Nd4jPointer state, void *x, Nd4jLong *xShapeInfo, void *z, Nd4jLong *zShapeInfo, void *extraArguments) {
|
|
|
|
DISPATCH_BY_OPNUM_T(execTransform, PARAMS(state, x, xShapeInfo, z, zShapeInfo, extraArguments), RANDOM_OPS)
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
void RandomFunction<X>::execTransform(int opNum, Nd4jPointer state, void *x, Nd4jLong *xShapeInfo, void *y, Nd4jLong *yShapeInfo, void *z, Nd4jLong *zShapeInfo, void *extraArguments) {
|
|
|
|
DISPATCH_BY_OPNUM_T(execTransform, PARAMS(state, x, xShapeInfo, y, yShapeInfo, z, zShapeInfo, extraArguments), RANDOM_OPS)
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
void RandomFunction<X>::execTransform(int opNum, Nd4jPointer state, void *z, Nd4jLong *zShapeInfo, void *extraArguments) {
|
|
|
|
DISPATCH_BY_OPNUM_T(execTransform, PARAMS(state, z, zShapeInfo, extraArguments), RANDOM_OPS)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-10 08:57:18 +01:00
|
|
|
//BUILD_SINGLE_TEMPLATE(template class ND4J_EXPORT RandomFunction, , FLOAT_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|