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 bug3 #6906

Merged
merged 7 commits into from Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 78 additions & 15 deletions libnd4j/blas/cpu/NDArray.cpp
Expand Up @@ -4032,8 +4032,11 @@ template <typename T>
NDArray NDArray::operator+(const T& scalar) const {
if (isS())
throw std::runtime_error("NDArray::operator+: you can't use this method on String array!");

auto tmp = NDArrayFactory::create(this->dataType(), scalar, _workspace);
DataType type;
if (!Environment::getInstance()->isExperimentalBuild()){
type = this->dataType();
}
auto tmp = NDArrayFactory::create(type, scalar, _workspace);
NDArray result(_shapeInfo, DataTypeUtils::pickPairwiseResultType(_dataType, DataTypeUtils::fromT<T>()), false, _workspace);
NativeOpExcutioner::execScalar(nd4j::scalar::Add, _buffer, _shapeInfo, result._buffer, result._shapeInfo, tmp.buffer(), tmp.shapeInfo(), nullptr);
return result;
Expand All @@ -4056,7 +4059,12 @@ NDArray NDArray::operator-(const T& scalar) const {
if (isS())
throw std::runtime_error("NDArray::operator-: you can't use this method on String array!");

auto tmp = NDArrayFactory::create(scalar);
DataType type;
if (!Environment::getInstance()->isExperimentalBuild()){
type = this->dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, _workspace);
NDArray result(_shapeInfo, DataTypeUtils::pickPairwiseResultType(_dataType, DataTypeUtils::fromT<T>()), false, _workspace);
NativeOpExcutioner::execScalar(nd4j::scalar::Subtract, _buffer, _shapeInfo, result._buffer, result._shapeInfo, tmp.buffer(), tmp.shapeInfo(), nullptr);
return result;
Expand All @@ -4079,7 +4087,12 @@ NDArray NDArray::operator*(const T& scalar) const {
if (isS())
throw std::runtime_error("NDArray::operator*: you can't use this method on String array!");

auto tmp = NDArrayFactory::create(this->dataType(), scalar, _workspace);
DataType type = DataTypeUtils::fromT<T>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = this->dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, _workspace);
NDArray result(_shapeInfo, DataTypeUtils::pickPairwiseResultType(_dataType, DataTypeUtils::fromT<T>()), false, _workspace);
NativeOpExcutioner::execScalar(nd4j::scalar::Multiply, _buffer, _shapeInfo, result._buffer, result._shapeInfo, tmp.buffer(), tmp.shapeInfo(), nullptr);
return result;
Expand All @@ -4105,7 +4118,12 @@ NDArray NDArray::operator/(const T& scalar) const {
if(scalar == (T)0.)
throw std::runtime_error("NDArray::operator/ (division operator) : division by zero !");

auto tmp = NDArrayFactory::create(this->dataType(), scalar, _workspace);
DataType type;
if (!Environment::getInstance()->isExperimentalBuild()){
type = this->dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, _workspace);
NDArray result(_shapeInfo, DataTypeUtils::pickPairwiseResultType(_dataType, DataTypeUtils::fromT<T>()), false, _workspace);
NativeOpExcutioner::execScalar(nd4j::scalar::Divide, _buffer, _shapeInfo, result._buffer, result._shapeInfo, tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand Down Expand Up @@ -4169,7 +4187,12 @@ ND4J_EXPORT NDArray operator-(const float16& scalar, const NDArray & arr) {
if (arr.isS())
throw std::runtime_error("NDArray::operator-: you can't use this method on String array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<float16>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<float16>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseSubtract, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4180,7 +4203,12 @@ ND4J_EXPORT NDArray operator-(const bfloat16& scalar, const NDArray & arr) {
if (arr.isS())
throw std::runtime_error("NDArray::operator-: you can't use this method on String array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<bfloat16>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<bfloat16>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseSubtract, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4190,7 +4218,12 @@ ND4J_EXPORT NDArray operator-(const float& scalar, const NDArray& arr) {
if (arr.isS())
throw std::runtime_error("NDArray::operator-: you can't use this method on String array!");

auto tmp = NDArrayFactory::create(arr.dataType(), scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<float>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<float>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseSubtract, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4213,7 +4246,7 @@ ND4J_EXPORT NDArray operator-(const Nd4jLong& scalar, const NDArray& arr) {
if (arr.isS())
throw std::runtime_error("NDArray::operator-: you can't use this method on String array!");

DataType type = DataTypeUtils::fromT<double>();
DataType type = DataTypeUtils::fromT<Nd4jLong>();
if (!Environment::getInstance()->isExperimentalBuild()) {
type = arr.dataType();
}
Expand All @@ -4227,7 +4260,12 @@ ND4J_EXPORT NDArray operator-(const int& scalar, const NDArray& arr) {
if (arr.isS())
throw std::runtime_error("NDArray::operator-: you can't use this method on String array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<int>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<int>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseSubtract, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4241,7 +4279,12 @@ ND4J_EXPORT NDArray operator/(const bfloat16& scalar, const NDArray& arr) {
if (arr.isB())
throw std::runtime_error("NDArray::operator/: you can't divide scalar by bool array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<bfloat16>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<bfloat16>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseDivide, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4253,7 +4296,12 @@ ND4J_EXPORT NDArray operator/(const float16& scalar, const NDArray& arr) {
if (arr.isB())
throw std::runtime_error("NDArray::operator/: you can't divide scalar by bool array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<float16>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<float16>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseDivide, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4265,7 +4313,12 @@ ND4J_EXPORT NDArray operator/(const float& scalar, const NDArray & arr) {
if (arr.isB())
throw std::runtime_error("NDArray::operator/: you can't divide scalar by bool array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<float>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<float>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseDivide, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4277,7 +4330,12 @@ ND4J_EXPORT NDArray operator/(const double& scalar, const NDArray & arr) {
if (arr.isB())
throw std::runtime_error("NDArray::operator/: you can't divide scalar by bool array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<double>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<double>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseDivide, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand All @@ -4289,7 +4347,12 @@ ND4J_EXPORT NDArray operator/(const int& scalar, const NDArray & arr) {
if (arr.isB())
throw std::runtime_error("NDArray::operator/: you can't divide scalar by bool array!");

auto tmp = NDArrayFactory::create(scalar, arr.getWorkspace());
DataType type = DataTypeUtils::fromT<int>();
if (!Environment::getInstance()->isExperimentalBuild()){
type = arr.dataType();
}

auto tmp = NDArrayFactory::create(type, scalar, arr.getWorkspace());
NDArray result(arr.getShapeInfo(), DataTypeUtils::pickPairwiseResultType(arr.dataType(), DataTypeUtils::fromT<int>()), false, arr.getWorkspace());
NativeOpExcutioner::execScalar(nd4j::scalar::ReverseDivide, arr.getBuffer(), arr.getShapeInfo(), result.getBuffer(), result.getShapeInfo(), tmp.getBuffer(), tmp.getShapeInfo(), nullptr);
return result;
Expand Down
54 changes: 40 additions & 14 deletions libnd4j/include/ops/declarable/generic/parity_ops/reduceMean.cpp
Expand Up @@ -20,7 +20,7 @@


#include <ops/declarable/CustomOperations.h>

#include <ops/declarable/helpers/axis.h>

namespace nd4j {
namespace ops {
Expand All @@ -30,9 +30,17 @@ CUSTOM_OP_IMPL(reduce_mean, 1, 1, false, 0, 0) {
auto input = INPUT_VARIABLE(0);
auto output = OUTPUT_VARIABLE(0);

const bool keepDims = block.getTArguments()->size() > 0 ? (bool)T_ARG(0) : false;

std::vector<int> dimensions = *block.getIArguments();
auto dimensions = *block.getIArguments();
if (block.width() > 1) {
auto axesVector = INPUT_VARIABLE(1);
helpers::adjustAxis(input, axesVector, dimensions);
}
// else if (block.getIArguments()->size())
bool keepDims = false;
if (block.getBArguments()->size())
keepDims = B_ARG(0);
else if (block.getTArguments()->size())
keepDims = (bool)T_ARG(0);

REQUIRE_TRUE(dimensions.size() <= input->rankOf(), 0, "REDUCE_MEAN OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());

Expand All @@ -51,10 +59,18 @@ CUSTOM_OP_IMPL(reduce_mean, 1, 1, false, 0, 0) {
->setAllowedOutputTypes({ALL_FLOATS});
}

DECLARE_SHAPE_FN(reduce_mean) {
const bool keepDims = block.getTArguments()->size() > 0 ? (bool)T_ARG(0) : false;

std::vector<int> dimensions = *block.getIArguments();
DECLARE_SHAPE_FN(reduce_mean) {
auto dimensions = *block.getIArguments();
if (block.width() > 1) {
auto axesVector = INPUT_VARIABLE(1);
helpers::adjustAxis(INPUT_VARIABLE(0), axesVector, dimensions);
}
// else if (block.getIArguments()->size())
bool keepDims = false;
if (block.getBArguments()->size())
keepDims = B_ARG(0);
else if (block.getTArguments()->size())
keepDims = (bool)T_ARG(0);

REQUIRE_TRUE(dimensions.size() <= inputShape->at(0)[0], 0, "REDUCE_MEAN OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());

Expand All @@ -81,9 +97,17 @@ CUSTOM_OP_IMPL(reduce_mean_bp, 2, 1, false, 0, 0) {

auto gradI = OUTPUT_VARIABLE(0);

const bool keepDims = block.getTArguments()->size() > 0 ? (bool)T_ARG(0) : false;

std::vector<int> dimensions = *block.getIArguments();
auto dimensions = *block.getIArguments();
if (block.width() > 2) {
auto axesVector = INPUT_VARIABLE(2);
helpers::adjustAxis(input, axesVector, dimensions);
}
// else if (block.getIArguments()->size())
bool keepDims = false;
if (block.getBArguments()->size())
keepDims = B_ARG(0);
else if (block.getTArguments()->size())
keepDims = (bool)T_ARG(0);

REQUIRE_TRUE(dimensions.size() <= input->rankOf(), 0, "REDUCE_MEAN_BP OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());

Expand Down Expand Up @@ -115,9 +139,11 @@ CUSTOM_OP_IMPL(reduce_mean_bp, 2, 1, false, 0, 0) {


DECLARE_SHAPE_FN(reduce_mean_bp) {
const bool keepDims = block.getTArguments()->size() > 0 ? (bool)T_ARG(0) : false;

std::vector<int> dimensions = *block.getIArguments();
auto dimensions = *block.getIArguments();
if (block.width() > 2) {
auto axesVector = INPUT_VARIABLE(2);
helpers::adjustAxis(INPUT_VARIABLE(0), axesVector, dimensions);
}

REQUIRE_TRUE(dimensions.size() <= inputShape->at(0)[0], 0, "REDUCE_MEAN_BP OP: the number of dimensions to reduce along must be <= input array rank, but got %i instead" , dimensions.size());

Expand Down