2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
2020-01-10 22:14:20 +01:00
|
|
|
// @author Yurii Shyrma (iuriish@yahoo.com)
|
2019-06-06 14:21:15 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <ops/declarable/helpers/batchnorm.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <helpers/ShapeUtils.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <helpers/OmpLaunchHelper.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2020-01-10 22:14:20 +01:00
|
|
|
static void batchnorm_(const NDArray* input, const NDArray* mean, const NDArray* variance, const NDArray* gamma, const NDArray* beta,
|
|
|
|
NDArray* output,
|
|
|
|
const std::vector<int>& axes, const double epsilon) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-10-26 13:14:21 +02:00
|
|
|
// formula: output = gamma * ((input - mean) / sqrt(variance + epsilon)) + beta
|
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
const T* x = input->bufferAsT<T>();
|
|
|
|
T* z = output->bufferAsT<T>();
|
|
|
|
const T* m = mean->bufferAsT<T>();
|
|
|
|
const T* v = variance->bufferAsT<T>();
|
|
|
|
const T* g = gamma == nullptr ? nullptr : gamma->bufferAsT<T>();
|
|
|
|
const T* b = beta == nullptr ? nullptr : beta->bufferAsT<T>();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
const bool xzSameOffset = shape::haveSameShapeAndStrides(input->getShapeInfo(), output->getShapeInfo());
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
bool paramSameOffset = shape::haveSameShapeAndStrides(mean->getShapeInfo(), variance->getShapeInfo());
|
|
|
|
if(paramSameOffset && gamma != nullptr)
|
|
|
|
paramSameOffset &= shape::haveSameShapeAndStrides(mean->getShapeInfo(), gamma->getShapeInfo());
|
|
|
|
if(paramSameOffset && beta != nullptr)
|
|
|
|
paramSameOffset &= shape::haveSameShapeAndStrides(mean->getShapeInfo(), beta->getShapeInfo());
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
const Nd4jLong lenBig = input->lengthOf();
|
|
|
|
const Nd4jLong lenSmall = mean->lengthOf();
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
const Nd4jLong steps = lenBig / lenSmall;
|
2019-06-06 14:21:15 +02:00
|
|
|
std::vector<int> dimsToExclude = ShapeUtils::evalDimsToExclude(input->rankOf(), axes);
|
|
|
|
|
|
|
|
OmpLaunchHelper info(lenBig, lenSmall);
|
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
auto func = PRAGMA_THREADS_DO {
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
Nd4jLong* xOffsets = new Nd4jLong[steps];
|
|
|
|
Nd4jLong* zOffsets = xzSameOffset ? xOffsets : new Nd4jLong[steps];
|
|
|
|
Nd4jLong* auxBuff = new Nd4jLong[2 * input->rankOf()];
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong j = 0; j < lenSmall; ++j) {
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
const bool isOwner = (j < info._numThreads) ? thread_id == j : thread_id == (j % info._numThreads);
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
if(!isOwner)
|
|
|
|
continue;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
const auto meanOffset = shape::getIndexOffset(j, mean->getShapeInfo());
|
|
|
|
const auto varOffset = paramSameOffset ? meanOffset : shape::getIndexOffset(j, variance->getShapeInfo());
|
|
|
|
|
|
|
|
const auto meanVal = m[meanOffset];
|
2020-03-02 10:49:41 +01:00
|
|
|
auto sigmaInvGam = static_cast<T>(1) / sd::math::nd4j_sqrt<T, T>(v[varOffset] + epsilon);
|
2020-01-10 22:14:20 +01:00
|
|
|
|
|
|
|
if(g != nullptr) {
|
|
|
|
const auto gammaOffset = paramSameOffset ? meanOffset : shape::getIndexOffset(j, gamma->getShapeInfo());
|
|
|
|
sigmaInvGam *= g[gammaOffset];
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2020-01-10 22:14:20 +01:00
|
|
|
|
|
|
|
T betaVal = static_cast<T>(0);
|
|
|
|
if(b != nullptr) {
|
|
|
|
const auto betaOffset = paramSameOffset ? meanOffset : shape::getIndexOffset(j, beta->getShapeInfo());
|
|
|
|
betaVal = b[betaOffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate offsets for input and output
|
|
|
|
shape::outerArrayOffsets(xOffsets, j, input->getShapeInfo(), mean->getShapeInfo(), auxBuff, dimsToExclude.data());
|
|
|
|
if(!xzSameOffset)
|
|
|
|
shape::outerArrayOffsets(zOffsets, j, output->getShapeInfo(), mean->getShapeInfo(), auxBuff, dimsToExclude.data());
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong i = 0; i < steps; ++i)
|
2020-01-10 22:14:20 +01:00
|
|
|
z[zOffsets[i]] = (x[xOffsets[i]] - meanVal) * sigmaInvGam + betaVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete []auxBuff;
|
|
|
|
delete []xOffsets;
|
|
|
|
if(!xzSameOffset)
|
|
|
|
delete []zOffsets;
|
|
|
|
};
|
|
|
|
|
2020-03-09 06:21:44 +01:00
|
|
|
sd::Threads::parallel_do(func, info._numThreads);
|
2020-01-10 22:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
static void batchnorm2_(const NDArray* input, const NDArray* mean, const NDArray* variance, const NDArray* gamma, const NDArray* beta,
|
|
|
|
NDArray* output,
|
|
|
|
const std::vector<int>& axes, const double epsilon) {
|
|
|
|
|
|
|
|
// formula: output = gamma * ((input - mean) / sqrt(variance + epsilon)) + beta
|
|
|
|
|
|
|
|
const auto x = input->bufferAsT<T>();
|
|
|
|
auto z = output->bufferAsT<T>();
|
|
|
|
const auto m = mean->bufferAsT<T>();
|
|
|
|
const auto v = variance->bufferAsT<T>();
|
|
|
|
const auto g = gamma == nullptr ? nullptr : gamma->bufferAsT<T>();
|
|
|
|
const auto b = beta == nullptr ? nullptr : beta->bufferAsT<T>();
|
|
|
|
|
|
|
|
// xRank == zRank, minRank = meanRank = varianceRank = gammaRank = betaRank
|
|
|
|
const uint xRank = input->rankOf();
|
|
|
|
const uint minRank = mean->rankOf();
|
|
|
|
const uint numAxes = axes.size();
|
|
|
|
|
|
|
|
const bool xzSameOffset = shape::haveSameShapeAndStrides(input->getShapeInfo(), output->getShapeInfo());
|
|
|
|
|
|
|
|
bool paramSameOffset = shape::haveSameShapeAndStrides(mean->getShapeInfo(), variance->getShapeInfo());
|
|
|
|
if(paramSameOffset && gamma != nullptr)
|
|
|
|
paramSameOffset &= shape::haveSameShapeAndStrides(mean->getShapeInfo(), gamma->getShapeInfo());
|
|
|
|
if(paramSameOffset && beta != nullptr)
|
|
|
|
paramSameOffset &= shape::haveSameShapeAndStrides(mean->getShapeInfo(), beta->getShapeInfo());
|
|
|
|
|
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
|
|
|
|
Nd4jLong coords[MAX_RANK];
|
|
|
|
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2020-01-10 22:14:20 +01:00
|
|
|
|
|
|
|
shape::index2coords(i, input->getShapeInfo(), coords);
|
|
|
|
|
|
|
|
const auto xOffset = shape::getOffset(input->getShapeInfo(), coords);
|
|
|
|
const auto zOffset = xzSameOffset ? xOffset : shape::getOffset(output->getShapeInfo(), coords);
|
|
|
|
|
|
|
|
if(minRank == xRank) {
|
|
|
|
for (uint i = 0, j = 0; i < xRank; ++i) {
|
|
|
|
if(j < numAxes && i != axes[j])
|
|
|
|
coords[i] = 0;
|
|
|
|
else
|
|
|
|
++j;
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 22:14:20 +01:00
|
|
|
else // minRank = numAxes = 1 in this case
|
|
|
|
coords[0] = coords[axes[0]];
|
|
|
|
|
|
|
|
const auto meanOffset = shape::getOffset(mean->getShapeInfo(), coords);
|
|
|
|
const auto varianceOffset = paramSameOffset ? meanOffset : shape::getOffset(variance->getShapeInfo(), coords);
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
T sigmaInvGam = 1. / sd::math::nd4j_sqrt<T, T>(v[varianceOffset] + epsilon);
|
2020-01-10 22:14:20 +01:00
|
|
|
|
|
|
|
if(g != nullptr) {
|
|
|
|
const auto gammaOffset = paramSameOffset ? meanOffset : shape::getOffset(gamma->getShapeInfo(), coords);
|
|
|
|
sigmaInvGam *= g[gammaOffset];
|
|
|
|
}
|
|
|
|
|
|
|
|
z[zOffset] = (x[xOffset] - m[meanOffset]) * sigmaInvGam;
|
|
|
|
|
|
|
|
if(b != nullptr) {
|
|
|
|
const auto betaOffset = paramSameOffset ? meanOffset : shape::getOffset(beta->getShapeInfo(), coords);
|
|
|
|
z[zOffset] += b[betaOffset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-11-13 15:15:18 +01:00
|
|
|
|
2020-03-09 06:21:44 +01:00
|
|
|
sd::Threads::parallel_for(func, 0, input->lengthOf());
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
void batchnorm(const NDArray* input, const NDArray* mean, const NDArray* variance, const NDArray* gamma, const NDArray* beta, NDArray* output, const std::vector<int>& axes, const double epsilon) {
|
|
|
|
|
2020-01-10 22:14:20 +01:00
|
|
|
// batchnorm2_ is slower
|
2019-06-06 14:21:15 +02:00
|
|
|
BUILD_SINGLE_SELECTOR(input->dataType(), batchnorm_, (input, mean, variance, gamma, beta, output, axes, epsilon), FLOAT_TYPES);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BUILD_SINGLE_TEMPLATE(template void batchnorm_, (const NDArray* input, const NDArray* mean, const NDArray* variance, const NDArray* gamma, const NDArray* beta, NDArray* output, const std::vector<int>& axes, const double epsilon), FLOAT_TYPES);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|