Files
deeplearning4j--deeplearning4j/libnd4j/include/ops/declarable/helpers/cpu/summaryStatReductions.cpp
T
2026-07-13 12:47:05 +08:00

97 lines
4.2 KiB
C++

/* ******************************************************************************
*
*
* 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 AbdelRauf (rauf@konduit.ai)
//
// CPU implementation of summary stat reductions (variance, standardDeviation)
//
#include <helpers/ConstantTadHelper.h>
#include <legacy/NativeOpExecutioner.h>
#include <ops/declarable/helpers/reductions.h>
#include <system/op_enums.h>
namespace sd {
namespace ops {
namespace helpers {
//////////////////////////////////////////////////////////////////////////
void variance(NDArray& input, NDArray& output, const std::vector<LongType>& dimensions, bool biasCorrected) {
// Prepares (syncs) buffer of which NDArrays will be used as read, write
NDArray::prepareSpecialUse({&output}, {&input});
if (output.isScalar()) {
NativeOpExecutioner::execSummaryStatsScalar(
LaunchContext::defaultContext(), variance::SummaryStatsVariance,
input.buffer(), input.shapeInfo(), input.specialBuffer(), input.specialShapeInfo(),
nullptr,
output.buffer(), output.shapeInfo(), output.specialBuffer(), output.specialShapeInfo(),
biasCorrected);
} else {
auto tadPack = ConstantTadHelper::getInstance().tadForDimensions(
input.shapeInfo(), const_cast<sd::LongType*>(dimensions.data()), dimensions.size());
NativeOpExecutioner::execSummaryStats(
LaunchContext::defaultContext(), variance::SummaryStatsVariance,
input.buffer(), input.shapeInfo(), input.specialBuffer(), input.specialShapeInfo(),
nullptr,
output.buffer(), output.shapeInfo(), output.specialBuffer(), output.specialShapeInfo(),
const_cast<LongType*>(dimensions.data()), dimensions.size(),
tadPack->primaryShapeInfo(), tadPack->primaryOffsets(),
biasCorrected);
}
// Inform that we are done with those buffers
NDArray::registerSpecialUse({&output}, {&input});
}
//////////////////////////////////////////////////////////////////////////
void standardDeviation(NDArray& input, NDArray& output, const std::vector<LongType>& dimensions, bool biasCorrected) {
// Prepares (syncs) buffer of which NDArrays will be used as read, write
NDArray::prepareSpecialUse({&output}, {&input});
if (output.isScalar()) {
NativeOpExecutioner::execSummaryStatsScalar(
LaunchContext::defaultContext(), variance::SummaryStatsStandardDeviation,
input.buffer(), input.shapeInfo(), input.specialBuffer(), input.specialShapeInfo(),
nullptr,
output.buffer(), output.shapeInfo(), output.specialBuffer(), output.specialShapeInfo(),
biasCorrected);
} else {
auto tadPack = ConstantTadHelper::getInstance().tadForDimensions(
input.shapeInfo(), const_cast<sd::LongType*>(dimensions.data()), dimensions.size());
NativeOpExecutioner::execSummaryStats(
LaunchContext::defaultContext(), variance::SummaryStatsStandardDeviation,
input.buffer(), input.shapeInfo(), input.specialBuffer(), input.specialShapeInfo(),
nullptr,
output.buffer(), output.shapeInfo(), output.specialBuffer(), output.specialShapeInfo(),
const_cast<LongType*>(dimensions.data()), dimensions.size(),
tadPack->primaryShapeInfo(), tadPack->primaryOffsets(),
biasCorrected);
}
// Inform that we are done with those buffers
NDArray::registerSpecialUse({&output}, {&input});
}
} // namespace helpers
} // namespace ops
} // namespace sd