From 08853c782985ffe56764bc7285965421bbeb7c4d Mon Sep 17 00:00:00 2001 From: shugeo Date: Wed, 6 Nov 2019 12:49:27 +0200 Subject: [PATCH] Shugeo random uniform int (#30) * Corrected randomuniform declaration. * Refactored uniform distribution for both cuda and cpu platforms. * Refactored uniform distribution and tests. * Fixed type usage with indices. * Refactored uniform distribution implementation and tests to full conform with TF implementation. * Refactored gamma function to use type util method. * Copyright changes and fixes with ConstantHelper. * Added error checking on allocate cuda device memory and operations. --- libnd4j/blas/NDArray.hpp | 1 + libnd4j/include/array/ConstantHolder.h | 1 + libnd4j/include/array/DataTypeUtils.h | 17 +++-- libnd4j/include/array/impl/ConstantHolder.cpp | 24 +++++-- libnd4j/include/graph/RandomGenerator.h | 18 ++++- .../include/helpers/cuda/ConstantHelper.cu | 2 + .../ops/declarable/generic/random/uniform.cpp | 40 ++++++----- .../include/ops/declarable/headers/random.h | 16 ++++- .../ops/declarable/helpers/cpu/random.cpp | 29 +++++++- .../ops/declarable/helpers/cpu/segment.cpp | 7 +- .../ops/declarable/helpers/cuda/random.cu | 69 ++++++++++++++++++- .../include/ops/declarable/helpers/random.h | 4 +- libnd4j/include/pairwise_util.h | 5 +- libnd4j/include/templatemath.h | 5 +- .../layers_tests/DeclarableOpsTests10.cpp | 1 + libnd4j/tests_cpu/layers_tests/RNGTests.cpp | 30 ++++++-- 16 files changed, 223 insertions(+), 46 deletions(-) diff --git a/libnd4j/blas/NDArray.hpp b/libnd4j/blas/NDArray.hpp index 9c1b44818..2a601033a 100644 --- a/libnd4j/blas/NDArray.hpp +++ b/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 diff --git a/libnd4j/include/array/ConstantHolder.h b/libnd4j/include/array/ConstantHolder.h index f6352fd2c..d0824483e 100644 --- a/libnd4j/include/array/ConstantHolder.h +++ b/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 diff --git a/libnd4j/include/array/DataTypeUtils.h b/libnd4j/include/array/DataTypeUtils.h index 8b3176c2b..4e879d247 100644 --- a/libnd4j/include/array/DataTypeUtils.h +++ b/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 @@ -29,8 +30,8 @@ #include #include #include -#include -#include +//#include +//#include #include namespace nd4j { @@ -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); @@ -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(typeX, typeY); + return nd4j_max(typeX, typeY); } else { // and we return first operand otherwise return typeX; @@ -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(typeX, typeY); + return nd4j_max(typeX, typeY); } else { // and we return first operand otherwise return typeX; @@ -367,8 +370,8 @@ FORCEINLINE std::string DataTypeUtils::asString(DataType dataType) { template 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(DataTypeUtils::max())) { newShapeInfo[e] = static_cast(originalShapeInfo[e]); } else diff --git a/libnd4j/include/array/impl/ConstantHolder.cpp b/libnd4j/include/array/impl/ConstantHolder.cpp index 5913d57a9..b3adf8516 100644 --- a/libnd4j/include/array/impl/ConstantHolder.cpp +++ b/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 #include - +#include +#include namespace nd4j { ConstantHolder::ConstantHolder(const ConstantHolder& other) { @@ -24,7 +40,7 @@ namespace nd4j { bool ConstantHolder::hasBuffer() { return hasBuffer(DataTypeUtils::fromT()); } - 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; @@ -34,7 +50,7 @@ namespace nd4j { void ConstantHolder::addBuffer(ConstantDataBuffer &pointer) { addBuffer(pointer, DataTypeUtils::fromT()); } - 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)) diff --git a/libnd4j/include/graph/RandomGenerator.h b/libnd4j/include/graph/RandomGenerator.h index b3018a120..e58f415c5 100644 --- a/libnd4j/include/graph/RandomGenerator.h +++ b/libnd4j/include/graph/RandomGenerator.h @@ -194,8 +194,22 @@ namespace nd4j { template _CUDA_HD FORCEINLINE T RandomGenerator::relativeT(Nd4jLong index, T from, T to) { - auto t = this->relativeT(index); - auto z = from + (t * (to - from)); + auto t = this->relativeT(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(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(index); + auto z = from + float(t * (to - from)); return z; } diff --git a/libnd4j/include/helpers/cuda/ConstantHelper.cu b/libnd4j/include/helpers/cuda/ConstantHelper.cu index 0d7bdf64c..6c8eaa21d 100644 --- a/libnd4j/include/helpers/cuda/ConstantHelper.cu +++ b/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 @@ -21,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/libnd4j/include/ops/declarable/generic/random/uniform.cpp b/libnd4j/include/ops/declarable/generic/random/uniform.cpp index b678852fb..47f7c4a0c 100644 --- a/libnd4j/include/ops/declarable/generic/random/uniform.cpp +++ b/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 @@ -23,6 +24,7 @@ #include #include +#include namespace nd4j { namespace ops { @@ -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::template execTransform>(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(); + auto dtype = DataType::FLOAT32; //ArrayOptions::dataType(inputShape->at(1)); // output type is by given min - auto newShape = ConstantShapeHelper::getInstance()->createShapeInfo(block.dataType(), 'c', shape); + 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(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}); } } } diff --git a/libnd4j/include/ops/declarable/headers/random.h b/libnd4j/include/ops/declarable/headers/random.h index 333fcc089..a361c8fde 100644 --- a/libnd4j/include/ops/declarable/headers/random.h +++ b/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 @@ -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) @@ -66,6 +79,7 @@ namespace nd4j { #if NOT_EXCLUDED(OP_random_poisson) DECLARE_CUSTOM_OP(random_poisson, 2, 1, false, 0, 0); #endif + } } diff --git a/libnd4j/include/ops/declarable/helpers/cpu/random.cpp b/libnd4j/include/ops/declarable/helpers/cpu/random.cpp index 5bbf618ef..124474f87 100644 --- a/libnd4j/include/ops/declarable/helpers/cpu/random.cpp +++ b/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 @@ -23,6 +23,7 @@ #include //#include #include +#include namespace nd4j { namespace ops { @@ -127,6 +128,32 @@ namespace helpers { BUILD_SINGLE_TEMPLATE(template void fillRandomPoisson_, (LaunchContext* context, graph::RandomGenerator& rng, NDArray* lambda, NDArray* output), FLOAT_TYPES); + template + void fillRandomUniform_(LaunchContext* context, graph::RandomGenerator& rng, NDArray* min, NDArray* max, NDArray* output) { + T minVal = T(0); + T maxVal = DataTypeUtils::infOrMax(); + if (min) + minVal = min->t(0); + if (max) + maxVal = max->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(i) = rng.relativeT(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); + } } } \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/segment.cpp b/libnd4j/include/ops/declarable/helpers/cpu/segment.cpp index 6ebfd9b07..e13cfb177 100644 --- a/libnd4j/include/ops/declarable/helpers/cpu/segment.cpp +++ b/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 @@ -91,7 +92,7 @@ namespace helpers { val = nd4j::math::nd4j_min(val, input->t(e)); } else { - idx = indices->e(e); + idx = indices->e(e); val = input->t(e); } output->t(idx) = val; @@ -111,14 +112,14 @@ namespace helpers { minT->assign(listOfTensors->at(0)); for (Nd4jLong i = 1; i < indices->lengthOf(); i++) { - if (indices->e(i) == idx) { + if (indices->e(i) == idx) { for (int e = 0; e < minT->lengthOf(); e++) { minT->p(e, nd4j::math::nd4j_min(minT->e(e), listOfTensors->at(i)->e(e))); } } else { - idx = indices->e(i); + idx = indices->e(i); minT = listOfOutTensors->at(idx); minT->assign(listOfTensors->at(i)); } diff --git a/libnd4j/include/ops/declarable/helpers/cuda/random.cu b/libnd4j/include/ops/declarable/helpers/cuda/random.cu index e1f8645b8..1c28b8f24 100644 --- a/libnd4j/include/ops/declarable/helpers/cuda/random.cu +++ b/libnd4j/include/ops/declarable/helpers/cuda/random.cu @@ -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 @@ -26,6 +26,7 @@ #include #include #include +#include namespace nd4j { namespace ops { @@ -181,6 +182,72 @@ namespace helpers { } BUILD_SINGLE_TEMPLATE(template void fillRandomPoisson_, (LaunchContext* context, graph::RandomGenerator& rng, NDArray* lambda, NDArray* output), FLOAT_NATIVE); + + template + static __global__ void fillUniformKernel(graph::RandomGenerator* devRng, T from, T to, T* output, Nd4jLong* outputShape) { + auto start = blockIdx.x * blockDim.x + threadIdx.x; + auto step = blockDim.x * gridDim.x; + + __shared__ Nd4jLong outputLen; + + if (0 == threadIdx.x) { + outputLen = shape::length(outputShape); + } + __syncthreads(); + + for (auto i = start; i < outputLen; i += step) { + auto zIndex = shape::getIndexOffset(i, outputShape); + output[zIndex] = devRng->relativeT(i, from, to); + } + + } + + template + static void fillRandomUniform_(LaunchContext* context, graph::RandomGenerator& rng, NDArray* min, NDArray* max, NDArray* output) { + T minVal = T(0); + T maxVal = DataTypeUtils::infOrMax(); + if (min) + minVal = min->t(0); + if (max) + maxVal = max->t(0); + + if (output->isR()) + RandomLauncher::fillUniform(context, rng, output, minVal, maxVal); + else { + auto stream = context->getCudaStream(); + graph::RandomGenerator *devRng; + auto err = cudaMalloc(&devRng, sizeof(graph::RandomGenerator)); + if (err != 0) { + cuda_exception::build("fillRandomUniform_: Cannot allocate device memory for random generator due error", err); + } + + err = cudaMemcpy(devRng, &rng, sizeof(graph::RandomGenerator), cudaMemcpyHostToDevice); + if (err != 0) { + cuda_exception::build("fillRandomUniform_: Cannot copy random generator to device", err); + } + auto outputBuf = output->dataBuffer()->specialAsT(); + auto outputShape = output->specialShapeInfo(); + fillUniformKernel<<<128, 128, 128, *stream>>>(devRng, minVal, maxVal, outputBuf, outputShape); + + err = cudaStreamSynchronize(*stream); + if (err != 0) { + cuda_exception::build("fillRandomUniform_: Cannot successfully finish kernel call", err); + } + + err = cudaFree(devRng); + if (err != 0) { + cuda_exception::build("fillRandomUniform_: Cannot deallocate device memory for random generator", err); + } + } + } + + 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); + } } } \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/random.h b/libnd4j/include/ops/declarable/helpers/random.h index a4603c0bd..db1b8ae53 100644 --- a/libnd4j/include/ops/declarable/helpers/random.h +++ b/libnd4j/include/ops/declarable/helpers/random.h @@ -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 @@ -33,7 +33,7 @@ namespace helpers { void fillRandomGamma(LaunchContext* context, graph::RandomGenerator& rng, NDArray* alpha, NDArray* beta, NDArray* output); void fillRandomPoisson(LaunchContext* context, graph::RandomGenerator& rng, NDArray* lambda, NDArray* output); - + void fillRandomUniform(LaunchContext* context, graph::RandomGenerator& rng, NDArray* min, NDArray* max, NDArray* output); } } } diff --git a/libnd4j/include/pairwise_util.h b/libnd4j/include/pairwise_util.h index e87e8961d..e0ed79a7e 100755 --- a/libnd4j/include/pairwise_util.h +++ b/libnd4j/include/pairwise_util.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 @@ -273,8 +274,8 @@ public: BlockInformation(Nd4jLong length, int threshold) { threads = length / threshold; - threads = nd4j::math::nd4j_max(1, threads); - threads = nd4j::math::nd4j_min(threads, omp_get_max_threads()); + threads = (1 < threads)?threads:1;//nd4j::math::nd4j_max(1, threads); + threads = (threads < omp_get_max_threads())?threads:omp_get_max_threads();//nd4j::math::nd4j_min(threads, omp_get_max_threads()); items = length / threads; remainder = length % threads; diff --git a/libnd4j/include/templatemath.h b/libnd4j/include/templatemath.h index f40591e17..96f97f762 100644 --- a/libnd4j/include/templatemath.h +++ b/libnd4j/include/templatemath.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 @@ -27,7 +28,7 @@ #include #include #include - +#include #define BFLOAT16_MAX_VALUE 32737. #define HALF_MAX_VALUE 65504. @@ -883,7 +884,7 @@ namespace nd4j { if (a > 171.624) { // Correct answer too large to display. Force +infinity. return Z(DOUBLE_MAX_VALUE); - //DataTypeUtils::infOrMax(); +// return DataTypeUtils::infOrMax(); } return nd4j::math::nd4j_exp(nd4j::math::nd4j_lgamma(a)); diff --git a/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests10.cpp b/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests10.cpp index 84e3b4e8f..3fd9d26c6 100644 --- a/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests10.cpp +++ b/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests10.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 diff --git a/libnd4j/tests_cpu/layers_tests/RNGTests.cpp b/libnd4j/tests_cpu/layers_tests/RNGTests.cpp index 29c1d5214..e81ea7964 100644 --- a/libnd4j/tests_cpu/layers_tests/RNGTests.cpp +++ b/libnd4j/tests_cpu/layers_tests/RNGTests.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 @@ -855,18 +856,39 @@ TEST_F(RNGTests, Test_GammaDistribution_3) { delete result; } +TEST_F(RNGTests, Test_UniformDistribution_04) { + auto x = NDArrayFactory::create('c', {1}, {10}); + auto al = NDArrayFactory::create(1); + auto be = NDArrayFactory::create(20); + auto exp0 = NDArrayFactory::create('c', {10}); + + + nd4j::ops::randomuniform op; + auto result = op.execute({&x, &al, &be}, {}, {DataType::INT32}); + ASSERT_EQ(Status::OK(), result->status()); + + auto z = result->at(0); + z->printIndexedBuffer("Uniform int distribution"); + ASSERT_TRUE(exp0.isSameShape(z)); + ASSERT_FALSE(exp0.equalsTo(z)); + + delete result; +} + namespace nd4j { namespace tests { static void fillList(Nd4jLong seed, int numberOfArrays, std::vector &shape, std::vector &list, nd4j::graph::RandomGenerator *rng) { rng->setSeed((int) seed); for (int i = 0; i < numberOfArrays; i++) { - auto array = NDArrayFactory::create_('c', shape); - + auto arrayI = NDArrayFactory::create(shape); + auto arrayR = NDArrayFactory::create_('c', shape); + auto min = NDArrayFactory::create(0.0); + auto max = NDArrayFactory::create(1.0); nd4j::ops::randomuniform op; - op.execute(*rng, {array}, {array}, {0.0, 1.0}, {}, {}, true); + op.execute(*rng, {&arrayI, &min, &max}, {arrayR}, {}, {DataType::DOUBLE}, {}, false); - list.emplace_back(array); + list.emplace_back(arrayR); } }; }