From 1a569a30a2f456ff1a3e0a665201e1c3ab92df80 Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Tue, 10 Sep 2019 17:20:34 +0100 Subject: COMPMID-2161 [NEON] Create IWeightManager class Change-Id: I1a9a46da2f98e896b825099151b56d1d8271dd31 Signed-off-by: Michalis Spyrou Reviewed-on: https://review.mlplatform.org/c/1915 Comments-Addressed: Arm Jenkins Reviewed-by: Georgios Pinitas Tested-by: Arm Jenkins --- .../functions/NEConvertFullyConnectedWeights.h | 48 +++++++++++- .../runtime/NEON/functions/NEDeconvolutionLayer.h | 2 +- .../runtime/NEON/functions/NEFullyConnectedLayer.h | 85 ++++++++++++++++------ arm_compute/runtime/NEON/functions/NEGEMM.h | 6 +- .../NEON/functions/NEGEMMAssemblyDispatch.h | 12 +-- .../NEON/functions/NEGEMMConvolutionLayer.h | 77 +++++++++++++++++--- arm_compute/runtime/NEON/functions/NERNNLayer.h | 4 +- .../functions/assembly/NEGEMMInterleavedWrapper.h | 5 +- 8 files changed, 193 insertions(+), 46 deletions(-) (limited to 'arm_compute/runtime/NEON/functions') diff --git a/arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h b/arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h index 8f261421e6..50a86bd7c4 100644 --- a/arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h +++ b/arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -26,7 +26,9 @@ #include "arm_compute/core/NEON/kernels/NEConvertFullyConnectedWeightsKernel.h" #include "arm_compute/runtime/IFunction.h" +#include "arm_compute/runtime/ITransformWeights.h" #include "arm_compute/runtime/NEON/NEScheduler.h" +#include "arm_compute/runtime/Tensor.h" namespace arm_compute { @@ -52,6 +54,8 @@ public: * @param[in] output The converted weights tensor info. Shape and Data Type: Same as @p input. * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer). * @param[in] data_layout The data layout the weights have been trained in. + * + * @return A Status */ static Status validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout); @@ -61,5 +65,45 @@ public: private: NEConvertFullyConnectedWeightsKernel _kernel; }; -} + +namespace weights_transformations +{ +/** Basic function to run @ref NEConvertFullyConnectedWeightsKernel. */ +class NEConvertFullyConnectedWeightsManaged : public ITransformWeights +{ +public: + void run() override + { + _output.allocator()->allocate(); + _func.run(); + _reshape_run = true; + } + + void release() override + { + _output.allocator()->free(); + } + + ITensor *get_weights() override + { + return &_output; + } + + uint32_t uid() override + { + return _uid; + } + + void configure(const ITensor *input, const TensorShape &original_input_shape, DataLayout data_layout) + { + _func.configure(input, &_output, original_input_shape, data_layout); + } + +private: + static constexpr uint32_t _uid = 0x4; + Tensor _output{}; + NEConvertFullyConnectedWeights _func{}; +}; +} // namespace weights_transformations +} // namespace arm_compute #endif /* __ARM_COMPUTE_NECONVERTFULLYCONNECTEDWEIGHTS_H__ */ diff --git a/arm_compute/runtime/NEON/functions/NEDeconvolutionLayer.h b/arm_compute/runtime/NEON/functions/NEDeconvolutionLayer.h index 360bb23f22..6880bbba6b 100644 --- a/arm_compute/runtime/NEON/functions/NEDeconvolutionLayer.h +++ b/arm_compute/runtime/NEON/functions/NEDeconvolutionLayer.h @@ -73,7 +73,7 @@ namespace arm_compute class NEDeconvolutionLayer : public IFunction { public: - /** Default constructor */ + /** Constructor */ NEDeconvolutionLayer(std::shared_ptr memory_manager = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ diff --git a/arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h b/arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h index 56ce274572..b80e0e49e0 100644 --- a/arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h +++ b/arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018 ARM Limited. + * Copyright (c) 2017-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -63,6 +63,46 @@ public: static Status validate(const ITensorInfo *input, const ITensorInfo *output); }; +namespace weights_transformations +{ +/** Basic function to manage the reshape weights generated from @ref NEFullyConnectedLayerReshapeWeights */ +class NEFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights +{ +public: + void run() override + { + _output.allocator()->allocate(); + _func.run(); + _reshape_run = true; + } + + void release() override + { + _output.allocator()->free(); + } + + ITensor *get_weights() override + { + return &_output; + } + + uint32_t uid() override + { + return _uid; + } + + void configure(const ITensor *input) + { + _func.configure(input, &_output); + } + +private: + static constexpr uint32_t _uid = 0x0; + Tensor _output{}; + NEFullyConnectedLayerReshapeWeights _func{}; +}; +} // namespace weights_transformations + /** Basic function to compute a Fully Connected layer on NEON. This function calls the following NEON kernels: * -# @ref NEIm2ColKernel (called when the input comes from a convolutional layer) * -# @ref NEFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once) @@ -75,7 +115,7 @@ class NEFullyConnectedLayer : public IFunction { public: /** Constructor */ - NEFullyConnectedLayer(std::shared_ptr memory_manager = nullptr); + NEFullyConnectedLayer(std::shared_ptr memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ NEFullyConnectedLayer(const NEFullyConnectedLayer &) = delete; /** Default move constructor */ @@ -128,25 +168,28 @@ private: void configure_conv_fc(const ITensor *input, const ITensor *weights, ITensor *output); void configure_mm(const ITensor *input, const ITensor *weights, ITensor *output); - MemoryGroup _memory_group; - NEFlattenLayerKernel _flatten_kernel; - NEConvertFullyConnectedWeights _convert_weights; - NEFullyConnectedLayerReshapeWeights _reshape_weights_function; - NEGEMM _mm_gemm; - NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; - NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; - NEGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel; - Tensor _flatten_output; - Tensor _gemmlowp_output; - Tensor _converted_weights_output; - Tensor _reshape_weights_output; - const ITensor *_original_weights; - bool _are_weights_converted; - bool _are_weights_reshaped; - bool _is_fc_after_conv; - bool _accumulate_biases; - bool _is_quantized; - bool _is_prepared; + MemoryGroup _memory_group; + IWeightsManager *_weights_manager; + NEFlattenLayerKernel _flatten_kernel; + NEConvertFullyConnectedWeights _convert_weights; + weights_transformations::NEConvertFullyConnectedWeightsManaged _convert_weights_managed; + NEFullyConnectedLayerReshapeWeights _reshape_weights_function; + weights_transformations::NEFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function; + NEGEMM _mm_gemm; + NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; + NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; + NEGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel; + Tensor _flatten_output; + Tensor _gemmlowp_output; + Tensor _converted_weights_output; + Tensor _reshape_weights_output; + const ITensor *_original_weights; + bool _are_weights_converted; + bool _are_weights_reshaped; + bool _is_fc_after_conv; + bool _accumulate_biases; + bool _is_quantized; + bool _is_prepared; }; } // namespace arm_compute #endif /* __ARM_COMPUTE_NEFULLYCONNECTEDLAYER_H__ */ diff --git a/arm_compute/runtime/NEON/functions/NEGEMM.h b/arm_compute/runtime/NEON/functions/NEGEMM.h index 7f9e3181bc..d947be1ef9 100644 --- a/arm_compute/runtime/NEON/functions/NEGEMM.h +++ b/arm_compute/runtime/NEON/functions/NEGEMM.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018 ARM Limited. + * Copyright (c) 2017-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -31,6 +31,7 @@ #include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h" #include "arm_compute/runtime/IFunction.h" #include "arm_compute/runtime/IMemoryManager.h" +#include "arm_compute/runtime/IWeightsManager.h" #include "arm_compute/runtime/MemoryGroup.h" #include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h" #include "arm_compute/runtime/Tensor.h" @@ -51,7 +52,7 @@ class NEGEMM : public IFunction { public: /** Constructor */ - NEGEMM(std::shared_ptr memory_manager = nullptr); + NEGEMM(std::shared_ptr memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ NEGEMM(const NEGEMM &) = delete; /** Default move constructor */ @@ -96,6 +97,7 @@ public: private: MemoryGroup _memory_group; + IWeightsManager *_weights_manager; NEGEMMInterleave4x4Kernel _interleave_kernel; NEGEMMTranspose1xWKernel _transpose_kernel; NEGEMMMatrixMultiplyKernel _mm_kernel; diff --git a/arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h b/arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h index ec4f700034..83e495e695 100644 --- a/arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h +++ b/arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h @@ -27,6 +27,7 @@ #include "arm_compute/core/NEON/kernels/assembly/NEGEMMAssemblyWrapperKernel.h" #include "arm_compute/runtime/IFunction.h" #include "arm_compute/runtime/IMemoryManager.h" +#include "arm_compute/runtime/IWeightsManager.h" #include "arm_compute/runtime/MemoryGroup.h" #include "arm_compute/runtime/Tensor.h" @@ -38,9 +39,8 @@ namespace arm_compute class NEGEMMAssemblyDispatch : public IFunction { public: - /** Default constructor */ - NEGEMMAssemblyDispatch(std::shared_ptr memory_manager = nullptr); - + /** Constructor */ + NEGEMMAssemblyDispatch(std::shared_ptr memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); /** Prevent instances of this class from being copy constructed */ NEGEMMAssemblyDispatch(const NEGEMMAssemblyDispatch &) = delete; /** Prevent instances of this class from being copied */ @@ -79,8 +79,9 @@ private: /** Interface for the arm_gemm fallback */ std::unique_ptr _arm_gemm; - MemoryGroup _memory_group; /**< Function memory group */ - std::shared_ptr _memory_manager; /**< Copy of the memory manager used to create the memory group to be used when instantiating new functions */ + MemoryGroup _memory_group; /**< Function memory group */ + std::shared_ptr _memory_manager; /**< Copy of the memory manager used to create the memory group to be used when instantiating new functions */ + IWeightsManager *_weights_manager; /**< Pointer to the weights manager */ public: /** If supported create an ACL function else fallback to the arm_gemm function. * @@ -117,6 +118,5 @@ public: void prepare() override; void run() override; }; - } // namespace arm_compute #endif /* __ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H__ */ diff --git a/arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h b/arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h index ace924f146..dccc35f0af 100644 --- a/arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h +++ b/arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h @@ -32,6 +32,7 @@ #include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h" #include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h" #include "arm_compute/core/Types.h" +#include "arm_compute/runtime/IWeightsManager.h" #include "arm_compute/runtime/MemoryGroup.h" #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" #include "arm_compute/runtime/NEON/functions/NEGEMM.h" @@ -54,6 +55,14 @@ class NEConvolutionLayerReshapeWeights : public IFunction public: /** Constructor */ NEConvolutionLayerReshapeWeights(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEConvolutionLayerReshapeWeights(const NEConvolutionLayerReshapeWeights &) = delete; + /** Default move constructor */ + NEConvolutionLayerReshapeWeights(NEConvolutionLayerReshapeWeights &&) = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEConvolutionLayerReshapeWeights &operator=(const NEConvolutionLayerReshapeWeights &) = delete; + /** Default move assignment operator */ + NEConvolutionLayerReshapeWeights &operator=(NEConvolutionLayerReshapeWeights &&) = default; /** Set the input and output tensors. * * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QASYMM8/F16/F32. @@ -78,6 +87,52 @@ private: NEWeightsReshapeKernel _weights_reshape_kernel; }; +namespace weights_transformations +{ +/** Basic function to manage the reshape weights generated from @ref NEConvolutionLayerReshapeWeights */ +class NEConvolutionLayerReshapeWeightsTransform : public ITransformWeights +{ +public: + void configure(const ITensor *input, const ITensor *biases) + { + _bias_bit = (biases != nullptr) ? 1 : 0; + _func.configure(input, biases, &_output); + } + + void run() override + { + _output.allocator()->allocate(); + _func.run(); + _reshape_run = true; + } + + ITensor *get_weights() override + { + return &_output; + } + + void release() override + { + _output.allocator()->free(); + } + + uint32_t uid() override + { + return ((0x8) | (_bias_bit << 7)); + } + + bool is_reshape_run() + { + return _reshape_run; + } + +private: + Tensor _output{}; + NEConvolutionLayerReshapeWeights _func{}; + int32_t _bias_bit{ 0 }; +}; +} // namespace weights_transformations + /** Basic function to compute the convolution layer. This function calls the following NEON kernels/functions: * * -# @ref NEIm2ColKernel @@ -92,7 +147,7 @@ class NEGEMMConvolutionLayer : public IFunction { public: /** Constructor */ - NEGEMMConvolutionLayer(const std::shared_ptr &memory_manager = nullptr); + NEGEMMConvolutionLayer(const std::shared_ptr &memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); /** Prevent instances of this class from being copied (As this class contains pointers) */ NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete; /** Default move constructor */ @@ -187,15 +242,17 @@ private: static Status validate_gemm3d(const ITensorInfo *input_info, const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col); private: - MemoryGroup _memory_group; - NEConvolutionLayerReshapeWeights _reshape_weights; - NEIm2ColKernel _im2col_kernel; - NEGEMM _mm_gemm; - NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; - NECol2ImKernel _col2im_kernel; - NEActivationLayer _activationlayer_function; - NEArithmeticAdditionKernel _add_bias_kernel; - NEReshapeLayer _reshape_layer; + MemoryGroup _memory_group; + IWeightsManager *_weights_manager; + NEConvolutionLayerReshapeWeights _reshape_weights; + weights_transformations::NEConvolutionLayerReshapeWeightsTransform _reshape_weights_managed; + NEIm2ColKernel _im2col_kernel; + NEGEMM _mm_gemm; + NEGEMMLowpMatrixMultiplyCore _mm_gemmlowp; + NECol2ImKernel _col2im_kernel; + NEActivationLayer _activationlayer_function; + NEArithmeticAdditionKernel _add_bias_kernel; + NEReshapeLayer _reshape_layer; const ITensor *_original_weights; diff --git a/arm_compute/runtime/NEON/functions/NERNNLayer.h b/arm_compute/runtime/NEON/functions/NERNNLayer.h index ec394392de..978c445927 100644 --- a/arm_compute/runtime/NEON/functions/NERNNLayer.h +++ b/arm_compute/runtime/NEON/functions/NERNNLayer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -86,7 +86,7 @@ private: NEGEMM _gemm_state_f; NEArithmeticAdditionKernel _add_kernel; NEActivationLayerKernel _activation_kernel; - NEFullyConnectedLayer _fully_connected_kernel; + NEFullyConnectedLayer _fully_connected; NECopyKernel _copy_kernel; Tensor _fully_connected_out; Tensor _gemm_output; diff --git a/arm_compute/runtime/NEON/functions/assembly/NEGEMMInterleavedWrapper.h b/arm_compute/runtime/NEON/functions/assembly/NEGEMMInterleavedWrapper.h index ad89e1fbec..d3dda9a95f 100644 --- a/arm_compute/runtime/NEON/functions/assembly/NEGEMMInterleavedWrapper.h +++ b/arm_compute/runtime/NEON/functions/assembly/NEGEMMInterleavedWrapper.h @@ -32,6 +32,7 @@ #include "arm_compute/runtime/IFunction.h" #include "arm_compute/runtime/IMemoryManager.h" #include "arm_compute/runtime/IScheduler.h" +#include "arm_compute/runtime/IWeightsManager.h" #include "arm_compute/runtime/MemoryGroup.h" #include "arm_compute/runtime/Tensor.h" @@ -94,8 +95,8 @@ public: class NEGEMMInterleavedWrapper : public IFunction { public: - NEGEMMInterleavedWrapper(std::shared_ptr memory_manager = nullptr); - ~NEGEMMInterleavedWrapper() = default; + NEGEMMInterleavedWrapper(std::shared_ptr memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); + ~NEGEMMInterleavedWrapper() = default; NEGEMMInterleavedWrapper(const NEGEMMInterleavedWrapper &) = delete; NEGEMMInterleavedWrapper &operator=(const NEGEMMInterleavedWrapper &) = delete; -- cgit v1.2.1