From c0b6f76561580414f08633a804fc548ccad65659 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Mon, 2 Nov 2020 01:37:17 +0000 Subject: COMPMID-3776: Indirect GEMM Signed-off-by: Georgios Pinitas Change-Id: I51a1b0f098bc3a8c408c50c92221e4df3061e12c Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4343 Tested-by: Arm Jenkins Reviewed-by: Sang-Hoon Park Reviewed-by: Michele Di Giorgio Comments-Addressed: Arm Jenkins --- src/core/NEON/kernels/arm_gemm/convolver.hpp | 182 +++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 src/core/NEON/kernels/arm_gemm/convolver.hpp (limited to 'src/core/NEON/kernels/arm_gemm/convolver.hpp') diff --git a/src/core/NEON/kernels/arm_gemm/convolver.hpp b/src/core/NEON/kernels/arm_gemm/convolver.hpp new file mode 100644 index 0000000000..1cd959523f --- /dev/null +++ b/src/core/NEON/kernels/arm_gemm/convolver.hpp @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2020 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#pragma once + +#include "convolution_parameters.hpp" + +#include +#include +#include +#include + +namespace arm_gemm { + +// Class to assist with convolution calculations. +// +// This is framed as a hierarchy of objects: +// +// - Top level object which depends only on convolution parameters. This sets up std::vectors for the padding and +// kernel offset arrays. From this you can request: +// +// - Mid level object (e.g. instantiated at start of 'ConvolutionInterleave'). This holds specifics about the +// input tensor, and the desired column range. Calculations specific to this can be done once when this is set +// up. From this you can request: +// +// - Low level object (instantiated for each range of rows). This contains methods to actually populate a row +// pointer array. + + +template +class convolver { +private: + const ConvolutionParameters m_params; + + // Vector of padding data + const std::vector m_pad_row; + + // X/Y offsets for each kernel position + std::vector m_kernel_y; + std::vector m_kernel_x; + + class column_handler { + private: + const convolver &m_parent; + + // Base/stride of input image + const T * const m_input_base; + const size_t m_input_stride; + + // Starting kernel point and channel offset within that point + const unsigned int m_start_pos; + const unsigned int m_start_offset; + + // Total length to process, rounded length of each input channel block. + const unsigned int m_length; + const unsigned int m_rounded_stringlen; + + class row_handler { + private: + const convolver &m_convolver; + const column_handler &m_parent; + + // These variables track progress through the current block of rows + unsigned int m_start_output_y=0; + unsigned int m_start_output_x=0; + + unsigned int m_length_remaining=0; + unsigned int m_current_pos=0; + + unsigned int m_active_height=0; + + public: + row_handler(const column_handler &parent, unsigned int start_row, unsigned int active_height) : + m_convolver(parent.m_parent), + m_parent(parent), + m_start_output_y(start_row / m_convolver.m_params.output_width), + m_start_output_x(start_row % m_convolver.m_params.output_width), + m_length_remaining(m_parent.m_length), + m_current_pos(m_parent.m_start_pos), + m_active_height(active_height) { } + + bool finished() const { + return (m_length_remaining == 0); + } + + std::tuple next_block(const T ** const row_ptr) { + if (finished()) { + return { 0, 0 }; + } + + // "in_width" in the amount of data that will be read in (copied) + // "out_width" is the total amount of data that will be produced (including padding) + unsigned int offset = (m_current_pos == m_parent.m_start_pos) ? m_parent.m_start_offset : 0; + unsigned int in_width = std::min(m_length_remaining, static_cast(m_convolver.m_params.input_channels) - offset); + unsigned int out_width = std::min(m_length_remaining, m_parent.m_rounded_stringlen - offset); + + unsigned int output_y = m_start_output_y; + unsigned int output_x = m_start_output_x; + + for (unsigned int row=0; row= m_convolver.m_params.input_height || input_x < 0 || input_x >= m_convolver.m_params.input_width) { + row_ptr[row] = m_convolver.m_pad_row.data(); + } else { + row_ptr[row] = m_parent.m_input_base + ((input_y * m_convolver.m_params.input_width) + input_x) * m_parent.m_input_stride; + } + + output_x++; + if (output_x == m_convolver.m_params.output_width) { + output_y++; + output_x=0; + } + } + + m_current_pos++; + m_length_remaining-=out_width; + + return { in_width, offset }; + } + }; // end of "row handler" class + + public: + column_handler(const convolver &parent, const T *input_base, size_t input_stride, + unsigned int k_start, unsigned int k_end, unsigned int rounded_stringlen) + : m_parent(parent), m_input_base(input_base), m_input_stride(input_stride), + m_start_pos(k_start / rounded_stringlen), + m_start_offset(k_start % rounded_stringlen), + m_length(k_end - k_start), + m_rounded_stringlen(rounded_stringlen) { } + + row_handler process_rows(unsigned int start_row, unsigned int active_height) const { + return row_handler(*this, start_row, active_height); + } + }; // end of "column handler" class + +public: + convolver(ConvolutionParameters params) : + m_params (params), m_pad_row(params.input_channels, static_cast(params.padding_value)), + m_kernel_y(params.kernel_width * params.kernel_height, 0), + m_kernel_x(params.kernel_width * params.kernel_height, 0) { + + // Kernel points are addressed across, then down (assumed weight layout is WHIO) + for (unsigned int ky=0; ky