2021-02-01 13:31:45 +01:00
|
|
|
/* ******************************************************************************
|
|
|
|
*
|
2019-06-06 14:21:15 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2021-02-01 13:31:45 +01:00
|
|
|
* See the NOTICE file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2019-06-06 14:21:15 +02:00
|
|
|
* 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 sgazeos@gmail.com
|
|
|
|
//
|
|
|
|
#ifndef __MIN_I_MAX_H_HELPERS__
|
|
|
|
#define __MIN_I_MAX_H_HELPERS__
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/op_boilerplate.h>
|
|
|
|
#include <array/NDArray.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <helpers/ShapeUtils.h>
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
template <typename T>
|
2019-06-06 14:21:15 +02:00
|
|
|
static void minimumBPFunctor_(NDArray* x, NDArray* y, NDArray* epsNext, NDArray* gradX, NDArray* gradY) {
|
|
|
|
|
|
|
|
auto lambdaX = LAMBDA_TTT(_e, _x, _y) {
|
|
|
|
return _x <= _y ? _e : (T) 0.;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto lambdaY = LAMBDA_TTT(_e, _x, _y) {
|
|
|
|
return _x >= _y ? _e : (T) 0.;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (x->isSameShape(y)) {
|
|
|
|
// PWT case case
|
|
|
|
|
|
|
|
// X gradient
|
2019-12-20 20:35:39 +01:00
|
|
|
epsNext->applyTriplewiseLambda<T>(*x, *y, lambdaX, *gradX);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
// Y gradient
|
2019-12-20 20:35:39 +01:00
|
|
|
epsNext->applyTriplewiseLambda<T>(*x, *y, lambdaY, *gradY);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
} else if (y->isScalar()) {
|
|
|
|
T s = y->e<T>(0);
|
|
|
|
auto lambdaS = LAMBDA_TT(_e, _x, s) {
|
|
|
|
return _x <= s ? _e : (T) 0.;
|
|
|
|
};
|
|
|
|
|
|
|
|
// scalar case
|
|
|
|
auto tmp = epsNext->reduceNumber(reduce::Sum);
|
|
|
|
if (x <= y)
|
|
|
|
gradY->assign(tmp);
|
|
|
|
else
|
|
|
|
gradY->assign(0.0f);
|
2019-12-20 20:35:39 +01:00
|
|
|
|
|
|
|
epsNext->applyPairwiseLambda<T>(*x, lambdaS, *gradX);
|
2019-06-06 14:21:15 +02:00
|
|
|
} else {
|
|
|
|
// broadcast case
|
|
|
|
|
|
|
|
// in this case we want to boost our X and Y shapes to the size of FF pass output (or epsNext, which has the same shape)
|
|
|
|
auto preX = x->dup();
|
|
|
|
auto preY = y->dup();
|
|
|
|
|
|
|
|
auto targetShape = epsNext->getShapeAsVector();
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
preX.tileToShape(targetShape, preX);
|
|
|
|
preY.tileToShape(targetShape, preY);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
epsNext->applyTriplewiseLambda<T>(preX, preY, lambdaX, preX);
|
|
|
|
epsNext->applyTriplewiseLambda<T>(preX, preY, lambdaY, preY);
|
|
|
|
|
|
|
|
auto axisX = ShapeUtils::evalBroadcastBackwardAxis(x->shapeInfo(), epsNext->shapeInfo());
|
|
|
|
auto axisY = ShapeUtils::evalBroadcastBackwardAxis(y->shapeInfo(), epsNext->shapeInfo());
|
|
|
|
|
|
|
|
if (axisX.size() > 0) {
|
2019-12-20 20:35:39 +01:00
|
|
|
auto sum = preX.reduceAlongDimension(reduce::Sum, axisX);
|
2019-06-06 14:21:15 +02:00
|
|
|
gradX->assign(sum);
|
2019-12-20 20:35:39 +01:00
|
|
|
} else
|
2019-06-06 14:21:15 +02:00
|
|
|
gradX->assign(preX);
|
|
|
|
|
|
|
|
if (axisY.size() > 0) {
|
2019-12-20 20:35:39 +01:00
|
|
|
auto sum = preY.reduceAlongDimension(reduce::Sum, axisY);
|
2019-06-06 14:21:15 +02:00
|
|
|
gradY->assign(sum);
|
|
|
|
} else
|
|
|
|
gradY->assign(preY);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void maximumBPFunctor_(NDArray* x, NDArray* y, NDArray* epsNext, NDArray* gradX, NDArray* gradY) {
|
|
|
|
|
|
|
|
auto lambdaX = LAMBDA_TTT(_e, _x, _y) {
|
|
|
|
return _x >= _y ? _e : (T) 0.;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto lambdaY = LAMBDA_TTT(_e, _x, _y) {
|
|
|
|
return _x <= _y ? _e : (T) 0.;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (x->isSameShape(y)) {
|
|
|
|
// PWT case case
|
|
|
|
|
|
|
|
// X gradient
|
2019-12-20 20:35:39 +01:00
|
|
|
epsNext->applyTriplewiseLambda<T>(*x, *y, lambdaX, *gradX);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
// Y gradient
|
2019-12-20 20:35:39 +01:00
|
|
|
epsNext->applyTriplewiseLambda<T>(*x, *y, lambdaY, *gradY);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
} else if (y->isScalar()) {
|
|
|
|
T s = y->e<T>(0);
|
|
|
|
auto lambdaS = LAMBDA_TT(_e, _x, s) {
|
|
|
|
return _x >= s ? _e : (T) 0.;
|
|
|
|
};
|
|
|
|
|
|
|
|
// scalar case
|
|
|
|
auto tmp = epsNext->reduceNumber(reduce::Sum);
|
|
|
|
if (x <= y)
|
|
|
|
gradY->assign(tmp);
|
|
|
|
else
|
|
|
|
gradY->assign(0.0f);
|
2019-12-20 20:35:39 +01:00
|
|
|
|
|
|
|
epsNext->applyPairwiseLambda<T>(*x, lambdaS, *gradX);
|
2019-06-06 14:21:15 +02:00
|
|
|
} else {
|
|
|
|
// broadcast case
|
|
|
|
|
|
|
|
// in this case we want to boost our X and Y shapes to the size of FF pass output (or epsNext, which has the same shape)
|
|
|
|
auto preX = x->dup();
|
|
|
|
auto preY = y->dup();
|
|
|
|
|
|
|
|
auto targetShape = epsNext->getShapeAsVector();
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
preX.tileToShape(targetShape, preX);
|
|
|
|
preY.tileToShape(targetShape, preY);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
epsNext->applyTriplewiseLambda<T>(preX, preY, lambdaX, preX);
|
|
|
|
epsNext->applyTriplewiseLambda<T>(preX, preY, lambdaY, preY);
|
|
|
|
|
|
|
|
auto axisX = ShapeUtils::evalBroadcastBackwardAxis(x->shapeInfo(), epsNext->shapeInfo());
|
|
|
|
auto axisY = ShapeUtils::evalBroadcastBackwardAxis(y->shapeInfo(), epsNext->shapeInfo());
|
|
|
|
|
|
|
|
if (axisX.size() > 0) {
|
2019-12-20 20:35:39 +01:00
|
|
|
auto sum = preX.reduceAlongDimension(reduce::Sum, axisX);
|
2019-06-06 14:21:15 +02:00
|
|
|
gradX->assign(sum);
|
2019-12-20 20:35:39 +01:00
|
|
|
} else
|
2019-06-06 14:21:15 +02:00
|
|
|
gradX->assign(preX);
|
|
|
|
|
|
|
|
if (axisY.size() > 0) {
|
2019-12-20 20:35:39 +01:00
|
|
|
auto sum = preY.reduceAlongDimension(reduce::Sum, axisY);
|
2019-06-06 14:21:15 +02:00
|
|
|
gradY->assign(sum);
|
|
|
|
} else
|
|
|
|
gradY->assign(preY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void minimumBPFunctor(sd::LaunchContext * context, NDArray* x, NDArray* y, NDArray* epsNext, NDArray* gradX, NDArray* gradY) {
|
2019-06-06 14:21:15 +02:00
|
|
|
BUILD_SINGLE_SELECTOR(x->dataType(), minimumBPFunctor_, (x, y, epsNext, gradX, gradY), NUMERIC_TYPES);
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void maximumBPFunctor(sd::LaunchContext * context, NDArray* x, NDArray* y, NDArray* epsNext, NDArray* gradX, NDArray* gradY) {
|
2019-06-06 14:21:15 +02:00
|
|
|
BUILD_SINGLE_SELECTOR(x->dataType(), maximumBPFunctor_, (x, y, epsNext, gradX, gradY), NUMERIC_TYPES);
|
|
|
|
}
|
|
|
|
BUILD_SINGLE_TEMPLATE(template void minimumBPFunctor_, (NDArray* x, NDArray* y, NDArray* epsNext, NDArray* gradX, NDArray* gradY), NUMERIC_TYPES);
|
|
|
|
BUILD_SINGLE_TEMPLATE(template void maximumBPFunctor_, (NDArray* x, NDArray* y, NDArray* epsNext, NDArray* gradX, NDArray* gradY), NUMERIC_TYPES);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|