mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 21:22:43 -06:00
* core: support fp8 depths in cv::sum cv::sum and cv::trace threw for CV_8F_E4M3FN and CV_8F_E4M3FNUZ. Add sum8f and sum8fnuz to the function table, and collect their float totals in blocks like CV_16F and CV_16BF. * core: test cv::sum on fp8 past the block size Add a (1 << 15) + 1 element case to Core_Sum_FP8, so the float total is moved to the double result and reset between blocks. * core: support fp8 depths in cv::mean cv::mean uses the same adding functions as cv::sum, so adding fp8 to that table made it accept these depths. Its blockSum check still left them out, so the float totals were written straight into the double result and the mean came back wrong. Use the block path for CV_8F_E4M3FN and CV_8F_E4M3FNUZ, as cv::sum does, and check cv::mean in Core_Sum_FP8. * core: test cv::mean with a mask on fp8 * core: split Core_Sum_FP8 into sum, mean and mean_mask tests Move the array setup into a fixture, and make the length a test parameter, so each case runs on its own.
228 lines
7.8 KiB
C++
228 lines
7.8 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html
|
|
|
|
|
|
#include "precomp.hpp"
|
|
#include "opencl_kernels_core.hpp"
|
|
#include "stat.hpp"
|
|
|
|
#include "stat_parallel.hpp"
|
|
#include "sum.simd.hpp"
|
|
#include "sum.simd_declarations.hpp" // defines CV_CPU_DISPATCH_MODES_ALL=AVX2,...,BASELINE based on CMakeLists.txt content
|
|
|
|
namespace cv
|
|
{
|
|
|
|
SumFunc getSumFunc(int depth)
|
|
{
|
|
CV_INSTRUMENT_REGION();
|
|
CV_CPU_DISPATCH(getSumFunc, (depth),
|
|
CV_CPU_DISPATCH_MODES_ALL);
|
|
}
|
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
bool ocl_sum( InputArray _src, Scalar & res, int sum_op, InputArray _mask,
|
|
InputArray _src2, bool calc2, const Scalar & res2 )
|
|
{
|
|
CV_Assert(sum_op == OCL_OP_SUM || sum_op == OCL_OP_SUM_ABS || sum_op == OCL_OP_SUM_SQR);
|
|
|
|
const ocl::Device & dev = ocl::Device::getDefault();
|
|
bool doubleSupport = dev.doubleFPConfig() > 0,
|
|
haveMask = _mask.kind() != _InputArray::NONE,
|
|
haveSrc2 = _src2.kind() != _InputArray::NONE;
|
|
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
|
|
kercn = cn == 1 && !haveMask ? ocl::predictOptimalVectorWidth(_src, _src2) : 1,
|
|
mcn = std::max(cn, kercn);
|
|
CV_Assert(!haveSrc2 || _src2.type() == type);
|
|
int convert_cn = haveSrc2 ? mcn : cn;
|
|
|
|
if ( (!doubleSupport && depth == CV_64F) || cn > 4 )
|
|
return false;
|
|
|
|
if (depth >= CV_16F)
|
|
return false;
|
|
|
|
int ngroups = dev.maxComputeUnits(), dbsize = ngroups * (calc2 ? 2 : 1);
|
|
size_t wgs = dev.maxWorkGroupSize();
|
|
|
|
int ddepth = std::max(sum_op == OCL_OP_SUM_SQR ? CV_32F : CV_32S, depth),
|
|
dtype = CV_MAKE_TYPE(ddepth, cn);
|
|
CV_Assert(!haveMask || _mask.type() == CV_8U || _mask.type() == CV_8S || _mask.type() == CV_Bool);
|
|
|
|
int wgs2_aligned = 1;
|
|
while (wgs2_aligned < (int)wgs)
|
|
wgs2_aligned <<= 1;
|
|
wgs2_aligned >>= 1;
|
|
|
|
static const char * const opMap[3] = { "OP_SUM", "OP_SUM_ABS", "OP_SUM_SQR" };
|
|
char cvt[2][50];
|
|
String opts = format("-D srcT=%s -D srcT1=%s -D dstT=%s -D dstTK=%s -D dstT1=%s -D ddepth=%d -D cn=%d"
|
|
" -D convertToDT=%s -D %s -D WGS=%d -D WGS2_ALIGNED=%d%s%s%s%s -D kercn=%d%s%s%s -D convertFromU=%s",
|
|
ocl::typeToStr(CV_MAKE_TYPE(depth, mcn)), ocl::typeToStr(depth),
|
|
ocl::typeToStr(dtype), ocl::typeToStr(CV_MAKE_TYPE(ddepth, mcn)),
|
|
ocl::typeToStr(ddepth), ddepth, cn,
|
|
ocl::convertTypeStr(depth, ddepth, mcn, cvt[0], sizeof(cvt[0])),
|
|
opMap[sum_op], (int)wgs, wgs2_aligned,
|
|
doubleSupport ? " -D DOUBLE_SUPPORT" : "",
|
|
haveMask ? " -D HAVE_MASK" : "",
|
|
_src.isContinuous() ? " -D HAVE_SRC_CONT" : "",
|
|
haveMask && _mask.isContinuous() ? " -D HAVE_MASK_CONT" : "", kercn,
|
|
haveSrc2 ? " -D HAVE_SRC2" : "", calc2 ? " -D OP_CALC2" : "",
|
|
haveSrc2 && _src2.isContinuous() ? " -D HAVE_SRC2_CONT" : "",
|
|
depth <= CV_32S && ddepth == CV_32S ? ocl::convertTypeStr(CV_8U, ddepth, convert_cn, cvt[1], sizeof(cvt[1])) : "noconvert");
|
|
|
|
ocl::Kernel k("reduce", ocl::core::reduce_oclsrc, opts);
|
|
if (k.empty())
|
|
return false;
|
|
|
|
UMat src = _src.getUMat(), src2 = _src2.getUMat(),
|
|
db(1, dbsize, dtype), mask = _mask.getUMat();
|
|
|
|
ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
|
|
dbarg = ocl::KernelArg::PtrWriteOnly(db),
|
|
maskarg = ocl::KernelArg::ReadOnlyNoSize(mask),
|
|
src2arg = ocl::KernelArg::ReadOnlyNoSize(src2);
|
|
|
|
if (haveMask)
|
|
{
|
|
if (haveSrc2)
|
|
k.args(srcarg, src.cols, (int)src.total(), ngroups, dbarg, maskarg, src2arg);
|
|
else
|
|
k.args(srcarg, src.cols, (int)src.total(), ngroups, dbarg, maskarg);
|
|
}
|
|
else
|
|
{
|
|
if (haveSrc2)
|
|
k.args(srcarg, src.cols, (int)src.total(), ngroups, dbarg, src2arg);
|
|
else
|
|
k.args(srcarg, src.cols, (int)src.total(), ngroups, dbarg);
|
|
}
|
|
|
|
size_t globalsize = ngroups * wgs;
|
|
if (k.run(1, &globalsize, &wgs, true))
|
|
{
|
|
typedef Scalar (*part_sum)(Mat m);
|
|
part_sum funcs[3] = { ocl_part_sum<int>, ocl_part_sum<float>, ocl_part_sum<double> },
|
|
func = funcs[ddepth - CV_32S];
|
|
|
|
Mat mres = db.getMat(ACCESS_READ);
|
|
if (calc2)
|
|
const_cast<Scalar &>(res2) = func(mres.colRange(ngroups, dbsize));
|
|
|
|
res = func(mres.colRange(0, ngroups));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#endif
|
|
|
|
// serial implementation, also used for the pieces of the parallel version
|
|
static Scalar sum_(const Mat& src)
|
|
{
|
|
Scalar _res;
|
|
int cn = src.channels();
|
|
|
|
if (src.dims <= 2)
|
|
{
|
|
CALL_HAL_RET2(sum, cv_hal_sum, _res, src.data, src.step, src.type(), src.cols, src.rows, &_res[0]);
|
|
}
|
|
else if (src.isContinuous())
|
|
{
|
|
CALL_HAL_RET2(sum, cv_hal_sum, _res, src.data, 0, src.type(), (int)src.total(), 1, &_res[0]);
|
|
}
|
|
|
|
int k, depth = src.depth();
|
|
SumFunc func = getSumFunc(depth);
|
|
if (func == nullptr) {
|
|
if (depth == CV_Bool && cn == 1)
|
|
return Scalar((double)countNonZero(src));
|
|
CV_Error_(Error::StsNotImplemented, ("sum: %s input is not supported, convert the input to another type (e.g. CV_32F) first",
|
|
typeToString(src.type()).c_str()));
|
|
}
|
|
|
|
const Mat* arrays[] = {&src, 0};
|
|
uchar* ptrs[1] = {};
|
|
NAryMatIterator it(arrays, ptrs);
|
|
int total = (int)it.size, blockSize = total, partialBlockSize = 0;
|
|
int j, count = 0;
|
|
int _buf[CV_CN_MAX];
|
|
int* buf = (int*)&_res[0];
|
|
size_t esz = 0;
|
|
bool partialSumIsInt = depth < CV_32S;
|
|
bool blockSum = partialSumIsInt || depth == CV_16F || depth == CV_16BF ||
|
|
depth == CV_8F_E4M3FN || depth == CV_8F_E4M3FNUZ;
|
|
|
|
if( blockSum )
|
|
{
|
|
partialBlockSize = depth <= CV_8S ? (1 << 23) : (1 << 15);
|
|
blockSize = std::min(blockSize, partialBlockSize);
|
|
buf = _buf;
|
|
for( k = 0; k < cn; k++ )
|
|
buf[k] = 0;
|
|
esz = src.elemSize();
|
|
}
|
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
|
{
|
|
for( j = 0; j < total; j += blockSize )
|
|
{
|
|
int bsz = std::min(total - j, blockSize);
|
|
func( ptrs[0], 0, (uchar*)buf, bsz, cn );
|
|
count += bsz;
|
|
if( blockSum && (count + blockSize >= partialBlockSize || (i+1 >= it.nplanes && j+bsz >= total)) )
|
|
{
|
|
if (partialSumIsInt) {
|
|
for( k = 0; k < cn; k++ )
|
|
{
|
|
_res[k] += buf[k];
|
|
buf[k] = 0;
|
|
}
|
|
} else {
|
|
for( k = 0; k < cn; k++ )
|
|
{
|
|
_res[k] += ((float*)buf)[k];
|
|
buf[k] = 0;
|
|
}
|
|
}
|
|
count = 0;
|
|
}
|
|
ptrs[0] += bsz*esz;
|
|
}
|
|
}
|
|
return _res;
|
|
}
|
|
|
|
Scalar sum(InputArray _src)
|
|
{
|
|
CV_INSTRUMENT_REGION();
|
|
|
|
Scalar _res;
|
|
#ifdef HAVE_OPENCL
|
|
CV_OCL_RUN_(OCL_PERFORMANCE_CHECK(_src.isUMat()) && _src.dims() <= 2,
|
|
ocl_sum(_src, _res, OCL_OP_SUM),
|
|
_res);
|
|
#endif
|
|
|
|
Mat src = _src.getMat();
|
|
CV_CheckLE( src.channels(), 4, "cv::sum does not support more than 4 channels" );
|
|
|
|
std::vector<StatChunk> chunks;
|
|
if (!splitForParallelStat(src, Mat(), chunks))
|
|
return sum_(src);
|
|
|
|
std::vector<Scalar> part(chunks.size());
|
|
parallel_for_(Range(0, (int)chunks.size()), [&](const Range& r)
|
|
{
|
|
for (int i = r.start; i < r.end; i++)
|
|
part[i] = sum_(chunks[i].src);
|
|
});
|
|
for (const Scalar& v : part)
|
|
_res += v;
|
|
return _res;
|
|
}
|
|
|
|
} // namespace
|