diff options
author | Fredrik Svedberg <fredrik.svedberg@arm.com> | 2020-05-19 10:43:01 +0200 |
---|---|---|
committer | Tim Hall <tim.hall@arm.com> | 2020-06-18 17:53:52 +0100 |
commit | 620d88c60482bad4d96da4d32cc4cca5561cca9e (patch) | |
tree | 7f1416d7829b98e2ec8275826bfb487bba1409f7 /ethosu | |
parent | f98c674377f2d073bb719a80f3cbc6aab73acd32 (diff) | |
download | ethos-u-vela-620d88c60482bad4d96da4d32cc4cca5561cca9e.tar.gz |
MLBEDSW-786: Fix Tanh/Sigmoid for int16
Fixed scaling for int16 tanh/sigmoid to match the reference.
Change-Id: I3110298b7e8638a82cc05bedc03de389dec27898
Signed-off-by: Fredrik Svedberg <fredrik.svedberg@arm.com>
Diffstat (limited to 'ethosu')
-rw-r--r-- | ethosu/vela/register_command_stream_generator.py | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/ethosu/vela/register_command_stream_generator.py b/ethosu/vela/register_command_stream_generator.py index e767071d..73418d52 100644 --- a/ethosu/vela/register_command_stream_generator.py +++ b/ethosu/vela/register_command_stream_generator.py @@ -602,10 +602,18 @@ def generate_register_command_stream(nng, sg, arch, verbose=False): # For valid padding vela has to output scaling values if faf == "Sigmoid" or faf == "Tanh": rescale = 0x3000 * cmd.ifm_tensor.quantization.scale_f32 - rescale_bits = len(bin(round_up_to_int(rescale))) - 2 + 1 + if cmd.ifm_tensor.dtype == DataType.int16: + multiplier = max(1, int(4096 * cmd.ifm_tensor.quantization.scale_f32)) + rescale *= 3 * multiplier + + rescale_bits = len(bin(round_up_to_int(rescale))) - 2 + 1 scale, shift = scaling.quantise_pooling_scale(k_height * k_width, rescale_bits) - scale = int(round_away_zero(scale * rescale)) + + if cmd.ifm_tensor.dtype == DataType.int16: + scale = (1 << shift) * 3 * multiplier + else: + scale = int(round_away_zero(scale * rescale)) else: # In case avg pool fused with concat or other memory operation, rescaling might be needed. # k_height == k_width == 1 is allways true in this case @@ -715,12 +723,20 @@ def generate_register_command_stream(nng, sg, arch, verbose=False): faf_max = quantise_float32(1.0, ofm_quant.scale_f32, ofm_quant.zero_point) elif faf == "Tanh": emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.TANH) - faf_min = quantise_float32(clamp_tanh(ifm_min), ofm_quant.scale_f32, ofm_quant.zero_point) - faf_max = quantise_float32(clamp_tanh(ifm_max), ofm_quant.scale_f32, ofm_quant.zero_point) + if primary_op.type in set(("AvgPool", "AvgPoolAct", "ResizeBilinear")): + faf_min = quantise_float32(-1.0, ofm_quant.scale_f32, ofm_quant.zero_point) + faf_max = quantise_float32(1.0, ofm_quant.scale_f32, ofm_quant.zero_point) + else: + faf_min = quantise_float32(clamp_tanh(ifm_min), ofm_quant.scale_f32, ofm_quant.zero_point) + faf_max = quantise_float32(clamp_tanh(ifm_max), ofm_quant.scale_f32, ofm_quant.zero_point) elif faf == "Sigmoid": emit.cmd0_with_param(cmd0.NPU_SET_ACTIVATION, activation.SIGMOID) - faf_min = quantise_float32(clamp_sigmoid(ifm_min), ofm_quant.scale_f32, ofm_quant.zero_point) - faf_max = quantise_float32(clamp_sigmoid(ifm_max), ofm_quant.scale_f32, ofm_quant.zero_point) + if primary_op.type in set(("AvgPool", "AvgPoolAct", "ResizeBilinear")): + faf_min = quantise_float32(0, ofm_quant.scale_f32, ofm_quant.zero_point) + faf_max = quantise_float32(1.0, ofm_quant.scale_f32, ofm_quant.zero_point) + else: + faf_min = quantise_float32(clamp_sigmoid(ifm_min), ofm_quant.scale_f32, ofm_quant.zero_point) + faf_max = quantise_float32(clamp_sigmoid(ifm_max), ofm_quant.scale_f32, ofm_quant.zero_point) else: raise Exception("Unsupported fused_activation_function = " + faf) |