From e5bf4c55eff186a8871206fbc1b02391fd8d75b2 Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Thu, 14 Feb 2019 17:47:33 +0000 Subject: COMPMID-1710: Fix CL logistic activation for QASYMM8 Logistic activation in QASYMM8 is performed in floating point and then converting back to QASYMM8. However, if input and output have different quantization information, a double conversion is done leading to loss in accuracy. This patch creates a special case kernel for logistic activation. Change-Id: Ia18ee0b2f84701674f88785bcd13753b50d64a08 Signed-off-by: Michele Di Giorgio Reviewed-on: https://review.mlplatform.org/696 Reviewed-by: Gian Marco Iodice Tested-by: Arm Jenkins --- src/core/CL/kernels/CLActivationLayerKernel.cpp | 41 ++++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'src/core/CL/kernels') diff --git a/src/core/CL/kernels/CLActivationLayerKernel.cpp b/src/core/CL/kernels/CLActivationLayerKernel.cpp index 73a4d7d2c6..100184d2f3 100644 --- a/src/core/CL/kernels/CLActivationLayerKernel.cpp +++ b/src/core/CL/kernels/CLActivationLayerKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2018 ARM Limited. + * Copyright (c) 2016-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -129,24 +129,25 @@ void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, Act b_const_int = input->info()->quantization_info().quantize(b_const, RoundingPolicy::TO_NEAREST_UP); } + const bool is_logistic_activation_quantized = is_data_type_quantized_asymmetric(dt) && act_info.activation() == ActivationLayerInfo::ActivationFunction::LOGISTIC; // Set build options - std::set build_opts; - build_opts.emplace(("-DACT=" + lower_string(string_from_activation_func(act_info.activation())))); - build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt))); - build_opts.emplace(("-DSELECT_DATA_TYPE=" + get_cl_select_type_from_data_type(dt))); - build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); + CLBuildOptions build_opts; + build_opts.add_option_if(!is_logistic_activation_quantized, "-DACT=" + lower_string(string_from_activation_func(act_info.activation()))); + build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt))); + build_opts.add_option(("-DSELECT_DATA_TYPE=" + get_cl_select_type_from_data_type(dt))); + build_opts.add_option(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); if(is_data_type_quantized(dt)) { - build_opts.emplace(("-DA_VAL=" + support::cpp11::to_string(a_const_int))); - build_opts.emplace(("-DB_VAL=" + support::cpp11::to_string(b_const_int))); + build_opts.add_option(("-DA_VAL=" + support::cpp11::to_string(a_const_int))); + build_opts.add_option(("-DB_VAL=" + support::cpp11::to_string(b_const_int))); const int o1 = input->info()->quantization_info().offset; const float s1 = input->info()->quantization_info().scale; // Quantized value of 0 corresponds to the offset o1 - build_opts.emplace(("-DCONST_0=" + support::cpp11::to_string(o1))); - build_opts.emplace(("-DS1_VAL=" + float_to_string_with_full_precision(s1))); - build_opts.emplace(("-DO1_VAL=" + support::cpp11::to_string(o1))); + build_opts.add_option(("-DCONST_0=" + support::cpp11::to_string(o1))); + build_opts.add_option(("-DS1_VAL=" + float_to_string_with_full_precision(s1))); + build_opts.add_option(("-DO1_VAL=" + support::cpp11::to_string(o1))); // Set scale and offset of the input and output if they have different quantization info if(is_data_type_quantized_asymmetric(dt) && output != nullptr) @@ -156,22 +157,26 @@ void CLActivationLayerKernel::configure(ICLTensor *input, ICLTensor *output, Act if(o1 != o2 || s1 != s2) { - build_opts.emplace(("-DS2_VAL=" + float_to_string_with_full_precision(s2))); - build_opts.emplace(("-DO2_VAL=" + support::cpp11::to_string(o2))); + build_opts.add_option(("-DS2_VAL=" + float_to_string_with_full_precision(s2))); + build_opts.add_option(("-DO2_VAL=" + support::cpp11::to_string(o2))); } } } else { - build_opts.emplace(("-DA_VAL=" + float_to_string_with_full_precision(a_const))); - build_opts.emplace(("-DB_VAL=" + float_to_string_with_full_precision(b_const))); + build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(a_const))); + build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(b_const))); } - build_opts.emplace((_run_in_place) ? "-DIN_PLACE" : ""); + build_opts.add_option_if(_run_in_place, "-DIN_PLACE"); // Create kernel - std::string kernel_name = is_data_type_quantized_asymmetric(dt) ? std::string("activation_layer_qa8") : std::string("activation_layer"); - _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); + std::string kernel_name = std::string("activation_layer"); + if(is_data_type_quantized_asymmetric(dt)) + { + kernel_name += is_logistic_activation_quantized ? std::string("_logistic_qa8") : std::string("_qa8"); + } + _kernel = static_cast(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); // Make sure _kernel is initialized before calling the parent's configure _input = input; -- cgit v1.2.1