From 9d2b843bd41dddcfbc69636fb002c2937c8a2532 Mon Sep 17 00:00:00 2001 From: xiaying Date: Fri, 11 Oct 2024 11:52:14 +0800 Subject: [PATCH] MNN:Bugfix: Fix bug for dynammic quant nan for same value input --- source/backend/cpu/compute/ConvInt8TiledExecutor.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/source/backend/cpu/compute/ConvInt8TiledExecutor.cpp b/source/backend/cpu/compute/ConvInt8TiledExecutor.cpp index 46b1f0739..ae2c1a8ff 100644 --- a/source/backend/cpu/compute/ConvInt8TiledExecutor.cpp +++ b/source/backend/cpu/compute/ConvInt8TiledExecutor.cpp @@ -727,9 +727,15 @@ ErrorCode DenseConvInt8TiledExecutor::onExecute(const std::vector& inpu /* Dynamic quant */ float range = maxVal - minVal; - quantscale = 255.0f / range; - dequantscale = range / 255.0f; - zeropoint = roundf(-minVal * 255.f / range) - 128.0f; + if (fabs(range) < 1e-7) { + zeropoint = maxVal; + quantscale = 1.0f; + dequantscale = 1.0f; + } else { + quantscale = 255.0f / range; + dequantscale = range / 255.0f; + zeropoint = roundf(-minVal * 255.f / range) - 128.0f; + } std::vectorqsVec(PackUnit, quantscale); auto sizeDiv = UP_DIV(inputsize, PackUnit); int inputPlane = input->batch() * mIm2ColParamter.iw * mIm2ColParamter.ih;