Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_NEGEMMLOWP_H__ |
| 25 | #define __ARM_COMPUTE_NEGEMMLOWP_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpFinalizeKernel.h" |
| 29 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpReductionKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/IMemoryManager.h" |
| 32 | #include "arm_compute/runtime/MemoryGroup.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" |
| 34 | #include "arm_compute/runtime/Tensor.h" |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 35 | |
| 36 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | class ITensor; |
| 41 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 42 | /** Basic function to execute GEMMLowp on NEON. This function calls the following NEON kernels/function: |
| 43 | * |
| 44 | * -# @ref NEGEMMLowpMatrixAReductionKernel |
| 45 | * -# @ref NEGEMMLowpMatrixBReductionKernel |
| 46 | * -# @ref NEGEMMLowpMatrixMultiplyCore |
| 47 | * -# @ref NEGEMMLowpFinalizeKernel |
| 48 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | */ |
| 50 | class NEGEMMLowp : public IFunction |
| 51 | { |
| 52 | public: |
| 53 | /** Constructor */ |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 54 | NEGEMMLowp(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | /** Initialise the kernel's inputs, output |
| 56 | * |
| 57 | * @note GEMM_LOWP: low precision GEMM kernel |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 58 | * This kernel performs the following computations: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | * |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 60 | * -# Convert a values from int8 to int32 and add a_offset to each of them. |
| 61 | * -# Convert b values from int8 to int32 and add b_offset to each of them. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | * -# Compute the int32 matrix product of the resulting a * b. |
| 63 | * -# Add output_offset to each entry of the result. |
| 64 | * -# Multiply each entry of the result and round to the nearest integer |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 65 | * -# Clamp the resulting int32 values to the [0..255] range and cast to int8. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | * |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 67 | * @param[in] a First input tensor (Matrix A). Data type supported: S8. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a |
| 69 | * @param[out] output Output tensor. Data type supported: same as @p a. |
| 70 | * @param[in] a_offset Offset to be added to each element of the matrix A. |
| 71 | * @param[in] b_offset Offset to be added to each element of the matrix B. |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 72 | * @param[in] c_offset Offset to be added to each element of the output matrix |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | * @param[in] output_mult_int Value to be multiplied to each element of the output matrix |
| 74 | * @param[in] shift Number of bits to shift right the result. |
| 75 | */ |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 76 | void configure(const ITensor *a, const ITensor *b, ITensor *output, int32_t a_offset, int32_t b_offset, int32_t c_offset, int32_t output_mult_int, int32_t shift); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 77 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | // Inherited methods overridden: |
| 79 | void run() override; |
| 80 | |
| 81 | private: |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 82 | MemoryGroup _memory_group; |
| 83 | NEGEMMLowpMatrixMultiplyCore _mm_func; |
| 84 | NEGEMMLowpMatrixAReductionKernel _mtx_a_reduction_kernel; |
| 85 | NEGEMMLowpMatrixBReductionKernel _mtx_b_reduction_kernel; |
| 86 | NEGEMMLowpFinalizeKernel _finalize_kernel; |
| 87 | Tensor _vector_sum_col; |
| 88 | Tensor _vector_sum_row; |
| 89 | Tensor _mm_output; |
| 90 | int32_t _a_offset; |
| 91 | int32_t _b_offset; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | }; |
| 93 | } |
| 94 | #endif /*__ARM_COMPUTE_NEGEMMLOWP_H__ */ |