From 714808245fb7a63bd21ea43c2b2cbcaa50bd0d02 Mon Sep 17 00:00:00 2001 From: Max Bergfelt Date: Thu, 22 May 2025 16:43:05 +0200 Subject: [PATCH] MLBEDSW-10816: Align LeakyRelu quantization scaling with reference Adjusted LeakyRelu quantization scaling to match the reference model bit-exactly. Lowered the precision of some parameters from double to float during calculations to align with reference behavior. Change-Id: I99bf5beeb41c7557fc1d2c6f7f1f44ed204637ad Signed-off-by: Max Bergfelt --- .../ethosu85/ethos_u85_register_cs_generator.cpp | 6 ++---- ethosu/regor/architecture/ethosu85/ethos_u85_scaling.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/ethosu/regor/architecture/ethosu85/ethos_u85_register_cs_generator.cpp b/ethosu/regor/architecture/ethosu85/ethos_u85_register_cs_generator.cpp index 322cc6c7..68cd2827 100644 --- a/ethosu/regor/architecture/ethosu85/ethos_u85_register_cs_generator.cpp +++ b/ethosu/regor/architecture/ethosu85/ethos_u85_register_cs_generator.cpp @@ -771,10 +771,8 @@ void EthosU85RCSGenerator::GenerateScalingForElementwise(HLCOperation *op) if ( opType == OpType::LeakyRelu ) { - // ifm1Scale is used for rescaling and ifm2Scale is used for alpha - float alpha = op->parameters.leaky_relu.alpha; - float ifm1Scale = float(input1Scale.Dequantize()); - input2Scale = QuantizedScale(alpha * ifm1Scale); + // input2Scale is used for alpha + input2Scale = op->ifm[0].quantization.scales.back(); ifmCnt = 2; } else if ( opType == OpType::Add || opType == OpType::Sub ) diff --git a/ethosu/regor/architecture/ethosu85/ethos_u85_scaling.cpp b/ethosu/regor/architecture/ethosu85/ethos_u85_scaling.cpp index 166774d0..cffe836a 100644 --- a/ethosu/regor/architecture/ethosu85/ethos_u85_scaling.cpp +++ b/ethosu/regor/architecture/ethosu85/ethos_u85_scaling.cpp @@ -162,7 +162,9 @@ void RescaleElementwise(HLCOperation *op) } else if ( opType == OpType::LeakyRelu ) { - input1Scale = QuantizedScale(ifm1Scale / ofmScale); + // input1Scale is used for rescaling and input2Scale is used for alpha. + input1Scale = QuantizedScale(float(ifm1Scale) / float(ofmScale)); + input2Scale = QuantizedScale(float(op->parameters.leaky_relu.alpha) * float(ifm1Scale) / float(ofmScale)); } else if ( opType == OpType::Add || opType == OpType::Sub ) { @@ -191,6 +193,10 @@ void RescaleElementwise(HLCOperation *op) ofmQuant->scales.push_back(outScale); ofmQuant->type = QuantizationType::EXPLICIT; } + if ( opType == OpType::LeakyRelu ) + { + ifm1Quant->scales.push_back(input2Scale); + } } void RescalePooling(HLCOperation *op, bool isNoOp) -- GitLab