blob: 4fb2f89418d50c6e7f982ce6108f866271a6a2f0 [file] [log] [blame]
Michalis Spyrou1a569a32019-09-10 17:20:34 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2019-2021 Arm Limited.
Michalis Spyrou1a569a32019-09-10 17:20:34 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_ITRANSFORMWEIGHTS_H
25#define ARM_COMPUTE_ITRANSFORMWEIGHTS_H
Michalis Spyrou1a569a32019-09-10 17:20:34 +010026
27#include <atomic>
Michalis Spyrouebcebf12020-10-21 00:04:14 +010028#include <utility>
Michalis Spyrou1a569a32019-09-10 17:20:34 +010029
30namespace arm_compute
31{
32// Forward declarations
33class ITensor;
34
35/** Weights tensor transform interface
36 * In order to identify the different reshape functions, each reshape function has
37 * to generate a unique id. We use the following conversion using an unsigned 32bit value:
38 *
39 * Lower two bits store the target:
Sheri Zhangac6499a2021-02-10 15:32:38 +000040 * 00 -> Neon
Michalis Spyrou1a569a32019-09-10 17:20:34 +010041 * 01 -> CL
42 * 10 -> GLES
43 * 11 -> Unused
44 *
45 * Five bits store the id of the reshape function:
46 * 00000 -> FullyConnectedLayerReshapeWeights
47 * 00001 -> ConvertFullyConnectedWeights
48 * 00010 -> ConvolutionLayerReshapeWeights
49 * 00011 -> DepthwiseConvolutionLayerReshapeWeights
50 * 00100 -> GEMMReshapeLHSMatrixKernel
51 * 00101 -> GEMMReshapeRHSMatrixKernel
52 *
53 * Rest of the bits are used for identifying special cases such as assembly functions and extra
54 * arguments in the reshape kernels.
55 *
56 * */
57class ITransformWeights
58{
59public:
60 /** Default Constructor */
61 ITransformWeights() = default;
62 /** Default Destructor */
63 virtual ~ITransformWeights() = default;
64 /** Prevent instances of this class to be copy constructed */
65 ITransformWeights(const ITransformWeights &) = delete;
66 /** Prevent instances of this class to be copied */
67 ITransformWeights &operator=(const ITransformWeights &) = delete;
68 /** Allow instances of this class to be move constructed */
Georgios Pinitas879d1312019-09-30 13:25:53 +010069 ITransformWeights(ITransformWeights &&other)
70 {
71 *this = std::move(other);
72 }
Michalis Spyrou1a569a32019-09-10 17:20:34 +010073 /** Allow instances of this class to be moved */
Georgios Pinitas879d1312019-09-30 13:25:53 +010074 ITransformWeights &operator=(ITransformWeights &&other)
75 {
76 if(this != &other)
77 {
78 _num_refcount = other._num_refcount.load();
79 _reshape_run = other._reshape_run;
80 }
81 return *this;
82 }
Michalis Spyrou1a569a32019-09-10 17:20:34 +010083
84 /** Get a pointer to the transformed weights
85 *
86 * @return The pointer to the transformed ITensor weights
87 */
88 virtual ITensor *get_weights() = 0;
89 /** Function that returns a unique id of the reshape function
90 *
91 * @return The computed unique id
92 */
93 virtual uint32_t uid() = 0;
94 /** Run the transformation function */
95 virtual void run() = 0;
96 /** Release transformed weights memory */
97 virtual void release() = 0;
98 /** Increase the object's refcount */
99 void increase_refcount()
100 {
101 ++_num_refcount;
102 }
103
104 /** Decrease the object's refcount and return the updated value
105 *
106 * @return The updated refcount
107 * */
108 int32_t decrease_refcount()
109 {
110 return --_num_refcount;
111 }
112
113 /** Function that returns a flag on whether the weights are reshaped or not
114 *
115 * @return True if the function is reshaped
116 */
117 bool is_reshape_run()
118 {
119 return _reshape_run;
120 }
121
122protected:
123 std::atomic<int32_t> _num_refcount{ 0 };
124 bool _reshape_run{ false };
125};
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100126} // arm_compute
127
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100128#endif /*ARM_COMPUTE_ITRANSFORMWEIGHTS_H */