From d004a7a707feab36e51f51cfc9eb2cb70729d5ad Mon Sep 17 00:00:00 2001 From: SiCong Li Date: Thu, 28 May 2020 15:26:41 +0100 Subject: COMPMID-3510 [Interface change] Fix definition of "axis" in NESoftmaxLayer and CLSoftmaxLayer * [Interface change] "axis" argument is renamed to "reduce_end_axis" * Unify the meaning of "axis"(now "reduce_end_axis") to be the last axis of the first n dimensions (inclusive)to reduce. This way the meaning of reduce_end_axis stays the same for both positive and negative values: it selects a dimension before which all dimensions (including the selected dimension) are reduced. Change-Id: I4ab03bd8360b1cd8cac4998df0b1571064a9d4ed Signed-off-by: SiCong Li Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/3278 Tested-by: Arm Jenkins Reviewed-by: Michele Di Giorgio Reviewed-by: Georgios Pinitas Comments-Addressed: Arm Jenkins --- src/runtime/CL/functions/CLSoftmaxLayer.cpp | 62 +++++++++++++++++------------ 1 file changed, 36 insertions(+), 26 deletions(-) (limited to 'src/runtime/CL/functions') diff --git a/src/runtime/CL/functions/CLSoftmaxLayer.cpp b/src/runtime/CL/functions/CLSoftmaxLayer.cpp index b0b2117cd9..71ccf9fa01 100644 --- a/src/runtime/CL/functions/CLSoftmaxLayer.cpp +++ b/src/runtime/CL/functions/CLSoftmaxLayer.cpp @@ -42,35 +42,38 @@ CLSoftmaxLayerGeneric::CLSoftmaxLayerGeneric(std::shared_ptr -void CLSoftmaxLayerGeneric::configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t axis) +void CLSoftmaxLayerGeneric::configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t first_n_reduce_axes) { - configure_reshape_input_kernel(CLKernelLibrary::get().get_compile_context(), input, output, axis); + configure_reshape_input_kernel(CLKernelLibrary::get().get_compile_context(), input, output, first_n_reduce_axes); } template -void CLSoftmaxLayerGeneric::configure_reshape_input_kernel(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *output, size_t axis) +void CLSoftmaxLayerGeneric::configure_reshape_input_kernel(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *output, size_t first_n_reduce_axes) { // Flatten the input - const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), axis); + const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input->info(), first_n_reduce_axes); // Initialize the flat input _input_flattened.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_flatten)); // If we need to flatten the input, we can use CLFlattenKernel or CLReshapeKernel - // If flattening on the third axes, we use CLFlattenKernel. + // If the number of reduced axes is 3 (max dimension), which means collapsing all axes except the batch axis, we use CLFlattenKernel. // In all other cases we have to use CLReshapeKernel - if(axis != 3) - { - auto reshape_kernel_ptr = support::cpp14::make_unique(); - reshape_kernel_ptr->configure(compile_context, input, &_input_flattened); - _flatten_kernel_ptr = std::move(reshape_kernel_ptr); - } - else + // Note that the "other cases" include both: + // 1. first_n_reduce_axes < 3: Reduce the first 1 (no need to reduce) or 2 dimensions (inclusive) + // 2. first_n_reduce_axes == 4: Reduce all 4 dimensions. This can only be handled by CLReshapeKernel instead of CLFlattenKernel. + if(first_n_reduce_axes == 3) { auto flatten_kernel_ptr = support::cpp14::make_unique(); flatten_kernel_ptr->configure(compile_context, input, &_input_flattened); _flatten_kernel_ptr = std::move(flatten_kernel_ptr); } + else + { + auto reshape_kernel_ptr = support::cpp14::make_unique(); + reshape_kernel_ptr->configure(compile_context, input, &_input_flattened); + _flatten_kernel_ptr = std::move(reshape_kernel_ptr); + } // We need to init the output tensor here. Indeed, the reshape kernel expects // both tensors to be already initialized @@ -78,20 +81,23 @@ void CLSoftmaxLayerGeneric::configure_reshape_input_kernel(const CLCompi } template -void CLSoftmaxLayerGeneric::configure(const ICLTensor *input, ICLTensor *output, float beta, size_t axis) +void CLSoftmaxLayerGeneric::configure(const ICLTensor *input, ICLTensor *output, float beta, size_t reduce_end_axis) { - configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, axis); + configure(CLKernelLibrary::get().get_compile_context(), input, output, beta, reduce_end_axis); } template -void CLSoftmaxLayerGeneric::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, size_t axis) +void CLSoftmaxLayerGeneric::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta, size_t reduce_end_axis) { // Perform validation step ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayerGeneric::validate(input->info(), output->info(), beta, axis)); + ARM_COMPUTE_ERROR_THROW_ON(CLSoftmaxLayerGeneric::validate(input->info(), output->info(), beta, reduce_end_axis)); - // We don't need flattening only in the case the input is 2D and axis is 1 - _needs_flattening = axis != 1; + // Convert reduce-before axis (inclusive) to first n axes to reduce + size_t first_n_reduce_axes = dim_index_2_num_dims(reduce_end_axis, input->info()->num_dimensions()); + + // We only need flattening when the number of axes to reduce is greater than 1 + _needs_flattening = first_n_reduce_axes > 1; // If we are dealing with a 4D tensor, we will: // - Flatten the input, so that we end up with a [width*height*depth] * batches 2D tensor @@ -102,8 +108,8 @@ void CLSoftmaxLayerGeneric::configure(const CLCompileContext &compile_co // Add to the memory manager _input_flattened _memory_group.manage(&_input_flattened); - // Cofigure _flatten_kernel and _input_flattened - configure_reshape_input_kernel(input, output, axis); + // Cofigure _flatten_kernel and _input_flattened + configure_reshape_input_kernel(input, output, first_n_reduce_axes); } // We want to deal with a 2D input. Either it is the flattened version of the original input (4D case) @@ -165,11 +171,15 @@ void CLSoftmaxLayerGeneric::configure(const CLCompileContext &compile_co } template -Status CLSoftmaxLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t axis) +Status CLSoftmaxLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *output, float beta, size_t reduce_end_axis) { ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4, "Only up to 4 dimensions are supported"); ARM_COMPUTE_UNUSED(beta); + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() <= reduce_end_axis); + + // Convert reduce-before axis (inclusive) to first n axes to reduce + size_t first_n_reduce_axes = dim_index_2_num_dims(reduce_end_axis, input->num_dimensions()); // Create intermediate tensor info DataType tmp_data_type = is_data_type_quantized_asymmetric(input->data_type()) ? DataType::S32 : input->data_type(); @@ -180,20 +190,20 @@ Status CLSoftmaxLayerGeneric::validate(const ITensorInfo *input, const I TensorInfo tensor_info_max(input->clone()->set_tensor_shape(max_sum_shape).set_is_resizable(true)); TensorInfo tensor_info_sum(input->clone()->set_tensor_shape(max_sum_shape).set_data_type(tmp_data_type).set_quantization_info(QuantizationInfo()).set_is_resizable(true)); - const bool needs_flattening = (axis != 1); + const bool needs_flattening = (first_n_reduce_axes > 1); if(needs_flattening) { - const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, axis); + const TensorShape shape_flatten = misc::shape_calculator::compute_softmax_shape(input, first_n_reduce_axes); TensorInfo tensor_info_flat(input->clone()->set_tensor_shape(shape_flatten).set_is_resizable(true)); - if(axis != 3) + if(first_n_reduce_axes == 3) { - ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayerKernel::validate(input, &tensor_info_flat)); + ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayerKernel::validate(input, &tensor_info_flat)); } else { - ARM_COMPUTE_RETURN_ON_ERROR(CLFlattenLayerKernel::validate(input, &tensor_info_flat)); + ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayerKernel::validate(input, &tensor_info_flat)); } } -- cgit v1.2.1