[CPU] Fix for FakeQuantize-1 conformance tests

The convert node was extended to support the conversion from Precision::BIN precision. The issue with FakeQuantize-1 conformance tests failures is related to the convert node. In the case of binarization, the FakeQuantize node is transformed into a binarization node and the convert node. But the convert node didn't support conversion from Precision::BIN precision. (#12149)
This commit is contained in:
Oleksii Zaderykhin
2022-08-15 02:03:27 -07:00
committed by GitHub
parent 9ec5d258fc
commit dd051be43a

View File

@@ -518,6 +518,40 @@ bool isConversionTruncatesRange(const Precision & from, const Precision & to) {
INTEL_CPU_CVT(FP32, FP32), INTEL_CPU_CVT(FP16, FP16), INTEL_CPU_CVT(BF16, BF16), INTEL_CPU_CVT(FP64, FP64), \
INTEL_CPU_CVT(BOOL, BOOL)
#define INTEL_CPU_CVT_FROM_BIN(DT) OV_CASE(Precision::DT, PrecisionInfo<Precision::DT>::value_type)
#define INTEL_CPU_CVT_FROM_BIN_LIST \
INTEL_CPU_CVT_FROM_BIN(FP32), INTEL_CPU_CVT_FROM_BIN(FP16), INTEL_CPU_CVT_FROM_BIN(BF16), \
INTEL_CPU_CVT_FROM_BIN(FP64), INTEL_CPU_CVT_FROM_BIN(I16), INTEL_CPU_CVT_FROM_BIN(U8), \
INTEL_CPU_CVT_FROM_BIN(I8), INTEL_CPU_CVT_FROM_BIN(U16), INTEL_CPU_CVT_FROM_BIN(I32), \
INTEL_CPU_CVT_FROM_BIN(U32), INTEL_CPU_CVT_FROM_BIN(I64), INTEL_CPU_CVT_FROM_BIN(U64), \
INTEL_CPU_CVT_FROM_BIN(BOOL)
struct ConvertFromBinContext {
const void *srcPtr;
void *dstPtr;
size_t size;
bool converted;
};
template<typename T>
struct ConvertFromBinPrecision {
void operator()(ConvertFromBinContext &ctx) {
auto src = static_cast<const uint8_t *>(ctx.srcPtr);
auto dst = static_cast<T *>(ctx.dstPtr);
const size_t nBits = 8;
const size_t nBytes = rnd_up(ctx.size, nBits);
parallel_for(nBytes, [&](size_t byteIndex) {
auto currentBitNum = std::min(nBits, ctx.size - byteIndex * nBits);
for (size_t bitIndex = 0; bitIndex < currentBitNum; ++bitIndex) {
dst[byteIndex * nBits + bitIndex] = static_cast<T>((src[byteIndex] & (1 << bitIndex)) >> bitIndex);
}
});
ctx.converted = true;
}
};
void cpu_convert(const void *srcPtr, void *dstPtr, Precision srcPrc, Precision dstPrc, const size_t size) {
cpu_convert(srcPtr, dstPtr, srcPrc, dstPrc, dstPrc, size);
}
@@ -545,8 +579,22 @@ void cpu_convert(const void *srcPtr,
} else {
cpu_memcpy(dstPtr, srcPtr, size * dstPrc.size());
}
} else if (srcPrc == Precision::BIN) {
if (srcPrc.bitsSize() != 1)
IE_THROW() << "cpu_convert can't convert from: " << srcPrc << " <bitsSize == " << srcPrc.bitsSize()
<< "> precision to: " << dstPrc << ". Not implemented.";
ConvertFromBinContext ctx {
srcPtr,
dstPtr,
size,
false
};
OV_SWITCH(intel_cpu, ConvertFromBinPrecision, ctx, dstPrc, INTEL_CPU_CVT_FROM_BIN_LIST);
if (!ctx.converted)
IE_THROW() << "cpu_convert can't convert from: " << srcPrc << " <bitsSize == " << srcPrc.bitsSize()
<< "> precision to: " << dstPrc;
} else {
ConvertContext ctx = {
ConvertContext ctx {
srcPtr,
dstPtr,
size,