Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shugeo random uniform int #30

Merged
merged 8 commits into from Nov 6, 2019
1 change: 1 addition & 0 deletions libnd4j/blas/NDArray.hpp
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down
1 change: 1 addition & 0 deletions libnd4j/include/array/ConstantHolder.h
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down
17 changes: 10 additions & 7 deletions libnd4j/include/array/DataTypeUtils.h
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down Expand Up @@ -29,8 +30,8 @@
#include <dll.h>
#include <Environment.h>
#include <ArrayOptions.h>
#include <templatemath.h>
#include <shape.h>
//#include <templatemath.h>
//#include <shape.h>
#include <helpers/logger.h>

namespace nd4j {
Expand Down Expand Up @@ -128,7 +129,9 @@ namespace nd4j {
// if both dtypes are the same - just return it
if (typeX == typeY)
return typeX;

auto nd4j_max = [](nd4j::DataType typeX, nd4j::DataType typeY) {
return typeX > typeY?typeX:typeY;
};
auto rX = isR(typeX);
auto rY = isR(typeY);

Expand All @@ -144,7 +147,7 @@ namespace nd4j {
if (rX && rY) {
// if we allow precision boost, then we pick bigger data type
if (nd4j::Environment::getInstance()->precisionBoostAllowed()) {
return nd4j::math::nd4j_max<nd4j::DataType>(typeX, typeY);
return nd4j_max(typeX, typeY);
} else {
// and we return first operand otherwise
return typeX;
Expand All @@ -155,7 +158,7 @@ namespace nd4j {
// if that's not real type, we apply same rules
if (!rX && !rY) {
if (nd4j::Environment::getInstance()->precisionBoostAllowed()) {
return nd4j::math::nd4j_max<nd4j::DataType>(typeX, typeY);
return nd4j_max(typeX, typeY);
} else {
// and we return first operand otherwise
return typeX;
Expand Down Expand Up @@ -367,8 +370,8 @@ FORCEINLINE std::string DataTypeUtils::asString(DataType dataType) {

template <typename T>
FORCEINLINE bool DataTypeUtils::castShapeInfo(const Nd4jLong *originalShapeInfo, T *newShapeInfo) {

for (int e = 0; e < shape::shapeInfoLength(originalShapeInfo); e++) {
auto shapeInfoLength = *originalShapeInfo * 2 + 4;
for (auto e = 0; e < shapeInfoLength; e++) {
if (originalShapeInfo[e] < static_cast<Nd4jLong>(DataTypeUtils::max<T>())) {
newShapeInfo[e] = static_cast<T>(originalShapeInfo[e]);
} else
Expand Down
24 changes: 20 additions & 4 deletions libnd4j/include/array/impl/ConstantHolder.cpp
@@ -1,10 +1,26 @@
/**
* Copyright (c) 2019 Konduit K.K.
*
* 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 raver on 5/17/2019.
//

#include <array/ConstantHolder.h>
#include <DataTypeUtils.h>

#include <array/ConstantHolder.h>
#include <shape.h>

namespace nd4j {
ConstantHolder::ConstantHolder(const ConstantHolder& other) {
Expand All @@ -24,7 +40,7 @@ namespace nd4j {
bool ConstantHolder::hasBuffer() {
return hasBuffer(DataTypeUtils::fromT<T>());
}
BUILD_SINGLE_TEMPLATE(template ND4J_EXPORT bool ConstantHolder::hasBuffer, (), LIBND4J_TYPES);
BUILD_SINGLE_TEMPLATE(template ND4J_EXPORT bool ConstantHolder::hasBuffer, (void), LIBND4J_TYPES);

void ConstantHolder::addBuffer(ConstantDataBuffer &pointer, nd4j::DataType dataType) {
_buffers[dataType] = pointer;
Expand All @@ -34,7 +50,7 @@ namespace nd4j {
void ConstantHolder::addBuffer(ConstantDataBuffer &pointer) {
addBuffer(pointer, DataTypeUtils::fromT<T>());
}
BUILD_SINGLE_TEMPLATE(template ND4J_EXPORT void ConstantHolder::addBuffer, (ConstantDataBuffer&), LIBND4J_TYPES);
BUILD_SINGLE_TEMPLATE(template ND4J_EXPORT void ConstantHolder::addBuffer, (ConstantDataBuffer& cb), LIBND4J_TYPES);

ConstantDataBuffer* ConstantHolder::getConstantDataBuffer(nd4j::DataType dataType) {
if (!hasBuffer(dataType))
Expand Down
18 changes: 16 additions & 2 deletions libnd4j/include/graph/RandomGenerator.h
Expand Up @@ -194,8 +194,22 @@ namespace nd4j {

template <typename T>
_CUDA_HD FORCEINLINE T RandomGenerator::relativeT(Nd4jLong index, T from, T to) {
auto t = this->relativeT<T>(index);
auto z = from + (t * (to - from));
auto t = this->relativeT<T>(index);
auto z = from + T(t * (to - from));
return z;
}

template <>
_CUDA_HD FORCEINLINE Nd4jLong RandomGenerator::relativeT(Nd4jLong index, Nd4jLong from, Nd4jLong to) {
auto t = this->relativeT<double>(index);
auto z = from + Nd4jLong(t * (to - from));
return z;
}

template <>
_CUDA_HD FORCEINLINE int RandomGenerator::relativeT(Nd4jLong index, int from, int to) {
auto t = this->relativeT<float>(index);
shugeo marked this conversation as resolved.
Show resolved Hide resolved
auto z = from + float(t * (to - from));
return z;
}

Expand Down
2 changes: 2 additions & 0 deletions libnd4j/include/helpers/cuda/ConstantHelper.cu
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -21,6 +22,7 @@
#include <exceptions/cuda_exception.h>
#include <ConstantHelper.h>
#include <DataTypeUtils.h>
#include <shape.h>
#include <execution/LaunchContext.h>
#include <specials.h>
#include <logger.h>
Expand Down
40 changes: 23 additions & 17 deletions libnd4j/include/ops/declarable/generic/random/uniform.cpp
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -23,6 +24,7 @@

#include <ops/declarable/CustomOperations.h>
#include <helpers/RandomLauncher.h>
#include <ops/declarable/helpers/random.h>

namespace nd4j {
namespace ops {
Expand All @@ -35,41 +37,45 @@ namespace nd4j {
* TArgs[0] - min for rng
* TArgs[1] - max for rng
*/
CUSTOM_OP_IMPL(randomuniform, 1, 1, true, 2, 0) {
CUSTOM_OP_IMPL(randomuniform, 1, 1, true, 0, 0) {
// uniform distribution
auto rng = block.randomGenerator();
auto dtype = DataType::FLOAT32;
if (block.getIArguments()->size())
dtype = (DataType)INT_ARG(0);

// FIXME: to be implemented
/*
if (rng == nullptr)
return Status::THROW("RNG is null, aborting...");
auto min = block.width() > 1?INPUT_VARIABLE(1):(NDArray*)nullptr;
auto max = block.width() > 2?INPUT_VARIABLE(2):(NDArray*)nullptr;

auto x = INPUT_VARIABLE(0);
auto z = OUTPUT_VARIABLE(0);
auto output = OUTPUT_VARIABLE(0);
REQUIRE_TRUE(output->dataType() == dtype, 0, "RandomUniform: data type of output should be equals to given.");

functions::random::RandomFunction<T>::template execTransform<randomOps::UniformDistribution<T>>(block.getRNG(), z->getBuffer(), z->getShapeInfo(), block.getTArguments()->data());

STORE_RESULT(*z);
*/
REQUIRE_TRUE(block.numT() > 1, 0, "RandomUniform: to/from must be set");

RandomLauncher::fillUniform(block.launchContext(), rng, OUTPUT_VARIABLE(0), T_ARG(0), T_ARG(1));
helpers::fillRandomUniform(block.launchContext(), rng, min, max, output);
return Status::OK();
}


DECLARE_SHAPE_FN(randomuniform) {
auto in = INPUT_VARIABLE(0);
//auto min = INPUT_VARIABLE(1);
auto shape = in->template asVectorT<Nd4jLong>();
auto dtype = DataType::FLOAT32; //ArrayOptions::dataType(inputShape->at(1)); // output type is by given min

if (block.getIArguments()->size())
dtype = (DataType)INT_ARG(0);
if (block.width() > 1)
REQUIRE_TRUE(dtype == INPUT_VARIABLE(1)->dataType(), 0, "RandomUniform: data type of output and min/max args should be the same");

auto newShape = ConstantShapeHelper::getInstance()->createShapeInfo(block.dataType(), 'c', shape);
auto newShape = ConstantShapeHelper::getInstance()->createShapeInfo(dtype, 'c', shape);
return SHAPELIST(newShape);
}

DECLARE_TYPES(randomuniform) {
getOpDescriptor()
->setAllowedInputTypes(nd4j::DataType::ANY)
->setAllowedOutputTypes({ALL_FLOATS});
->setAllowedInputTypes(0, {ALL_INTS})
->setAllowedInputTypes(1, {ALL_INTS, ALL_FLOATS})
->setAllowedInputTypes(2, {ALL_INTS, ALL_FLOATS})
->setAllowedOutputTypes({ALL_FLOATS, ALL_INTS});
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion libnd4j/include/ops/declarable/headers/random.h
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down Expand Up @@ -33,8 +34,20 @@ namespace nd4j {
DECLARE_CUSTOM_OP(get_seed, -2, 1, false, 0, 0);
#endif

/*
* random_uniform distribution for types int32,int64, float16, float and double
* by default dtype is float32
*
* input:
* 0 - shape of output (1D int tensor)
* 1 - min val (0D of output type) - optional (0 as default)
* 2 - max val (0D of output type) - optional (inf as default)
*
* output:
* 0 - uniformly distributed values of given type (between min and max)
*/
#if NOT_EXCLUDED(OP_randomuniform)
DECLARE_CUSTOM_OP(randomuniform, 1, 1, true, 2, 0);
DECLARE_CUSTOM_OP(randomuniform, 1, 1, false, 0, 0);
#endif

#if NOT_EXCLUDED(OP_random_normal)
Expand Down Expand Up @@ -66,6 +79,7 @@ namespace nd4j {
#if NOT_EXCLUDED(OP_random_poisson)
DECLARE_CUSTOM_OP(random_poisson, 2, 1, false, 0, 0);
#endif

}
}

Expand Down
29 changes: 28 additions & 1 deletion libnd4j/include/ops/declarable/helpers/cpu/random.cpp
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand All @@ -23,6 +23,7 @@
#include <memory>
//#include <graph/Context.h>
#include <ShapeUtils.h>
#include <helpers/RandomLauncher.h>

namespace nd4j {
namespace ops {
Expand Down Expand Up @@ -127,6 +128,32 @@ namespace helpers {
BUILD_SINGLE_TEMPLATE(template void fillRandomPoisson_, (LaunchContext* context,
graph::RandomGenerator& rng, NDArray* lambda, NDArray* output), FLOAT_TYPES);

template <typename T>
void fillRandomUniform_(LaunchContext* context, graph::RandomGenerator& rng, NDArray* min, NDArray* max, NDArray* output) {
T minVal = T(0);
T maxVal = DataTypeUtils::infOrMax<T>();
if (min)
minVal = min->t<T>(0);
if (max)
maxVal = max->t<T>(0);

if (output->isR())
RandomLauncher::fillUniform(context, rng, output, minVal, maxVal);
else {
PRAGMA_OMP_PARALLEL_FOR
for (auto i = 0; i < output->lengthOf(); i++) {
output->t<T>(i) = rng.relativeT<T>(i, minVal, maxVal);
}
}
}

void fillRandomUniform(LaunchContext* context, graph::RandomGenerator& rng, NDArray* min, NDArray* max, NDArray* output) {
BUILD_SINGLE_SELECTOR(output->dataType(), fillRandomUniform_, (context, rng, min, max, output), NUMERIC_TYPES);
}

BUILD_SINGLE_TEMPLATE(template void fillRandomUniform_, (LaunchContext* context,
graph::RandomGenerator& rng, NDArray* min, NDArray* max, NDArray* output), NUMERIC_TYPES);

}
}
}
7 changes: 4 additions & 3 deletions libnd4j/include/ops/declarable/helpers/cpu/segment.cpp
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
* Copyright (c) 2019 Konduit K.K.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
Expand Down Expand Up @@ -91,7 +92,7 @@ namespace helpers {
val = nd4j::math::nd4j_min<T>(val, input->t<T>(e));
}
else {
idx = indices->e<int>(e);
idx = indices->e<Nd4jLong>(e);
val = input->t<T>(e);
}
output->t<T>(idx) = val;
Expand All @@ -111,14 +112,14 @@ namespace helpers {
minT->assign(listOfTensors->at(0));

for (Nd4jLong i = 1; i < indices->lengthOf(); i++) {
if (indices->e<T>(i) == idx) {
if (indices->e<Nd4jLong>(i) == idx) {

for (int e = 0; e < minT->lengthOf(); e++) {
minT->p(e, nd4j::math::nd4j_min(minT->e<T>(e), listOfTensors->at(i)->e<T>(e)));
}
}
else {
idx = indices->e<T>(i);
idx = indices->e<Nd4jLong>(i);
minT = listOfOutTensors->at(idx);
minT->assign(listOfTensors->at(i));
}
Expand Down