chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_random_bernoulli)
|
||||
|
||||
#include <helpers/RandomLauncher.h>
|
||||
#include <ops/declarable/headers/random.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
CUSTOM_OP_IMPL(random_bernoulli, 1, 1, true, 1, 0) {
|
||||
auto rng = block.getRng();
|
||||
auto z = OUTPUT_VARIABLE(0);
|
||||
auto f = T_ARG(0);
|
||||
|
||||
RandomLauncher::fillBernoulli(block.launchContext(), rng, z, f);
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(random_bernoulli) {
|
||||
auto in = INPUT_VARIABLE(0);
|
||||
auto shape = in->template asVectorT<LongType>();
|
||||
|
||||
auto newShape = ConstantShapeHelper::getInstance().createShapeInfo(block.dataType(), 'c', shape);
|
||||
return SHAPELIST(newShape);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(random_bernoulli) {
|
||||
getOpDescriptor()->setAllowedInputTypes(ANY)->setAllowedOutputTypes({ALL_FLOATS});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,135 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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 GS <sgazeos@gmail.com>
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_dropout)
|
||||
|
||||
#include <ops/declarable/headers/parity_ops.h>
|
||||
#include <ops/declarable/helpers/dropout.h>
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
CONFIGURABLE_OP_IMPL(dropout, 1, 2, true, 1, 1) {
|
||||
auto input = INPUT_VARIABLE(0); // lookup param
|
||||
bool inverted = block.numB() > 0 ? B_ARG(0) : false;
|
||||
NDArray* reduceShape = nullptr; // this param is optional
|
||||
auto output = OUTPUT_NULLIFIED(0); //
|
||||
auto mask = OUTPUT_NULLIFIED(1);
|
||||
|
||||
int seed = INT_ARG(0);
|
||||
|
||||
double probValue = T_ARG(0);
|
||||
if(inverted) {
|
||||
probValue = 1 - probValue;
|
||||
}
|
||||
|
||||
|
||||
REQUIRE_TRUE(probValue >= 0.f && probValue <= 1.f, 0, "dropout: Probability should be with range 0 to 1.");
|
||||
|
||||
if (probValue == 1.0f) {
|
||||
*output = *input;
|
||||
double one = 1.0;
|
||||
mask->assign(one);
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
return helpers::dropOutFunctor(block, input, output, reduceShape, seed, probValue, mask);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(dropout) {
|
||||
getOpDescriptor()
|
||||
->setAllowedInputTypes(0, {ALL_FLOATS})
|
||||
->setAllowedInputTypes(1, {ALL_FLOATS,ALL_INTS})
|
||||
->setAllowedOutputTypes({ALL_FLOATS})
|
||||
->setSameMode(true);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
CONFIGURABLE_OP_IMPL(dropout_bp, 3, 1, false, 1, 1) {
|
||||
auto input = INPUT_VARIABLE(0); // lookup param
|
||||
auto mask = INPUT_VARIABLE(1);
|
||||
auto gradOut = INPUT_VARIABLE(2); // lookup param
|
||||
bool inverted = block.numB() > 0 ? B_ARG(0) : false;
|
||||
|
||||
NDArray* reduceShape = nullptr; // this param is optional
|
||||
auto output = OUTPUT_NULLIFIED(0); //
|
||||
|
||||
int seed = INT_ARG(0);
|
||||
|
||||
double probValue = T_ARG(0);
|
||||
if(inverted) {
|
||||
probValue = 1 - probValue;
|
||||
}
|
||||
|
||||
|
||||
REQUIRE_TRUE((probValue > 0. && probValue <= 1.), 0, "dropout_bp: Probability should be with range 0 to 1.");
|
||||
if (probValue == 1.0) {
|
||||
float zero = 0.0f;
|
||||
output->assign(zero); // fill up output with 0
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
REQUIRE_TRUE(sd::ops::helpers::dropOutFunctorBP(block, input, gradOut, output, reduceShape, seed, probValue,
|
||||
mask) == sd::Status::OK,
|
||||
0, "dropout_bp: Cannot backprop dropout.");
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_TYPES(dropout_bp) {
|
||||
getOpDescriptor()->setAllowedInputTypes({ALL_FLOATS, ALL_INTS})->setAllowedOutputTypes({ALL_FLOATS});
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
CONFIGURABLE_OP_IMPL(alpha_dropout_bp, 2, 1, false, 4, 1) {
|
||||
NDArray* input = INPUT_VARIABLE(0); // lookup param
|
||||
NDArray *mask = INPUT_VARIABLE(1); // lookup param
|
||||
NDArray* gradOut = INPUT_VARIABLE(2); // lookup param
|
||||
|
||||
NDArray* reduceShape = nullptr; // this param is optional
|
||||
NDArray* output = OUTPUT_VARIABLE(0); //
|
||||
|
||||
if (block.width() > 2) reduceShape = INPUT_VARIABLE(2);
|
||||
|
||||
int seed = INT_ARG(0);
|
||||
|
||||
double probValue = T_ARG(0);
|
||||
double alphaValue = T_ARG(1);
|
||||
double alpha1Value = T_ARG(2);
|
||||
double betaValue = T_ARG(3);
|
||||
|
||||
REQUIRE_TRUE(probValue > 0. && probValue <= 1., 0, "dropout_bp: Probability should be with range 0 to 1.");
|
||||
if (probValue == 1.0) {
|
||||
double zero = 0.0;
|
||||
output->assign(zero); // fill up output with 0
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
return helpers::alphaDropOutFunctorBP(block, input, gradOut, output, reduceShape, seed, probValue, alphaValue,
|
||||
alpha1Value, betaValue, mask);
|
||||
}
|
||||
DECLARE_TYPES(alpha_dropout_bp) { getOpDescriptor()->setAllowedInputTypes({ALL_FLOATS})->setSameMode(true); }
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,56 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_random_exponential)
|
||||
|
||||
#include <helpers/RandomLauncher.h>
|
||||
#include <ops/declarable/headers/random.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
CUSTOM_OP_IMPL(random_exponential, 1, 1, true, 1, 0) {
|
||||
// random generator for distribution
|
||||
auto rng = block.randomGenerator();
|
||||
auto z = OUTPUT_VARIABLE(0);
|
||||
auto lambda = T_ARG(0);
|
||||
|
||||
RandomLauncher::fillExponential(block.launchContext(), rng, z, lambda);
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(random_exponential) {
|
||||
auto in = INPUT_VARIABLE(0);
|
||||
auto shape = in->template asVectorT<LongType>();
|
||||
|
||||
auto newShape = ConstantShapeHelper::getInstance().createShapeInfo(block.dataType(), 'c', shape);
|
||||
return SHAPELIST(newShape);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(random_exponential) {
|
||||
getOpDescriptor()->setAllowedInputTypes(ANY)->setAllowedOutputTypes({ALL_FLOATS});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,88 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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 George A. Shulinok <sgazeos@gmail.com>
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_random_gamma)
|
||||
|
||||
#include <ops/declarable/headers/random.h>
|
||||
#include <ops/declarable/helpers/random.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
CUSTOM_OP_IMPL(random_gamma, 2, 1, false, 0, 0) {
|
||||
// gamma distribution
|
||||
auto rng = block.randomGenerator();
|
||||
auto shape = INPUT_VARIABLE(0);
|
||||
auto alpha = INPUT_VARIABLE(1);
|
||||
NDArray* beta = nullptr;
|
||||
|
||||
if (block.width() > 2) {
|
||||
beta = INPUT_VARIABLE(2);
|
||||
REQUIRE_TRUE(ShapeUtils::areShapesBroadcastable(*alpha, *beta), 0,
|
||||
"random_gamma: alpha and beta shapes should be broadcastable.");
|
||||
}
|
||||
|
||||
auto output = OUTPUT_VARIABLE(0);
|
||||
auto seed = 0;
|
||||
|
||||
if (block.getIArguments()->size()) {
|
||||
seed = INT_ARG(0);
|
||||
}
|
||||
|
||||
rng.setSeed(seed);
|
||||
|
||||
helpers::fillRandomGamma(block.launchContext(), rng, alpha, beta, output);
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(random_gamma) {
|
||||
auto in = INPUT_VARIABLE(0);
|
||||
auto shape = in->template asVectorT<LongType>();
|
||||
auto alphaShape = inputShape->at(1);
|
||||
auto additionalShape = alphaShape;
|
||||
if (inputShape->size() > 2) {
|
||||
auto rest = inputShape->at(2);
|
||||
additionalShape = nullptr;
|
||||
REQUIRE_TRUE(ShapeUtils::areShapesBroadcastable(alphaShape, rest), 0,
|
||||
"random_gamma: alpha and beta shapes should be broadcastable.");
|
||||
LongType* additionalShapeBroadcasted = nullptr;
|
||||
ShapeUtils::evalBroadcastShapeInfo(alphaShape, rest, true, additionalShapeBroadcasted, block.workspace());
|
||||
additionalShape = additionalShapeBroadcasted;
|
||||
}
|
||||
auto dtype = block.numD() > 0 ? D_ARG(0) : ArrayOptions::dataType(alphaShape);
|
||||
for (LongType i = 0; i < shape::rank(additionalShape); i++) shape.push_back(shape::sizeAt(additionalShape, i));
|
||||
auto newShape = ConstantShapeHelper::getInstance().createShapeInfo(dtype, 'c', shape);
|
||||
return SHAPELIST(newShape);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(random_gamma) {
|
||||
getOpDescriptor()
|
||||
->setAllowedInputTypes(0, {ALL_INTS})
|
||||
->setAllowedInputTypes(1, {ALL_FLOATS})
|
||||
->setAllowedInputTypes(2, {ALL_FLOATS})
|
||||
->setAllowedOutputTypes({ALL_FLOATS});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,50 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_get_seed)
|
||||
|
||||
#include <ops/declarable/CustomOperations.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
CUSTOM_OP_IMPL(get_seed, -2, 1, false, 0, 0) {
|
||||
auto rng = block.getRng();
|
||||
auto z = OUTPUT_VARIABLE(0);
|
||||
|
||||
z->p(LongType(0), rng.rootState());
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(get_seed) {
|
||||
auto newshape = ConstantShapeHelper::getInstance().scalarShapeInfo(INT64);
|
||||
return SHAPELIST(newshape);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(get_seed) {
|
||||
getOpDescriptor()->setAllowedInputTypes(ANY)->setAllowedOutputTypes(INT64);
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* ******************************************************************************
|
||||
* *
|
||||
* *
|
||||
* * 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.
|
||||
* *
|
||||
* * See the NOTICE file distributed with this work for additional
|
||||
* * information regarding copyright ownership.
|
||||
* * 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 Oleh Semeniv (oleg.semeniv@gmail.com)
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_random_multinomial)
|
||||
|
||||
#include <helpers/RandomLauncher.h>
|
||||
#include <ops/declarable/CustomOperations.h>
|
||||
#include <ops/declarable/helpers/random.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
///////////////////////
|
||||
/**
|
||||
* multinomial (categorical) random generator
|
||||
* takes 2D ndarray with logits with shape [batch_size (N), num_classes (K)]
|
||||
* and array with one scalar value of samples number, number of independent samples to draw for each experiment 1,N.
|
||||
* represents the unnormalized log-probabilities for all classes.
|
||||
* Int arguments: 0 - optional argument, corresponds to dimension with batch_size
|
||||
* Int arguments: 1 - optional argument, integer type to use for the output. Default int64.
|
||||
*/
|
||||
// used https://en.wikipedia.org/wiki/Categorical_distribution
|
||||
// methods: gumbel trick + softmax + argmax
|
||||
CUSTOM_OP_IMPL(random_multinomial, 2, 1, false, 0, 0) {
|
||||
auto input = INPUT_VARIABLE(0);
|
||||
auto output = OUTPUT_NULLIFIED(0);
|
||||
auto inputSamples = INPUT_VARIABLE(1);
|
||||
|
||||
REQUIRE_TRUE(!input->isEmpty(), 0, "RANDOM_MULTINOMIAL OP: Have to be provided at least one logits. ");
|
||||
|
||||
REQUIRE_TRUE(inputSamples->lengthOf() == 1, 0,
|
||||
"RANDOM_MULTINOMIAL OP: Have to be specified at least one sample,"
|
||||
" but got no argumets instead.");
|
||||
|
||||
LongType numOfSamples = static_cast<LongType>(inputSamples->e<int>(0));
|
||||
// do nothing if number of samples = 0
|
||||
if (0 == numOfSamples) return Status::OK;
|
||||
|
||||
REQUIRE_TRUE(numOfSamples > 0, 0, "RANDOM_MULTINOMIAL OP: Number of samples should be greater then 0, got %i. ",
|
||||
numOfSamples);
|
||||
|
||||
const int rank = input->rankOf();
|
||||
REQUIRE_TRUE(rank == 2, 0,
|
||||
"RANDOM_MULTINOMIAL OP: Logits should be a matrix with rank = 2, but got instead rank = %i.", rank);
|
||||
|
||||
const int argSize = block.getIArguments()->size();
|
||||
const int dimC = argSize > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + rank) : rank - 1;
|
||||
|
||||
auto dimA = (0 == dimC) ? 1 : 0;
|
||||
if (1 == input->sizeAt(dimA)) {
|
||||
*output = 0;
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
auto rng = block.randomGenerator();
|
||||
helpers::fillRandomMultiNomial(block.launchContext(), rng, *input, *output, numOfSamples, dimC);
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(random_multinomial) {
|
||||
auto input = INPUT_VARIABLE(0);
|
||||
auto inputSamples = INPUT_VARIABLE(1);
|
||||
|
||||
REQUIRE_TRUE(inputSamples->lengthOf() == 1, 0,
|
||||
"RANDOM_MULTINOMIAL OP: Have to be specified at least one sample,"
|
||||
" but got no argumets instead.");
|
||||
|
||||
LongType numOfSamples = static_cast<LongType>(inputSamples->e<int>(0));
|
||||
|
||||
REQUIRE_TRUE(numOfSamples > 0, 0, "RANDOM_MULTINOMIAL OP: Number of samples should be greater then 0, got %i. ",
|
||||
numOfSamples);
|
||||
|
||||
const int rank = input->rankOf();
|
||||
REQUIRE_TRUE(rank == 2, 0,
|
||||
"RANDOM_MULTINOMIAL OP: Logits should be a matrix with rank = 2, but got instead rank = %i.", rank);
|
||||
|
||||
const int argSize = block.getIArguments()->size();
|
||||
const int dimC = argSize > 0 ? (INT_ARG(0) >= 0 ? INT_ARG(0) : INT_ARG(0) + rank) : rank - 1;
|
||||
|
||||
auto* inputShapeVec = input->getShapeAsVector();
|
||||
auto dimA = (0 == dimC) ? 1 : 0;
|
||||
(*inputShapeVec)[dimA] = numOfSamples;
|
||||
|
||||
DataType nType =
|
||||
(argSize > 1) ? (INT_ARG(1) >= 0 ? static_cast<DataType>(INT_ARG(1)) : INT64) : INT64;
|
||||
auto result = SHAPELIST(ConstantShapeHelper::getInstance().createShapeInfo(nType, input->ordering(), *inputShapeVec));
|
||||
delete inputShapeVec;
|
||||
return result;
|
||||
}
|
||||
|
||||
DECLARE_TYPES(random_multinomial) {
|
||||
getOpDescriptor()
|
||||
->setAllowedInputTypes(0, {ALL_FLOATS, ALL_INTS})
|
||||
->setAllowedInputTypes(1, {INT32})
|
||||
->setAllowedOutputTypes(0, {ALL_INDICES});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_random_normal)
|
||||
|
||||
#include <helpers/RandomLauncher.h>
|
||||
#include <ops/declarable/headers/random.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
CUSTOM_OP_IMPL(random_normal, 1, 1, true, 2, 0) {
|
||||
// normal distribution
|
||||
auto rng = block.randomGenerator();
|
||||
|
||||
RandomLauncher::fillGaussian(block.launchContext(), rng, OUTPUT_VARIABLE(0), T_ARG(0), T_ARG(1));
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(random_normal) {
|
||||
auto in = INPUT_VARIABLE(0);
|
||||
auto shape = in->template asVectorT<LongType>();
|
||||
if(block.getDArguments()->size() > 0) {
|
||||
auto newShape = ConstantShapeHelper::getInstance().createShapeInfo(D_ARG(0), 'c', shape);
|
||||
return SHAPELIST(newShape);
|
||||
} else {
|
||||
auto newShape = ConstantShapeHelper::getInstance().createShapeInfo(block.dataType(), 'c', shape);
|
||||
return SHAPELIST(newShape);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
DECLARE_SYN(randomnormal, random_normal);
|
||||
|
||||
DECLARE_TYPES(random_normal) {
|
||||
getOpDescriptor()->setAllowedInputTypes(ANY)->setAllowedOutputTypes({ALL_FLOATS});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,68 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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 George A. Shulinok <sgazeos@gmail.com>
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_random_poisson)
|
||||
|
||||
#include <ops/declarable/headers/random.h>
|
||||
#include <ops/declarable/helpers/random.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
CUSTOM_OP_IMPL(random_poisson, 2, 1, false, 0, 0) {
|
||||
// gamma distribution
|
||||
auto rng = block.randomGenerator();
|
||||
auto shape = INPUT_VARIABLE(0);
|
||||
auto lambda = INPUT_VARIABLE(1);
|
||||
auto output = OUTPUT_VARIABLE(0);
|
||||
auto seed = 0;
|
||||
if (block.getIArguments()->size()) {
|
||||
seed = INT_ARG(0);
|
||||
}
|
||||
rng.setSeed(seed);
|
||||
helpers::fillRandomPoisson(block.launchContext(), rng, lambda, output);
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(random_poisson) {
|
||||
auto in = INPUT_VARIABLE(0);
|
||||
auto shape = in->template asVectorT<LongType>();
|
||||
auto lambdaShape = inputShape->at(1);
|
||||
auto dtype = block.numD() > 0 ? D_ARG(0) : ArrayOptions::dataType(lambdaShape);
|
||||
for (LongType d = 0; d < shape::rank(lambdaShape); ++d) {
|
||||
shape.emplace_back(shape::sizeAt(lambdaShape, d));
|
||||
}
|
||||
auto newShape = ConstantShapeHelper::getInstance().createShapeInfo(dtype, 'c', shape);
|
||||
return SHAPELIST(newShape);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(random_poisson) {
|
||||
getOpDescriptor()
|
||||
->setAllowedInputTypes(0, {ALL_INTS})
|
||||
->setAllowedInputTypes(1, {ALL_FLOATS})
|
||||
->setAllowedOutputTypes({ALL_FLOATS});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,72 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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 GS <sgazeos@gmail.com>
|
||||
//
|
||||
|
||||
#include <ops/declarable/CustomOperations.h>
|
||||
#include <ops/declarable/helpers/random_crop.h>
|
||||
#if NOT_EXCLUDED(OP_random_crop)
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
CUSTOM_OP_IMPL(random_crop, 2, 1, false, 0, 0) {
|
||||
auto input = INPUT_VARIABLE(0); // values for crop
|
||||
auto shape = INPUT_VARIABLE(1); // shape for result
|
||||
|
||||
NDArray* reduceShape = nullptr; // this param is optional
|
||||
auto output = OUTPUT_VARIABLE(0); //
|
||||
|
||||
int seed = 0;
|
||||
|
||||
if (block.getIArguments()->size() > 0) seed = INT_ARG(0);
|
||||
|
||||
REQUIRE_TRUE(shape->isVector(), 0, "random_crop: Shape tensor should be a vector.");
|
||||
|
||||
REQUIRE_TRUE(input->rankOf() == shape->lengthOf(), 0,
|
||||
"random_crop: The length of the shape vector is not match input rank. %i and %i were given.",
|
||||
input->rankOf(), shape->lengthOf());
|
||||
|
||||
for (int e = 0; e < shape->lengthOf(); ++e) {
|
||||
REQUIRE_TRUE((*shape).e<sd::LongType>(e) <= input->sizeAt(e), 0,
|
||||
"random_crop: Shape tensor should be less than proper input dimension (dim %i, %i > %i).", e,
|
||||
(*shape).e<sd::LongType>(e), input->sizeAt(e));
|
||||
}
|
||||
|
||||
return helpers::randomCropFunctor(block, input, shape, output, seed);
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(random_crop) {
|
||||
auto in = INPUT_VARIABLE(1);
|
||||
auto typeShape = inputShape->at(0);
|
||||
std::vector<LongType> shape(in->lengthOf());
|
||||
|
||||
for (size_t e = 0; e < shape.size(); e++) shape[e] = (*in).e<LongType>(e);
|
||||
|
||||
auto newShape = ConstantShapeHelper::getInstance().createShapeInfo(ArrayOptions::dataType(typeShape), 'c', shape);
|
||||
return SHAPELIST(newShape);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(random_crop) {
|
||||
getOpDescriptor()->setAllowedInputTypes(ANY)->setAllowedOutputTypes({ALL_FLOATS});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
#endif
|
||||
@@ -0,0 +1,50 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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 (iuriish@yahoo.com), created on 26.01.2018
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_random_shuffle)
|
||||
|
||||
#include <ops/declarable/CustomOperations.h>
|
||||
#include <ops/declarable/helpers/transforms.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
|
||||
OP_IMPL(random_shuffle, 1, 1, true) {
|
||||
auto input = INPUT_VARIABLE(0);
|
||||
const bool isInplace = block.isInplace();
|
||||
auto output = isInplace ? nullptr : OUTPUT_VARIABLE(0);
|
||||
|
||||
// sd::random::RandomBuffer* rng = block.getRNG();
|
||||
RandomGenerator rng = block.randomGenerator();
|
||||
// REQUIRE_TRUE(rng != nullptr, 0, "RANDOM_SHUFFLE op: RNG should be defined in Graph !");
|
||||
|
||||
helpers::randomShuffle(block.launchContext(), *input, *output, rng, isInplace);
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_TYPES(random_shuffle) { getOpDescriptor()->setAllowedInputTypes(ANY)->setSameMode(true); }
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,60 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* 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
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_set_seed)
|
||||
|
||||
#include <legacy/NativeOps.h>
|
||||
#include <ops/declarable/CustomOperations.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
CUSTOM_OP_IMPL(set_seed, -2, 1, false, 0, -2) {
|
||||
auto rng = block.getRng(); //.getRNG();
|
||||
|
||||
LongType seed = 0;
|
||||
if (block.getIArguments()->size() > 0) {
|
||||
seed = INT_ARG(0);
|
||||
} else if (block.width() > 0) {
|
||||
auto input = INPUT_VARIABLE(0);
|
||||
REQUIRE_TRUE(input->isScalar(), 0, "SetSeed: Seed operand should be scalar");
|
||||
seed = input->e<LongType>(0);
|
||||
} else {
|
||||
REQUIRE_TRUE(false, 0, "SetSeed: either IArg or scalr input should be provided");
|
||||
}
|
||||
|
||||
// FIXME: this approach isn't really good for cuda, since it'll assume that CUDA might get nullptr instead of stream
|
||||
// refreshBuffer(nullptr, seed, (sd::Pointer) rng);
|
||||
rng.setSeed((int)seed);
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(set_seed) {
|
||||
auto newshape = ConstantShapeHelper::getInstance().scalarShapeInfo(block.dataType());
|
||||
return SHAPELIST(newshape);
|
||||
}
|
||||
|
||||
DECLARE_TYPES(set_seed) { getOpDescriptor()->setAllowedInputTypes({ALL_INTS})->setAllowedOutputTypes({ALL_FLOATS}); }
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* ******************************************************************************
|
||||
* *
|
||||
* *
|
||||
* * 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.
|
||||
* *
|
||||
* * See the NOTICE file distributed with this work for additional
|
||||
* * information regarding copyright ownership.
|
||||
* * 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 29/10/17.
|
||||
//
|
||||
|
||||
#include <system/op_boilerplate.h>
|
||||
#if NOT_EXCLUDED(OP_randomuniform)
|
||||
|
||||
#include <helpers/RandomLauncher.h>
|
||||
#include <ops/declarable/CustomOperations.h>
|
||||
#include <ops/declarable/helpers/random.h>
|
||||
|
||||
namespace sd {
|
||||
namespace ops {
|
||||
///////////////////////
|
||||
/**
|
||||
* uniform distribution
|
||||
* takes 1 ndarray
|
||||
*
|
||||
* T arguments map:
|
||||
* TArgs[0] - min for rng
|
||||
* TArgs[1] - max for rng
|
||||
*/
|
||||
CUSTOM_OP_IMPL(randomuniform, -1, 1, true, 0, -2) {
|
||||
// uniform distribution
|
||||
auto rng = block.randomGenerator();
|
||||
auto dtype = FLOAT32;
|
||||
if (block.getIArguments()->size()) dtype = (DataType)INT_ARG(0);
|
||||
|
||||
if (block.getIArguments()->size() > 1) {
|
||||
auto seed = INT_ARG(1);
|
||||
rng.setStates(seed, seed ^ 0xdeadbeef);
|
||||
sd_debug("randomuniform: Setting seed %d\n", seed);
|
||||
}
|
||||
|
||||
auto min = block.width() > 1 ? INPUT_VARIABLE(1) : (NDArray*)nullptr;
|
||||
auto max = block.width() > 2 ? INPUT_VARIABLE(2) : (NDArray*)nullptr;
|
||||
|
||||
bool localMin = false;
|
||||
bool localMax = false;
|
||||
|
||||
if ((min == nullptr && max == nullptr) || block.numT() >= 2) {
|
||||
min = NDArrayFactory::create_(dtype, block.launchContext());
|
||||
max = NDArrayFactory::create_(dtype, block.launchContext());
|
||||
min->p(0, T_ARG(0));
|
||||
max->p(0, T_ARG(1));
|
||||
localMin = true;
|
||||
localMax = true;
|
||||
}
|
||||
|
||||
auto output = OUTPUT_VARIABLE(0);
|
||||
REQUIRE_TRUE(output->dataType() == dtype, 0, "RandomUniform: data type of output should be equals to given.");
|
||||
|
||||
helpers::fillRandomUniform(block.launchContext(), rng, min, max, output);
|
||||
|
||||
if (localMin) delete min;
|
||||
if (localMax) delete max;
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
DECLARE_SHAPE_FN(randomuniform) {
|
||||
auto in = INPUT_VARIABLE(0);
|
||||
auto shape = in->template asVectorT<LongType>();
|
||||
auto dtype = block.getDArguments()->size() > 0 ? D_ARG(0) : FLOAT32;
|
||||
|
||||
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(0, {ALL_INTS})
|
||||
->setAllowedInputTypes(1, {ALL_INTS, ALL_FLOATS})
|
||||
->setAllowedInputTypes(2, {ALL_INTS, ALL_FLOATS})
|
||||
->setAllowedOutputTypes({ALL_FLOATS, ALL_INTS});
|
||||
}
|
||||
} // namespace ops
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user